]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add utilities to encode/decode an int in SNMP oid
authorPat Ruddy <pat@voltanet.io>
Thu, 1 Oct 2020 11:09:01 +0000 (12:09 +0100)
committerPat Ruddy <pat@voltanet.io>
Tue, 2 Feb 2021 09:37:08 +0000 (09:37 +0000)
Add 2 functions to encode/decode intergers to/from SNMP OIDs. Make
sure oid is in network format.

Signed-off-by: Pat Ruddy <pat@voltanet.io>
lib/smux.h
lib/snmp.c

index 0efe029d3d9f015ab1757852be38f0e210c890c6..66a45a0e77bffe3df528a3942eb52afa83efd46b 100644 (file)
@@ -104,8 +104,10 @@ extern int smux_trap(struct variable *, size_t, const oid *, size_t,
 
 extern int oid_compare(const oid *, int, const oid *, int);
 extern void oid2in_addr(oid[], int, struct in_addr *);
+extern void oid2int(oid oid[], int *dest);
 extern void *oid_copy(void *, const void *, size_t);
 extern void oid_copy_addr(oid[], const struct in_addr *, int);
+extern void oid_copy_int(oid oid[], int *val);
 extern void oid2string(oid oid[], int len, char *string);
 extern void oid_copy_str(oid oid[], const char *string, int len);
 
index cc317d7a3b96de20eb1c4035af7d73eaf48268e3..e92f622bb95642ba3cdcc7d35c7595e830ab7681 100644 (file)
@@ -64,6 +64,19 @@ void oid2in_addr(oid oid[], int len, struct in_addr *addr)
                *pnt++ = oid[i];
 }
 
+void oid2int(oid oid[], int *dest)
+{
+       uint8_t i;
+       uint8_t *pnt;
+       int network_dest;
+
+       pnt = (uint8_t *)&network_dest;
+
+       for (i = 0; i < sizeof(int); i++)
+               *pnt++ = oid[i];
+       *dest = ntohl(network_dest);
+}
+
 void oid_copy_addr(oid oid[], const struct in_addr *addr, int len)
 {
        int i;
@@ -78,6 +91,19 @@ void oid_copy_addr(oid oid[], const struct in_addr *addr, int len)
                oid[i] = *pnt++;
 }
 
+void oid_copy_int(oid oid[], int *val)
+{
+       uint8_t i;
+       const uint8_t *pnt;
+       int network_val;
+
+       network_val = htonl(*val);
+       pnt = (uint8_t *)&network_val;
+
+       for (i = 0; i < sizeof(int); i++)
+               oid[i] = *pnt++;
+}
+
 void oid2string(oid oid[], int len, char *string)
 {
        int i;
@@ -89,7 +115,7 @@ void oid2string(oid oid[], int len, char *string)
        pnt = (uint8_t *)string;
 
        for (i = 0; i < len; i++)
-               *pnt++ = oid[i];
+               *pnt++ = (uint8_t)oid[i];
 }
 
 void oid_copy_str(oid oid[], const char *string, int len)