Blame SOURCES/net-snmp-5.7.2-ipCidrRouteTable-duplicates.patch

b5ae06
1172013 - NetworkManager causes snmp OID not increasing
b5ae06
b5ae06
commit 664ed943f63dfe9393e959840ecd23c31c9d8f89
b5ae06
Author: Bill Fenner <fenner@gmail.com>
b5ae06
Date:   Wed Aug 27 16:02:57 2014 -0400
b5ae06
b5ae06
    Handle duplicates in a binary_array container
b5ae06
    
b5ae06
    The CONTAINER_KEY_ALLOW_DUPLICATES setting is fundamentally flawed;
b5ae06
    it really effectively meant "I promise I won't insert duplicates
b5ae06
    so don't check at insert time".  However, the ip-forward-mib
b5ae06
    sets this flag but relies on the duplicate prevention at insert
b5ae06
    time under certain scenarios (e.g., multiple attachments to the
b5ae06
    same subnet on MacOS), resulting in a loop in ip-forward-mib
b5ae06
    in these scenarios.  So, now it means "check for duplicates at
b5ae06
    getnext time" - the binary search will find an arbitrary one
b5ae06
    of the entries with the same key, and when we've incremented
b5ae06
    we have to check whether or not we've actually incremented past
b5ae06
    any duplicates.  This costs an extra key compare per getnext.
b5ae06
    
b5ae06
    If there's a scenario in the future where a MIB implementation
b5ae06
    can really guarantee that it isn't inserting duplicates, we
b5ae06
    might want to add a "CONTAINER_KEY_I_PROMISE_I_WONT_INSERT_DUPLICATES",
b5ae06
    that disables the insertion check but doesn't perform the getnext
b5ae06
    check.
b5ae06
b5ae06
diff --git a/snmplib/container_binary_array.c b/snmplib/container_binary_array.c
b5ae06
index 249a3a9..10ae67f 100644
b5ae06
--- a/snmplib/container_binary_array.c
b5ae06
+++ b/snmplib/container_binary_array.c
b5ae06
@@ -284,6 +284,22 @@ netsnmp_binary_array_get(netsnmp_container *c, const void *key, int exact)
b5ae06
     if (key) {
b5ae06
         if ((index = binary_search(key, c, exact)) == -1)
b5ae06
             return NULL;
b5ae06
+        if (!exact &&
b5ae06
+            c->flags & CONTAINER_KEY_ALLOW_DUPLICATES) {
b5ae06
+            int result;
b5ae06
+
b5ae06
+            /*
b5ae06
+             * If duplicates are allowed, we have to be extra
b5ae06
+             * sure that we didn't just increment to a duplicate,
b5ae06
+             * thus causing a getnext loop.
b5ae06
+             */
b5ae06
+            result = c->compare(t->data[index], key);
b5ae06
+            while (result == 0) {
b5ae06
+                if (++index == t->count)
b5ae06
+                   return NULL;
b5ae06
+                result = c->compare(t->data[index], key);
b5ae06
+            }
b5ae06
+        }
b5ae06
     }
b5ae06
 
b5ae06
     return t->data[index];
b5ae06
diff --git a/testing/fulltests/unit-tests/T021binary_array_oid_duplicates_clib.c b/testing/fulltests/unit-tests/T021binary_array_oid_duplicates_clib.c
b5ae06
new file mode 100644
b5ae06
index 0000000..c027329
b5ae06
--- /dev/null
b5ae06
+++ b/testing/fulltests/unit-tests/T021binary_array_oid_duplicates_clib.c
b5ae06
@@ -0,0 +1,72 @@
b5ae06
+/* HEADER Testing duplicate handling in binary OID array */
b5ae06
+
b5ae06
+/* Much copied from T012 */
b5ae06
+static const char test_name[] = "binary-array-of-OIDs-duplicate-test";
b5ae06
+oid o1 = 1;
b5ae06
+oid o2 = 2;
b5ae06
+oid o3 = 6;
b5ae06
+oid o4 = 8;
b5ae06
+oid o5 = 9;
b5ae06
+oid ox = 7;
b5ae06
+oid oy = 10;
b5ae06
+netsnmp_index i1, i2, i3, i4, i5, ix, iy, *ip;
b5ae06
+netsnmp_index *b[] = { &i4, &i2, &i3, &i1, &i5 };
b5ae06
+netsnmp_container *c;
b5ae06
+int i;
b5ae06
+
b5ae06
+init_snmp(test_name);
b5ae06
+
b5ae06
+c = netsnmp_container_get_binary_array();
b5ae06
+c->compare = netsnmp_compare_netsnmp_index;
b5ae06
+netsnmp_binary_array_options_set(c, 1,
b5ae06
+				 CONTAINER_KEY_ALLOW_DUPLICATES);
b5ae06
+
b5ae06
+i1.oids = &o1;
b5ae06
+i2.oids = &o2;
b5ae06
+i3.oids = &o3;
b5ae06
+i4.oids = &o4;
b5ae06
+i5.oids = &o5;
b5ae06
+ix.oids = &ox;
b5ae06
+iy.oids = &oy;
b5ae06
+i1.len = i2.len = i3.len = i4.len = i5.len = ix.len = iy.len = 1;
b5ae06
+
b5ae06
+for (i = 0; i < sizeof(b)/sizeof(b[0]); ++i)
b5ae06
+    CONTAINER_INSERT(c, b[i]);
b5ae06
+
b5ae06
+#define MAX_ROUNDS 6
b5ae06
+/* Insert some duplicates of i4; also insert a duplicate of
b5ae06
+ * i1 to move the contents of the array around. */
b5ae06
+for (i = 0; i < MAX_ROUNDS; ++i) {
b5ae06
+    switch (i) {
b5ae06
+	case 0:
b5ae06
+	    /* First round: no insert */
b5ae06
+	    break;
b5ae06
+	case 1:
b5ae06
+	case 2:
b5ae06
+	case 4:
b5ae06
+	case 5:
b5ae06
+	    /* Insert another duplicate of our target object */
b5ae06
+	    CONTAINER_INSERT(c, &i4;;
b5ae06
+	    break;
b5ae06
+	case 3:
b5ae06
+	    /* Insert a dulicate of an earlier OID, so that it
b5ae06
+	     * changes the binary search behavior */
b5ae06
+	    CONTAINER_INSERT(c, &i1;;
b5ae06
+	    break;
b5ae06
+    }
b5ae06
+    /* Primary requirement: getnext returns the next value! */
b5ae06
+    ip = CONTAINER_FIND(c, &i4;;
b5ae06
+    OKF(ip, ("FIND returned a value"));
b5ae06
+    OKF(c->compare(&i4, ip) == 0,
b5ae06
+        ("FIND returned oid %" NETSNMP_PRIo "d", ip->oids[0]));
b5ae06
+    ip = CONTAINER_NEXT(c, &i4;;
b5ae06
+    OKF(ip, ("NEXT returned a value"));
b5ae06
+    OKF(c->compare(&i5, ip) == 0,
b5ae06
+        ("NEXT returned index 5 = %" NETSNMP_PRIo "d", i5.oids[0]));
b5ae06
+}
b5ae06
+
b5ae06
+while ((ip = CONTAINER_FIRST(c)))
b5ae06
+  CONTAINER_REMOVE(c, ip);
b5ae06
+CONTAINER_FREE(c);
b5ae06
+
b5ae06
+snmp_shutdown(test_name);