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