Make MAC address UUID more robust
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Mar 2024 07:53:42 +0000 (08:53 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Mar 2024 07:53:42 +0000 (08:53 +0100)
org.argeo.api.uuid/src/org/argeo/api/uuid/MacAddressUuidFactory.java

index 51d68e3ef2aefb0420366ae212a17c108e229ceb..6cbe98178123bb59b7a4342a87efdd7a260869f9 100644 (file)
@@ -35,17 +35,19 @@ public class MacAddressUuidFactory extends ConcurrentUuidFactory {
                try {
                        localHost = InetAddress.getLocalHost();
                        NetworkInterface nic = NetworkInterface.getByInetAddress(localHost);
-                       if (nic != null)
+                       if (nic != null && nic.getHardwareAddress() != null)
                                return hardwareAddressToNodeId(nic);
                        Enumeration<NetworkInterface> netInterfaces = null;
-                       try {
-                               netInterfaces = NetworkInterface.getNetworkInterfaces();
-                       } catch (SocketException e) {
-                               throw new IllegalStateException(e);
-                       }
+                       netInterfaces = NetworkInterface.getNetworkInterfaces();
                        if (netInterfaces == null || !netInterfaces.hasMoreElements())
                                throw new IllegalStateException("No interfaces");
-                       return hardwareAddressToNodeId(netInterfaces.nextElement());
+                       while (netInterfaces.hasMoreElements()) {
+                               // TODO find out public/physical interfaces
+                               nic = netInterfaces.nextElement();
+                               if (nic.getHardwareAddress() != null)
+                                       return hardwareAddressToNodeId(nic);
+                       }
+                       throw new IllegalStateException("No interfaces with a MAC address");
                } catch (UnknownHostException | SocketException e) {
                        throw new IllegalStateException(e);
                }