VARBIND.H: #ifndef VARBIND_H #define VARBIND_H struct snmp_varbind *varbind_new_type_gauge(const struct snmp_obj_id *oid, u32 value); struct snmp_varbind *varbind_new_type_integer(const struct snmp_obj_id *oid, s32 value); struct snmp_varbind *varbind_new_type_string(const struct snmp_obj_id *oid, const char *s); struct snmp_varbind *varbind_new_type_null(const struct snmp_obj_id *oid); struct snmp_varbind *varbind_new_from_id(const struct snmp_obj_id *oid); void varbind_list_insert(struct snmp_varbind **varbind_list, struct snmp_varbind *varbind); void varbind_list_free(struct snmp_varbind *varbind_list); #endif /*VARBIND_H*/ VARBIND.C: #include #include #include "varbind.h" // Not a lwIP public function, yet. u8_t snmp_get_node_instance_from_oid(const u32_t *oid, u8_t oid_len, struct snmp_node_instance *node_instance); struct snmp_varbind *varbind_new_type_null(const struct snmp_obj_id *oid) { struct snmp_varbind *varbind; // Allocate space for varbind varbind = calloc(sizeof(*varbind)); if (!varbind) { return NULL; } varbind->oid = *oid; varbind->type = SNMP_ASN1_TYPE_NULL; varbind->value = NULL; varbind->value_len = 0; return varbind; } struct snmp_varbind *varbind_new_type_gauge(const struct snmp_obj_id *oid, u32 value) { struct snmp_varbind *varbind; // Allocate space for varbind varbind = calloc(sizeof(*varbind)); if (!varbind) { return NULL; } // Allocate space for varbind value varbind->value = malloc(sizeof(u32_t)); if (!varbind->value) { free(varbind); return NULL; } varbind->oid = *oid; varbind->type = SNMP_ASN1_TYPE_GAUGE; *(u32_t *)varbind->value = value; varbind->value_len = sizeof(u32_t); return varbind; } struct snmp_varbind *varbind_new_type_integer(const struct snmp_obj_id *oid, s32 value) { struct snmp_varbind *varbind; // Allocate space for varbind varbind = calloc(sizeof(*varbind)); if (!varbind) { return NULL; } // Allocate space for varbind value varbind->value = malloc(sizeof(s32_t)); if (!varbind->value) { free(varbind); return NULL; } varbind->oid = *oid; varbind->type = SNMP_ASN1_TYPE_INTEGER; *(s32_t *)varbind->value = value; varbind->value_len = sizeof(s32_t); return varbind; } struct snmp_varbind *varbind_new_type_string(const struct snmp_obj_id *oid, const char *s) { struct snmp_varbind *varbind; int len = strlen(s); // Allocate space for varbind varbind = calloc(sizeof(*varbind)); if (!varbind) { return NULL; } // Allocate space for varbind value varbind->value = malloc(len); if (!varbind->value) { free(varbind); return NULL; } varbind->oid = *oid; varbind->type = SNMP_ASN1_TYPE_OCTET_STRING; memcpy(varbind->value, s, len); varbind->value_len = len; return varbind; } struct snmp_varbind *varbind_new_from_id(const struct snmp_obj_id *oid) { struct snmp_varbind *varbind; struct snmp_node_instance node_instance; // Allocate space for varbind varbind = calloc(sizeof(*varbind)); if (!varbind) { return NULL; } // Get oid mode if (snmp_get_node_instance_from_oid(oid->id, oid->len, &node_instance)) { free(varbind); return NULL; } // Allocate space for varbind value varbind->value = malloc(SNMP_MAX_VALUE_SIZE); if (!varbind->value) { free(varbind); return NULL; } // Set varbind values varbind->oid = *oid; varbind->type = node_instance.asn1_type; varbind->value_len = node_instance.get_value(&node_instance, varbind->value); return varbind; } static void varbind_free(struct snmp_varbind *varbind) { if (varbind->value) { free(varbind->value); } free(varbind); } void varbind_list_insert(struct snmp_varbind **varbind_list, struct snmp_varbind *varbind) { if (!varbind) { // Nothing to add return; } if (*varbind_list) { // List not empty (*varbind_list)->prev = varbind; } // Insert to list varbind->next = *varbind_list; *varbind_list = varbind; } void varbind_list_free(struct snmp_varbind *varbind_list) { while (varbind_list) { struct snmp_varbind *next = varbind_list->next; varbind_free(varbind_list); varbind_list = next; } }