]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
SNMP: Refactoring (readability)
authorVojtech Vilimek <vojtech.vilimek@nic.cz>
Mon, 4 Sep 2023 11:51:29 +0000 (13:51 +0200)
committerVojtech Vilimek <vojtech.vilimek@nic.cz>
Mon, 4 Sep 2023 11:51:29 +0000 (13:51 +0200)
proto/snmp/subagent.c
proto/snmp/subagent.h

index 864496293fa9830ce3e1a309ed9b2fc9124e08f0..66ebb4695e1803a268536cf92b0fd68729d4707c 100644 (file)
@@ -38,6 +38,8 @@ static uint update_packet_size(struct snmp_proto *p, byte *start, byte *end);
 static struct oid *search_mib(struct snmp_proto *p, const struct oid *o_start, const struct oid *o_end, struct oid *o_curr, struct snmp_pdu_context *c, enum snmp_search_res *result);
 static void notify_pdu(struct snmp_proto *p, struct oid *oid, void *opaque, uint size, int include_uptime);
 
+u32 snmp_internet[] = { SNMP_ISO, SNMP_ORG, SNMP_DOD, SNMP_INTERNET };
+
 static const char * const snmp_errs[] = {
   #define SNMP_ERR_SHIFT 256
   [AGENTX_RES_OPEN_FAILED        - SNMP_ERR_SHIFT] = "Open failed",
@@ -152,7 +154,7 @@ notify_pdu(struct snmp_proto *p, struct oid *oid, void *opaque, uint size, int i
     /* sysUpTime.0 oid */
     struct oid uptime = {
       .n_subid = 4,
-      .prefix = 2,
+      .prefix = SNMP_MGMT,
       .include = 0,
       .pad = 0,
     };
@@ -662,7 +664,7 @@ u8
 snmp_get_mib_class(const struct oid *oid)
 {
   // TODO check code paths for oid->n_subid < 3
-  if (oid->prefix != 2 && oid->ids[0] != SNMP_MIB_2)
+  if (oid->prefix != SNMP_MGMT && oid->ids[0] != SNMP_MIB_2)
     return SNMP_CLASS_INVALID;
 
   switch (oid->ids[1])
@@ -1433,7 +1435,6 @@ snmp_prefixize(struct snmp_proto *proto, const struct oid *oid, int byte_ord)
 {
   ASSERT(oid != NULL);
   snmp_log("snmp_prefixize()");
-  const u32 prefix[] = {1, 3, 6, 1};
 
   if (snmp_is_oid_empty(oid))
   {
@@ -1453,7 +1454,7 @@ snmp_prefixize(struct snmp_proto *proto, const struct oid *oid, int byte_ord)
   {  snmp_log("too small"); return NULL; }
 
   for (int i = 0; i < 4; i++)
-    if (LOAD_U32(oid->ids[i], byte_ord) != prefix[i])
+    if (LOAD_U32(oid->ids[i], byte_ord) != snmp_internet[i])
       { snmp_log("different prefix"); return NULL; }
 
   /* validity check here */
@@ -1482,20 +1483,13 @@ snmp_mib_fill2(struct snmp_proto *p, struct oid *oid,
 
   snmp_log("critical part");
   if (c->size < snmp_varbind_hdr_size_from_oid(oid))
-  {
     snmp_manage_tbuf(p, c);
-  }
-  else
-  {
-    u32 *ptr = mb_alloc(p->p.pool, sizeof(u32) * 4);
-    *ptr = 0xbeef;
-    mb_free(ptr);
-  }
+
   snmp_log("critical part done");
 
   struct agentx_varbind *vb = snmp_create_varbind(c->buffer, oid);
 
-  if (oid->n_subid < 2 || (oid->prefix != 2 && oid->ids[0] != 1))
+  if (oid->n_subid < 2 || (oid->prefix != SNMP_MGMT && oid->ids[0] != SNMP_MIB_2))
   {
     vb->type = AGENTX_NO_SUCH_OBJECT;
     ADVANCE(c->buffer, c->size, snmp_varbind_header_size(vb));
index 296589816b320e038ce0f20b5defa46754fad712..57e1f5eb58a2db8ce5d5f050a9e81a4d3d518cce 100644 (file)
@@ -14,10 +14,18 @@ void snmp_ping(struct snmp_proto *p);
 #define SNMP_STATE_BGP 1
 #define SNMP_STATE_INVALID 2
 
-#define SNMP_MIB_2        1           /* last of oid .1.3.6.1.2.1     */
-#define SNMP_OSPF_MIB    14          /* part of oid .1.3.6.1.2.1.14  */
-#define SNMP_BGP4_MIB    15          /* part of oid .1.3.6.1.2.1.15  */
-#define SNMP_OSPFv3_MIB 192          /* part of oid .1.3.6.1.2.1.192 */
+#define SNMP_ISO         1           /* last of oid .1               */
+#define SNMP_ORG         3           /* last of oid .1.3             */
+#define SNMP_DOD         6           /* last of oid .1.3.6           */
+#define SNMP_INTERNET    1           /* last of oid .1.3.6.1         */
+
+#define SNMP_MGMT        2           /* last of oid .1.3.6.1.2       */
+#define SNMP_MIB_2       1           /* last of oid .1.3.6.1.2.1     */
+#define SNMP_OSPF_MIB   14           /* part of oid .1.3.6.1.2.1.14  */
+#define SNMP_BGP4_MIB   15           /* part of oid .1.3.6.1.2.1.15  */
+#define SNMP_OSPFv3_MIB        192           /* part of oid .1.3.6.1.2.1.192 */
+
+extern u32 snmp_internet[4];
 
 enum SNMP_CLASSES {
   SNMP_CLASS_INVALID = 0,