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

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