3 * MIB tree access/construction functions.
7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * Author: Christiaan Simons <christiaan.simons@axon.tv>
37 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
39 #include "lwip/snmp_structs.h"
42 /** .iso.org.dod.internet address prefix, @see snmp_iso_*() */
43 const s32_t prefix[4] = {1, 3, 6, 1};
45 #define NODE_STACK_SIZE (LWIP_SNMP_OBJ_ID_LEN)
46 /** node stack entry (old news?) */
50 struct mib_node* r_ptr;
51 /** right child identifier */
53 /** right child next level */
56 static u8_t node_stack_cnt;
57 static struct nse node_stack[NODE_STACK_SIZE];
60 * Pushes nse struct onto stack.
63 push_node(struct nse* node)
65 LWIP_ASSERT("node_stack_cnt < NODE_STACK_SIZE",node_stack_cnt < NODE_STACK_SIZE);
66 LWIP_DEBUGF(SNMP_MIB_DEBUG,("push_node() node=%p id=%"S32_F"\n",(void*)(node->r_ptr),node->r_id));
67 if (node_stack_cnt < NODE_STACK_SIZE)
69 node_stack[node_stack_cnt] = *node;
75 * Pops nse struct from stack.
78 pop_node(struct nse* node)
80 if (node_stack_cnt > 0)
83 *node = node_stack[node_stack_cnt];
85 LWIP_DEBUGF(SNMP_MIB_DEBUG,("pop_node() node=%p id=%"S32_F"\n",(void *)(node->r_ptr),node->r_id));
89 * Conversion from ifIndex to lwIP netif
90 * @param ifindex is a s32_t object sub-identifier
91 * @param netif points to returned netif struct pointer
94 snmp_ifindextonetif(s32_t ifindex, struct netif **netif)
96 struct netif *nif = netif_list;
101 while ((nif != NULL) && (i < ifidx))
110 * Conversion from lwIP netif to ifIndex
111 * @param netif points to a netif struct
112 * @param ifidx points to s32_t object sub-identifier
115 snmp_netiftoifindex(struct netif *netif, s32_t *ifidx)
117 struct netif *nif = netif_list;
130 * Conversion from oid to lwIP ip_addr
131 * @param ident points to s32_t ident[4] input
132 * @param ip points to output struct
135 snmp_oidtoip(s32_t *ident, struct ip_addr *ip)
150 * Conversion from lwIP ip_addr to oid
151 * @param ip points to input struct
152 * @param ident points to s32_t ident[4] output
155 snmp_iptooid(struct ip_addr *ip, s32_t *ident)
160 ident[0] = (ipa >> 24) & 0xff;
161 ident[1] = (ipa >> 16) & 0xff;
162 ident[2] = (ipa >> 8) & 0xff;
163 ident[3] = ipa & 0xff;
166 struct mib_list_node *
167 snmp_mib_ln_alloc(s32_t id)
169 struct mib_list_node *ln;
171 ln = (struct mib_list_node *)mem_malloc(sizeof(struct mib_list_node));
183 snmp_mib_ln_free(struct mib_list_node *ln)
188 struct mib_list_rootnode *
189 snmp_mib_lrn_alloc(void)
191 struct mib_list_rootnode *lrn;
193 lrn = (struct mib_list_rootnode*)mem_malloc(sizeof(struct mib_list_rootnode));
196 lrn->get_object_def = noleafs_get_object_def;
197 lrn->get_value = noleafs_get_value;
198 lrn->set_test = noleafs_set_test;
199 lrn->set_value = noleafs_set_value;
200 lrn->node_type = MIB_NODE_LR;
210 snmp_mib_lrn_free(struct mib_list_rootnode *lrn)
216 * Inserts node in idx list in a sorted
217 * (ascending order) fashion and
218 * allocates the node if needed.
220 * @param rn points to the root node
221 * @param objid is the object sub identifier
222 * @param insn points to a pointer to the inserted node
223 * used for constructing the tree.
224 * @return -1 if failed, 1 if inserted, 2 if present.
227 snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **insn)
229 struct mib_list_node *nn;
232 LWIP_ASSERT("rn != NULL",rn != NULL);
234 /* -1 = malloc failure, 0 = not inserted, 1 = inserted, 2 = was present */
236 if (rn->head == NULL)
238 /* empty list, add first node */
239 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc empty list objid==%"S32_F"\n",objid));
240 nn = snmp_mib_ln_alloc(objid);
255 struct mib_list_node *n;
256 /* at least one node is present */
258 while ((n != NULL) && (insert == 0))
260 if (n->objid == objid)
262 /* node is already there */
263 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node already there objid==%"S32_F"\n",objid));
267 else if (n->objid < objid)
271 /* alloc and insert at the tail */
272 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins tail objid==%"S32_F"\n",objid));
273 nn = snmp_mib_ln_alloc(objid);
285 /* insertion failure */
291 /* there's more to explore: traverse list */
292 LWIP_DEBUGF(SNMP_MIB_DEBUG,("traverse list\n"));
298 /* n->objid > objid */
299 /* alloc and insert between n->prev and n */
300 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins n->prev, objid==%"S32_F", n\n",objid));
301 nn = snmp_mib_ln_alloc(objid);
306 /* insert at the head */
314 /* insert in the middle */
325 /* insertion failure */
335 LWIP_ASSERT("insert != 0",insert != 0);
340 * Finds node in idx list and returns deletion mark.
342 * @param rn points to the root node
343 * @param objid is the object sub identifier
344 * @param fn returns pointer to found node
345 * @return 0 if not found, 1 if deletable,
346 * 2 can't delete (2 or more children), 3 not a list_node
349 snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn)
352 struct mib_list_node *n;
354 LWIP_ASSERT("rn != NULL",rn != NULL);
356 while ((n != NULL) && (n->objid != objid))
364 else if (n->nptr == NULL)
366 /* leaf, can delete node */
371 struct mib_list_rootnode *r;
373 if (n->nptr->node_type == MIB_NODE_LR)
375 r = (struct mib_list_rootnode *)n->nptr;
378 /* can't delete node */
383 /* count <= 1, can delete node */
389 /* other node type */
398 * Removes node from idx list
399 * if it has a single child left.
401 * @param rn points to the root node
402 * @param n points to the node to delete
403 * @return the nptr to be freed by caller
405 struct mib_list_rootnode *
406 snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n)
408 struct mib_list_rootnode *next;
410 LWIP_ASSERT("rn != NULL",rn != NULL);
411 LWIP_ASSERT("n != NULL",n != NULL);
413 /* caller must remove this sub-tree */
414 next = (struct mib_list_rootnode*)(n->nptr);
422 /* not last node, new list begin */
423 n->next->prev = NULL;
426 else if (n == rn->tail)
431 /* not last node, new list end */
432 n->prev->next = NULL;
437 /* node must be in the middle */
438 n->prev->next = n->next;
439 n->next->prev = n->prev;
441 LWIP_DEBUGF(SNMP_MIB_DEBUG,("free list objid==%"S32_F"\n",n->objid));
454 * Searches tree for the supplied (scalar?) object identifier.
456 * @param node points to the root of the tree ('.internet')
457 * @param ident_len the length of the supplied object identifier
458 * @param ident points to the array of sub identifiers
459 * @param np points to the found object instance (rerurn)
460 * @return pointer to the requested parent (!) node if success, NULL otherwise
463 snmp_search_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np)
465 u8_t node_type, ext_level;
468 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node==%p *ident==%"S32_F"\n",(void*)node,*ident));
471 node_type = node->node_type;
472 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
474 struct mib_array_node *an;
479 /* array node (internal ROM or RAM, fixed length) */
480 an = (struct mib_array_node *)node;
482 while ((i < an->maxlength) && (an->objid[i] != *ident))
486 if (i < an->maxlength)
488 /* found it, if available proceed to child, otherwise inspect leaf */
489 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
490 if (an->nptr[i] == NULL)
492 /* a scalar leaf OR table,
493 inspect remaining instance number / table index */
494 np->ident_len = ident_len;
496 return (struct mib_node*)an;
500 /* follow next child pointer */
508 /* search failed, identifier mismatch (nosuchname) */
509 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed *ident==%"S32_F"\n",*ident));
515 /* search failed, short object identifier (nosuchname) */
516 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed, short object identifier\n"));
520 else if(node_type == MIB_NODE_LR)
522 struct mib_list_rootnode *lrn;
523 struct mib_list_node *ln;
527 /* list root node (internal 'RAM', variable length) */
528 lrn = (struct mib_list_rootnode *)node;
530 /* iterate over list, head to tail */
531 while ((ln != NULL) && (ln->objid != *ident))
537 /* found it, proceed to child */;
538 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
539 if (ln->nptr == NULL)
541 np->ident_len = ident_len;
543 return (struct mib_node*)lrn;
547 /* follow next child pointer */
556 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed *ident==%"S32_F"\n",*ident));
562 /* search failed, short object identifier (nosuchname) */
563 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed, short object identifier\n"));
567 else if(node_type == MIB_NODE_EX)
569 struct mib_external_node *en;
574 /* external node (addressing and access via functions) */
575 en = (struct mib_external_node *)node;
578 len = en->level_length(en->addr_inf,ext_level);
579 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) != 0))
587 en->get_objid(en->addr_inf,ext_level,i,&debug_id);
588 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid==%"S32_F" *ident==%"S32_F"\n",debug_id,*ident));
589 if ((ext_level + 1) == en->tree_levels)
591 np->ident_len = ident_len;
593 return (struct mib_node*)en;
597 /* found it, proceed to child */
606 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed *ident==%"S32_F"\n",*ident));
612 /* search failed, short object identifier (nosuchname) */
613 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed, short object identifier\n"));
617 else if (node_type == MIB_NODE_SC)
621 sn = (mib_scalar_node *)node;
622 if ((ident_len == 1) && (*ident == 0))
624 np->ident_len = ident_len;
626 return (struct mib_node*)sn;
630 /* search failed, short object identifier (nosuchname) */
631 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed, invalid object identifier length\n"));
637 /* unknown node_type */
638 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node_type %"U16_F" unkown\n",(u16_t)node_type));
642 /* done, found nothing */
643 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node==%p\n",(void*)node));
648 * Test table for presence of at least one table entry.
651 empty_table(struct mib_node *node)
658 node_type = node->node_type;
659 if (node_type == MIB_NODE_LR)
661 struct mib_list_rootnode *lrn;
662 lrn = (struct mib_list_rootnode *)node;
663 if ((lrn->count == 0) || (lrn->head == NULL))
668 else if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
670 struct mib_array_node *an;
671 an = (struct mib_array_node *)node;
672 if ((an->maxlength == 0) || (an->nptr == NULL))
677 else if (node_type == MIB_NODE_EX)
679 struct mib_external_node *en;
680 en = (struct mib_external_node *)node;
681 if (en->tree_levels == 0)
694 snmp_expand_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
696 u8_t node_type, ext_level, climb_tree;
699 /* reset node stack */
704 node_type = node->node_type;
705 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
707 struct mib_array_node *an;
710 /* array node (internal ROM or RAM, fixed length) */
711 an = (struct mib_array_node *)node;
715 while ((i < an->maxlength) && (an->objid[i] < *ident))
719 if (i < an->maxlength)
721 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
722 /* add identifier to oidret */
723 oidret->id[oidret->len] = an->objid[i];
726 if (an->nptr[i] == NULL)
728 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
729 /* leaf node (e.g. in a fixed size table) */
730 if (an->objid[i] > *ident)
732 return (struct mib_node*)an;
734 else if ((i + 1) < an->maxlength)
736 /* an->objid[i] == *ident */
738 oidret->id[oidret->len] = an->objid[i + 1];
740 return (struct mib_node*)an;
744 /* (i + 1) == an->maxlength */
754 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
755 /* non-leaf, store right child ptr and id */
757 while ((j < an->maxlength) && (empty_table(an->nptr[j])))
761 if (j < an->maxlength)
763 cur_node.r_ptr = an->nptr[j];
764 cur_node.r_id = an->objid[j];
769 cur_node.r_ptr = NULL;
771 push_node(&cur_node);
772 if (an->objid[i] == *ident)
779 /* an->objid[i] < *ident */
782 /* follow next child pointer */
788 /* i == an->maxlength */
795 /* ident_len == 0, complete with leftmost '.thing' */
797 while ((j < an->maxlength) && empty_table(an->nptr[j]))
801 if (j < an->maxlength)
803 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left an->objid[j]==%"S32_F"\n",an->objid[j]));
804 oidret->id[oidret->len] = an->objid[j];
806 if (an->nptr[j] == NULL)
809 return (struct mib_node*)an;
813 /* no leaf, continue */
819 /* j == an->maxlength */
824 else if(node_type == MIB_NODE_LR)
826 struct mib_list_rootnode *lrn;
827 struct mib_list_node *ln;
829 /* list root node (internal 'RAM', variable length) */
830 lrn = (struct mib_list_rootnode *)node;
834 /* iterate over list, head to tail */
835 while ((ln != NULL) && (ln->objid < *ident))
841 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
842 oidret->id[oidret->len] = ln->objid;
844 if (ln->nptr == NULL)
847 if (ln->objid > *ident)
849 return (struct mib_node*)lrn;
851 else if (ln->next != NULL)
853 /* ln->objid == *ident */
855 oidret->id[oidret->len] = ln->next->objid;
857 return (struct mib_node*)lrn;
861 /* ln->next == NULL */
868 struct mib_list_node *jn;
871 /* non-leaf, store right child ptr and id */
873 while ((jn != NULL) && empty_table(jn->nptr))
879 cur_node.r_ptr = jn->nptr;
880 cur_node.r_id = jn->objid;
885 cur_node.r_ptr = NULL;
887 push_node(&cur_node);
888 if (ln->objid == *ident)
895 /* ln->objid < *ident */
898 /* follow next child pointer */
911 struct mib_list_node *jn;
912 /* ident_len == 0, complete with leftmost '.thing' */
914 while ((jn != NULL) && empty_table(jn->nptr))
920 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left jn->objid==%"S32_F"\n",jn->objid));
921 oidret->id[oidret->len] = jn->objid;
923 if (jn->nptr == NULL)
926 LWIP_DEBUGF(SNMP_MIB_DEBUG,("jn->nptr == NULL\n"));
927 return (struct mib_node*)lrn;
931 /* no leaf, continue */
942 else if(node_type == MIB_NODE_EX)
944 struct mib_external_node *en;
947 /* external node (addressing and access via functions) */
948 en = (struct mib_external_node *)node;
954 len = en->level_length(en->addr_inf,ext_level);
955 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) < 0))
961 /* add identifier to oidret */
962 en->get_objid(en->addr_inf,ext_level,i,&ex_id);
963 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,ex_id,*ident));
964 oidret->id[oidret->len] = ex_id;
967 if ((ext_level + 1) == en->tree_levels)
969 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
973 return (struct mib_node*)en;
975 else if ((i + 1) < len)
977 /* ex_id == *ident */
978 en->get_objid(en->addr_inf,ext_level,i + 1,&ex_id);
980 oidret->id[oidret->len] = ex_id;
982 return (struct mib_node*)en;
996 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
997 /* non-leaf, store right child ptr and id */
1001 /* right node is the current external node */
1002 cur_node.r_ptr = node;
1003 en->get_objid(en->addr_inf,ext_level,j,&cur_node.r_id);
1004 cur_node.r_nl = ext_level + 1;
1008 cur_node.r_ptr = NULL;
1010 push_node(&cur_node);
1011 if (en->ident_cmp(en->addr_inf,ext_level,i,*ident) == 0)
1018 /* external id < *ident */
1021 /* proceed to child */
1027 /* i == len (en->level_len()) */
1033 /* ident_len == 0, complete with leftmost '.thing' */
1034 en->get_objid(en->addr_inf,ext_level,0,&ex_id);
1035 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left en->objid==%"S32_F"\n",ex_id));
1036 oidret->id[oidret->len] = ex_id;
1038 if ((ext_level + 1) == en->tree_levels)
1041 LWIP_DEBUGF(SNMP_MIB_DEBUG,("(ext_level + 1) == en->tree_levels\n"));
1042 return (struct mib_node*)en;
1046 /* no leaf, proceed to child */
1051 else if(node_type == MIB_NODE_SC)
1053 mib_scalar_node *sn;
1056 sn = (mib_scalar_node *)node;
1064 /* ident_len == 0, complete object identifier */
1065 oidret->id[oidret->len] = 0;
1068 LWIP_DEBUGF(SNMP_MIB_DEBUG,("completed scalar leaf\n"));
1069 return (struct mib_node*)sn;
1074 /* unknown/unhandled node_type */
1075 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node_type %"U16_F" unkown\n",(u16_t)node_type));
1083 /* find right child ptr */
1087 while ((node_stack_cnt > 0) && (child.r_ptr == NULL))
1090 /* trim returned oid */
1093 if (child.r_ptr != NULL)
1095 /* incoming ident is useless beyond this point */
1097 oidret->id[oidret->len] = child.r_id;
1100 ext_level = child.r_nl;
1104 /* tree ends here ... */
1105 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed, tree ends here\n"));
1110 /* done, found nothing */
1111 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node==%p\n",(void*)node));
1116 * Test object identifier for the iso.org.dod.internet prefix.
1118 * @param ident_len the length of the supplied object identifier
1119 * @param ident points to the array of sub identifiers
1120 * @return 1 if it matches, 0 otherwise
1123 snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident)
1125 if ((ident_len > 3) &&
1126 (ident[0] == 1) && (ident[1] == 3) &&
1127 (ident[2] == 6) && (ident[3] == 1))
1138 * Expands object identifier to the iso.org.dod.internet
1139 * prefix for use in getnext operation.
1141 * @param ident_len the length of the supplied object identifier
1142 * @param ident points to the array of sub identifiers
1143 * @param oidret points to returned expanded object identifier
1144 * @return 1 if it matches, 0 otherwise
1146 * @note ident_len 0 is allowed, expanding to the first known object id!!
1149 snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
1151 const s32_t *prefix_ptr;
1156 prefix_ptr = &prefix[0];
1157 ret_ptr = &oidret->id[0];
1158 ident_len = ((ident_len < 4)?ident_len:4);
1159 while ((i < ident_len) && ((*ident) <= (*prefix_ptr)))
1161 *ret_ptr++ = *prefix_ptr++;
1167 /* match, complete missing bits */
1170 *ret_ptr++ = *prefix_ptr++;
1178 /* i != ident_len */
1183 #endif /* LWIP_SNMP */