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 <pat@voltanet.io>
This commit is contained in:
Pat Ruddy 2020-10-01 12:09:01 +01:00
parent 0760a74d2f
commit 4f13e83dcd
2 changed files with 29 additions and 1 deletions

View 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);

View 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)