1 /**
2  * @file
3  * SNMP scalar node support implementation.
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
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.
20  *
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
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Martin Hentschel <info@cl-soft.de>
35  *
36  */
37 
38 #include "lwip/apps/snmp_opts.h"
39 
40 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
41 
42 #include "lwip/apps/snmp_scalar.h"
43 #include "lwip/apps/snmp_core.h"
44 
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);
48 
49 snmp_err_t
snmp_scalar_get_instance(const u32_t * root_oid,u8_t root_oid_len,struct snmp_node_instance * instance)50 snmp_scalar_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
51 {
52   const struct snmp_scalar_node *scalar_node = (const struct snmp_scalar_node *)(const void *)instance->node;
53 
54   LWIP_UNUSED_ARG(root_oid);
55   LWIP_UNUSED_ARG(root_oid_len);
56 
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;
60   }
61 
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;
68 }
69 
70 snmp_err_t
snmp_scalar_get_next_instance(const u32_t * root_oid,u8_t root_oid_len,struct snmp_node_instance * instance)71 snmp_scalar_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
72 {
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;
77 
78     return snmp_scalar_get_instance(root_oid, root_oid_len, instance);
79   }
80 
81   return SNMP_ERR_NOSUCHINSTANCE;
82 }
83 
84 
85 snmp_err_t
snmp_scalar_array_get_instance(const u32_t * root_oid,u8_t root_oid_len,struct snmp_node_instance * instance)86 snmp_scalar_array_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
87 {
88   LWIP_UNUSED_ARG(root_oid);
89   LWIP_UNUSED_ARG(root_oid_len);
90 
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;
94     u32_t i = 0;
95 
96     while (i < array_node->array_node_count) {
97       if (array_node_def->oid == instance->instance_oid.id[0]) {
98         break;
99       }
100 
101       array_node_def++;
102       i++;
103     }
104 
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;
112 
113       return SNMP_ERR_NOERROR;
114     }
115   }
116 
117   return SNMP_ERR_NOSUCHINSTANCE;
118 }
119 
120 snmp_err_t
snmp_scalar_array_get_next_instance(const u32_t * root_oid,u8_t root_oid_len,struct snmp_node_instance * instance)121 snmp_scalar_array_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
122 {
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;
126 
127   LWIP_UNUSED_ARG(root_oid);
128   LWIP_UNUSED_ARG(root_oid_len);
129 
130   if ((instance->instance_oid.len == 0) && (array_node->array_node_count > 0)) {
131     /* return node with lowest OID */
132     u16_t i = 0;
133 
134     result = array_node_def;
135     array_node_def++;
136 
137     for (i = 1; i < array_node->array_node_count; i++) {
138       if (array_node_def->oid < result->oid) {
139         result = array_node_def;
140       }
141       array_node_def++;
142     }
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 */
146       u16_t i = 0;
147       while (i < array_node->array_node_count) {
148         if (array_node_def->oid == instance->instance_oid.id[0]) {
149           result = array_node_def;
150           break;
151         }
152 
153         array_node_def++;
154         i++;
155       }
156     }
157     if (result == NULL) {
158       u32_t oid_dist = 0xFFFFFFFFUL;
159       u16_t i        = 0;
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];
166         }
167 
168         array_node_def++;
169         i++;
170       }
171     }
172   }
173 
174   if (result == NULL) {
175     /* nothing to return */
176     return SNMP_ERR_NOSUCHINSTANCE;
177   }
178 
179   instance->instance_oid.len   = 2;
180   instance->instance_oid.id[0] = result->oid;
181   instance->instance_oid.id[1] = 0;
182 
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;
189 
190   return SNMP_ERR_NOERROR;
191 }
192 
193 static s16_t
snmp_scalar_array_get_value(struct snmp_node_instance * instance,void * value)194 snmp_scalar_array_get_value(struct snmp_node_instance *instance, void *value)
195 {
196   s16_t result = -1;
197   const struct snmp_scalar_array_node *array_node = (const struct snmp_scalar_array_node *)(const void *)instance->node;
198   const struct snmp_scalar_array_node_def *array_node_def = (const struct snmp_scalar_array_node_def *)instance->reference.const_ptr;
199 
200   if (array_node->get_value != NULL) {
201     result = array_node->get_value(array_node_def, value);
202   }
203   return result;
204 }
205 
206 static snmp_err_t
snmp_scalar_array_set_test(struct snmp_node_instance * instance,u16_t value_len,void * value)207 snmp_scalar_array_set_test(struct snmp_node_instance *instance, u16_t value_len, void *value)
208 {
209   snmp_err_t result = SNMP_ERR_NOTWRITABLE;
210   const struct snmp_scalar_array_node *array_node = (const struct snmp_scalar_array_node *)(const void *)instance->node;
211   const struct snmp_scalar_array_node_def *array_node_def = (const struct snmp_scalar_array_node_def *)instance->reference.const_ptr;
212 
213   if (array_node->set_test != NULL) {
214     result = array_node->set_test(array_node_def, value_len, value);
215   }
216   return result;
217 }
218 
219 static snmp_err_t
snmp_scalar_array_set_value(struct snmp_node_instance * instance,u16_t value_len,void * value)220 snmp_scalar_array_set_value(struct snmp_node_instance *instance, u16_t value_len, void *value)
221 {
222   snmp_err_t result = SNMP_ERR_NOTWRITABLE;
223   const struct snmp_scalar_array_node *array_node = (const struct snmp_scalar_array_node *)(const void *)instance->node;
224   const struct snmp_scalar_array_node_def *array_node_def = (const struct snmp_scalar_array_node_def *)instance->reference.const_ptr;
225 
226   if (array_node->set_value != NULL) {
227     result = array_node->set_value(array_node_def, value_len, value);
228   }
229   return result;
230 }
231 
232 #endif /* LWIP_SNMP */
233