Prechádzať zdrojové kódy

Find the network interface by index on windows operating systems.

Jan Pohlmann 8 rokov pred
rodič
commit
222530cfb7

+ 10 - 2
RISE-V2G-Shared/src/main/java/org/v2gclarity/risev2g/shared/utils/MiscUtils.java

@@ -65,7 +65,11 @@ public final class MiscUtils {
 		NetworkInterface nif = null;
 		
 		try {
-			nif = NetworkInterface.getByName(networkInterfaceConfig);
+			if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
+				nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
+			} else {
+				nif = NetworkInterface.getByName(networkInterfaceConfig);
+			}
 			Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
 			
 			while (inetAddresses.hasMoreElements()) {
@@ -96,7 +100,11 @@ public final class MiscUtils {
 		byte[] macAddress = null;
 		
 		try {
-			nif = NetworkInterface.getByName(networkInterfaceConfig);
+			if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
+				nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
+			} else {
+				nif = NetworkInterface.getByName(networkInterfaceConfig);
+			}
 			macAddress = nif.getHardwareAddress();
 		} catch (SocketException e) {
 			getLogger().error("Failed to retrieve local mac address (SocketException)", e);