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

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