]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
tmp: compiles
authorVojtech Vilimek <vojtech.vilimek@nic.cz>
Tue, 29 Nov 2022 15:30:20 +0000 (16:30 +0100)
committerVojtech Vilimek <vojtech.vilimek@nic.cz>
Tue, 29 Nov 2022 15:30:20 +0000 (16:30 +0100)
proto/snmp/bgp_mib.h
proto/snmp/snmp.c
proto/snmp/snmp_test.c
proto/snmp/snmp_utils.c
proto/snmp/snmp_utils.h
proto/snmp/subagent.c
proto/snmp/subagent.h

index 9e18158ba6d11f0f66e56f70bf8d99ec1eeec2d9..56080010c2d857b7377e6e51236e7bb0a3d51d94 100644 (file)
@@ -2,6 +2,7 @@
 #define _BIRD_SNMP_BGP_MIB_H_
 
 #include "snmp.h"
+#include "subagent.h"
 
 /* peers attributes */
 enum BGP4_MIB {
index 0343168dcb3017c93def79df54ff7207845d7a74..eb517243af007d4ae705318bcd670a6285ca9902 100644 (file)
@@ -23,6 +23,15 @@ static void snmp_startup(struct snmp_proto *p);
 static void snmp_startup_timeout(timer *t);
 static void snmp_start_locked(struct object_lock *lock);
 
+
+static const char * const snmp_state[] = {
+  [SNMP_ERR]     = "SNMP ERROR",
+  [SNMP_DELAY]   = "SNMP DELAY",
+  [SNMP_INIT]    = "SNMP INIT",
+  [SNMP_REGISTR]  = "SNMP REGISTERING",
+  [SNMP_CONN]    = "SNMP CONNECTED",
+};
+
 static struct proto *
 snmp_init(struct proto_config *CF)
 {
@@ -50,6 +59,7 @@ snmp_init(struct proto_config *CF)
 static void
 snmp_startup_timeout(timer *t)
 {
+  log(L_INFO "startup timer triggered");
   snmp_startup(t->data);
 }
 
@@ -59,6 +69,9 @@ snmp_startup(struct snmp_proto *p)
   /* starting agentX communicaiton channel */
   log(L_INFO "preparing lock");
   struct object_lock *lock;
+  log(L_INFO "snmp_startup() object lock state %p", p->lock);
+
+  /* we could have the lock already acquired but be in ERROR state */
   lock = p->lock = olock_new(p->p.pool);
 
   lock->type = OBJLOCK_TCP;
@@ -136,6 +149,9 @@ snmp_sock_err(sock *sk, int err)
   rfree(p->sock);
   p->sock = NULL;
 
+  rfree(p->lock);
+  p->lock = NULL;
+
   p->state = SNMP_ERR;
   tm_start(p->startup_timer, 15 S);
 }
@@ -222,9 +238,12 @@ snmp_reconfigure(struct proto *P, struct proto_config *CF)
 
 static void snmp_show_proto_info(struct proto *P)
 {
-  //struct snmp_proto *sp = (void *) P;
+  struct snmp_proto *sp = (void *) P;
   struct snmp_config *c = (void *) P->cf;
 
+  cli_msg(-1006, "");
+  cli_msg(-1006, " snmp status %s", snmp_state[sp->state]);
+  cli_msg(-1006, "");
   cli_msg(-1006, "  BGP peers");
   struct snmp_bond *bond;
   WALK_LIST(bond, c->bgp_entries)
index d4cb8300436ccd790c119e479888444121ea0be6..d6acda34c78c8f42cd145a8ce6d1b64019479302 100644 (file)
   #define BYTE_ORD 0
 #endif
 
-void
-dump_oid(struct oid *oid)
-{
-  bt_debug(" OID DUMP: \n");
-  bt_debug("  n_subid = %3u  prefix = %3u  include %s  --- \n",
-    oid->n_subid, oid->prefix, (oid->include != 0) ? "yes" : "no" );
-
-  for (int i = 0; i < oid->n_subid; i++)
-    bt_debug(" %u:  %u\n", i + 1, oid->ids[i]);
+#define OID_ALLOCATE(size) mb_alloc(&root_pool, sizeof(struct oid) + (size) * sizeof (u32))
 
-  bt_debug(" OID DUMP END\n");
-}
+#define OID_INIT(oid, n_subid_, prefix_, include_, arr_)      \
+  (oid)->n_subid = (n_subid_);                               \
+  (oid)->prefix = (prefix_);                                 \
+  (oid)->include = (include_);                               \
+  memcpy((oid)->ids, (arr_), sizeof(arr_));                  \
 
 void
 test_fill(struct snmp_proto *p)
@@ -68,7 +63,7 @@ test_oid(struct oid *oid, uint base_size)
   oid->n_subid = base_size + 4;
   oid->ids[2] = 3;
   oid->ids[3] = 1;   // BGP4-MIB::bgpPeerEntry
-  dump_oid(oid);
+  snmp_oid_dump(oid);
   SNMP_EXPECTED(snmp_bgp_state(oid), BGP_INTERNAL_PEER_ENTRY);
   bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_PEER_ENTRY);
 
@@ -215,6 +210,13 @@ t_s_prefixize(void)
   test_fill(&snmp_proto);
 
   bt_debug("before seg fault\n");
+
+  if (snmp_is_oid_empty(NULL))
+    bt_debug("null oid is empty");
+  else
+    bt_debug("null oid is not empty");
+  
+  bt_debug("main cause\n");
   struct oid *tmp = snmp_prefixize(&snmp_proto, nulled, BYTE_ORD);
   bt_debug("after snmp_prefixize() call\n");
   bt_assert( NULL == tmp );
@@ -270,6 +272,135 @@ t_s_prefixize(void)
   return 1;
 }
 
