From b08604d44de317ee772a47c4500f77c91aae651c Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Mon, 20 Jan 2020 10:21:04 +0100
Subject: [PATCH] use loopback interface if its the only one up-and-running.

---
 new_discovery.c | 35 +++++++++++++++++++++++++++++------
 old_discovery.c | 42 +++++++++++++++++++++++++-----------------
 2 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/new_discovery.c b/new_discovery.c
index 4d7a497..3fba2ea 100644
--- a/new_discovery.c
+++ b/new_discovery.c
@@ -71,14 +71,37 @@ void new_discovery() {
     struct ifaddrs *addrs,*ifa;
     getifaddrs(&addrs);
     ifa = addrs;
+
+//
+//  count number of "up and running" interfaces
+//
+    int num_up_and_running = 0;
+    while (ifa) {
+      if (ifa->ifa_addr) {
+        if (ifa->ifa_addr->sa_family == AF_INET
+            && (ifa->ifa_flags & IFF_UP) == IFF_UP
+            && (ifa->ifa_flags & IFF_RUNNING) == IFF_RUNNING) {
+                num_up_and_running++;
+        }
+      }
+      ifa=ifa->ifa_next;
+    }
+
+    ifa=addrs;
     while (ifa) {
         g_main_context_iteration(NULL, 0);
-        if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
-            if((ifa->ifa_flags&IFF_UP)==IFF_UP
-                && (ifa->ifa_flags&IFF_RUNNING)==IFF_RUNNING
-                && (ifa->ifa_flags&IFF_LOOPBACK)!=IFF_LOOPBACK) {
-                new_discover(ifa);
-            }
+        if (ifa->ifa_addr) {
+          if(ifa->ifa_addr->sa_family == AF_INET &&
+            (ifa->ifa_flags&IFF_UP)==IFF_UP &&
+            (ifa->ifa_flags&IFF_RUNNING)==IFF_RUNNING) {
+//
+// if this is the loopback interface, do the discover only if
+// there is at most one interface
+//
+                if ((ifa->ifa_flags & IFF_LOOPBACK) != IFF_LOOPBACK
+                    || num_up_and_running <= 1)
+                                new_discover(ifa);
+          }
         }
         ifa = ifa->ifa_next;
     }
diff --git a/old_discovery.c b/old_discovery.c
index 9f065b7..82b4230 100644
--- a/old_discovery.c
+++ b/old_discovery.c
@@ -515,33 +515,41 @@ void old_discovery() {
 fprintf(stderr,"old_discovery\n");
     getifaddrs(&addrs);
     ifa = addrs;
+//
+//  count number of "up and running" interfaces
+//
+    int num_up_and_running = 0;
+    while (ifa) {
+      if (ifa->ifa_addr) {
+	if (ifa->ifa_addr->sa_family == AF_INET
+	    && (ifa->ifa_flags & IFF_UP) == IFF_UP
+            && (ifa->ifa_flags & IFF_RUNNING) == IFF_RUNNING) {
+		num_up_and_running++;
+	}
+      }
+      ifa=ifa->ifa_next;
+    }
+	
+    ifa=addrs;
     while (ifa) {
         g_main_context_iteration(NULL, 0);
-        if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
-#if 0
+        if (ifa->ifa_addr) {
+ 	  if(ifa->ifa_addr->sa_family == AF_INET &&
+            (ifa->ifa_flags&IFF_UP)==IFF_UP &&
+            (ifa->ifa_flags&IFF_RUNNING)==IFF_RUNNING) {
 //
-// used to be a hook for RADIOBERRY, but does is really need the
-// loopback interface?
+// if this is the loopback interface, do the discover only if
+// there is at most one interface
 //
-			if((ifa->ifa_flags&IFF_UP)==IFF_UP
-                && (ifa->ifa_flags&IFF_RUNNING)==IFF_RUNNING) {
+		if ((ifa->ifa_flags & IFF_LOOPBACK) != IFF_LOOPBACK
+		    || num_up_and_running <= 1)
 				discover(ifa);
-			}
-#else
-            if((ifa->ifa_flags&IFF_UP)==IFF_UP
-                && (ifa->ifa_flags&IFF_RUNNING)==IFF_RUNNING
-                && (ifa->ifa_flags&IFF_LOOPBACK)!=IFF_LOOPBACK) {
-                discover(ifa);
-            }
-#endif 
+	  }
         }
         ifa = ifa->ifa_next;
     }
     freeifaddrs(addrs);
 
-    // Do one additional "discover" for a fixed TCP address
-    discover(NULL);
-
     fprintf(stderr, "discovery found %d devices\n",devices);
 
     int i;
-- 
2.45.2