From: Pat Ruddy Date: Thu, 1 Oct 2020 11:09:01 +0000 (+0100) Subject: lib: add utilities to encode/decode an int in SNMP oid X-Git-Tag: base_8.0~405^2~11 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=4f13e83dcd034d8e684a8f3a8fdd18b5368d16ff;p=mirror%2Ffrr.git lib: add utilities to encode/decode an int in SNMP oid Add 2 functions to encode/decode intergers to/from SNMP OIDs. Make sure oid is in network format. Signed-off-by: Pat Ruddy --- diff --git a/lib/smux.h b/lib/smux.h index 0efe029d3d..66a45a0e77 100644 --- a/lib/smux.h +++ b/lib/smux.h @@ -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); diff --git a/lib/snmp.c b/lib/snmp.c index cc317d7a3b..e92f622bb9 100644 --- a/lib/snmp.c +++ b/lib/snmp.c @@ -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)