3 * SNMP scalar node support implementation.
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
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 * This file is part of the lwIP TCP/IP stack.
34 * Author: Martin Hentschel <info@cl-soft.de>
38 #include "lwip/apps/snmp_opts.h"
40 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
42 #include "lwip/apps/snmp_scalar.h"
43 #include "lwip/apps/snmp_core.h"
45 static s16_t snmp_scalar_array_get_value(struct snmp_node_instance* instance, void* value);
46 static snmp_err_t snmp_scalar_array_set_test(struct snmp_node_instance* instance, u16_t value_len, void* value);
47 static snmp_err_t snmp_scalar_array_set_value(struct snmp_node_instance* instance, u16_t value_len, void* value);
50 snmp_scalar_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
52 const struct snmp_scalar_node* scalar_node = (const struct snmp_scalar_node*)(const void*)instance->node;
54 LWIP_UNUSED_ARG(root_oid);
55 LWIP_UNUSED_ARG(root_oid_len);
57 /* scalar only has one dedicated instance: .0 */
58 if ((instance->instance_oid.len != 1) || (instance->instance_oid.id[0] != 0)) {
59 return SNMP_ERR_NOSUCHINSTANCE;
62 instance->access = scalar_node->access;
63 instance->asn1_type = scalar_node->asn1_type;
64 instance->get_value = scalar_node->get_value;
65 instance->set_test = scalar_node->set_test;
66 instance->set_value = scalar_node->set_value;
67 return SNMP_ERR_NOERROR;
71 snmp_scalar_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
73 /* because our only instance is .0 we can only return a next instance if no instance oid is passed */
74 if (instance->instance_oid.len == 0) {
75 instance->instance_oid.len = 1;
76 instance->instance_oid.id[0] = 0;
78 return snmp_scalar_get_instance(root_oid, root_oid_len, instance);
81 return SNMP_ERR_NOSUCHINSTANCE;
86 snmp_scalar_array_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
88 LWIP_UNUSED_ARG(root_oid);
89 LWIP_UNUSED_ARG(root_oid_len);
91 if ((instance->instance_oid.len == 2) && (instance->instance_oid.id[1] == 0)) {
92 const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)(const void*)instance->node;
93 const struct snmp_scalar_array_node_def* array_node_def = array_node->array_nodes;
96 while (i < array_node->array_node_count) {
97 if (array_node_def->oid == instance->instance_oid.id[0]) {
105 if (i < array_node->array_node_count) {
106 instance->access = array_node_def->access;
107 instance->asn1_type = array_node_def->asn1_type;
108 instance->get_value = snmp_scalar_array_get_value;
109 instance->set_test = snmp_scalar_array_set_test;
110 instance->set_value = snmp_scalar_array_set_value;
111 instance->reference.const_ptr = array_node_def;
113 return SNMP_ERR_NOERROR;
117 return SNMP_ERR_NOSUCHINSTANCE;
121 snmp_scalar_array_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
123 const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)(const void*)instance->node;
124 const struct snmp_scalar_array_node_def* array_node_def = array_node->array_nodes;
125 const struct snmp_scalar_array_node_def* result = NULL;
127 LWIP_UNUSED_ARG(root_oid);
128 LWIP_UNUSED_ARG(root_oid_len);
130 if ((instance->instance_oid.len == 0) && (array_node->array_node_count > 0)) {
131 /* return node with lowest OID */
134 result = array_node_def;
137 for (i = 1; i < array_node->array_node_count; i++) {
138 if (array_node_def->oid < result->oid) {
139 result = array_node_def;
143 } else if (instance->instance_oid.len >= 1) {
144 if (instance->instance_oid.len == 1) {
145 /* if we have the requested OID we return its instance, otherwise we search for the next available */
147 while (i < array_node->array_node_count) {
148 if (array_node_def->oid == instance->instance_oid.id[0]) {
149 result = array_node_def;
157 if (result == NULL) {
158 u32_t oid_dist = 0xFFFFFFFFUL;
160 array_node_def = array_node->array_nodes; /* may be already at the end when if case before was executed without result -> reinitialize to start */
161 while (i < array_node->array_node_count) {
162 if ((array_node_def->oid > instance->instance_oid.id[0]) &&
163 ((u32_t)(array_node_def->oid - instance->instance_oid.id[0]) < oid_dist)) {
164 result = array_node_def;
165 oid_dist = array_node_def->oid - instance->instance_oid.id[0];
174 if (result == NULL) {
175 /* nothing to return */
176 return SNMP_ERR_NOSUCHINSTANCE;
179 instance->instance_oid.len = 2;
180 instance->instance_oid.id[0] = result->oid;
181 instance->instance_oid.id[1] = 0;
183 instance->access = result->access;
184 instance->asn1_type = result->asn1_type;
185 instance->get_value = snmp_scalar_array_get_value;
186 instance->set_test = snmp_scalar_array_set_test;
187 instance->set_value = snmp_scalar_array_set_value;
188 instance->reference.const_ptr = result;
190 return SNMP_ERR_NOERROR;
194 snmp_scalar_array_get_value(struct snmp_node_instance* instance, void* value)
196 const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)(const void*)instance->node;
197 const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
199 return array_node->get_value(array_node_def, value);
203 snmp_scalar_array_set_test(struct snmp_node_instance* instance, u16_t value_len, void* value)
205 const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)(const void*)instance->node;
206 const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
208 return array_node->set_test(array_node_def, value_len, value);
212 snmp_scalar_array_set_value(struct snmp_node_instance* instance, u16_t value_len, void* value)
214 const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)(const void*)instance->node;
215 const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
217 return array_node->set_value(array_node_def, value_len, value);
220 #endif /* LWIP_SNMP */