+static int
+t_oid_compare(void)
+{
+  /* same length, no prefix */
+  struct oid *l1 = OID_ALLOCATE(5);
+  {
+    u32 arr[] = { 1, 2, 3, 4, 5 };
+    OID_INIT(l1, 5, 0, 1, arr);
+  }
+
+
+  struct oid *r1 = OID_ALLOCATE(5);
+  {
+    u32 arr[] = { 1, 2, 3, 4, 6 };
+    OID_INIT(r1, 5, 0, 0, arr);
+  }
+
+  bt_assert(snmp_oid_compare(l1, r1) == -1);
+  bt_assert(snmp_oid_compare(r1, l1) ==  1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  /* same results for prefixed oids */
+  l1->prefix = 1;
+  r1->prefix = 1;
+
+  bt_assert(snmp_oid_compare(l1, r1) == -1);
+  bt_assert(snmp_oid_compare(r1, l1) ==  1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  mb_free(l1);
+  mb_free(r1);
+
+
+  /* different length, no prefix */
+  l1 = OID_ALLOCATE(4);
+  {
+    u32 arr[] = { 1, 2, 3, 4 };
+    OID_INIT(l1, 4, 0, 0, arr);
+  }
+
+  r1 = OID_ALLOCATE(5);
+  {
+    u32 arr[] = { 1, 2, 3, 4, 1 };
+    OID_INIT(l1, 5, 0, 1, arr);
+  }
+
+  bt_assert(snmp_oid_compare(l1, r1) == -1);
+  bt_assert(snmp_oid_compare(r1, l1) ==  1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  /* same results for prefixed oids */
+  l1->prefix = 3;
+  r1->prefix = 3;
+
+  bt_assert(snmp_oid_compare(l1, r1) == -1);
+  bt_assert(snmp_oid_compare(r1, l1) ==  1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  mb_free(l1);
+  mb_free(r1);
+
+
+  /* inverse order different length, no prefix */  
+  l1 = OID_ALLOCATE(4);
+  {
+    u32 arr[] = { 1, 2, 3, 5 };
+    OID_INIT(l1, 4, 0, 0, arr);
+  }
+
+  r1 = OID_ALLOCATE(5);
+  {
+    u32 arr[] = { 1, 2, 3, 4, 1 };
+    OID_INIT(r1, 5, 0, 0, arr);
+  }
+
+  bt_assert(snmp_oid_compare(l1, r1) ==  1);
+  bt_assert(snmp_oid_compare(r1, l1) == -1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  /* same results for prefixed oids */
+  l1->prefix = 254;
+  r1->prefix = 254;
+
+  bt_assert(snmp_oid_compare(l1, r1) ==  1);
+  bt_assert(snmp_oid_compare(r1, l1) == -1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  mb_free(l1);
+  mb_free(r1);
+
+
+/* ==== MIXED PREFIXED / NON PREFIXED OID compare ==== */
+  /* same length, mixed */
+  l1 = OID_ALLOCATE(6);  /* OID .1.2.17.3.21.4 */
+  {
+    u32 arr[] = { 1, 2, 17, 3, 21, 4 };
+    OID_INIT(l1, 6, 0, 1, arr);
+  }
+
+  r1 = OID_ALLOCATE(1);  /* OID .1.3.6.1.5.3 */
+  {
+    u32 arr[] = { 3 };
+    OID_INIT(l1, 1, 5, 1, arr);
+  }
+
+  bt_assert(snmp_oid_compare(l1, r1) == -1);
+  bt_assert(snmp_oid_compare(r1, l1) ==  1);
+
+  bt_assert(snmp_oid_compare(l1, l1) ==  0);
+  bt_assert(snmp_oid_compare(r1, r1) ==  0);
+
+  mb_free(l1);
+  mb_free(r1);
+
+  return 1;
+}
+
 static int
 t_s_bgp_state(void)
 {
@@ -323,5 +454,7 @@ int main(int argc, char **argv)
 
   bt_test_suite(t_s_prefixize, "Function snmp_prefixize()");
 
+  bt_test_suite(t_oid_compare, "Function snmp_oid_compare()");
+
   return bt_exit_value();
 }
index 7bda1d741b9ba0c9300262c5e10b28c778434d1e..ba3510530da219df0cf54f737d8048716a3ed2da 100644 (file)
@@ -238,3 +238,63 @@ void snmp_oid_dump(struct oid *oid)
   log(L_WARN "OID DUMP END ====");
   log(L_WARN);
 }
+
+/** snmp_oid_compare - find the lexicographical order relation between @left and @right
+ * @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
+ */
+int
+snmp_oid_compare(struct oid *left, struct oid *right)
+{
+  const u32 INTERNET_PREFIX[] = {1, 3, 6, 1};
+
+  if (left->prefix == 0 && right->prefix == 0)
+    goto test_ids;
+
+  if (right->prefix == 0)
+  {
+    struct oid *temp = left;
+    left = right;
+    right = temp;
+  }
+
+  if (left->prefix == 0)
+  {
+    for (int i = 0; i < 4; i++)
+      if (left->ids[i] < INTERNET_PREFIX[i])
+       return -1;
+      else if (left->ids[i] > INTERNET_PREFIX[i])
+       return 1;
+
+    for (int i = 0; i < MIN(left->n_subid - 4, right->n_subid); i++)
+      if (left->ids[i + 4] < right->ids[i])
+       return -1;
+      else if (left->ids[i + 4] > right->ids[i])
+       return 1;
+
+    goto all_same;
+  }
+
+  if (left->prefix < right->prefix)
+    return -1;
+  else if (left->prefix > right->prefix)
+    return 1;
+
+test_ids:
+  for (int i = 0; i < MIN(left->n_subid, right->n_subid); i++)
+    if (left->ids[i] < right->ids[i])
+      return -1;
+    else if (left->ids[i] > right->ids[i])
+      return 1;
+
+all_same:
+  /* shorter sequence is before longer in lexicografical order  */
+  if (left->n_subid < right->n_subid)
+    return -1;
+  else if (left->n_subid > right->n_subid)
+    return 1;
+  else
+    return 0;
+}
index 6449f5d36fff6470906f65f11d2312c2b9a7eb94..1d2136f3f5bbfdb8c91b3472422cf7b253af229a 100644 (file)
@@ -29,4 +29,6 @@ void snmp_oid_ip4_index(struct oid *o, uint start, ip4_addr addr);
 
 void snmp_oid_dump(struct oid *oid);
 
+int snmp_oid_compare(struct oid *left, struct oid *right);
+
 #endif
index 68b489cd7f6802233b395114a52527ce6cd12c25..e4742e1ecbfd7e00d9cef1ede20c7775c8ed357b 100644 (file)
@@ -26,6 +26,9 @@
  *
  */
 
+static byte *snmp_mib_fill(struct snmp_proto *p, struct oid *oid, u8 mib_class,
+byte *buf, uint size, struct snmp_error *error, uint contid, int byte_ord);
+
 static int parse_response(struct snmp_proto *p, byte *buf, uint size);
 static int snmp_stop_ack(sock *sk, uint size);
 static void do_response(struct snmp_proto *p, byte *buf, uint size);
@@ -36,6 +39,7 @@ static void response_err_ind(byte *buf, uint err, uint ind);
 static struct oid *search_mib(struct snmp_proto *p, struct oid *o_start, struct oid *o_end, struct oid *o_curr, u8 mib_class, uint contid);
 static inline byte *find_n_fill(struct snmp_proto *p, struct oid *o, byte *buf, uint size, uint contid, int byte_ord);
 
+
 static const char * const snmp_errs[] = {
   #define SNMP_ERR_SHIFT 256
   [AGENTX_RES_OPEN_FAILED - SNMP_ERR_SHIFT] = "Open failed",
@@ -541,15 +545,28 @@ byte *pkt, uint rsize, uint contid, u8 mib_class, int byte_ord)
     .type = AGENTX_END_OF_MIB_VIEW,
   };
 
+  /*
   pkt = snmp_mib_fill(
     p, o_copy, mib_class, pkt, rsize, &error, contid, byte_ord
   );
+  */
 
   if (o_copy)
   {
+    pkt = snmp_mib_fill(
+      p, o_copy, mib_class, pkt, rsize, &error, contid, byte_ord
+    );
+
     mb_free(o_copy);
   }
+  else
+  {
+    struct agentx_varbind *vb = snmp_create_varbind(pkt, o_start);
+    pkt += snmp_varbind_size(vb);
+    vb->type = AGENTX_NO_SUCH_OBJECT;
+  }
 
+  log(L_INFO "over HERE ");
   return pkt;
 }
 
@@ -1317,6 +1334,9 @@ snmp_prefixize(struct snmp_proto *proto, struct oid *oid, int byte_ord)
 {
   const u32 prefix[] = {1, 3, 6, 1};
 
+  if (oid == NULL)
+    return NULL;
+
   if (snmp_is_oid_empty(oid))
   {
     /* allocate new zeroed oid */
index 4042f1896b625254a768a43beb891bacbffb1aba..a4217ebf5144a2ec5ac7e1b00c826c16e52392aa 100644 (file)
@@ -274,7 +274,4 @@ enum agentx_response_err {
 
 int snmp_rx(sock *sk, uint size);
 
-static byte *snmp_mib_fill(struct snmp_proto *p, struct oid *oid, u8 mib_class,
-byte *buf, uint size, struct snmp_error *error, uint contid, int byte_ord);
-
 #endif