STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
snmp_core.h
Go to the documentation of this file.
1 
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  * Martin Hentschel <info@cl-soft.de>
36  */
37 
38 #ifndef LWIP_HDR_APPS_SNMP_CORE_H
39 #define LWIP_HDR_APPS_SNMP_CORE_H
40 
41 #include "lwip/apps/snmp_opts.h"
42 
43 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
44 
45 #include "lwip/ip_addr.h"
46 #include "lwip/err.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /* basic ASN1 defines */
53 #define SNMP_ASN1_CLASS_UNIVERSAL 0x00
54 #define SNMP_ASN1_CLASS_APPLICATION 0x40
55 #define SNMP_ASN1_CLASS_CONTEXT 0x80
56 #define SNMP_ASN1_CLASS_PRIVATE 0xC0
57 
58 #define SNMP_ASN1_CONTENTTYPE_PRIMITIVE 0x00
59 #define SNMP_ASN1_CONTENTTYPE_CONSTRUCTED 0x20
60 
61 /* universal tags (from ASN.1 spec.) */
62 #define SNMP_ASN1_UNIVERSAL_END_OF_CONTENT 0
63 #define SNMP_ASN1_UNIVERSAL_INTEGER 2
64 #define SNMP_ASN1_UNIVERSAL_OCTET_STRING 4
65 #define SNMP_ASN1_UNIVERSAL_NULL 5
66 #define SNMP_ASN1_UNIVERSAL_OBJECT_ID 6
67 #define SNMP_ASN1_UNIVERSAL_SEQUENCE_OF 16
68 
69 /* application specific (SNMP) tags (from SNMPv2-SMI) */
70 #define SNMP_ASN1_APPLICATION_IPADDR 0 /* [APPLICATION 0] IMPLICIT OCTET STRING (SIZE (4)) */
71 #define SNMP_ASN1_APPLICATION_COUNTER 1 /* [APPLICATION 1] IMPLICIT INTEGER (0..4294967295) => u32_t */
72 #define SNMP_ASN1_APPLICATION_GAUGE 2 /* [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) => u32_t */
73 #define SNMP_ASN1_APPLICATION_TIMETICKS 3 /* [APPLICATION 3] IMPLICIT INTEGER (0..4294967295) => u32_t */
74 #define SNMP_ASN1_APPLICATION_OPAQUE 4 /* [APPLICATION 4] IMPLICIT OCTET STRING */
75 #define SNMP_ASN1_APPLICATION_COUNTER64 6 /* [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615) */
76 
77 /* context specific (SNMP) tags (from RFC 1905) */
78 #define SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_INSTANCE 1
79 
80 /* full ASN1 type defines */
81 #define SNMP_ASN1_TYPE_END_OF_CONTENT (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_END_OF_CONTENT)
82 #define SNMP_ASN1_TYPE_INTEGER (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_INTEGER)
83 #define SNMP_ASN1_TYPE_OCTET_STRING (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_OCTET_STRING)
84 #define SNMP_ASN1_TYPE_NULL (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_NULL)
85 #define SNMP_ASN1_TYPE_OBJECT_ID (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_OBJECT_ID)
86 #define SNMP_ASN1_TYPE_SEQUENCE (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_CONSTRUCTED | SNMP_ASN1_UNIVERSAL_SEQUENCE_OF)
87 #define SNMP_ASN1_TYPE_IPADDR (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_IPADDR)
88 #define SNMP_ASN1_TYPE_COUNTER (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_COUNTER)
89 #define SNMP_ASN1_TYPE_GAUGE (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_GAUGE)
90 #define SNMP_ASN1_TYPE_TIMETICKS (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_TIMETICKS)
91 #define SNMP_ASN1_TYPE_OPAQUE (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_OPAQUE)
92 #define SNMP_ASN1_TYPE_COUNTER64 (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_COUNTER64)
93 
94 #define SNMP_VARBIND_EXCEPTION_OFFSET 0xF0
95 #define SNMP_VARBIND_EXCEPTION_MASK 0x0F
96 
97 /* error codes predefined by SNMP prot. */
98 typedef enum {
99  SNMP_ERR_NOERROR = 0,
100 /*
101 outdated v1 error codes. do not use anmore!
102 #define SNMP_ERR_NOSUCHNAME 2 use SNMP_ERR_NOSUCHINSTANCE instead
103 #define SNMP_ERR_BADVALUE 3 use SNMP_ERR_WRONGTYPE,SNMP_ERR_WRONGLENGTH,SNMP_ERR_WRONGENCODING or SNMP_ERR_WRONGVALUE instead
104 #define SNMP_ERR_READONLY 4 use SNMP_ERR_NOTWRITABLE instead
105 */
106  SNMP_ERR_GENERROR = 5,
107  SNMP_ERR_NOACCESS = 6,
108  SNMP_ERR_WRONGTYPE = 7,
109  SNMP_ERR_WRONGLENGTH = 8,
110  SNMP_ERR_WRONGENCODING = 9,
111  SNMP_ERR_WRONGVALUE = 10,
112  SNMP_ERR_NOCREATION = 11,
113  SNMP_ERR_INCONSISTENTVALUE = 12,
114  SNMP_ERR_RESOURCEUNAVAILABLE = 13,
115  SNMP_ERR_COMMITFAILED = 14,
116  SNMP_ERR_UNDOFAILED = 15,
117  SNMP_ERR_NOTWRITABLE = 17,
118  SNMP_ERR_INCONSISTENTNAME = 18,
119 
120  SNMP_ERR_NOSUCHINSTANCE = SNMP_VARBIND_EXCEPTION_OFFSET + SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_INSTANCE
121 } snmp_err_t;
122 
124 struct snmp_obj_id
125 {
126  u8_t len;
128 };
129 
130 struct snmp_obj_id_const_ref
131 {
132  u8_t len;
133  const u32_t* id;
134 };
135 
136 extern const struct snmp_obj_id_const_ref snmp_zero_dot_zero; /* administrative identifier from SNMPv2-SMI */
137 
138 union snmp_variant_value
139 {
140  void* ptr;
141  const void* const_ptr;
142  u32_t u32;
143  s32_t s32;
144 };
145 
146 
153 #define SNMP_NODE_TREE 0x00
154 /* predefined leaf node types */
155 #define SNMP_NODE_SCALAR 0x01
156 #define SNMP_NODE_SCALAR_ARRAY 0x02
157 #define SNMP_NODE_TABLE 0x03
158 #define SNMP_NODE_THREADSYNC 0x04
159 
161 struct snmp_node
162 {
164  u8_t node_type;
166  u32_t oid;
167 };
168 
169 /* SNMP node instance access types */
170 typedef enum {
171  SNMP_NODE_INSTANCE_ACCESS_READ = 1,
172  SNMP_NODE_INSTANCE_ACCESS_WRITE = 2,
173  SNMP_NODE_INSTANCE_READ_ONLY = SNMP_NODE_INSTANCE_ACCESS_READ,
174  SNMP_NODE_INSTANCE_READ_WRITE = (SNMP_NODE_INSTANCE_ACCESS_READ | SNMP_NODE_INSTANCE_ACCESS_WRITE),
175  SNMP_NODE_INSTANCE_WRITE_ONLY = SNMP_NODE_INSTANCE_ACCESS_WRITE,
176  SNMP_NODE_INSTANCE_NOT_ACCESSIBLE = 0
177 } snmp_access_t;
178 
179 struct snmp_node_instance;
180 
181 typedef u16_t (*node_instance_get_value_method)(struct snmp_node_instance*, void*);
182 typedef snmp_err_t (*node_instance_set_test_method)(struct snmp_node_instance*, u16_t, void*);
183 typedef snmp_err_t (*node_instance_set_value_method)(struct snmp_node_instance*, u16_t, void*);
184 typedef void (*node_instance_release_method)(struct snmp_node_instance*);
185 
186 #define SNMP_GET_VALUE_RAW_DATA 0x8000
187 
188 struct snmp_node_instance
189 {
191  const struct snmp_node* node;
193  struct snmp_obj_id instance_oid;
194 
196  u8_t asn1_type;
198  snmp_access_t access;
199 
201  node_instance_get_value_method get_value;
203  node_instance_set_test_method set_test;
205  node_instance_set_value_method set_value;
207  node_instance_release_method release_instance;
208 
210  union snmp_variant_value reference;
212  u32_t reference_len;
213 };
214 
215 
216 struct snmp_tree_node
217 {
218  /* inherited "base class" members */
219  struct snmp_node node;
220  u16_t subnode_count;
221  const struct snmp_node* const *subnodes;
222 };
223 
224 #define SNMP_CREATE_TREE_NODE(oid, subnodes) \
225  {{ SNMP_NODE_TREE, (oid) }, \
226  (u16_t)LWIP_ARRAYSIZE(subnodes), (subnodes) }
227 
228 #define SNMP_CREATE_EMPTY_TREE_NODE(oid) \
229  {{ SNMP_NODE_TREE, (oid) }, \
230  0, NULL }
231 
232 struct snmp_leaf_node
233 {
234  /* inherited "base class" members */
235  struct snmp_node node;
236  snmp_err_t (*get_instance)(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance);
237  snmp_err_t (*get_next_instance)(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance);
238 };
239 
240 /* represents a single mib with its base oid and root node */
241 struct snmp_mib
242 {
243  const u32_t *base_oid;
244  u8_t base_oid_len;
245  const struct snmp_node *root_node;
246 };
247 
248 #define SNMP_MIB_CREATE(oid_list, root_node) { (oid_list), (u8_t)LWIP_ARRAYSIZE(oid_list), root_node }
249 
251 struct snmp_oid_range
252 {
253  u32_t min;
254  u32_t max;
255 };
256 
258 u8_t snmp_oid_in_range(const u32_t *oid_in, u8_t oid_len, const struct snmp_oid_range *oid_ranges, u8_t oid_ranges_len);
259 
260 typedef enum {
261  SNMP_NEXT_OID_STATUS_SUCCESS,
262  SNMP_NEXT_OID_STATUS_NO_MATCH,
263  SNMP_NEXT_OID_STATUS_BUF_TO_SMALL
264 } snmp_next_oid_status_t;
265 
267 struct snmp_next_oid_state
268 {
269  const u32_t* start_oid;
270  u8_t start_oid_len;
271 
272  u32_t* next_oid;
273  u8_t next_oid_len;
274  u8_t next_oid_max_len;
275 
276  snmp_next_oid_status_t status;
277  void* reference;
278 };
279 
281 void snmp_next_oid_init(struct snmp_next_oid_state *state,
282  const u32_t *start_oid, u8_t start_oid_len,
283  u32_t *next_oid_buf, u8_t next_oid_max_len);
287 u8_t snmp_next_oid_precheck(struct snmp_next_oid_state *state, const u32_t *oid, const u8_t oid_len);
289 u8_t snmp_next_oid_check(struct snmp_next_oid_state *state, const u32_t *oid, const u8_t oid_len, void* reference);
290 
291 void snmp_oid_assign(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
292 void snmp_oid_combine(struct snmp_obj_id* target, const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
293 void snmp_oid_prefix(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
294 void snmp_oid_append(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
295 u8_t snmp_oid_equal(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
296 s8_t snmp_oid_compare(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
297 
298 #if LWIP_IPV4
299 u8_t snmp_oid_to_ip4(const u32_t *oid, ip4_addr_t *ip);
300 void snmp_ip4_to_oid(const ip4_addr_t *ip, u32_t *oid);
301 #endif /* LWIP_IPV4 */
302 #if LWIP_IPV6
303 u8_t snmp_oid_to_ip6(const u32_t *oid, ip6_addr_t *ip);
304 void snmp_ip6_to_oid(const ip6_addr_t *ip, u32_t *oid);
305 #endif /* LWIP_IPV6 */
306 #if LWIP_IPV4 || LWIP_IPV6
307 u8_t snmp_ip_to_oid(const ip_addr_t *ip, u32_t *oid);
308 u8_t snmp_ip_port_to_oid(const ip_addr_t *ip, u16_t port, u32_t *oid);
309 
310 u8_t snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip);
311 u8_t snmp_oid_to_ip_port(const u32_t *oid, u8_t oid_len, ip_addr_t *ip, u16_t *port);
312 #endif /* LWIP_IPV4 || LWIP_IPV6 */
313 
314 snmp_err_t snmp_set_test_ok(struct snmp_node_instance* instance, u16_t value_len, void* value); /* generic function which can be used if test is always successful */
315 
316 err_t snmp_decode_bits(const u8_t *buf, u32_t buf_len, u32_t *bit_value);
317 err_t snmp_decode_truthvalue(const s32_t *asn1_value, u8_t *bool_value);
318 u8_t snmp_encode_bits(u8_t *buf, u32_t buf_len, u32_t bit_value, u8_t bit_count);
319 u8_t snmp_encode_truthvalue(s32_t *asn1_value, u32_t bool_value);
320 
321 struct snmp_statistics
322 {
323  u32_t inpkts;
324  u32_t outpkts;
325  u32_t inbadversions;
326  u32_t inbadcommunitynames;
327  u32_t inbadcommunityuses;
328  u32_t inasnparseerrs;
329  u32_t intoobigs;
330  u32_t innosuchnames;
331  u32_t inbadvalues;
332  u32_t inreadonlys;
333  u32_t ingenerrs;
334  u32_t intotalreqvars;
335  u32_t intotalsetvars;
336  u32_t ingetrequests;
337  u32_t ingetnexts;
338  u32_t insetrequests;
339  u32_t ingetresponses;
340  u32_t intraps;
341  u32_t outtoobigs;
342  u32_t outnosuchnames;
343  u32_t outbadvalues;
344  u32_t outgenerrs;
345  u32_t outgetrequests;
346  u32_t outgetnexts;
347  u32_t outsetrequests;
348  u32_t outgetresponses;
349  u32_t outtraps;
350 };
351 
352 extern struct snmp_statistics snmp_stats;
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif /* LWIP_SNMP */
359 
360 #endif /* LWIP_HDR_APPS_SNMP_CORE_H */
signed char s8_t
Definition: cc.h:39
#define SNMP_MAX_OBJ_ID_LEN
Definition: snmp_opts.h:120
unsigned long u32_t
Definition: cc.h:42
s8_t err_t
Definition: err.h:47
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
signed long s32_t
Definition: cc.h:43
unsigned short u16_t
Definition: cc.h:40