Blame SOURCES/net-snmp-5.8-ipAddress-faster-load.patch

f13900
diff -urNp a/agent/mibgroup/mibII/ipAddr.c b/agent/mibgroup/mibII/ipAddr.c
f13900
--- a/agent/mibgroup/mibII/ipAddr.c	2020-06-10 14:14:30.113696471 +0200
f13900
+++ b/agent/mibgroup/mibII/ipAddr.c	2020-06-10 14:27:15.345354018 +0200
f13900
@@ -495,14 +495,16 @@ Address_Scan_Next(Index, Retin_ifaddr)
f13900
 }
f13900
 
f13900
 #elif defined(linux)
f13900
+#include <errno.h>
f13900
 static struct ifreq *ifr;
f13900
 static int ifr_counter;
f13900
 
f13900
 static void
f13900
 Address_Scan_Init(void)
f13900
 {
f13900
-    int num_interfaces = 0;
f13900
+    int i;
f13900
     int fd;
f13900
+    int lastlen = 0;
f13900
 
f13900
     /* get info about all interfaces */
f13900
 
f13900
@@ -510,28 +512,45 @@ Address_Scan_Init(void)
f13900
     SNMP_FREE(ifc.ifc_buf);
f13900
     ifr_counter = 0;
f13900
 
f13900
-    do
f13900
-    {
f13900
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
f13900
 	{
f13900
 	    DEBUGMSGTL(("snmpd", "socket open failure in Address_Scan_Init\n"));
f13900
 	    return;
f13900
 	}
f13900
-	num_interfaces += 16;
f13900
 
f13900
-	ifc.ifc_len = sizeof(struct ifreq) * num_interfaces;
f13900
-	ifc.ifc_buf = (char*) realloc(ifc.ifc_buf, ifc.ifc_len);
f13900
-	
f13900
-	    if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
f13900
-	    {
f13900
-		ifr=NULL;
f13900
-		close(fd);
f13900
-	   	return;
f13900
-	    }
f13900
-	    close(fd);
f13900
+    /*
f13900
+     * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF
f13900
+     * on some platforms; see W. R. Stevens, ``Unix Network Programming
f13900
+     * Volume I'', p.435...
f13900
+     */
f13900
+
f13900
+    for (i = 8;; i *= 2) {
f13900
+        ifc.ifc_len = sizeof(struct ifreq) * i;
f13900
+        ifc.ifc_req = calloc(i, sizeof(struct ifreq));
f13900
+
f13900
+        if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
f13900
+            if (errno != EINVAL || lastlen != 0) {
f13900
+                /*
f13900
+                 * Something has gone genuinely wrong...
f13900
+                 */
f13900
+                snmp_log(LOG_ERR, "bad rc from ioctl, errno %d", errno);
f13900
+                SNMP_FREE(ifc.ifc_buf);
f13900
+                close(fd);
f13900
+                return;
f13900
+            }
f13900
+        } else {
f13900
+            if (ifc.ifc_len == lastlen) {
f13900
+                /*
f13900
+                 * The length is the same as the last time; we're done...
f13900
+                 */
f13900
+                break;
f13900
+            }
f13900
+            lastlen = ifc.ifc_len;
f13900
+        }
f13900
+        free(ifc.ifc_buf); /* no SNMP_FREE, getting ready to reassign */
f13900
     }
f13900
-    while (ifc.ifc_len >= (sizeof(struct ifreq) * num_interfaces));
f13900
-    
f13900
+
f13900
+    close(fd);
f13900
     ifr = ifc.ifc_req;
f13900
 }
f13900