Skip to content

Commit

Permalink
Improved SNMP implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yagoor committed Aug 15, 2019
1 parent 5211beb commit 7fd1624
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 3 deletions.
10 changes: 10 additions & 0 deletions os/net/app-layer/snmp/snmp-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "snmp-message.h"
#include "snmp-ber.h"
#include "snmp-oid.h"

static void
snmp_api_replace_oid(snmp_varbind_t *varbind, uint32_t *oid)
Expand Down Expand Up @@ -64,3 +65,12 @@ snmp_api_set_time_ticks(snmp_varbind_t *varbind, uint32_t *oid, uint32_t integer
varbind->value_type = SNMP_DATA_TYPE_TIME_TICKS;
varbind->value.integer = integer;
}
void
snmp_api_set_oid(snmp_varbind_t *varbind, uint32_t *oid, uint32_t *ret_oid)
{

snmp_api_replace_oid(varbind, oid);
varbind->value_type = BER_DATA_TYPE_OID;
snmp_oid_print(ret_oid);
varbind->value.oid = ret_oid;
}
28 changes: 27 additions & 1 deletion os/net/app-layer/snmp/snmp-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@
*/
typedef void (*snmp_mib_resource_handler_t)(snmp_varbind_t *varbind, uint32_t *oid);

/**
* @brief The MIB Resource struct
*/
typedef struct snmp_mib_resource_s snmp_mib_resource_t;

/**
* @brief Initializes statically an oid with the "null" terminator
*
* @remarks This should be used inside handlers when declaring an oid
*
* @param name A name for the oid
* @param ... The Oid (comma-separeted)
*/
#define OID(name, ...) \
static uint32_t name[] = { __VA_ARGS__, -1 };

/**
* @brief Declare a MIB resource
*
Expand Down Expand Up @@ -94,11 +110,21 @@ snmp_api_set_string(snmp_varbind_t *varbind, uint32_t *oid, char *string);
*
* @param varbind The varbind from the handler
* @param oid The oid from the handler
* @param string The time tick value
* @param integer The time tick value
*/
void
snmp_api_set_time_ticks(snmp_varbind_t *varbind, uint32_t *oid, uint32_t integer);

/**
* @brief
*
* @param varbind
* @param oid
* @param ret_oid
*/
void
snmp_api_set_oid(snmp_varbind_t *varbind, uint32_t *oid, uint32_t *ret_oid);

/** @}*/

#endif /* SNMP_API_H_ */
Expand Down
1 change: 1 addition & 0 deletions os/net/app-layer/snmp/snmp-ber.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define BER_DATA_TYPE_INTEGER 0x02
#define BER_DATA_TYPE_OCTET_STRING 0x04
#define BER_DATA_TYPE_NULL 0x05
#define BER_DATA_TYPE_OID 0x06
#define BER_DATA_TYPE_SEQUENCE 0x30

/**
Expand Down
7 changes: 7 additions & 0 deletions os/net/app-layer/snmp/snmp-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ snmp_message_encode(unsigned char *out, uint32_t *out_len, snmp_header_t *header
case BER_DATA_TYPE_OCTET_STRING:
out = snmp_ber_encode_string_len(out, out_len, varbind->value.string.string, varbind->value.string.length);
break;
case BER_DATA_TYPE_OID:
LOG_DBG("HERE\n");
snmp_oid_print(varbind->value.oid);

out = snmp_oid_encode_oid(out, out_len, varbind->value.oid);
LOG_DBG("HERE\n");
break;
case BER_DATA_TYPE_NULL:
case SNMP_DATA_TYPE_NO_SUCH_INSTANCE:
case SNMP_DATA_TYPE_END_OF_MIB_VIEW:
Expand Down
32 changes: 30 additions & 2 deletions os/net/app-layer/snmp/snmp-mib.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,42 @@ void
snmp_mib_add(snmp_mib_resource_t *new_resource)
{
snmp_mib_resource_t *resource;
#if SNMP_DEBUG
/*
* We print the entire resource table
*/
LOG_DBG("Table before insert.\n");
for(resource = list_head(snmp_mib);
resource; resource = resource->next) {

if(snmp_oid_cmp_oid(resource->oid, new_resource->oid) < 0) {
snmp_oid_print(resource->oid);
}
#endif /* SNMP_DEBUG */

for(resource = list_head(snmp_mib);
resource; resource = resource->next) {

if(snmp_oid_cmp_oid(resource->oid, new_resource->oid) > 0) {
break;
}
}
list_insert(snmp_mib, resource, new_resource);
if(resource == NULL) {
list_add(snmp_mib, new_resource);
} else {
list_insert(snmp_mib, new_resource, resource);
}

#if SNMP_DEBUG
/*
* We print the entire resource table
*/
LOG_DBG("Table after insert.\n");
for(resource = list_head(snmp_mib);
resource; resource = resource->next) {

snmp_oid_print(resource->oid);
}
#endif /* SNMP_DEBUG */
}
void
snmp_mib_init(void)
Expand Down
30 changes: 30 additions & 0 deletions os/net/app-layer/snmp/snmp-oid.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ snmp_oid_cmp_oid(uint32_t *oid1, uint32_t *oid2)
{
uint8_t i;

#if SNMP_DEBUG
LOG_DBG("Comparing:\n");
snmp_oid_print(oid1);
snmp_oid_print(oid2);
#endif

i = 0;
while(oid1[i] != ((uint32_t)-1) &&
oid2[i] != ((uint32_t)-1)) {
if(oid1[i] != oid2[i]) {
if(oid1[i] < oid2[i]) {
#if SNMP_DEBUG
LOG_DBG("oid1[i] < oid2[i]\n");
#endif
return -1;
}
#if SNMP_DEBUG
LOG_DBG("oid1[i] > oid2[i]\n");
#endif
return 1;
}
i++;
Expand Down Expand Up @@ -195,3 +207,21 @@ snmp_oid_copy(uint32_t *dst, uint32_t *src)
dst[i] = src[i];
}
/*---------------------------------------------------------------------------*/
#if SNMP_DEBUG
void
snmp_oid_print(uint32_t *oid)
{
uint8_t i;

i = 0;
LOG_DBG("{");
while(oid[i] != ((uint32_t)-1)) {
LOG_DBG_("%lu", (unsigned long)oid[i]);
i++;
if(oid[i] != ((uint32_t)-1)) {
LOG_DBG_(".");
}
}
LOG_DBG_("}\n");
}
#endif /* SNMP_DEBUG */
10 changes: 10 additions & 0 deletions os/net/app-layer/snmp/snmp-oid.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,15 @@ snmp_oid_decode_oid(unsigned char *buf, uint32_t *buf_len, uint32_t *oid, uint32
void
snmp_oid_copy(uint32_t *dst, uint32_t *src);

#if SNMP_DEBUG
/**
* @brief Prints a oid
*
* @param oid A oid
*/
void
snmp_oid_print(uint32_t *oid);
#endif /* SNMP_DEBUG */

#endif /* SNMP_OID_H_ */
/** @} */
4 changes: 4 additions & 0 deletions os/net/app-layer/snmp/snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ typedef struct snmp_varbind_s {
*/
uint32_t length;
} string;
/**
* @brief A pointer to the beggining of a oid array
*/
uint32_t *oid;
} value;
} snmp_varbind_t;

Expand Down

0 comments on commit 7fd1624

Please sign in to comment.