-
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
219 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/src/main/java/ch/cyberduck/core/socket/HardwareAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ch.cyberduck.core.socket; | ||
|
||
/* | ||
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import ch.cyberduck.core.exception.BackgroundException; | ||
|
||
public interface HardwareAddress { | ||
|
||
byte[] getAddress() throws BackgroundException; | ||
} |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/ch/cyberduck/core/socket/IOKitHardwareAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ch.cyberduck.core.socket; | ||
|
||
/* | ||
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import ch.cyberduck.core.exception.BackgroundException; | ||
import ch.cyberduck.core.exception.UnsupportedException; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import com.sun.jna.platform.mac.CoreFoundation; | ||
import com.sun.jna.platform.mac.IOKit; | ||
import com.sun.jna.platform.mac.IOKitUtil; | ||
|
||
public class IOKitHardwareAddress implements HardwareAddress { | ||
private static final Logger log = LogManager.getLogger(IOKitHardwareAddress.class); | ||
|
||
// #define kIOMACAddress "IOMACAddress" | ||
private static final CoreFoundation.CFStringRef kIOMACAddress = CoreFoundation.CFStringRef.createCFString("IOMACAddress"); | ||
// #define kIOServicePlane "IOService" | ||
private final String kIOServicePlane = "IOService"; | ||
|
||
private final int kIORegistryIterateRecursively = 0x00000001; | ||
private final int kIORegistryIterateParents = 0x00000002; | ||
|
||
@Override | ||
public byte[] getAddress() throws BackgroundException { | ||
final IOKit.IOService en0 = IOKitUtil.getMatchingService(IOKitUtil.getBSDNameMatchingDict("en0")); | ||
if(null == en0) { | ||
// Interface is not found when link is down #fail | ||
log.warn("No network interface en0"); | ||
throw new UnsupportedException("No network interface en0"); | ||
} | ||
final CoreFoundation.CFTypeRef property = IOKit.INSTANCE.IORegistryEntrySearchCFProperty(en0, kIOServicePlane, kIOMACAddress, | ||
CoreFoundation.INSTANCE.CFAllocatorGetDefault(), kIORegistryIterateRecursively | kIORegistryIterateParents); | ||
if(null == property) { | ||
log.error("Cannot determine MAC address"); | ||
throw new UnsupportedException("No hardware address for network interface en0"); | ||
} | ||
final CoreFoundation.CFDataRef dataRef = new CoreFoundation.CFDataRef(property.getPointer()); | ||
return dataRef.getBytePtr().getByteArray(0, dataRef.getLength()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/ch/cyberduck/core/socket/NetworkInterfaceHardwareAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ch.cyberduck.core.socket; | ||
|
||
/* | ||
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import ch.cyberduck.core.DefaultIOExceptionMappingService; | ||
import ch.cyberduck.core.exception.BackgroundException; | ||
import ch.cyberduck.core.exception.UnsupportedException; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
|
||
public class NetworkInterfaceHardwareAddress implements HardwareAddress { | ||
private static final Logger log = LogManager.getLogger(NetworkInterfaceHardwareAddress.class); | ||
|
||
@Override | ||
public byte[] getAddress() throws BackgroundException { | ||
try { | ||
final NetworkInterface en0 = NetworkInterface.getByName("en0"); | ||
if(null == en0) { | ||
// Interface is not found when link is down #fail | ||
log.warn("No network interface en0"); | ||
throw new UnsupportedException("No network interface en0"); | ||
} | ||
final byte[] address = en0.getHardwareAddress(); | ||
if(null == address) { | ||
log.error("Cannot determine MAC address"); | ||
throw new UnsupportedException("No hardware address for network interface en0"); | ||
} | ||
return address; | ||
} | ||
catch(SocketException e) { | ||
throw new DefaultIOExceptionMappingService().map(e); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
core/src/test/java/ch/cyberduck/core/socket/IOKitHardwareAddressTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ch.cyberduck.core.socket; | ||
|
||
/* | ||
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class IOKitHardwareAddressTest { | ||
|
||
@Test | ||
public void getAddress() throws Exception { | ||
final byte[] address = new IOKitHardwareAddress().getAddress(); | ||
assertNotNull(address); | ||
assertEquals(6, address.length); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
core/src/test/java/ch/cyberduck/core/socket/NetworkInterfaceHardwareAddressTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ch.cyberduck.core.socket; | ||
|
||
/* | ||
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved. | ||
* https://cyberduck.io/ | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class NetworkInterfaceHardwareAddressTest { | ||
|
||
@Test | ||
public void getAddress() throws Exception { | ||
final byte[] address = new NetworkInterfaceHardwareAddress().getAddress(); | ||
assertNotNull(address); | ||
assertEquals(6, address.length); | ||
} | ||
} |