Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1 /**
simon 0:350011bf8be7 2 * @file
simon 0:350011bf8be7 3 * SNMP input message processing (RFC1157).
simon 0:350011bf8be7 4 */
simon 0:350011bf8be7 5
simon 0:350011bf8be7 6 /*
simon 0:350011bf8be7 7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
simon 0:350011bf8be7 8 * All rights reserved.
simon 0:350011bf8be7 9 *
simon 0:350011bf8be7 10 * Redistribution and use in source and binary forms, with or without modification,
simon 0:350011bf8be7 11 * are permitted provided that the following conditions are met:
simon 0:350011bf8be7 12 *
simon 0:350011bf8be7 13 * 1. Redistributions of source code must retain the above copyright notice,
simon 0:350011bf8be7 14 * this list of conditions and the following disclaimer.
simon 0:350011bf8be7 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
simon 0:350011bf8be7 16 * this list of conditions and the following disclaimer in the documentation
simon 0:350011bf8be7 17 * and/or other materials provided with the distribution.
simon 0:350011bf8be7 18 * 3. The name of the author may not be used to endorse or promote products
simon 0:350011bf8be7 19 * derived from this software without specific prior written permission.
simon 0:350011bf8be7 20 *
simon 0:350011bf8be7 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
simon 0:350011bf8be7 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
simon 0:350011bf8be7 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
simon 0:350011bf8be7 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
simon 0:350011bf8be7 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
simon 0:350011bf8be7 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
simon 0:350011bf8be7 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
simon 0:350011bf8be7 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
simon 0:350011bf8be7 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
simon 0:350011bf8be7 30 * OF SUCH DAMAGE.
simon 0:350011bf8be7 31 *
simon 0:350011bf8be7 32 * Author: Christiaan Simons <christiaan.simons@axon.tv>
simon 0:350011bf8be7 33 */
simon 0:350011bf8be7 34
simon 0:350011bf8be7 35 #include "lwip/opt.h"
simon 0:350011bf8be7 36
simon 0:350011bf8be7 37 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 38
simon 0:350011bf8be7 39 #include "lwip/snmp.h"
simon 0:350011bf8be7 40 #include "lwip/snmp_asn1.h"
simon 0:350011bf8be7 41 #include "lwip/snmp_msg.h"
simon 0:350011bf8be7 42 #include "lwip/snmp_structs.h"
simon 0:350011bf8be7 43 #include "lwip/ip_addr.h"
simon 0:350011bf8be7 44 #include "lwip/memp.h"
simon 0:350011bf8be7 45 #include "lwip/udp.h"
simon 0:350011bf8be7 46 #include "lwip/stats.h"
simon 0:350011bf8be7 47
simon 0:350011bf8be7 48 #include <string.h>
simon 0:350011bf8be7 49
simon 0:350011bf8be7 50 /* public (non-static) constants */
simon 0:350011bf8be7 51 /** SNMP v1 == 0 */
simon 0:350011bf8be7 52 const s32_t snmp_version = 0;
simon 0:350011bf8be7 53 /** default SNMP community string */
simon 0:350011bf8be7 54 const char snmp_publiccommunity[7] = "public";
simon 0:350011bf8be7 55
simon 0:350011bf8be7 56 /* statically allocated buffers for SNMP_CONCURRENT_REQUESTS */
simon 0:350011bf8be7 57 struct snmp_msg_pstat msg_input_list[SNMP_CONCURRENT_REQUESTS];
simon 0:350011bf8be7 58 /* UDP Protocol Control Block */
simon 0:350011bf8be7 59 struct udp_pcb *snmp1_pcb;
simon 0:350011bf8be7 60
simon 0:350011bf8be7 61 static void snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
simon 0:350011bf8be7 62 static err_t snmp_pdu_header_check(struct pbuf *p, u16_t ofs, u16_t pdu_len, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat);
simon 0:350011bf8be7 63 static err_t snmp_pdu_dec_varbindlist(struct pbuf *p, u16_t ofs, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat);
simon 0:350011bf8be7 64
simon 0:350011bf8be7 65
simon 0:350011bf8be7 66 /**
simon 0:350011bf8be7 67 * Starts SNMP Agent.
simon 0:350011bf8be7 68 * Allocates UDP pcb and binds it to IP_ADDR_ANY port 161.
simon 0:350011bf8be7 69 */
simon 0:350011bf8be7 70 void
simon 0:350011bf8be7 71 snmp_init(void)
simon 0:350011bf8be7 72 {
simon 0:350011bf8be7 73 struct snmp_msg_pstat *msg_ps;
simon 0:350011bf8be7 74 u8_t i;
simon 0:350011bf8be7 75
simon 0:350011bf8be7 76 snmp1_pcb = udp_new();
simon 0:350011bf8be7 77 if (snmp1_pcb != NULL)
simon 0:350011bf8be7 78 {
simon 0:350011bf8be7 79 udp_recv(snmp1_pcb, snmp_recv, (void *)SNMP_IN_PORT);
simon 0:350011bf8be7 80 udp_bind(snmp1_pcb, IP_ADDR_ANY, SNMP_IN_PORT);
simon 0:350011bf8be7 81 }
simon 0:350011bf8be7 82 msg_ps = &msg_input_list[0];
simon 0:350011bf8be7 83 for (i=0; i<SNMP_CONCURRENT_REQUESTS; i++)
simon 0:350011bf8be7 84 {
simon 0:350011bf8be7 85 msg_ps->state = SNMP_MSG_EMPTY;
simon 0:350011bf8be7 86 msg_ps->error_index = 0;
simon 0:350011bf8be7 87 msg_ps->error_status = SNMP_ES_NOERROR;
simon 0:350011bf8be7 88 msg_ps++;
simon 0:350011bf8be7 89 }
simon 0:350011bf8be7 90 trap_msg.pcb = snmp1_pcb;
simon 0:350011bf8be7 91
simon 0:350011bf8be7 92 #ifdef SNMP_PRIVATE_MIB_INIT
simon 0:350011bf8be7 93 /* If defined, rhis must be a function-like define to initialize the
simon 0:350011bf8be7 94 * private MIB after the stack has been initialized.
simon 0:350011bf8be7 95 * The private MIB can also be initialized in tcpip_callback (or after
simon 0:350011bf8be7 96 * the stack is initialized), this define is only for convenience. */
simon 0:350011bf8be7 97 SNMP_PRIVATE_MIB_INIT();
simon 0:350011bf8be7 98 #endif /* SNMP_PRIVATE_MIB_INIT */
simon 0:350011bf8be7 99
simon 0:350011bf8be7 100 /* The coldstart trap will only be output
simon 0:350011bf8be7 101 if our outgoing interface is up & configured */
simon 0:350011bf8be7 102 snmp_coldstart_trap();
simon 0:350011bf8be7 103 }
simon 0:350011bf8be7 104
simon 0:350011bf8be7 105 static void
simon 0:350011bf8be7 106 snmp_error_response(struct snmp_msg_pstat *msg_ps, u8_t error)
simon 0:350011bf8be7 107 {
simon 0:350011bf8be7 108 snmp_varbind_list_free(&msg_ps->outvb);
simon 0:350011bf8be7 109 msg_ps->outvb = msg_ps->invb;
simon 0:350011bf8be7 110 msg_ps->invb.head = NULL;
simon 0:350011bf8be7 111 msg_ps->invb.tail = NULL;
simon 0:350011bf8be7 112 msg_ps->invb.count = 0;
simon 0:350011bf8be7 113 msg_ps->error_status = error;
simon 0:350011bf8be7 114 msg_ps->error_index = 1 + msg_ps->vb_idx;
simon 0:350011bf8be7 115 snmp_send_response(msg_ps);
simon 0:350011bf8be7 116 snmp_varbind_list_free(&msg_ps->outvb);
simon 0:350011bf8be7 117 msg_ps->state = SNMP_MSG_EMPTY;
simon 0:350011bf8be7 118 }
simon 0:350011bf8be7 119
simon 0:350011bf8be7 120 static void
simon 0:350011bf8be7 121 snmp_ok_response(struct snmp_msg_pstat *msg_ps)
simon 0:350011bf8be7 122 {
simon 0:350011bf8be7 123 err_t err_ret;
simon 0:350011bf8be7 124
simon 0:350011bf8be7 125 err_ret = snmp_send_response(msg_ps);
simon 0:350011bf8be7 126 if (err_ret == ERR_MEM)
simon 0:350011bf8be7 127 {
simon 0:350011bf8be7 128 /* serious memory problem, can't return tooBig */
simon 0:350011bf8be7 129 }
simon 0:350011bf8be7 130 else
simon 0:350011bf8be7 131 {
simon 0:350011bf8be7 132 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event = %"S32_F"\n",msg_ps->error_status));
simon 0:350011bf8be7 133 }
simon 0:350011bf8be7 134 /* free varbinds (if available) */
simon 0:350011bf8be7 135 snmp_varbind_list_free(&msg_ps->invb);
simon 0:350011bf8be7 136 snmp_varbind_list_free(&msg_ps->outvb);
simon 0:350011bf8be7 137 msg_ps->state = SNMP_MSG_EMPTY;
simon 0:350011bf8be7 138 }
simon 0:350011bf8be7 139
simon 0:350011bf8be7 140 /**
simon 0:350011bf8be7 141 * Service an internal or external event for SNMP GET.
simon 0:350011bf8be7 142 *
simon 0:350011bf8be7 143 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
simon 0:350011bf8be7 144 * @param msg_ps points to the assosicated message process state
simon 0:350011bf8be7 145 */
simon 0:350011bf8be7 146 static void
simon 0:350011bf8be7 147 snmp_msg_get_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
simon 0:350011bf8be7 148 {
simon 0:350011bf8be7 149 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_get_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
simon 0:350011bf8be7 150
simon 0:350011bf8be7 151 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
simon 0:350011bf8be7 152 {
simon 0:350011bf8be7 153 struct mib_external_node *en;
simon 0:350011bf8be7 154 struct snmp_name_ptr np;
simon 0:350011bf8be7 155
simon 0:350011bf8be7 156 /* get_object_def() answer*/
simon 0:350011bf8be7 157 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 158 np = msg_ps->ext_name_ptr;
simon 0:350011bf8be7 159
simon 0:350011bf8be7 160 /* translate answer into a known lifeform */
simon 0:350011bf8be7 161 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
simon 0:350011bf8be7 162 if ((msg_ps->ext_object_def.instance != MIB_OBJECT_NONE) &&
simon 0:350011bf8be7 163 (msg_ps->ext_object_def.access & MIB_ACCESS_READ))
simon 0:350011bf8be7 164 {
simon 0:350011bf8be7 165 msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE;
simon 0:350011bf8be7 166 en->get_value_q(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 167 }
simon 0:350011bf8be7 168 else
simon 0:350011bf8be7 169 {
simon 0:350011bf8be7 170 en->get_object_def_pc(request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 171 /* search failed, object id points to unknown object (nosuchname) */
simon 0:350011bf8be7 172 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 173 }
simon 0:350011bf8be7 174 }
simon 0:350011bf8be7 175 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_VALUE)
simon 0:350011bf8be7 176 {
simon 0:350011bf8be7 177 struct mib_external_node *en;
simon 0:350011bf8be7 178 struct snmp_varbind *vb;
simon 0:350011bf8be7 179
simon 0:350011bf8be7 180 /* get_value() answer */
simon 0:350011bf8be7 181 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 182
simon 0:350011bf8be7 183 /* allocate output varbind */
simon 0:350011bf8be7 184 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
simon 0:350011bf8be7 185 LWIP_ASSERT("vb != NULL",vb != NULL);
simon 0:350011bf8be7 186 if (vb != NULL)
simon 0:350011bf8be7 187 {
simon 0:350011bf8be7 188 vb->next = NULL;
simon 0:350011bf8be7 189 vb->prev = NULL;
simon 0:350011bf8be7 190
simon 0:350011bf8be7 191 /* move name from invb to outvb */
simon 0:350011bf8be7 192 vb->ident = msg_ps->vb_ptr->ident;
simon 0:350011bf8be7 193 vb->ident_len = msg_ps->vb_ptr->ident_len;
simon 0:350011bf8be7 194 /* ensure this memory is refereced once only */
simon 0:350011bf8be7 195 msg_ps->vb_ptr->ident = NULL;
simon 0:350011bf8be7 196 msg_ps->vb_ptr->ident_len = 0;
simon 0:350011bf8be7 197
simon 0:350011bf8be7 198 vb->value_type = msg_ps->ext_object_def.asn_type;
simon 0:350011bf8be7 199 LWIP_ASSERT("invalid length", msg_ps->ext_object_def.v_len <= 0xff);
simon 0:350011bf8be7 200 vb->value_len = (u8_t)msg_ps->ext_object_def.v_len;
simon 0:350011bf8be7 201 if (vb->value_len > 0)
simon 0:350011bf8be7 202 {
simon 0:350011bf8be7 203 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low", vb->value_len <= SNMP_MAX_VALUE_SIZE);
simon 0:350011bf8be7 204 vb->value = memp_malloc(MEMP_SNMP_VALUE);
simon 0:350011bf8be7 205 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
simon 0:350011bf8be7 206 if (vb->value != NULL)
simon 0:350011bf8be7 207 {
simon 0:350011bf8be7 208 en->get_value_a(request_id, &msg_ps->ext_object_def, vb->value_len, vb->value);
simon 0:350011bf8be7 209 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 210 /* search again (if vb_idx < msg_ps->invb.count) */
simon 0:350011bf8be7 211 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 212 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 213 }
simon 0:350011bf8be7 214 else
simon 0:350011bf8be7 215 {
simon 0:350011bf8be7 216 en->get_value_pc(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 217 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no variable space\n"));
simon 0:350011bf8be7 218 msg_ps->vb_ptr->ident = vb->ident;
simon 0:350011bf8be7 219 msg_ps->vb_ptr->ident_len = vb->ident_len;
simon 0:350011bf8be7 220 memp_free(MEMP_SNMP_VARBIND, vb);
simon 0:350011bf8be7 221 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 222 }
simon 0:350011bf8be7 223 }
simon 0:350011bf8be7 224 else
simon 0:350011bf8be7 225 {
simon 0:350011bf8be7 226 /* vb->value_len == 0, empty value (e.g. empty string) */
simon 0:350011bf8be7 227 en->get_value_a(request_id, &msg_ps->ext_object_def, 0, NULL);
simon 0:350011bf8be7 228 vb->value = NULL;
simon 0:350011bf8be7 229 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 230 /* search again (if vb_idx < msg_ps->invb.count) */
simon 0:350011bf8be7 231 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 232 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 233 }
simon 0:350011bf8be7 234 }
simon 0:350011bf8be7 235 else
simon 0:350011bf8be7 236 {
simon 0:350011bf8be7 237 en->get_value_pc(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 238 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no outvb space\n"));
simon 0:350011bf8be7 239 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 240 }
simon 0:350011bf8be7 241 }
simon 0:350011bf8be7 242
simon 0:350011bf8be7 243 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 244 (msg_ps->vb_idx < msg_ps->invb.count))
simon 0:350011bf8be7 245 {
simon 0:350011bf8be7 246 struct mib_node *mn;
simon 0:350011bf8be7 247 struct snmp_name_ptr np;
simon 0:350011bf8be7 248
simon 0:350011bf8be7 249 if (msg_ps->vb_idx == 0)
simon 0:350011bf8be7 250 {
simon 0:350011bf8be7 251 msg_ps->vb_ptr = msg_ps->invb.head;
simon 0:350011bf8be7 252 }
simon 0:350011bf8be7 253 else
simon 0:350011bf8be7 254 {
simon 0:350011bf8be7 255 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
simon 0:350011bf8be7 256 }
simon 0:350011bf8be7 257 /** test object identifier for .iso.org.dod.internet prefix */
simon 0:350011bf8be7 258 if (snmp_iso_prefix_tst(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident))
simon 0:350011bf8be7 259 {
simon 0:350011bf8be7 260 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
simon 0:350011bf8be7 261 msg_ps->vb_ptr->ident + 4, &np);
simon 0:350011bf8be7 262 if (mn != NULL)
simon 0:350011bf8be7 263 {
simon 0:350011bf8be7 264 if (mn->node_type == MIB_NODE_EX)
simon 0:350011bf8be7 265 {
simon 0:350011bf8be7 266 /* external object */
simon 0:350011bf8be7 267 struct mib_external_node *en = (struct mib_external_node*)mn;
simon 0:350011bf8be7 268
simon 0:350011bf8be7 269 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 270 /* save en && args in msg_ps!! */
simon 0:350011bf8be7 271 msg_ps->ext_mib_node = en;
simon 0:350011bf8be7 272 msg_ps->ext_name_ptr = np;
simon 0:350011bf8be7 273
simon 0:350011bf8be7 274 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 275 }
simon 0:350011bf8be7 276 else
simon 0:350011bf8be7 277 {
simon 0:350011bf8be7 278 /* internal object */
simon 0:350011bf8be7 279 struct obj_def object_def;
simon 0:350011bf8be7 280
simon 0:350011bf8be7 281 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 282 mn->get_object_def(np.ident_len, np.ident, &object_def);
simon 0:350011bf8be7 283 if ((object_def.instance != MIB_OBJECT_NONE) &&
simon 0:350011bf8be7 284 (object_def.access & MIB_ACCESS_READ))
simon 0:350011bf8be7 285 {
simon 0:350011bf8be7 286 mn = mn;
simon 0:350011bf8be7 287 }
simon 0:350011bf8be7 288 else
simon 0:350011bf8be7 289 {
simon 0:350011bf8be7 290 /* search failed, object id points to unknown object (nosuchname) */
simon 0:350011bf8be7 291 mn = NULL;
simon 0:350011bf8be7 292 }
simon 0:350011bf8be7 293 if (mn != NULL)
simon 0:350011bf8be7 294 {
simon 0:350011bf8be7 295 struct snmp_varbind *vb;
simon 0:350011bf8be7 296
simon 0:350011bf8be7 297 msg_ps->state = SNMP_MSG_INTERNAL_GET_VALUE;
simon 0:350011bf8be7 298 /* allocate output varbind */
simon 0:350011bf8be7 299 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
simon 0:350011bf8be7 300 LWIP_ASSERT("vb != NULL",vb != NULL);
simon 0:350011bf8be7 301 if (vb != NULL)
simon 0:350011bf8be7 302 {
simon 0:350011bf8be7 303 vb->next = NULL;
simon 0:350011bf8be7 304 vb->prev = NULL;
simon 0:350011bf8be7 305
simon 0:350011bf8be7 306 /* move name from invb to outvb */
simon 0:350011bf8be7 307 vb->ident = msg_ps->vb_ptr->ident;
simon 0:350011bf8be7 308 vb->ident_len = msg_ps->vb_ptr->ident_len;
simon 0:350011bf8be7 309 /* ensure this memory is refereced once only */
simon 0:350011bf8be7 310 msg_ps->vb_ptr->ident = NULL;
simon 0:350011bf8be7 311 msg_ps->vb_ptr->ident_len = 0;
simon 0:350011bf8be7 312
simon 0:350011bf8be7 313 vb->value_type = object_def.asn_type;
simon 0:350011bf8be7 314 LWIP_ASSERT("invalid length", object_def.v_len <= 0xff);
simon 0:350011bf8be7 315 vb->value_len = (u8_t)object_def.v_len;
simon 0:350011bf8be7 316 if (vb->value_len > 0)
simon 0:350011bf8be7 317 {
simon 0:350011bf8be7 318 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low",
simon 0:350011bf8be7 319 vb->value_len <= SNMP_MAX_VALUE_SIZE);
simon 0:350011bf8be7 320 vb->value = memp_malloc(MEMP_SNMP_VALUE);
simon 0:350011bf8be7 321 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
simon 0:350011bf8be7 322 if (vb->value != NULL)
simon 0:350011bf8be7 323 {
simon 0:350011bf8be7 324 mn->get_value(&object_def, vb->value_len, vb->value);
simon 0:350011bf8be7 325 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 326 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 327 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 328 }
simon 0:350011bf8be7 329 else
simon 0:350011bf8be7 330 {
simon 0:350011bf8be7 331 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate variable space\n"));
simon 0:350011bf8be7 332 msg_ps->vb_ptr->ident = vb->ident;
simon 0:350011bf8be7 333 msg_ps->vb_ptr->ident_len = vb->ident_len;
simon 0:350011bf8be7 334 memp_free(MEMP_SNMP_VARBIND, vb);
simon 0:350011bf8be7 335 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 336 }
simon 0:350011bf8be7 337 }
simon 0:350011bf8be7 338 else
simon 0:350011bf8be7 339 {
simon 0:350011bf8be7 340 /* vb->value_len == 0, empty value (e.g. empty string) */
simon 0:350011bf8be7 341 vb->value = NULL;
simon 0:350011bf8be7 342 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 343 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 344 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 345 }
simon 0:350011bf8be7 346 }
simon 0:350011bf8be7 347 else
simon 0:350011bf8be7 348 {
simon 0:350011bf8be7 349 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate outvb space\n"));
simon 0:350011bf8be7 350 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 351 }
simon 0:350011bf8be7 352 }
simon 0:350011bf8be7 353 }
simon 0:350011bf8be7 354 }
simon 0:350011bf8be7 355 }
simon 0:350011bf8be7 356 else
simon 0:350011bf8be7 357 {
simon 0:350011bf8be7 358 mn = NULL;
simon 0:350011bf8be7 359 }
simon 0:350011bf8be7 360 if (mn == NULL)
simon 0:350011bf8be7 361 {
simon 0:350011bf8be7 362 /* mn == NULL, noSuchName */
simon 0:350011bf8be7 363 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 364 }
simon 0:350011bf8be7 365 }
simon 0:350011bf8be7 366 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 367 (msg_ps->vb_idx == msg_ps->invb.count))
simon 0:350011bf8be7 368 {
simon 0:350011bf8be7 369 snmp_ok_response(msg_ps);
simon 0:350011bf8be7 370 }
simon 0:350011bf8be7 371 }
simon 0:350011bf8be7 372
simon 0:350011bf8be7 373 /**
simon 0:350011bf8be7 374 * Service an internal or external event for SNMP GETNEXT.
simon 0:350011bf8be7 375 *
simon 0:350011bf8be7 376 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
simon 0:350011bf8be7 377 * @param msg_ps points to the assosicated message process state
simon 0:350011bf8be7 378 */
simon 0:350011bf8be7 379 static void
simon 0:350011bf8be7 380 snmp_msg_getnext_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
simon 0:350011bf8be7 381 {
simon 0:350011bf8be7 382 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_getnext_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
simon 0:350011bf8be7 383
simon 0:350011bf8be7 384 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
simon 0:350011bf8be7 385 {
simon 0:350011bf8be7 386 struct mib_external_node *en;
simon 0:350011bf8be7 387
simon 0:350011bf8be7 388 /* get_object_def() answer*/
simon 0:350011bf8be7 389 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 390
simon 0:350011bf8be7 391 /* translate answer into a known lifeform */
simon 0:350011bf8be7 392 en->get_object_def_a(request_id, 1, &msg_ps->ext_oid.id[msg_ps->ext_oid.len - 1], &msg_ps->ext_object_def);
simon 0:350011bf8be7 393 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
simon 0:350011bf8be7 394 {
simon 0:350011bf8be7 395 msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE;
simon 0:350011bf8be7 396 en->get_value_q(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 397 }
simon 0:350011bf8be7 398 else
simon 0:350011bf8be7 399 {
simon 0:350011bf8be7 400 en->get_object_def_pc(request_id, 1, &msg_ps->ext_oid.id[msg_ps->ext_oid.len - 1]);
simon 0:350011bf8be7 401 /* search failed, object id points to unknown object (nosuchname) */
simon 0:350011bf8be7 402 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 403 }
simon 0:350011bf8be7 404 }
simon 0:350011bf8be7 405 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_VALUE)
simon 0:350011bf8be7 406 {
simon 0:350011bf8be7 407 struct mib_external_node *en;
simon 0:350011bf8be7 408 struct snmp_varbind *vb;
simon 0:350011bf8be7 409
simon 0:350011bf8be7 410 /* get_value() answer */
simon 0:350011bf8be7 411 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 412
simon 0:350011bf8be7 413 LWIP_ASSERT("invalid length", msg_ps->ext_object_def.v_len <= 0xff);
simon 0:350011bf8be7 414 vb = snmp_varbind_alloc(&msg_ps->ext_oid,
simon 0:350011bf8be7 415 msg_ps->ext_object_def.asn_type,
simon 0:350011bf8be7 416 (u8_t)msg_ps->ext_object_def.v_len);
simon 0:350011bf8be7 417 if (vb != NULL)
simon 0:350011bf8be7 418 {
simon 0:350011bf8be7 419 en->get_value_a(request_id, &msg_ps->ext_object_def, vb->value_len, vb->value);
simon 0:350011bf8be7 420 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 421 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 422 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 423 }
simon 0:350011bf8be7 424 else
simon 0:350011bf8be7 425 {
simon 0:350011bf8be7 426 en->get_value_pc(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 427 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_getnext_event: couldn't allocate outvb space\n"));
simon 0:350011bf8be7 428 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 429 }
simon 0:350011bf8be7 430 }
simon 0:350011bf8be7 431
simon 0:350011bf8be7 432 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 433 (msg_ps->vb_idx < msg_ps->invb.count))
simon 0:350011bf8be7 434 {
simon 0:350011bf8be7 435 struct mib_node *mn;
simon 0:350011bf8be7 436 struct snmp_obj_id oid;
simon 0:350011bf8be7 437
simon 0:350011bf8be7 438 if (msg_ps->vb_idx == 0)
simon 0:350011bf8be7 439 {
simon 0:350011bf8be7 440 msg_ps->vb_ptr = msg_ps->invb.head;
simon 0:350011bf8be7 441 }
simon 0:350011bf8be7 442 else
simon 0:350011bf8be7 443 {
simon 0:350011bf8be7 444 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
simon 0:350011bf8be7 445 }
simon 0:350011bf8be7 446 if (snmp_iso_prefix_expand(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident, &oid))
simon 0:350011bf8be7 447 {
simon 0:350011bf8be7 448 if (msg_ps->vb_ptr->ident_len > 3)
simon 0:350011bf8be7 449 {
simon 0:350011bf8be7 450 /* can offset ident_len and ident */
simon 0:350011bf8be7 451 mn = snmp_expand_tree((struct mib_node*)&internet,
simon 0:350011bf8be7 452 msg_ps->vb_ptr->ident_len - 4,
simon 0:350011bf8be7 453 msg_ps->vb_ptr->ident + 4, &oid);
simon 0:350011bf8be7 454 }
simon 0:350011bf8be7 455 else
simon 0:350011bf8be7 456 {
simon 0:350011bf8be7 457 /* can't offset ident_len -4, ident + 4 */
simon 0:350011bf8be7 458 mn = snmp_expand_tree((struct mib_node*)&internet, 0, NULL, &oid);
simon 0:350011bf8be7 459 }
simon 0:350011bf8be7 460 }
simon 0:350011bf8be7 461 else
simon 0:350011bf8be7 462 {
simon 0:350011bf8be7 463 mn = NULL;
simon 0:350011bf8be7 464 }
simon 0:350011bf8be7 465 if (mn != NULL)
simon 0:350011bf8be7 466 {
simon 0:350011bf8be7 467 if (mn->node_type == MIB_NODE_EX)
simon 0:350011bf8be7 468 {
simon 0:350011bf8be7 469 /* external object */
simon 0:350011bf8be7 470 struct mib_external_node *en = (struct mib_external_node*)mn;
simon 0:350011bf8be7 471
simon 0:350011bf8be7 472 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 473 /* save en && args in msg_ps!! */
simon 0:350011bf8be7 474 msg_ps->ext_mib_node = en;
simon 0:350011bf8be7 475 msg_ps->ext_oid = oid;
simon 0:350011bf8be7 476
simon 0:350011bf8be7 477 en->get_object_def_q(en->addr_inf, request_id, 1, &oid.id[oid.len - 1]);
simon 0:350011bf8be7 478 }
simon 0:350011bf8be7 479 else
simon 0:350011bf8be7 480 {
simon 0:350011bf8be7 481 /* internal object */
simon 0:350011bf8be7 482 struct obj_def object_def;
simon 0:350011bf8be7 483 struct snmp_varbind *vb;
simon 0:350011bf8be7 484
simon 0:350011bf8be7 485 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 486 mn->get_object_def(1, &oid.id[oid.len - 1], &object_def);
simon 0:350011bf8be7 487
simon 0:350011bf8be7 488 LWIP_ASSERT("invalid length", object_def.v_len <= 0xff);
simon 0:350011bf8be7 489 vb = snmp_varbind_alloc(&oid, object_def.asn_type, (u8_t)object_def.v_len);
simon 0:350011bf8be7 490 if (vb != NULL)
simon 0:350011bf8be7 491 {
simon 0:350011bf8be7 492 msg_ps->state = SNMP_MSG_INTERNAL_GET_VALUE;
simon 0:350011bf8be7 493 mn->get_value(&object_def, object_def.v_len, vb->value);
simon 0:350011bf8be7 494 snmp_varbind_tail_add(&msg_ps->outvb, vb);
simon 0:350011bf8be7 495 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 496 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 497 }
simon 0:350011bf8be7 498 else
simon 0:350011bf8be7 499 {
simon 0:350011bf8be7 500 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space\n"));
simon 0:350011bf8be7 501 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
simon 0:350011bf8be7 502 }
simon 0:350011bf8be7 503 }
simon 0:350011bf8be7 504 }
simon 0:350011bf8be7 505 if (mn == NULL)
simon 0:350011bf8be7 506 {
simon 0:350011bf8be7 507 /* mn == NULL, noSuchName */
simon 0:350011bf8be7 508 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 509 }
simon 0:350011bf8be7 510 }
simon 0:350011bf8be7 511 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 512 (msg_ps->vb_idx == msg_ps->invb.count))
simon 0:350011bf8be7 513 {
simon 0:350011bf8be7 514 snmp_ok_response(msg_ps);
simon 0:350011bf8be7 515 }
simon 0:350011bf8be7 516 }
simon 0:350011bf8be7 517
simon 0:350011bf8be7 518 /**
simon 0:350011bf8be7 519 * Service an internal or external event for SNMP SET.
simon 0:350011bf8be7 520 *
simon 0:350011bf8be7 521 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
simon 0:350011bf8be7 522 * @param msg_ps points to the assosicated message process state
simon 0:350011bf8be7 523 */
simon 0:350011bf8be7 524 static void
simon 0:350011bf8be7 525 snmp_msg_set_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
simon 0:350011bf8be7 526 {
simon 0:350011bf8be7 527 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_set_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
simon 0:350011bf8be7 528
simon 0:350011bf8be7 529 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
simon 0:350011bf8be7 530 {
simon 0:350011bf8be7 531 struct mib_external_node *en;
simon 0:350011bf8be7 532 struct snmp_name_ptr np;
simon 0:350011bf8be7 533
simon 0:350011bf8be7 534 /* get_object_def() answer*/
simon 0:350011bf8be7 535 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 536 np = msg_ps->ext_name_ptr;
simon 0:350011bf8be7 537
simon 0:350011bf8be7 538 /* translate answer into a known lifeform */
simon 0:350011bf8be7 539 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
simon 0:350011bf8be7 540 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
simon 0:350011bf8be7 541 {
simon 0:350011bf8be7 542 msg_ps->state = SNMP_MSG_EXTERNAL_SET_TEST;
simon 0:350011bf8be7 543 en->set_test_q(request_id, &msg_ps->ext_object_def);
simon 0:350011bf8be7 544 }
simon 0:350011bf8be7 545 else
simon 0:350011bf8be7 546 {
simon 0:350011bf8be7 547 en->get_object_def_pc(request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 548 /* search failed, object id points to unknown object (nosuchname) */
simon 0:350011bf8be7 549 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 550 }
simon 0:350011bf8be7 551 }
simon 0:350011bf8be7 552 else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_TEST)
simon 0:350011bf8be7 553 {
simon 0:350011bf8be7 554 struct mib_external_node *en;
simon 0:350011bf8be7 555
simon 0:350011bf8be7 556 /* set_test() answer*/
simon 0:350011bf8be7 557 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 558
simon 0:350011bf8be7 559 if (msg_ps->ext_object_def.access & MIB_ACCESS_WRITE)
simon 0:350011bf8be7 560 {
simon 0:350011bf8be7 561 if ((msg_ps->ext_object_def.asn_type == msg_ps->vb_ptr->value_type) &&
simon 0:350011bf8be7 562 (en->set_test_a(request_id,&msg_ps->ext_object_def,
simon 0:350011bf8be7 563 msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value) != 0))
simon 0:350011bf8be7 564 {
simon 0:350011bf8be7 565 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 566 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 567 }
simon 0:350011bf8be7 568 else
simon 0:350011bf8be7 569 {
simon 0:350011bf8be7 570 en->set_test_pc(request_id,&msg_ps->ext_object_def);
simon 0:350011bf8be7 571 /* bad value */
simon 0:350011bf8be7 572 snmp_error_response(msg_ps,SNMP_ES_BADVALUE);
simon 0:350011bf8be7 573 }
simon 0:350011bf8be7 574 }
simon 0:350011bf8be7 575 else
simon 0:350011bf8be7 576 {
simon 0:350011bf8be7 577 en->set_test_pc(request_id,&msg_ps->ext_object_def);
simon 0:350011bf8be7 578 /* object not available for set */
simon 0:350011bf8be7 579 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 580 }
simon 0:350011bf8be7 581 }
simon 0:350011bf8be7 582 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF_S)
simon 0:350011bf8be7 583 {
simon 0:350011bf8be7 584 struct mib_external_node *en;
simon 0:350011bf8be7 585 struct snmp_name_ptr np;
simon 0:350011bf8be7 586
simon 0:350011bf8be7 587 /* get_object_def() answer*/
simon 0:350011bf8be7 588 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 589 np = msg_ps->ext_name_ptr;
simon 0:350011bf8be7 590
simon 0:350011bf8be7 591 /* translate answer into a known lifeform */
simon 0:350011bf8be7 592 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
simon 0:350011bf8be7 593 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
simon 0:350011bf8be7 594 {
simon 0:350011bf8be7 595 msg_ps->state = SNMP_MSG_EXTERNAL_SET_VALUE;
simon 0:350011bf8be7 596 en->set_value_q(request_id, &msg_ps->ext_object_def,
simon 0:350011bf8be7 597 msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value);
simon 0:350011bf8be7 598 }
simon 0:350011bf8be7 599 else
simon 0:350011bf8be7 600 {
simon 0:350011bf8be7 601 en->get_object_def_pc(request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 602 /* set_value failed, object has disappeared for some odd reason?? */
simon 0:350011bf8be7 603 snmp_error_response(msg_ps,SNMP_ES_GENERROR);
simon 0:350011bf8be7 604 }
simon 0:350011bf8be7 605 }
simon 0:350011bf8be7 606 else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_VALUE)
simon 0:350011bf8be7 607 {
simon 0:350011bf8be7 608 struct mib_external_node *en;
simon 0:350011bf8be7 609
simon 0:350011bf8be7 610 /** set_value_a() */
simon 0:350011bf8be7 611 en = msg_ps->ext_mib_node;
simon 0:350011bf8be7 612 en->set_value_a(request_id, &msg_ps->ext_object_def,
simon 0:350011bf8be7 613 msg_ps->vb_ptr->value_len, msg_ps->vb_ptr->value);
simon 0:350011bf8be7 614
simon 0:350011bf8be7 615 /** @todo use set_value_pc() if toobig */
simon 0:350011bf8be7 616 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
simon 0:350011bf8be7 617 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 618 }
simon 0:350011bf8be7 619
simon 0:350011bf8be7 620 /* test all values before setting */
simon 0:350011bf8be7 621 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 622 (msg_ps->vb_idx < msg_ps->invb.count))
simon 0:350011bf8be7 623 {
simon 0:350011bf8be7 624 struct mib_node *mn;
simon 0:350011bf8be7 625 struct snmp_name_ptr np;
simon 0:350011bf8be7 626
simon 0:350011bf8be7 627 if (msg_ps->vb_idx == 0)
simon 0:350011bf8be7 628 {
simon 0:350011bf8be7 629 msg_ps->vb_ptr = msg_ps->invb.head;
simon 0:350011bf8be7 630 }
simon 0:350011bf8be7 631 else
simon 0:350011bf8be7 632 {
simon 0:350011bf8be7 633 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
simon 0:350011bf8be7 634 }
simon 0:350011bf8be7 635 /** test object identifier for .iso.org.dod.internet prefix */
simon 0:350011bf8be7 636 if (snmp_iso_prefix_tst(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident))
simon 0:350011bf8be7 637 {
simon 0:350011bf8be7 638 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
simon 0:350011bf8be7 639 msg_ps->vb_ptr->ident + 4, &np);
simon 0:350011bf8be7 640 if (mn != NULL)
simon 0:350011bf8be7 641 {
simon 0:350011bf8be7 642 if (mn->node_type == MIB_NODE_EX)
simon 0:350011bf8be7 643 {
simon 0:350011bf8be7 644 /* external object */
simon 0:350011bf8be7 645 struct mib_external_node *en = (struct mib_external_node*)mn;
simon 0:350011bf8be7 646
simon 0:350011bf8be7 647 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 648 /* save en && args in msg_ps!! */
simon 0:350011bf8be7 649 msg_ps->ext_mib_node = en;
simon 0:350011bf8be7 650 msg_ps->ext_name_ptr = np;
simon 0:350011bf8be7 651
simon 0:350011bf8be7 652 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 653 }
simon 0:350011bf8be7 654 else
simon 0:350011bf8be7 655 {
simon 0:350011bf8be7 656 /* internal object */
simon 0:350011bf8be7 657 struct obj_def object_def;
simon 0:350011bf8be7 658
simon 0:350011bf8be7 659 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
simon 0:350011bf8be7 660 mn->get_object_def(np.ident_len, np.ident, &object_def);
simon 0:350011bf8be7 661 if (object_def.instance != MIB_OBJECT_NONE)
simon 0:350011bf8be7 662 {
simon 0:350011bf8be7 663 mn = mn;
simon 0:350011bf8be7 664 }
simon 0:350011bf8be7 665 else
simon 0:350011bf8be7 666 {
simon 0:350011bf8be7 667 /* search failed, object id points to unknown object (nosuchname) */
simon 0:350011bf8be7 668 mn = NULL;
simon 0:350011bf8be7 669 }
simon 0:350011bf8be7 670 if (mn != NULL)
simon 0:350011bf8be7 671 {
simon 0:350011bf8be7 672 msg_ps->state = SNMP_MSG_INTERNAL_SET_TEST;
simon 0:350011bf8be7 673
simon 0:350011bf8be7 674 if (object_def.access & MIB_ACCESS_WRITE)
simon 0:350011bf8be7 675 {
simon 0:350011bf8be7 676 if ((object_def.asn_type == msg_ps->vb_ptr->value_type) &&
simon 0:350011bf8be7 677 (mn->set_test(&object_def,msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value) != 0))
simon 0:350011bf8be7 678 {
simon 0:350011bf8be7 679 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 680 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 681 }
simon 0:350011bf8be7 682 else
simon 0:350011bf8be7 683 {
simon 0:350011bf8be7 684 /* bad value */
simon 0:350011bf8be7 685 snmp_error_response(msg_ps,SNMP_ES_BADVALUE);
simon 0:350011bf8be7 686 }
simon 0:350011bf8be7 687 }
simon 0:350011bf8be7 688 else
simon 0:350011bf8be7 689 {
simon 0:350011bf8be7 690 /* object not available for set */
simon 0:350011bf8be7 691 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 692 }
simon 0:350011bf8be7 693 }
simon 0:350011bf8be7 694 }
simon 0:350011bf8be7 695 }
simon 0:350011bf8be7 696 }
simon 0:350011bf8be7 697 else
simon 0:350011bf8be7 698 {
simon 0:350011bf8be7 699 mn = NULL;
simon 0:350011bf8be7 700 }
simon 0:350011bf8be7 701 if (mn == NULL)
simon 0:350011bf8be7 702 {
simon 0:350011bf8be7 703 /* mn == NULL, noSuchName */
simon 0:350011bf8be7 704 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
simon 0:350011bf8be7 705 }
simon 0:350011bf8be7 706 }
simon 0:350011bf8be7 707
simon 0:350011bf8be7 708 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
simon 0:350011bf8be7 709 (msg_ps->vb_idx == msg_ps->invb.count))
simon 0:350011bf8be7 710 {
simon 0:350011bf8be7 711 msg_ps->vb_idx = 0;
simon 0:350011bf8be7 712 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
simon 0:350011bf8be7 713 }
simon 0:350011bf8be7 714
simon 0:350011bf8be7 715 /* set all values "atomically" (be as "atomic" as possible) */
simon 0:350011bf8be7 716 while ((msg_ps->state == SNMP_MSG_INTERNAL_SET_VALUE) &&
simon 0:350011bf8be7 717 (msg_ps->vb_idx < msg_ps->invb.count))
simon 0:350011bf8be7 718 {
simon 0:350011bf8be7 719 struct mib_node *mn;
simon 0:350011bf8be7 720 struct snmp_name_ptr np;
simon 0:350011bf8be7 721
simon 0:350011bf8be7 722 if (msg_ps->vb_idx == 0)
simon 0:350011bf8be7 723 {
simon 0:350011bf8be7 724 msg_ps->vb_ptr = msg_ps->invb.head;
simon 0:350011bf8be7 725 }
simon 0:350011bf8be7 726 else
simon 0:350011bf8be7 727 {
simon 0:350011bf8be7 728 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
simon 0:350011bf8be7 729 }
simon 0:350011bf8be7 730 /* skip iso prefix test, was done previously while settesting() */
simon 0:350011bf8be7 731 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
simon 0:350011bf8be7 732 msg_ps->vb_ptr->ident + 4, &np);
simon 0:350011bf8be7 733 /* check if object is still available
simon 0:350011bf8be7 734 (e.g. external hot-plug thingy present?) */
simon 0:350011bf8be7 735 if (mn != NULL)
simon 0:350011bf8be7 736 {
simon 0:350011bf8be7 737 if (mn->node_type == MIB_NODE_EX)
simon 0:350011bf8be7 738 {
simon 0:350011bf8be7 739 /* external object */
simon 0:350011bf8be7 740 struct mib_external_node *en = (struct mib_external_node*)mn;
simon 0:350011bf8be7 741
simon 0:350011bf8be7 742 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF_S;
simon 0:350011bf8be7 743 /* save en && args in msg_ps!! */
simon 0:350011bf8be7 744 msg_ps->ext_mib_node = en;
simon 0:350011bf8be7 745 msg_ps->ext_name_ptr = np;
simon 0:350011bf8be7 746
simon 0:350011bf8be7 747 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
simon 0:350011bf8be7 748 }
simon 0:350011bf8be7 749 else
simon 0:350011bf8be7 750 {
simon 0:350011bf8be7 751 /* internal object */
simon 0:350011bf8be7 752 struct obj_def object_def;
simon 0:350011bf8be7 753
simon 0:350011bf8be7 754 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF_S;
simon 0:350011bf8be7 755 mn->get_object_def(np.ident_len, np.ident, &object_def);
simon 0:350011bf8be7 756 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
simon 0:350011bf8be7 757 mn->set_value(&object_def,msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value);
simon 0:350011bf8be7 758 msg_ps->vb_idx += 1;
simon 0:350011bf8be7 759 }
simon 0:350011bf8be7 760 }
simon 0:350011bf8be7 761 }
simon 0:350011bf8be7 762 if ((msg_ps->state == SNMP_MSG_INTERNAL_SET_VALUE) &&
simon 0:350011bf8be7 763 (msg_ps->vb_idx == msg_ps->invb.count))
simon 0:350011bf8be7 764 {
simon 0:350011bf8be7 765 /* simply echo the input if we can set it
simon 0:350011bf8be7 766 @todo do we need to return the actual value?
simon 0:350011bf8be7 767 e.g. if value is silently modified or behaves sticky? */
simon 0:350011bf8be7 768 msg_ps->outvb = msg_ps->invb;
simon 0:350011bf8be7 769 msg_ps->invb.head = NULL;
simon 0:350011bf8be7 770 msg_ps->invb.tail = NULL;
simon 0:350011bf8be7 771 msg_ps->invb.count = 0;
simon 0:350011bf8be7 772 snmp_ok_response(msg_ps);
simon 0:350011bf8be7 773 }
simon 0:350011bf8be7 774 }
simon 0:350011bf8be7 775
simon 0:350011bf8be7 776
simon 0:350011bf8be7 777 /**
simon 0:350011bf8be7 778 * Handle one internal or external event.
simon 0:350011bf8be7 779 * Called for one async event. (recv external/private answer)
simon 0:350011bf8be7 780 *
simon 0:350011bf8be7 781 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
simon 0:350011bf8be7 782 */
simon 0:350011bf8be7 783 void
simon 0:350011bf8be7 784 snmp_msg_event(u8_t request_id)
simon 0:350011bf8be7 785 {
simon 0:350011bf8be7 786 struct snmp_msg_pstat *msg_ps;
simon 0:350011bf8be7 787
simon 0:350011bf8be7 788 if (request_id < SNMP_CONCURRENT_REQUESTS)
simon 0:350011bf8be7 789 {
simon 0:350011bf8be7 790 msg_ps = &msg_input_list[request_id];
simon 0:350011bf8be7 791 if (msg_ps->rt == SNMP_ASN1_PDU_GET_NEXT_REQ)
simon 0:350011bf8be7 792 {
simon 0:350011bf8be7 793 snmp_msg_getnext_event(request_id, msg_ps);
simon 0:350011bf8be7 794 }
simon 0:350011bf8be7 795 else if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ)
simon 0:350011bf8be7 796 {
simon 0:350011bf8be7 797 snmp_msg_get_event(request_id, msg_ps);
simon 0:350011bf8be7 798 }
simon 0:350011bf8be7 799 else if(msg_ps->rt == SNMP_ASN1_PDU_SET_REQ)
simon 0:350011bf8be7 800 {
simon 0:350011bf8be7 801 snmp_msg_set_event(request_id, msg_ps);
simon 0:350011bf8be7 802 }
simon 0:350011bf8be7 803 }
simon 0:350011bf8be7 804 }
simon 0:350011bf8be7 805
simon 0:350011bf8be7 806
simon 0:350011bf8be7 807 /* lwIP UDP receive callback function */
simon 0:350011bf8be7 808 static void
simon 0:350011bf8be7 809 snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
simon 0:350011bf8be7 810 {
simon 0:350011bf8be7 811 struct snmp_msg_pstat *msg_ps;
simon 0:350011bf8be7 812 u8_t req_idx;
simon 0:350011bf8be7 813 err_t err_ret;
simon 0:350011bf8be7 814 u16_t payload_len = p->tot_len;
simon 0:350011bf8be7 815 u16_t payload_ofs = 0;
simon 0:350011bf8be7 816 u16_t varbind_ofs = 0;
simon 0:350011bf8be7 817
simon 0:350011bf8be7 818 /* suppress unused argument warning */
simon 0:350011bf8be7 819 LWIP_UNUSED_ARG(arg);
simon 0:350011bf8be7 820
simon 0:350011bf8be7 821 /* traverse input message process list, look for SNMP_MSG_EMPTY */
simon 0:350011bf8be7 822 msg_ps = &msg_input_list[0];
simon 0:350011bf8be7 823 req_idx = 0;
simon 0:350011bf8be7 824 while ((req_idx < SNMP_CONCURRENT_REQUESTS) && (msg_ps->state != SNMP_MSG_EMPTY))
simon 0:350011bf8be7 825 {
simon 0:350011bf8be7 826 req_idx++;
simon 0:350011bf8be7 827 msg_ps++;
simon 0:350011bf8be7 828 }
simon 0:350011bf8be7 829 if (req_idx == SNMP_CONCURRENT_REQUESTS)
simon 0:350011bf8be7 830 {
simon 0:350011bf8be7 831 /* exceeding number of concurrent requests */
simon 0:350011bf8be7 832 pbuf_free(p);
simon 0:350011bf8be7 833 return;
simon 0:350011bf8be7 834 }
simon 0:350011bf8be7 835
simon 0:350011bf8be7 836 /* accepting request */
simon 0:350011bf8be7 837 snmp_inc_snmpinpkts();
simon 0:350011bf8be7 838 /* record used 'protocol control block' */
simon 0:350011bf8be7 839 msg_ps->pcb = pcb;
simon 0:350011bf8be7 840 /* source address (network order) */
simon 0:350011bf8be7 841 msg_ps->sip = *addr;
simon 0:350011bf8be7 842 /* source port (host order (lwIP oddity)) */
simon 0:350011bf8be7 843 msg_ps->sp = port;
simon 0:350011bf8be7 844
simon 0:350011bf8be7 845 /* check total length, version, community, pdu type */
simon 0:350011bf8be7 846 err_ret = snmp_pdu_header_check(p, payload_ofs, payload_len, &varbind_ofs, msg_ps);
simon 0:350011bf8be7 847 /* Only accept requests and requests without error (be robust) */
simon 0:350011bf8be7 848 /* Reject response and trap headers or error requests as input! */
simon 0:350011bf8be7 849 if ((err_ret != ERR_OK) ||
simon 0:350011bf8be7 850 ((msg_ps->rt != SNMP_ASN1_PDU_GET_REQ) &&
simon 0:350011bf8be7 851 (msg_ps->rt != SNMP_ASN1_PDU_GET_NEXT_REQ) &&
simon 0:350011bf8be7 852 (msg_ps->rt != SNMP_ASN1_PDU_SET_REQ)) ||
simon 0:350011bf8be7 853 ((msg_ps->error_status != SNMP_ES_NOERROR) ||
simon 0:350011bf8be7 854 (msg_ps->error_index != 0)) )
simon 0:350011bf8be7 855 {
simon 0:350011bf8be7 856 /* header check failed drop request silently, do not return error! */
simon 0:350011bf8be7 857 pbuf_free(p);
simon 0:350011bf8be7 858 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_header_check() failed\n"));
simon 0:350011bf8be7 859 return;
simon 0:350011bf8be7 860 }
simon 0:350011bf8be7 861 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv ok, community %s\n", msg_ps->community));
simon 0:350011bf8be7 862
simon 0:350011bf8be7 863 /* Builds a list of variable bindings. Copy the varbinds from the pbuf
simon 0:350011bf8be7 864 chain to glue them when these are divided over two or more pbuf's. */
simon 0:350011bf8be7 865 err_ret = snmp_pdu_dec_varbindlist(p, varbind_ofs, &varbind_ofs, msg_ps);
simon 0:350011bf8be7 866 /* we've decoded the incoming message, release input msg now */
simon 0:350011bf8be7 867 pbuf_free(p);
simon 0:350011bf8be7 868 if ((err_ret != ERR_OK) || (msg_ps->invb.count == 0))
simon 0:350011bf8be7 869 {
simon 0:350011bf8be7 870 /* varbind-list decode failed, or varbind list empty.
simon 0:350011bf8be7 871 drop request silently, do not return error!
simon 0:350011bf8be7 872 (errors are only returned for a specific varbind failure) */
simon 0:350011bf8be7 873 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_dec_varbindlist() failed\n"));
simon 0:350011bf8be7 874 return;
simon 0:350011bf8be7 875 }
simon 0:350011bf8be7 876
simon 0:350011bf8be7 877 msg_ps->error_status = SNMP_ES_NOERROR;
simon 0:350011bf8be7 878 msg_ps->error_index = 0;
simon 0:350011bf8be7 879 /* find object for each variable binding */
simon 0:350011bf8be7 880 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
simon 0:350011bf8be7 881 /* first variable binding from list to inspect */
simon 0:350011bf8be7 882 msg_ps->vb_idx = 0;
simon 0:350011bf8be7 883
simon 0:350011bf8be7 884 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv varbind cnt=%"U16_F"\n",(u16_t)msg_ps->invb.count));
simon 0:350011bf8be7 885
simon 0:350011bf8be7 886 /* handle input event and as much objects as possible in one go */
simon 0:350011bf8be7 887 snmp_msg_event(req_idx);
simon 0:350011bf8be7 888 }
simon 0:350011bf8be7 889
simon 0:350011bf8be7 890 /**
simon 0:350011bf8be7 891 * Checks and decodes incoming SNMP message header, logs header errors.
simon 0:350011bf8be7 892 *
simon 0:350011bf8be7 893 * @param p points to pbuf chain of SNMP message (UDP payload)
simon 0:350011bf8be7 894 * @param ofs points to first octet of SNMP message
simon 0:350011bf8be7 895 * @param pdu_len the length of the UDP payload
simon 0:350011bf8be7 896 * @param ofs_ret returns the ofset of the variable bindings
simon 0:350011bf8be7 897 * @param m_stat points to the current message request state return
simon 0:350011bf8be7 898 * @return
simon 0:350011bf8be7 899 * - ERR_OK SNMP header is sane and accepted
simon 0:350011bf8be7 900 * - ERR_ARG SNMP header is either malformed or rejected
simon 0:350011bf8be7 901 */
simon 0:350011bf8be7 902 static err_t
simon 0:350011bf8be7 903 snmp_pdu_header_check(struct pbuf *p, u16_t ofs, u16_t pdu_len, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat)
simon 0:350011bf8be7 904 {
simon 0:350011bf8be7 905 err_t derr;
simon 0:350011bf8be7 906 u16_t len, ofs_base;
simon 0:350011bf8be7 907 u8_t len_octets;
simon 0:350011bf8be7 908 u8_t type;
simon 0:350011bf8be7 909 s32_t version;
simon 0:350011bf8be7 910
simon 0:350011bf8be7 911 ofs_base = ofs;
simon 0:350011bf8be7 912 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 913 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 914 if ((derr != ERR_OK) ||
simon 0:350011bf8be7 915 (pdu_len != (1 + len_octets + len)) ||
simon 0:350011bf8be7 916 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)))
simon 0:350011bf8be7 917 {
simon 0:350011bf8be7 918 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 919 return ERR_ARG;
simon 0:350011bf8be7 920 }
simon 0:350011bf8be7 921 ofs += (1 + len_octets);
simon 0:350011bf8be7 922 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 923 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 924 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
simon 0:350011bf8be7 925 {
simon 0:350011bf8be7 926 /* can't decode or no integer (version) */
simon 0:350011bf8be7 927 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 928 return ERR_ARG;
simon 0:350011bf8be7 929 }
simon 0:350011bf8be7 930 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &version);
simon 0:350011bf8be7 931 if (derr != ERR_OK)
simon 0:350011bf8be7 932 {
simon 0:350011bf8be7 933 /* can't decode */
simon 0:350011bf8be7 934 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 935 return ERR_ARG;
simon 0:350011bf8be7 936 }
simon 0:350011bf8be7 937 if (version != 0)
simon 0:350011bf8be7 938 {
simon 0:350011bf8be7 939 /* not version 1 */
simon 0:350011bf8be7 940 snmp_inc_snmpinbadversions();
simon 0:350011bf8be7 941 return ERR_ARG;
simon 0:350011bf8be7 942 }
simon 0:350011bf8be7 943 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 944 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 945 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 946 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR)))
simon 0:350011bf8be7 947 {
simon 0:350011bf8be7 948 /* can't decode or no octet string (community) */
simon 0:350011bf8be7 949 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 950 return ERR_ARG;
simon 0:350011bf8be7 951 }
simon 0:350011bf8be7 952 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, SNMP_COMMUNITY_STR_LEN, m_stat->community);
simon 0:350011bf8be7 953 if (derr != ERR_OK)
simon 0:350011bf8be7 954 {
simon 0:350011bf8be7 955 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 956 return ERR_ARG;
simon 0:350011bf8be7 957 }
simon 0:350011bf8be7 958 /* add zero terminator */
simon 0:350011bf8be7 959 len = ((len < (SNMP_COMMUNITY_STR_LEN))?(len):(SNMP_COMMUNITY_STR_LEN));
simon 0:350011bf8be7 960 m_stat->community[len] = 0;
simon 0:350011bf8be7 961 m_stat->com_strlen = (u8_t)len;
simon 0:350011bf8be7 962 if (strncmp(snmp_publiccommunity, (const char*)m_stat->community, SNMP_COMMUNITY_STR_LEN) != 0)
simon 0:350011bf8be7 963 {
simon 0:350011bf8be7 964 /** @todo: move this if we need to check more names */
simon 0:350011bf8be7 965 snmp_inc_snmpinbadcommunitynames();
simon 0:350011bf8be7 966 snmp_authfail_trap();
simon 0:350011bf8be7 967 return ERR_ARG;
simon 0:350011bf8be7 968 }
simon 0:350011bf8be7 969 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 970 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 971 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 972 if (derr != ERR_OK)
simon 0:350011bf8be7 973 {
simon 0:350011bf8be7 974 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 975 return ERR_ARG;
simon 0:350011bf8be7 976 }
simon 0:350011bf8be7 977 switch(type)
simon 0:350011bf8be7 978 {
simon 0:350011bf8be7 979 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_REQ):
simon 0:350011bf8be7 980 /* GetRequest PDU */
simon 0:350011bf8be7 981 snmp_inc_snmpingetrequests();
simon 0:350011bf8be7 982 derr = ERR_OK;
simon 0:350011bf8be7 983 break;
simon 0:350011bf8be7 984 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_NEXT_REQ):
simon 0:350011bf8be7 985 /* GetNextRequest PDU */
simon 0:350011bf8be7 986 snmp_inc_snmpingetnexts();
simon 0:350011bf8be7 987 derr = ERR_OK;
simon 0:350011bf8be7 988 break;
simon 0:350011bf8be7 989 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP):
simon 0:350011bf8be7 990 /* GetResponse PDU */
simon 0:350011bf8be7 991 snmp_inc_snmpingetresponses();
simon 0:350011bf8be7 992 derr = ERR_ARG;
simon 0:350011bf8be7 993 break;
simon 0:350011bf8be7 994 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_SET_REQ):
simon 0:350011bf8be7 995 /* SetRequest PDU */
simon 0:350011bf8be7 996 snmp_inc_snmpinsetrequests();
simon 0:350011bf8be7 997 derr = ERR_OK;
simon 0:350011bf8be7 998 break;
simon 0:350011bf8be7 999 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP):
simon 0:350011bf8be7 1000 /* Trap PDU */
simon 0:350011bf8be7 1001 snmp_inc_snmpintraps();
simon 0:350011bf8be7 1002 derr = ERR_ARG;
simon 0:350011bf8be7 1003 break;
simon 0:350011bf8be7 1004 default:
simon 0:350011bf8be7 1005 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1006 derr = ERR_ARG;
simon 0:350011bf8be7 1007 break;
simon 0:350011bf8be7 1008 }
simon 0:350011bf8be7 1009 if (derr != ERR_OK)
simon 0:350011bf8be7 1010 {
simon 0:350011bf8be7 1011 /* unsupported input PDU for this agent (no parse error) */
simon 0:350011bf8be7 1012 return ERR_ARG;
simon 0:350011bf8be7 1013 }
simon 0:350011bf8be7 1014 m_stat->rt = type & 0x1F;
simon 0:350011bf8be7 1015 ofs += (1 + len_octets);
simon 0:350011bf8be7 1016 if (len != (pdu_len - (ofs - ofs_base)))
simon 0:350011bf8be7 1017 {
simon 0:350011bf8be7 1018 /* decoded PDU length does not equal actual payload length */
simon 0:350011bf8be7 1019 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1020 return ERR_ARG;
simon 0:350011bf8be7 1021 }
simon 0:350011bf8be7 1022 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1023 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1024 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
simon 0:350011bf8be7 1025 {
simon 0:350011bf8be7 1026 /* can't decode or no integer (request ID) */
simon 0:350011bf8be7 1027 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1028 return ERR_ARG;
simon 0:350011bf8be7 1029 }
simon 0:350011bf8be7 1030 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->rid);
simon 0:350011bf8be7 1031 if (derr != ERR_OK)
simon 0:350011bf8be7 1032 {
simon 0:350011bf8be7 1033 /* can't decode */
simon 0:350011bf8be7 1034 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1035 return ERR_ARG;
simon 0:350011bf8be7 1036 }
simon 0:350011bf8be7 1037 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 1038 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1039 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1040 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
simon 0:350011bf8be7 1041 {
simon 0:350011bf8be7 1042 /* can't decode or no integer (error-status) */
simon 0:350011bf8be7 1043 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1044 return ERR_ARG;
simon 0:350011bf8be7 1045 }
simon 0:350011bf8be7 1046 /* must be noError (0) for incoming requests.
simon 0:350011bf8be7 1047 log errors for mib-2 completeness and for debug purposes */
simon 0:350011bf8be7 1048 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->error_status);
simon 0:350011bf8be7 1049 if (derr != ERR_OK)
simon 0:350011bf8be7 1050 {
simon 0:350011bf8be7 1051 /* can't decode */
simon 0:350011bf8be7 1052 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1053 return ERR_ARG;
simon 0:350011bf8be7 1054 }
simon 0:350011bf8be7 1055 switch (m_stat->error_status)
simon 0:350011bf8be7 1056 {
simon 0:350011bf8be7 1057 case SNMP_ES_TOOBIG:
simon 0:350011bf8be7 1058 snmp_inc_snmpintoobigs();
simon 0:350011bf8be7 1059 break;
simon 0:350011bf8be7 1060 case SNMP_ES_NOSUCHNAME:
simon 0:350011bf8be7 1061 snmp_inc_snmpinnosuchnames();
simon 0:350011bf8be7 1062 break;
simon 0:350011bf8be7 1063 case SNMP_ES_BADVALUE:
simon 0:350011bf8be7 1064 snmp_inc_snmpinbadvalues();
simon 0:350011bf8be7 1065 break;
simon 0:350011bf8be7 1066 case SNMP_ES_READONLY:
simon 0:350011bf8be7 1067 snmp_inc_snmpinreadonlys();
simon 0:350011bf8be7 1068 break;
simon 0:350011bf8be7 1069 case SNMP_ES_GENERROR:
simon 0:350011bf8be7 1070 snmp_inc_snmpingenerrs();
simon 0:350011bf8be7 1071 break;
simon 0:350011bf8be7 1072 }
simon 0:350011bf8be7 1073 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 1074 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1075 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1076 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
simon 0:350011bf8be7 1077 {
simon 0:350011bf8be7 1078 /* can't decode or no integer (error-index) */
simon 0:350011bf8be7 1079 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1080 return ERR_ARG;
simon 0:350011bf8be7 1081 }
simon 0:350011bf8be7 1082 /* must be 0 for incoming requests.
simon 0:350011bf8be7 1083 decode anyway to catch bad integers (and dirty tricks) */
simon 0:350011bf8be7 1084 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->error_index);
simon 0:350011bf8be7 1085 if (derr != ERR_OK)
simon 0:350011bf8be7 1086 {
simon 0:350011bf8be7 1087 /* can't decode */
simon 0:350011bf8be7 1088 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1089 return ERR_ARG;
simon 0:350011bf8be7 1090 }
simon 0:350011bf8be7 1091 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 1092 *ofs_ret = ofs;
simon 0:350011bf8be7 1093 return ERR_OK;
simon 0:350011bf8be7 1094 }
simon 0:350011bf8be7 1095
simon 0:350011bf8be7 1096 static err_t
simon 0:350011bf8be7 1097 snmp_pdu_dec_varbindlist(struct pbuf *p, u16_t ofs, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat)
simon 0:350011bf8be7 1098 {
simon 0:350011bf8be7 1099 err_t derr;
simon 0:350011bf8be7 1100 u16_t len, vb_len;
simon 0:350011bf8be7 1101 u8_t len_octets;
simon 0:350011bf8be7 1102 u8_t type;
simon 0:350011bf8be7 1103
simon 0:350011bf8be7 1104 /* variable binding list */
simon 0:350011bf8be7 1105 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1106 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &vb_len);
simon 0:350011bf8be7 1107 if ((derr != ERR_OK) ||
simon 0:350011bf8be7 1108 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)))
simon 0:350011bf8be7 1109 {
simon 0:350011bf8be7 1110 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1111 return ERR_ARG;
simon 0:350011bf8be7 1112 }
simon 0:350011bf8be7 1113 ofs += (1 + len_octets);
simon 0:350011bf8be7 1114
simon 0:350011bf8be7 1115 /* start with empty list */
simon 0:350011bf8be7 1116 m_stat->invb.count = 0;
simon 0:350011bf8be7 1117 m_stat->invb.head = NULL;
simon 0:350011bf8be7 1118 m_stat->invb.tail = NULL;
simon 0:350011bf8be7 1119
simon 0:350011bf8be7 1120 while (vb_len > 0)
simon 0:350011bf8be7 1121 {
simon 0:350011bf8be7 1122 struct snmp_obj_id oid, oid_value;
simon 0:350011bf8be7 1123 struct snmp_varbind *vb;
simon 0:350011bf8be7 1124
simon 0:350011bf8be7 1125 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1126 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1127 if ((derr != ERR_OK) ||
simon 0:350011bf8be7 1128 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)) ||
simon 0:350011bf8be7 1129 (len == 0) || (len > vb_len))
simon 0:350011bf8be7 1130 {
simon 0:350011bf8be7 1131 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1132 /* free varbinds (if available) */
simon 0:350011bf8be7 1133 snmp_varbind_list_free(&m_stat->invb);
simon 0:350011bf8be7 1134 return ERR_ARG;
simon 0:350011bf8be7 1135 }
simon 0:350011bf8be7 1136 ofs += (1 + len_octets);
simon 0:350011bf8be7 1137 vb_len -= (1 + len_octets);
simon 0:350011bf8be7 1138
simon 0:350011bf8be7 1139 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1140 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1141 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID)))
simon 0:350011bf8be7 1142 {
simon 0:350011bf8be7 1143 /* can't decode object name length */
simon 0:350011bf8be7 1144 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1145 /* free varbinds (if available) */
simon 0:350011bf8be7 1146 snmp_varbind_list_free(&m_stat->invb);
simon 0:350011bf8be7 1147 return ERR_ARG;
simon 0:350011bf8be7 1148 }
simon 0:350011bf8be7 1149 derr = snmp_asn1_dec_oid(p, ofs + 1 + len_octets, len, &oid);
simon 0:350011bf8be7 1150 if (derr != ERR_OK)
simon 0:350011bf8be7 1151 {
simon 0:350011bf8be7 1152 /* can't decode object name */
simon 0:350011bf8be7 1153 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1154 /* free varbinds (if available) */
simon 0:350011bf8be7 1155 snmp_varbind_list_free(&m_stat->invb);
simon 0:350011bf8be7 1156 return ERR_ARG;
simon 0:350011bf8be7 1157 }
simon 0:350011bf8be7 1158 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 1159 vb_len -= (1 + len_octets + len);
simon 0:350011bf8be7 1160
simon 0:350011bf8be7 1161 snmp_asn1_dec_type(p, ofs, &type);
simon 0:350011bf8be7 1162 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
simon 0:350011bf8be7 1163 if (derr != ERR_OK)
simon 0:350011bf8be7 1164 {
simon 0:350011bf8be7 1165 /* can't decode object value length */
simon 0:350011bf8be7 1166 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1167 /* free varbinds (if available) */
simon 0:350011bf8be7 1168 snmp_varbind_list_free(&m_stat->invb);
simon 0:350011bf8be7 1169 return ERR_ARG;
simon 0:350011bf8be7 1170 }
simon 0:350011bf8be7 1171
simon 0:350011bf8be7 1172 switch (type)
simon 0:350011bf8be7 1173 {
simon 0:350011bf8be7 1174 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
simon 0:350011bf8be7 1175 vb = snmp_varbind_alloc(&oid, type, sizeof(s32_t));
simon 0:350011bf8be7 1176 if (vb != NULL)
simon 0:350011bf8be7 1177 {
simon 0:350011bf8be7 1178 s32_t *vptr = (s32_t*)vb->value;
simon 0:350011bf8be7 1179
simon 0:350011bf8be7 1180 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, vptr);
simon 0:350011bf8be7 1181 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1182 }
simon 0:350011bf8be7 1183 else
simon 0:350011bf8be7 1184 {
simon 0:350011bf8be7 1185 derr = ERR_ARG;
simon 0:350011bf8be7 1186 }
simon 0:350011bf8be7 1187 break;
simon 0:350011bf8be7 1188 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
simon 0:350011bf8be7 1189 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
simon 0:350011bf8be7 1190 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
simon 0:350011bf8be7 1191 vb = snmp_varbind_alloc(&oid, type, sizeof(u32_t));
simon 0:350011bf8be7 1192 if (vb != NULL)
simon 0:350011bf8be7 1193 {
simon 0:350011bf8be7 1194 u32_t *vptr = (u32_t*)vb->value;
simon 0:350011bf8be7 1195
simon 0:350011bf8be7 1196 derr = snmp_asn1_dec_u32t(p, ofs + 1 + len_octets, len, vptr);
simon 0:350011bf8be7 1197 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1198 }
simon 0:350011bf8be7 1199 else
simon 0:350011bf8be7 1200 {
simon 0:350011bf8be7 1201 derr = ERR_ARG;
simon 0:350011bf8be7 1202 }
simon 0:350011bf8be7 1203 break;
simon 0:350011bf8be7 1204 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
simon 0:350011bf8be7 1205 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
simon 0:350011bf8be7 1206 LWIP_ASSERT("invalid length", len <= 0xff);
simon 0:350011bf8be7 1207 vb = snmp_varbind_alloc(&oid, type, (u8_t)len);
simon 0:350011bf8be7 1208 if (vb != NULL)
simon 0:350011bf8be7 1209 {
simon 0:350011bf8be7 1210 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, vb->value_len, (u8_t*)vb->value);
simon 0:350011bf8be7 1211 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1212 }
simon 0:350011bf8be7 1213 else
simon 0:350011bf8be7 1214 {
simon 0:350011bf8be7 1215 derr = ERR_ARG;
simon 0:350011bf8be7 1216 }
simon 0:350011bf8be7 1217 break;
simon 0:350011bf8be7 1218 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
simon 0:350011bf8be7 1219 vb = snmp_varbind_alloc(&oid, type, 0);
simon 0:350011bf8be7 1220 if (vb != NULL)
simon 0:350011bf8be7 1221 {
simon 0:350011bf8be7 1222 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1223 derr = ERR_OK;
simon 0:350011bf8be7 1224 }
simon 0:350011bf8be7 1225 else
simon 0:350011bf8be7 1226 {
simon 0:350011bf8be7 1227 derr = ERR_ARG;
simon 0:350011bf8be7 1228 }
simon 0:350011bf8be7 1229 break;
simon 0:350011bf8be7 1230 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
simon 0:350011bf8be7 1231 derr = snmp_asn1_dec_oid(p, ofs + 1 + len_octets, len, &oid_value);
simon 0:350011bf8be7 1232 if (derr == ERR_OK)
simon 0:350011bf8be7 1233 {
simon 0:350011bf8be7 1234 vb = snmp_varbind_alloc(&oid, type, oid_value.len * sizeof(s32_t));
simon 0:350011bf8be7 1235 if (vb != NULL)
simon 0:350011bf8be7 1236 {
simon 0:350011bf8be7 1237 u8_t i = oid_value.len;
simon 0:350011bf8be7 1238 s32_t *vptr = (s32_t*)vb->value;
simon 0:350011bf8be7 1239
simon 0:350011bf8be7 1240 while(i > 0)
simon 0:350011bf8be7 1241 {
simon 0:350011bf8be7 1242 i--;
simon 0:350011bf8be7 1243 vptr[i] = oid_value.id[i];
simon 0:350011bf8be7 1244 }
simon 0:350011bf8be7 1245 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1246 derr = ERR_OK;
simon 0:350011bf8be7 1247 }
simon 0:350011bf8be7 1248 else
simon 0:350011bf8be7 1249 {
simon 0:350011bf8be7 1250 derr = ERR_ARG;
simon 0:350011bf8be7 1251 }
simon 0:350011bf8be7 1252 }
simon 0:350011bf8be7 1253 break;
simon 0:350011bf8be7 1254 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
simon 0:350011bf8be7 1255 if (len == 4)
simon 0:350011bf8be7 1256 {
simon 0:350011bf8be7 1257 /* must be exactly 4 octets! */
simon 0:350011bf8be7 1258 vb = snmp_varbind_alloc(&oid, type, 4);
simon 0:350011bf8be7 1259 if (vb != NULL)
simon 0:350011bf8be7 1260 {
simon 0:350011bf8be7 1261 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, vb->value_len, (u8_t*)vb->value);
simon 0:350011bf8be7 1262 snmp_varbind_tail_add(&m_stat->invb, vb);
simon 0:350011bf8be7 1263 }
simon 0:350011bf8be7 1264 else
simon 0:350011bf8be7 1265 {
simon 0:350011bf8be7 1266 derr = ERR_ARG;
simon 0:350011bf8be7 1267 }
simon 0:350011bf8be7 1268 }
simon 0:350011bf8be7 1269 else
simon 0:350011bf8be7 1270 {
simon 0:350011bf8be7 1271 derr = ERR_ARG;
simon 0:350011bf8be7 1272 }
simon 0:350011bf8be7 1273 break;
simon 0:350011bf8be7 1274 default:
simon 0:350011bf8be7 1275 derr = ERR_ARG;
simon 0:350011bf8be7 1276 break;
simon 0:350011bf8be7 1277 }
simon 0:350011bf8be7 1278 if (derr != ERR_OK)
simon 0:350011bf8be7 1279 {
simon 0:350011bf8be7 1280 snmp_inc_snmpinasnparseerrs();
simon 0:350011bf8be7 1281 /* free varbinds (if available) */
simon 0:350011bf8be7 1282 snmp_varbind_list_free(&m_stat->invb);
simon 0:350011bf8be7 1283 return ERR_ARG;
simon 0:350011bf8be7 1284 }
simon 0:350011bf8be7 1285 ofs += (1 + len_octets + len);
simon 0:350011bf8be7 1286 vb_len -= (1 + len_octets + len);
simon 0:350011bf8be7 1287 }
simon 0:350011bf8be7 1288
simon 0:350011bf8be7 1289 if (m_stat->rt == SNMP_ASN1_PDU_SET_REQ)
simon 0:350011bf8be7 1290 {
simon 0:350011bf8be7 1291 snmp_add_snmpintotalsetvars(m_stat->invb.count);
simon 0:350011bf8be7 1292 }
simon 0:350011bf8be7 1293 else
simon 0:350011bf8be7 1294 {
simon 0:350011bf8be7 1295 snmp_add_snmpintotalreqvars(m_stat->invb.count);
simon 0:350011bf8be7 1296 }
simon 0:350011bf8be7 1297
simon 0:350011bf8be7 1298 *ofs_ret = ofs;
simon 0:350011bf8be7 1299 return ERR_OK;
simon 0:350011bf8be7 1300 }
simon 0:350011bf8be7 1301
simon 0:350011bf8be7 1302 struct snmp_varbind*
simon 0:350011bf8be7 1303 snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len)
simon 0:350011bf8be7 1304 {
simon 0:350011bf8be7 1305 struct snmp_varbind *vb;
simon 0:350011bf8be7 1306
simon 0:350011bf8be7 1307 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
simon 0:350011bf8be7 1308 LWIP_ASSERT("vb != NULL",vb != NULL);
simon 0:350011bf8be7 1309 if (vb != NULL)
simon 0:350011bf8be7 1310 {
simon 0:350011bf8be7 1311 u8_t i;
simon 0:350011bf8be7 1312
simon 0:350011bf8be7 1313 vb->next = NULL;
simon 0:350011bf8be7 1314 vb->prev = NULL;
simon 0:350011bf8be7 1315 i = oid->len;
simon 0:350011bf8be7 1316 vb->ident_len = i;
simon 0:350011bf8be7 1317 if (i > 0)
simon 0:350011bf8be7 1318 {
simon 0:350011bf8be7 1319 LWIP_ASSERT("SNMP_MAX_TREE_DEPTH is configured too low", i <= SNMP_MAX_TREE_DEPTH);
simon 0:350011bf8be7 1320 /* allocate array of s32_t for our object identifier */
simon 0:350011bf8be7 1321 vb->ident = (s32_t*)memp_malloc(MEMP_SNMP_VALUE);
simon 0:350011bf8be7 1322 LWIP_ASSERT("vb->ident != NULL",vb->ident != NULL);
simon 0:350011bf8be7 1323 if (vb->ident == NULL)
simon 0:350011bf8be7 1324 {
simon 0:350011bf8be7 1325 memp_free(MEMP_SNMP_VARBIND, vb);
simon 0:350011bf8be7 1326 return NULL;
simon 0:350011bf8be7 1327 }
simon 0:350011bf8be7 1328 while(i > 0)
simon 0:350011bf8be7 1329 {
simon 0:350011bf8be7 1330 i--;
simon 0:350011bf8be7 1331 vb->ident[i] = oid->id[i];
simon 0:350011bf8be7 1332 }
simon 0:350011bf8be7 1333 }
simon 0:350011bf8be7 1334 else
simon 0:350011bf8be7 1335 {
simon 0:350011bf8be7 1336 /* i == 0, pass zero length object identifier */
simon 0:350011bf8be7 1337 vb->ident = NULL;
simon 0:350011bf8be7 1338 }
simon 0:350011bf8be7 1339 vb->value_type = type;
simon 0:350011bf8be7 1340 vb->value_len = len;
simon 0:350011bf8be7 1341 if (len > 0)
simon 0:350011bf8be7 1342 {
simon 0:350011bf8be7 1343 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low", vb->value_len <= SNMP_MAX_VALUE_SIZE);
simon 0:350011bf8be7 1344 /* allocate raw bytes for our object value */
simon 0:350011bf8be7 1345 vb->value = memp_malloc(MEMP_SNMP_VALUE);
simon 0:350011bf8be7 1346 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
simon 0:350011bf8be7 1347 if (vb->value == NULL)
simon 0:350011bf8be7 1348 {
simon 0:350011bf8be7 1349 if (vb->ident != NULL)
simon 0:350011bf8be7 1350 {
simon 0:350011bf8be7 1351 memp_free(MEMP_SNMP_VALUE, vb->ident);
simon 0:350011bf8be7 1352 }
simon 0:350011bf8be7 1353 memp_free(MEMP_SNMP_VARBIND, vb);
simon 0:350011bf8be7 1354 return NULL;
simon 0:350011bf8be7 1355 }
simon 0:350011bf8be7 1356 }
simon 0:350011bf8be7 1357 else
simon 0:350011bf8be7 1358 {
simon 0:350011bf8be7 1359 /* ASN1_NUL type, or zero length ASN1_OC_STR */
simon 0:350011bf8be7 1360 vb->value = NULL;
simon 0:350011bf8be7 1361 }
simon 0:350011bf8be7 1362 }
simon 0:350011bf8be7 1363 return vb;
simon 0:350011bf8be7 1364 }
simon 0:350011bf8be7 1365
simon 0:350011bf8be7 1366 void
simon 0:350011bf8be7 1367 snmp_varbind_free(struct snmp_varbind *vb)
simon 0:350011bf8be7 1368 {
simon 0:350011bf8be7 1369 if (vb->value != NULL )
simon 0:350011bf8be7 1370 {
simon 0:350011bf8be7 1371 memp_free(MEMP_SNMP_VALUE, vb->value);
simon 0:350011bf8be7 1372 }
simon 0:350011bf8be7 1373 if (vb->ident != NULL )
simon 0:350011bf8be7 1374 {
simon 0:350011bf8be7 1375 memp_free(MEMP_SNMP_VALUE, vb->ident);
simon 0:350011bf8be7 1376 }
simon 0:350011bf8be7 1377 memp_free(MEMP_SNMP_VARBIND, vb);
simon 0:350011bf8be7 1378 }
simon 0:350011bf8be7 1379
simon 0:350011bf8be7 1380 void
simon 0:350011bf8be7 1381 snmp_varbind_list_free(struct snmp_varbind_root *root)
simon 0:350011bf8be7 1382 {
simon 0:350011bf8be7 1383 struct snmp_varbind *vb, *prev;
simon 0:350011bf8be7 1384
simon 0:350011bf8be7 1385 vb = root->tail;
simon 0:350011bf8be7 1386 while ( vb != NULL )
simon 0:350011bf8be7 1387 {
simon 0:350011bf8be7 1388 prev = vb->prev;
simon 0:350011bf8be7 1389 snmp_varbind_free(vb);
simon 0:350011bf8be7 1390 vb = prev;
simon 0:350011bf8be7 1391 }
simon 0:350011bf8be7 1392 root->count = 0;
simon 0:350011bf8be7 1393 root->head = NULL;
simon 0:350011bf8be7 1394 root->tail = NULL;
simon 0:350011bf8be7 1395 }
simon 0:350011bf8be7 1396
simon 0:350011bf8be7 1397 void
simon 0:350011bf8be7 1398 snmp_varbind_tail_add(struct snmp_varbind_root *root, struct snmp_varbind *vb)
simon 0:350011bf8be7 1399 {
simon 0:350011bf8be7 1400 if (root->count == 0)
simon 0:350011bf8be7 1401 {
simon 0:350011bf8be7 1402 /* add first varbind to list */
simon 0:350011bf8be7 1403 root->head = vb;
simon 0:350011bf8be7 1404 root->tail = vb;
simon 0:350011bf8be7 1405 }
simon 0:350011bf8be7 1406 else
simon 0:350011bf8be7 1407 {
simon 0:350011bf8be7 1408 /* add nth varbind to list tail */
simon 0:350011bf8be7 1409 root->tail->next = vb;
simon 0:350011bf8be7 1410 vb->prev = root->tail;
simon 0:350011bf8be7 1411 root->tail = vb;
simon 0:350011bf8be7 1412 }
simon 0:350011bf8be7 1413 root->count += 1;
simon 0:350011bf8be7 1414 }
simon 0:350011bf8be7 1415
simon 0:350011bf8be7 1416 struct snmp_varbind*
simon 0:350011bf8be7 1417 snmp_varbind_tail_remove(struct snmp_varbind_root *root)
simon 0:350011bf8be7 1418 {
simon 0:350011bf8be7 1419 struct snmp_varbind* vb;
simon 0:350011bf8be7 1420
simon 0:350011bf8be7 1421 if (root->count > 0)
simon 0:350011bf8be7 1422 {
simon 0:350011bf8be7 1423 /* remove tail varbind */
simon 0:350011bf8be7 1424 vb = root->tail;
simon 0:350011bf8be7 1425 root->tail = vb->prev;
simon 0:350011bf8be7 1426 vb->prev->next = NULL;
simon 0:350011bf8be7 1427 root->count -= 1;
simon 0:350011bf8be7 1428 }
simon 0:350011bf8be7 1429 else
simon 0:350011bf8be7 1430 {
simon 0:350011bf8be7 1431 /* nothing to remove */
simon 0:350011bf8be7 1432 vb = NULL;
simon 0:350011bf8be7 1433 }
simon 0:350011bf8be7 1434 return vb;
simon 0:350011bf8be7 1435 }
simon 0:350011bf8be7 1436
simon 0:350011bf8be7 1437 #endif /* LWIP_SNMP */