snmp_structs.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @file
  3. * Generic MIB tree structures.
  4. *
  5. * @todo namespace prefixes
  6. */
  7. /*
  8. * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  25. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  27. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. * OF SUCH DAMAGE.
  32. *
  33. * Author: Christiaan Simons <christiaan.simons@axon.tv>
  34. */
  35. #ifndef __LWIP_SNMP_STRUCTS_H__
  36. #define __LWIP_SNMP_STRUCTS_H__
  37. #include "lwip/opt.h"
  38. #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
  39. #include "lwip/snmp.h"
  40. #if SNMP_PRIVATE_MIB
  41. /* When using a private MIB, you have to create a file 'private_mib.h' that contains
  42. * a 'struct mib_array_node mib_private' which contains your MIB. */
  43. #include "private_mib.h"
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /* MIB object instance */
  49. #define MIB_OBJECT_NONE 0
  50. #define MIB_OBJECT_SCALAR 1
  51. #define MIB_OBJECT_TAB 2
  52. /* MIB access types */
  53. #define MIB_ACCESS_READ 1
  54. #define MIB_ACCESS_WRITE 2
  55. /* MIB object access */
  56. #define MIB_OBJECT_READ_ONLY MIB_ACCESS_READ
  57. #define MIB_OBJECT_READ_WRITE (MIB_ACCESS_READ | MIB_ACCESS_WRITE)
  58. #define MIB_OBJECT_WRITE_ONLY MIB_ACCESS_WRITE
  59. #define MIB_OBJECT_NOT_ACCESSIBLE 0
  60. /** object definition returned by (get_object_def)() */
  61. struct obj_def
  62. {
  63. /* MIB_OBJECT_NONE (0), MIB_OBJECT_SCALAR (1), MIB_OBJECT_TAB (2) */
  64. u8_t instance;
  65. /* 0 read-only, 1 read-write, 2 write-only, 3 not-accessible */
  66. u8_t access;
  67. /* ASN type for this object */
  68. u8_t asn_type;
  69. /* value length (host length) */
  70. u16_t v_len;
  71. /* length of instance part of supplied object identifier */
  72. u8_t id_inst_len;
  73. /* instance part of supplied object identifier */
  74. s32_t *id_inst_ptr;
  75. };
  76. struct snmp_name_ptr
  77. {
  78. u8_t ident_len;
  79. s32_t *ident;
  80. };
  81. /** MIB const scalar (.0) node */
  82. #define MIB_NODE_SC 0x01
  83. /** MIB const array node */
  84. #define MIB_NODE_AR 0x02
  85. /** MIB array node (mem_malloced from RAM) */
  86. #define MIB_NODE_RA 0x03
  87. /** MIB list root node (mem_malloced from RAM) */
  88. #define MIB_NODE_LR 0x04
  89. /** MIB node for external objects */
  90. #define MIB_NODE_EX 0x05
  91. /** node "base class" layout, the mandatory fields for a node */
  92. struct mib_node
  93. {
  94. /** returns struct obj_def for the given object identifier */
  95. void (*get_object_def)(u8_t ident_len, s32_t *ident, struct obj_def *od);
  96. /** returns object value for the given object identifier,
  97. @note the caller must allocate at least len bytes for the value */
  98. void (*get_value)(struct obj_def *od, u16_t len, void *value);
  99. /** tests length and/or range BEFORE setting */
  100. u8_t (*set_test)(struct obj_def *od, u16_t len, void *value);
  101. /** sets object value, only to be called when set_test() */
  102. void (*set_value)(struct obj_def *od, u16_t len, void *value);
  103. /** One out of MIB_NODE_AR, MIB_NODE_LR or MIB_NODE_EX */
  104. u8_t node_type;
  105. /* array or max list length */
  106. u16_t maxlength;
  107. };
  108. /** derived node for scalars .0 index */
  109. typedef struct mib_node mib_scalar_node;
  110. /** derived node, points to a fixed size const array
  111. of sub-identifiers plus a 'child' pointer */
  112. struct mib_array_node
  113. {
  114. /* inherited "base class" members */
  115. void (*get_object_def)(u8_t ident_len, s32_t *ident, struct obj_def *od);
  116. void (*get_value)(struct obj_def *od, u16_t len, void *value);
  117. u8_t (*set_test)(struct obj_def *od, u16_t len, void *value);
  118. void (*set_value)(struct obj_def *od, u16_t len, void *value);
  119. u8_t node_type;
  120. u16_t maxlength;
  121. /* additional struct members */
  122. const s32_t *objid;
  123. struct mib_node* const *nptr;
  124. };
  125. /** derived node, points to a fixed size mem_malloced array
  126. of sub-identifiers plus a 'child' pointer */
  127. struct mib_ram_array_node
  128. {
  129. /* inherited "base class" members */
  130. void (*get_object_def)(u8_t ident_len, s32_t *ident, struct obj_def *od);
  131. void (*get_value)(struct obj_def *od, u16_t len, void *value);
  132. u8_t (*set_test)(struct obj_def *od, u16_t len, void *value);
  133. void (*set_value)(struct obj_def *od, u16_t len, void *value);
  134. u8_t node_type;
  135. u16_t maxlength;
  136. /* aditional struct members */
  137. s32_t *objid;
  138. struct mib_node **nptr;
  139. };
  140. struct mib_list_node
  141. {
  142. struct mib_list_node *prev;
  143. struct mib_list_node *next;
  144. s32_t objid;
  145. struct mib_node *nptr;
  146. };
  147. /** derived node, points to a doubly linked list
  148. of sub-identifiers plus a 'child' pointer */
  149. struct mib_list_rootnode
  150. {
  151. /* inherited "base class" members */
  152. void (*get_object_def)(u8_t ident_len, s32_t *ident, struct obj_def *od);
  153. void (*get_value)(struct obj_def *od, u16_t len, void *value);
  154. u8_t (*set_test)(struct obj_def *od, u16_t len, void *value);
  155. void (*set_value)(struct obj_def *od, u16_t len, void *value);
  156. u8_t node_type;
  157. u16_t maxlength;
  158. /* additional struct members */
  159. struct mib_list_node *head;
  160. struct mib_list_node *tail;
  161. /* counts list nodes in list */
  162. u16_t count;
  163. };
  164. /** derived node, has access functions for mib object in external memory or device
  165. using 'tree_level' and 'idx', with a range 0 .. (level_length() - 1) */
  166. struct mib_external_node
  167. {
  168. /* inherited "base class" members */
  169. void (*get_object_def)(u8_t ident_len, s32_t *ident, struct obj_def *od);
  170. void (*get_value)(struct obj_def *od, u16_t len, void *value);
  171. u8_t (*set_test)(struct obj_def *od, u16_t len, void *value);
  172. void (*set_value)(struct obj_def *od, u16_t len, void *value);
  173. u8_t node_type;
  174. u16_t maxlength;
  175. /* additional struct members */
  176. /** points to an external (in memory) record of some sort of addressing
  177. information, passed to and interpreted by the funtions below */
  178. void* addr_inf;
  179. /** tree levels under this node */
  180. u8_t tree_levels;
  181. /** number of objects at this level */
  182. u16_t (*level_length)(void* addr_inf, u8_t level);
  183. /** compares object sub identifier with external id
  184. return zero when equal, nonzero when unequal */
  185. s32_t (*ident_cmp)(void* addr_inf, u8_t level, u16_t idx, s32_t sub_id);
  186. void (*get_objid)(void* addr_inf, u8_t level, u16_t idx, s32_t *sub_id);
  187. /** async Questions */
  188. void (*get_object_def_q)(void* addr_inf, u8_t rid, u8_t ident_len, s32_t *ident);
  189. void (*get_value_q)(u8_t rid, struct obj_def *od);
  190. void (*set_test_q)(u8_t rid, struct obj_def *od);
  191. void (*set_value_q)(u8_t rid, struct obj_def *od, u16_t len, void *value);
  192. /** async Answers */
  193. void (*get_object_def_a)(u8_t rid, u8_t ident_len, s32_t *ident, struct obj_def *od);
  194. void (*get_value_a)(u8_t rid, struct obj_def *od, u16_t len, void *value);
  195. u8_t (*set_test_a)(u8_t rid, struct obj_def *od, u16_t len, void *value);
  196. void (*set_value_a)(u8_t rid, struct obj_def *od, u16_t len, void *value);
  197. /** async Panic Close (agent returns error reply,
  198. e.g. used for external transaction cleanup) */
  199. void (*get_object_def_pc)(u8_t rid, u8_t ident_len, s32_t *ident);
  200. void (*get_value_pc)(u8_t rid, struct obj_def *od);
  201. void (*set_test_pc)(u8_t rid, struct obj_def *od);
  202. void (*set_value_pc)(u8_t rid, struct obj_def *od);
  203. };
  204. /** export MIB tree from mib2.c */
  205. extern const struct mib_array_node internet;
  206. /** dummy function pointers for non-leaf MIB nodes from mib2.c */
  207. void noleafs_get_object_def(u8_t ident_len, s32_t *ident, struct obj_def *od);
  208. void noleafs_get_value(struct obj_def *od, u16_t len, void *value);
  209. u8_t noleafs_set_test(struct obj_def *od, u16_t len, void *value);
  210. void noleafs_set_value(struct obj_def *od, u16_t len, void *value);
  211. void snmp_oidtoip(s32_t *ident, ip_addr_t *ip);
  212. void snmp_iptooid(ip_addr_t *ip, s32_t *ident);
  213. void snmp_ifindextonetif(s32_t ifindex, struct netif **netif);
  214. void snmp_netiftoifindex(struct netif *netif, s32_t *ifidx);
  215. struct mib_list_node* snmp_mib_ln_alloc(s32_t id);
  216. void snmp_mib_ln_free(struct mib_list_node *ln);
  217. struct mib_list_rootnode* snmp_mib_lrn_alloc(void);
  218. void snmp_mib_lrn_free(struct mib_list_rootnode *lrn);
  219. s8_t snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **insn);
  220. s8_t snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn);
  221. struct mib_list_rootnode *snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n);
  222. struct mib_node* snmp_search_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np);
  223. struct mib_node* snmp_expand_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret);
  224. u8_t snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident);
  225. u8_t snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret);
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #endif /* LWIP_SNMP */
  230. #endif /* __LWIP_SNMP_STRUCTS_H__ */