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>
38 #include "lwip/snmp_structs.h"
43 /** .iso.org.dod.internet address prefix, @see snmp_iso_*() */
44 const s32_t prefix[4] = {1, 3, 6, 1};
46 #define NODE_STACK_SIZE (LWIP_SNMP_OBJ_ID_LEN)
47 /** node stack entry (old news?) */
51 struct mib_node* r_ptr;
52 /** right child identifier */
54 /** right child next level */
57 static u8_t node_stack_cnt = 0;
58 static struct nse node_stack[NODE_STACK_SIZE];
61 * Pushes nse struct onto stack.
64 push_node(struct nse* node)
66 LWIP_ASSERT("node_stack_cnt < NODE_STACK_SIZE",node_stack_cnt < NODE_STACK_SIZE);
67 LWIP_DEBUGF(SNMP_MIB_DEBUG,("push_node() node=%p id=%"S32_F"\n",(void*)(node->r_ptr),node->r_id));
68 if (node_stack_cnt < NODE_STACK_SIZE)
70 node_stack[node_stack_cnt] = *node;
76 * Pops nse struct from stack.
79 pop_node(struct nse* node)
81 if (node_stack_cnt > 0)
84 *node = node_stack[node_stack_cnt];
86 LWIP_DEBUGF(SNMP_MIB_DEBUG,("pop_node() node=%p id=%"S32_F"\n",(void *)(node->r_ptr),node->r_id));
90 * Conversion from ifIndex to lwIP netif
91 * @param ifindex is a s32_t object sub-identifier
92 * @param netif points to returned netif struct pointer
95 snmp_ifindextonetif(s32_t ifindex, struct netif **netif)
97 struct netif *nif = netif_list;
102 while ((nif != NULL) && (i < ifidx))
111 * Conversion from lwIP netif to ifIndex
112 * @param netif points to a netif struct
113 * @param ifindex points to s32_t object sub-identifier
116 snmp_netiftoifindex(struct netif *netif, s32_t *ifidx)
118 struct netif *nif = netif_list;
131 * Conversion from oid to lwIP ip_addr
132 * @param ident points to s32_t ident[4] input
133 * @param ip points to output struct
136 snmp_oidtoip(s32_t *ident, struct ip_addr *ip)
151 * Conversion from lwIP ip_addr to oid
152 * @param ip points to input struct
153 * @param ident points to s32_t ident[4] output
156 snmp_iptooid(struct ip_addr *ip, s32_t *ident)
161 ident[0] = (ipa >> 24) & 0xff;
162 ident[1] = (ipa >> 16) & 0xff;
163 ident[2] = (ipa >> 8) & 0xff;
164 ident[3] = ipa & 0xff;
167 struct mib_list_node *
168 snmp_mib_ln_alloc(s32_t id)
170 struct mib_list_node *ln;
172 ln = (struct mib_list_node *)mem_malloc(sizeof(struct mib_list_node));
184 snmp_mib_ln_free(struct mib_list_node *ln)
189 struct mib_list_rootnode *
190 snmp_mib_lrn_alloc(void)
192 struct mib_list_rootnode *lrn;
194 lrn = (struct mib_list_rootnode*)mem_malloc(sizeof(struct mib_list_rootnode));
197 lrn->get_object_def = noleafs_get_object_def;
198 lrn->get_value = noleafs_get_value;
199 lrn->set_test = noleafs_set_test;
200 lrn->set_value = noleafs_set_value;
201 lrn->node_type = MIB_NODE_LR;
211 snmp_mib_lrn_free(struct mib_list_rootnode *lrn)
217 * Inserts node in idx list in a sorted
218 * (ascending order) fashion and
219 * allocates the node if needed.
221 * @param rn points to the root node
222 * @param objid is the object sub identifier
223 * @param insn points to a pointer to the inserted node
224 * used for constructing the tree.
225 * @return -1 if failed, 1 if inserted, 2 if present.
228 snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **insn)
230 struct mib_list_node *nn;
233 LWIP_ASSERT("rn != NULL",rn != NULL);
235 /* -1 = malloc failure, 0 = not inserted, 1 = inserted, 2 = was present */
237 if (rn->head == NULL)
239 /* empty list, add first node */
240 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc empty list objid==%"S32_F"\n",objid));
241 nn = snmp_mib_ln_alloc(objid);
256 struct mib_list_node *n;
257 /* at least one node is present */
259 while ((n != NULL) && (insert == 0))
261 if (n->objid == objid)
263 /* node is already there */
264 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node already there objid==%"S32_F"\n",objid));
268 else if (n->objid < objid)
272 /* alloc and insert at the tail */
273 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins tail objid==%"S32_F"\n",objid));
274 nn = snmp_mib_ln_alloc(objid);
286 /* insertion failure */
292 /* there's more to explore: traverse list */
293 LWIP_DEBUGF(SNMP_MIB_DEBUG,("traverse list\n"));
299 /* n->objid > objid */
300 /* alloc and insert between n->prev and n */
301 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins n->prev, objid==%"S32_F", n\n",objid));
302 nn = snmp_mib_ln_alloc(objid);
307 /* insert at the head */
315 /* insert in the middle */
326 /* insertion failure */
336 LWIP_ASSERT("insert != 0",insert != 0);
341 * Finds node in idx list and returns deletion mark.
343 * @param rn points to the root node
344 * @param objid is the object sub identifier
345 * @param fn returns pointer to found node
346 * @return 0 if not found, 1 if deletable,
347 * 2 can't delete (2 or more children), 3 not a list_node
350 snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn)
353 struct mib_list_node *n;
355 LWIP_ASSERT("rn != NULL",rn != NULL);
357 while ((n != NULL) && (n->objid != objid))
365 else if (n->nptr == NULL)
367 /* leaf, can delete node */
372 struct mib_list_rootnode *rn;
374 if (n->nptr->node_type == MIB_NODE_LR)
376 rn = (struct mib_list_rootnode *)n->nptr;
379 /* can't delete node */
384 /* count <= 1, can delete node */
390 /* other node type */
399 * Removes node from idx list
400 * if it has a single child left.
402 * @param rn points to the root node
403 * @param n points to the node to delete
404 * @return the nptr to be freed by caller
406 struct mib_list_rootnode *
407 snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n)
409 struct mib_list_rootnode *next;
411 LWIP_ASSERT("rn != NULL",rn != NULL);
412 LWIP_ASSERT("n != NULL",n != NULL);
414 /* caller must remove this sub-tree */
415 next = (struct mib_list_rootnode*)(n->nptr);
423 /* not last node, new list begin */
424 n->next->prev = NULL;
427 else if (n == rn->tail)
432 /* not last node, new list end */
433 n->prev->next = NULL;
438 /* node must be in the middle */
439 n->prev->next = n->next;
440 n->next->prev = n->prev;
442 LWIP_DEBUGF(SNMP_MIB_DEBUG,("free list objid==%"S32_F"\n",n->objid));
455 * Searches tree for the supplied (scalar?) object identifier.
457 * @param node points to the root of the tree ('.internet')
458 * @param ident_len the length of the supplied object identifier
459 * @param ident points to the array of sub identifiers
460 * @param np points to the found object instance (rerurn)
461 * @return pointer to the requested parent (!) node if success, NULL otherwise
464 snmp_search_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np)
466 u8_t node_type, ext_level;
469 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node==%p *ident==%"S32_F"\n",(void*)node,*ident));
472 node_type = node->node_type;
473 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
475 struct mib_array_node *an;
480 /* array node (internal ROM or RAM, fixed length) */
481 an = (struct mib_array_node *)node;
483 while ((i < an->maxlength) && (an->objid[i] != *ident))
487 if (i < an->maxlength)
489 /* found it, if available proceed to child, otherwise inspect leaf */
490 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
491 if (an->nptr[i] == NULL)
493 /* a scalar leaf OR table,
494 inspect remaining instance number / table index */
495 np->ident_len = ident_len;
497 return (struct mib_node*)an;
501 /* follow next child pointer */
509 /* search failed, identifier mismatch (nosuchname) */
510 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed *ident==%"S32_F"\n",*ident));
516 /* search failed, short object identifier (nosuchname) */
517 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed, short object identifier\n"));
521 else if(node_type == MIB_NODE_LR)
523 struct mib_list_rootnode *lrn;
524 struct mib_list_node *ln;
528 /* list root node (internal 'RAM', variable length) */
529 lrn = (struct mib_list_rootnode *)node;
531 /* iterate over list, head to tail */
532 while ((ln != NULL) && (ln->objid != *ident))
538 /* found it, proceed to child */;
539 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
540 if (ln->nptr == NULL)
542 np->ident_len = ident_len;
544 return (struct mib_node*)lrn;
548 /* follow next child pointer */
557 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed *ident==%"S32_F"\n",*ident));
563 /* search failed, short object identifier (nosuchname) */
564 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed, short object identifier\n"));
568 else if(node_type == MIB_NODE_EX)
570 struct mib_external_node *en;
575 /* external node (addressing and access via functions) */
576 en = (struct mib_external_node *)node;
579 len = en->level_length(en->addr_inf,ext_level);
580 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) != 0))
588 en->get_objid(en->addr_inf,ext_level,i,&debug_id);
589 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid==%"S32_F" *ident==%"S32_F"\n",debug_id,*ident));
590 if ((ext_level + 1) == en->tree_levels)
592 np->ident_len = ident_len;
594 return (struct mib_node*)en;
598 /* found it, proceed to child */
607 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed *ident==%"S32_F"\n",*ident));
613 /* search failed, short object identifier (nosuchname) */
614 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed, short object identifier\n"));
618 else if (node_type == MIB_NODE_SC)
622 sn = (mib_scalar_node *)node;
623 if ((ident_len == 1) && (*ident == 0))
625 np->ident_len = ident_len;
627 return (struct mib_node*)sn;
631 /* search failed, short object identifier (nosuchname) */
632 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed, invalid object identifier length\n"));
638 /* unknown node_type */
639 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node_type %"U16_F" unkown\n",(u16_t)node_type));
643 /* done, found nothing */
644 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node==%p\n",(void*)node));
649 * Test table for presence of at least one table entry.
652 empty_table(struct mib_node *node)
659 node_type = node->node_type;
660 if (node_type == MIB_NODE_LR)
662 struct mib_list_rootnode *lrn;
663 lrn = (struct mib_list_rootnode *)node;
664 if ((lrn->count == 0) || (lrn->head == NULL))
669 else if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
671 struct mib_array_node *an;
672 an = (struct mib_array_node *)node;
673 if ((an->maxlength == 0) || (an->nptr == NULL))
678 else if (node_type == MIB_NODE_EX)
680 struct mib_external_node *en;
681 en = (struct mib_external_node *)node;
682 if (en->tree_levels == 0)
695 snmp_expand_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
697 u8_t node_type, ext_level, climb_tree;
700 /* reset node stack */
705 node_type = node->node_type;
706 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
708 struct mib_array_node *an;
711 /* array node (internal ROM or RAM, fixed length) */
712 an = (struct mib_array_node *)node;
716 while ((i < an->maxlength) && (an->objid[i] < *ident))
720 if (i < an->maxlength)
722 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
723 /* add identifier to oidret */
724 oidret->id[oidret->len] = an->objid[i];
727 if (an->nptr[i] == NULL)
729 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
730 /* leaf node (e.g. in a fixed size table) */
731 if (an->objid[i] > *ident)
733 return (struct mib_node*)an;
735 else if ((i + 1) < an->maxlength)
737 /* an->objid[i] == *ident */
739 oidret->id[oidret->len] = an->objid[i + 1];
741 return (struct mib_node*)an;
745 /* (i + 1) == an->maxlength */
755 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
756 /* non-leaf, store right child ptr and id */
758 while ((j < an->maxlength) && (empty_table(an->nptr[j])))
762 if (j < an->maxlength)
764 cur_node.r_ptr = an->nptr[j];
765 cur_node.r_id = an->objid[j];
770 cur_node.r_ptr = NULL;
772 push_node(&cur_node);
773 if (an->objid[i] == *ident)
780 /* an->objid[i] < *ident */
783 /* follow next child pointer */
789 /* i == an->maxlength */
796 /* ident_len == 0, complete with leftmost '.thing' */
798 while ((j < an->maxlength) && empty_table(an->nptr[j]))
802 if (j < an->maxlength)
804 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left an->objid[j]==%"S32_F"\n",an->objid[j]));
805 oidret->id[oidret->len] = an->objid[j];
807 if (an->nptr[j] == NULL)
810 return (struct mib_node*)an;
814 /* no leaf, continue */
820 /* j == an->maxlength */
825 else if(node_type == MIB_NODE_LR)
827 struct mib_list_rootnode *lrn;
828 struct mib_list_node *ln;
830 /* list root node (internal 'RAM', variable length) */
831 lrn = (struct mib_list_rootnode *)node;
835 /* iterate over list, head to tail */
836 while ((ln != NULL) && (ln->objid < *ident))
842 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
843 oidret->id[oidret->len] = ln->objid;
845 if (ln->nptr == NULL)
848 if (ln->objid > *ident)
850 return (struct mib_node*)lrn;
852 else if (ln->next != NULL)
854 /* ln->objid == *ident */
856 oidret->id[oidret->len] = ln->next->objid;
858 return (struct mib_node*)lrn;
862 /* ln->next == NULL */
869 struct mib_list_node *jn;
872 /* non-leaf, store right child ptr and id */
874 while ((jn != NULL) && empty_table(jn->nptr))
880 cur_node.r_ptr = jn->nptr;
881 cur_node.r_id = jn->objid;
886 cur_node.r_ptr = NULL;
888 push_node(&cur_node);
889 if (ln->objid == *ident)
896 /* ln->objid < *ident */
899 /* follow next child pointer */
912 struct mib_list_node *jn;
913 /* ident_len == 0, complete with leftmost '.thing' */
915 while ((jn != NULL) && empty_table(jn->nptr))
921 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left jn->objid==%"S32_F"\n",jn->objid));
922 oidret->id[oidret->len] = jn->objid;
924 if (jn->nptr == NULL)
927 LWIP_DEBUGF(SNMP_MIB_DEBUG,("jn->nptr == NULL\n"));
928 return (struct mib_node*)lrn;
932 /* no leaf, continue */
943 else if(node_type == MIB_NODE_EX)
945 struct mib_external_node *en;
948 /* external node (addressing and access via functions) */
949 en = (struct mib_external_node *)node;
955 len = en->level_length(en->addr_inf,ext_level);
956 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) < 0))
962 /* add identifier to oidret */
963 en->get_objid(en->addr_inf,ext_level,i,&ex_id);
964 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,ex_id,*ident));
965 oidret->id[oidret->len] = ex_id;
968 if ((ext_level + 1) == en->tree_levels)
970 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
974 return (struct mib_node*)en;
976 else if ((i + 1) < len)
978 /* ex_id == *ident */
979 en->get_objid(en->addr_inf,ext_level,i + 1,&ex_id);
981 oidret->id[oidret->len] = ex_id;
983 return (struct mib_node*)en;
997 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
998 /* non-leaf, store right child ptr and id */
1002 /* right node is the current external node */
1003 cur_node.r_ptr = node;
1004 en->get_objid(en->addr_inf,ext_level,j,&cur_node.r_id);
1005 cur_node.r_nl = ext_level + 1;
1009 cur_node.r_ptr = NULL;
1011 push_node(&cur_node);
1012 if (en->ident_cmp(en->addr_inf,ext_level,i,*ident) == 0)
1019 /* external id < *ident */
1022 /* proceed to child */
1028 /* i == len (en->level_len()) */
1034 /* ident_len == 0, complete with leftmost '.thing' */
1035 en->get_objid(en->addr_inf,ext_level,0,&ex_id);
1036 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left en->objid==%"S32_F"\n",ex_id));
1037 oidret->id[oidret->len] = ex_id;
1039 if ((ext_level + 1) == en->tree_levels)
1042 LWIP_DEBUGF(SNMP_MIB_DEBUG,("(ext_level + 1) == en->tree_levels\n"));
1043 return (struct mib_node*)en;
1047 /* no leaf, proceed to child */
1052 else if(node_type == MIB_NODE_SC)
1054 mib_scalar_node *sn;
1057 sn = (mib_scalar_node *)node;
1065 /* ident_len == 0, complete object identifier */
1066 oidret->id[oidret->len] = 0;
1069 LWIP_DEBUGF(SNMP_MIB_DEBUG,("completed scalar leaf\n"));
1070 return (struct mib_node*)sn;
1075 /* unknown/unhandled node_type */
1076 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node_type %"U16_F" unkown\n",(u16_t)node_type));
1084 /* find right child ptr */
1088 while ((node_stack_cnt > 0) && (child.r_ptr == NULL))
1091 /* trim returned oid */
1094 if (child.r_ptr != NULL)
1096 /* incoming ident is useless beyond this point */
1098 oidret->id[oidret->len] = child.r_id;
1101 ext_level = child.r_nl;
1105 /* tree ends here ... */
1106 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed, tree ends here\n"));
1111 /* done, found nothing */
1112 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node==%p\n",(void*)node));
1117 * Test object identifier for the iso.org.dod.internet prefix.
1119 * @param ident_len the length of the supplied object identifier
1120 * @param ident points to the array of sub identifiers
1121 * @return 1 if it matches, 0 otherwise
1124 snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident)
1126 if ((ident_len > 3) &&
1127 (ident[0] == 1) && (ident[1] == 3) &&
1128 (ident[2] == 6) && (ident[3] == 1))
1139 * Expands object identifier to the iso.org.dod.internet
1140 * prefix for use in getnext operation.
1142 * @param ident_len the length of the supplied object identifier
1143 * @param ident points to the array of sub identifiers
1144 * @param oidret points to returned expanded object identifier
1145 * @return 1 if it matches, 0 otherwise
1147 * @note ident_len 0 is allowed, expanding to the first known object id!!
1150 snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
1152 const s32_t *prefix_ptr;
1157 prefix_ptr = &prefix[0];
1158 ret_ptr = &oidret->id[0];
1159 ident_len = ((ident_len < 4)?ident_len:4);
1160 while ((i < ident_len) && ((*ident) <= (*prefix_ptr)))
1162 *ret_ptr++ = *prefix_ptr++;
1168 /* match, complete missing bits */
1171 *ret_ptr++ = *prefix_ptr++;
1179 /* i != ident_len */
1184 #endif /* LWIP_SNMP */