From db2fdc6683fca73cb21d7e07005b63900e2026a1 Mon Sep 17 00:00:00 2001
From: terry-norbraten <tnorb@comcast.net>
Date: Wed, 10 Jun 2020 14:06:31 -0700
Subject: [PATCH] strengthen the findIp4Interface search criteria

---
 .../dis7/utilities/DisThreadedNetIF.java      | 54 ++++++++++---------
 1 file changed, 28 insertions(+), 26 deletions(-)

diff --git a/src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java b/src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
index d056a315fe..c44a36b13e 100644
--- a/src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
+++ b/src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
@@ -327,32 +327,34 @@ public class DisThreadedNetIF
     catch (InterruptedException ex) {}
   }
 
-  /** Find proper interface
-   * @return a network interface to use to join a multicast group
-   */
-  public static NetworkInterface findIp4Interface()
-  {
-    Enumeration<NetworkInterface> ifaces = null;
-      try {
-          ifaces = NetworkInterface.getNetworkInterfaces();
-      } catch (SocketException ex) {
-          Logger.getLogger(DisThreadedNetIF.class.getName()).log(Level.SEVERE, null, ex);
-      }
-    NetworkInterface nif;
-    Enumeration<InetAddress> addresses;
-    InetAddress addr;
-    
-    while (ifaces != null && ifaces.hasMoreElements()) {
-      nif = ifaces.nextElement();
-      addresses = nif.getInetAddresses();
-      while (addresses.hasMoreElements()) {
-        addr = addresses.nextElement();
-        if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
-          System.out.println("Using network interface " + nif.getDisplayName());
-          return nif;
+    /**
+     * Find proper interface
+     *
+     * @return a network interface to use to join a multicast group
+     */
+    public static NetworkInterface findIp4Interface() {
+        try {
+            Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
+            NetworkInterface nif;
+            Enumeration<InetAddress> addresses;
+            InetAddress addr;
+
+            while (ifaces != null && ifaces.hasMoreElements()) {
+                nif = ifaces.nextElement();
+                if (nif.isUp()) {
+                    addresses = nif.getInetAddresses();
+                    while (addresses.hasMoreElements()) {
+                        addr = addresses.nextElement();
+                        if (addr instanceof Inet4Address && !addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) {
+                            System.out.println("Using network interface " + nif.getDisplayName());
+                            return nif;
+                        }
+                    }
+                }
+            }
+        } catch (SocketException ex) {
+            Logger.getLogger(DisThreadedNetIF.class.getName()).log(Level.SEVERE, null, ex);
         }
-      }
+        return null;
     }
-    return null;
-  }
 }
-- 
GitLab