]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
tmp: add new helper function
authorVojtech Vilimek <vojtech.vilimek@nic.cz>
Fri, 24 Mar 2023 14:00:54 +0000 (15:00 +0100)
committerVojtech Vilimek <vojtech.vilimek@nic.cz>
Fri, 24 Mar 2023 14:00:54 +0000 (15:00 +0100)
proto/snmp/snmp_utils.c
proto/snmp/snmp_utils.h

index 9d349cd0c8822a7899bbddad4a2a4e75f9b52873..3a06b0c9e8037911be29052bdc86b25ed4f57fde 100644 (file)
@@ -131,8 +131,8 @@ snmp_valid_ip4_index_unsafe(struct oid *o, uint start)
   for (int i = 0; i < 4; i++)
     if (o->ids[start + i] >= 256)
       return 0;        // false
-  return 1; // true 
+
+  return 1; // true
 }
 
 /**
@@ -163,6 +163,17 @@ snmp_put_str(byte *buf, const char *str)
   return buf + snmp_str_size(str);
 }
 
+byte *
+snmp_put_ip4(byte *buf, ip_addr addr)
+{
+  /* octet string has size 4 bytes */
+  STORE_PTR(buf, 4);
+
+  put_u32(buf+4, ipa_to_u32(addr));
+
+  return buf + 8;
+}
+
 byte *
 snmp_put_blank(byte *buf)
 {
@@ -258,25 +269,35 @@ void snmp_oid_dump(struct oid *oid)
 }
 
 /** snmp_oid_compare - find the lexicographical order relation between @left and @right
+ * both @left and @right has to be non-blank.
  * @left: left object id relation operant
  * @right: right object id relation operant
  *
- * function returns 0 if left == right -1 if left < right and 1 otherwise
+ * function returns 0 if left == right,
+ *   -1 if left < right,
+ *   and 1 otherwise
  */
 int
 snmp_oid_compare(struct oid *left, struct oid *right)
 {
   const u32 INTERNET_PREFIX[] = {1, 3, 6, 1};
 
+  /*
+  if (snmp_is_oid_empty(left) && snmp_is_oid_empty(right))
+    return 0;
+
+  if (snmp_is_oid_empty(right))
+    return -1;
+
+  if (snmp_is_oid_empty(left))
+    return 1;
+  */
+
   if (left->prefix == 0 && right->prefix == 0)
     goto test_ids;
 
   if (right->prefix == 0)
-  {
-    struct oid *temp = left;
-    left = right;
-    right = temp;
-  }
+    return (-1) * snmp_oid_compare(right, left);
 
   if (left->prefix == 0)
   {
index 3c38827d3712665897be2eb02a2cb607b39f3b85..913cf717476d727fe36c55bc80f0adbe17848cb0 100644 (file)
@@ -26,6 +26,8 @@ byte *snmp_put_str(byte *buf, const char *str);
 byte *snmp_put_blank(byte *buf);
 byte *snmp_put_oid(byte *buf, struct oid *oid);
 
+byte *snmp_put_ip4(byte *buf, ip_addr ip4);
+
 byte *snmp_put_fbyte(byte *buf, u8 data);
 
 void snmp_oid_ip4_index(struct oid *o, uint start, ip4_addr addr);