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

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