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 output message processing (RFC1157).
simon 0:350011bf8be7 4 *
simon 0:350011bf8be7 5 * Output responses and traps are build in two passes:
simon 0:350011bf8be7 6 *
simon 0:350011bf8be7 7 * Pass 0: iterate over the output message backwards to determine encoding lengths
simon 0:350011bf8be7 8 * Pass 1: the actual forward encoding of internal form into ASN1
simon 0:350011bf8be7 9 *
simon 0:350011bf8be7 10 * The single-pass encoding method described by Comer & Stevens
simon 0:350011bf8be7 11 * requires extra buffer space and copying for reversal of the packet.
simon 0:350011bf8be7 12 * The buffer requirement can be prohibitively large for big payloads
simon 0:350011bf8be7 13 * (>= 484) therefore we use the two encoding passes.
simon 0:350011bf8be7 14 */
simon 0:350011bf8be7 15
simon 0:350011bf8be7 16 /*
simon 0:350011bf8be7 17 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
simon 0:350011bf8be7 18 * All rights reserved.
simon 0:350011bf8be7 19 *
simon 0:350011bf8be7 20 * Redistribution and use in source and binary forms, with or without modification,
simon 0:350011bf8be7 21 * are permitted provided that the following conditions are met:
simon 0:350011bf8be7 22 *
simon 0:350011bf8be7 23 * 1. Redistributions of source code must retain the above copyright notice,
simon 0:350011bf8be7 24 * this list of conditions and the following disclaimer.
simon 0:350011bf8be7 25 * 2. Redistributions in binary form must reproduce the above copyright notice,
simon 0:350011bf8be7 26 * this list of conditions and the following disclaimer in the documentation
simon 0:350011bf8be7 27 * and/or other materials provided with the distribution.
simon 0:350011bf8be7 28 * 3. The name of the author may not be used to endorse or promote products
simon 0:350011bf8be7 29 * derived from this software without specific prior written permission.
simon 0:350011bf8be7 30 *
simon 0:350011bf8be7 31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
simon 0:350011bf8be7 32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
simon 0:350011bf8be7 33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
simon 0:350011bf8be7 34 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
simon 0:350011bf8be7 35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
simon 0:350011bf8be7 36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
simon 0:350011bf8be7 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
simon 0:350011bf8be7 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
simon 0:350011bf8be7 39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
simon 0:350011bf8be7 40 * OF SUCH DAMAGE.
simon 0:350011bf8be7 41 *
simon 0:350011bf8be7 42 * Author: Christiaan Simons <christiaan.simons@axon.tv>
simon 0:350011bf8be7 43 */
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 #include "lwip/opt.h"
simon 0:350011bf8be7 46
simon 0:350011bf8be7 47 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 48
simon 0:350011bf8be7 49 #include "lwip/udp.h"
simon 0:350011bf8be7 50 #include "lwip/netif.h"
simon 0:350011bf8be7 51 #include "lwip/snmp.h"
simon 0:350011bf8be7 52 #include "lwip/snmp_asn1.h"
simon 0:350011bf8be7 53 #include "lwip/snmp_msg.h"
simon 0:350011bf8be7 54
simon 0:350011bf8be7 55 struct snmp_trap_dst
simon 0:350011bf8be7 56 {
simon 0:350011bf8be7 57 /* destination IP address in network order */
simon 0:350011bf8be7 58 ip_addr_t dip;
simon 0:350011bf8be7 59 /* set to 0 when disabled, >0 when enabled */
simon 0:350011bf8be7 60 u8_t enable;
simon 0:350011bf8be7 61 };
simon 0:350011bf8be7 62 struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS];
simon 0:350011bf8be7 63
simon 0:350011bf8be7 64 /** TRAP message structure */
simon 0:350011bf8be7 65 struct snmp_msg_trap trap_msg;
simon 0:350011bf8be7 66
simon 0:350011bf8be7 67 static u16_t snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len);
simon 0:350011bf8be7 68 static u16_t snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len);
simon 0:350011bf8be7 69 static u16_t snmp_varbind_list_sum(struct snmp_varbind_root *root);
simon 0:350011bf8be7 70
simon 0:350011bf8be7 71 static u16_t snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p);
simon 0:350011bf8be7 72 static u16_t snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p);
simon 0:350011bf8be7 73 static u16_t snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs);
simon 0:350011bf8be7 74
simon 0:350011bf8be7 75 /**
simon 0:350011bf8be7 76 * Sets enable switch for this trap destination.
simon 0:350011bf8be7 77 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
simon 0:350011bf8be7 78 * @param enable switch if 0 destination is disabled >0 enabled.
simon 0:350011bf8be7 79 */
simon 0:350011bf8be7 80 void
simon 0:350011bf8be7 81 snmp_trap_dst_enable(u8_t dst_idx, u8_t enable)
simon 0:350011bf8be7 82 {
simon 0:350011bf8be7 83 if (dst_idx < SNMP_TRAP_DESTINATIONS)
simon 0:350011bf8be7 84 {
simon 0:350011bf8be7 85 trap_dst[dst_idx].enable = enable;
simon 0:350011bf8be7 86 }
simon 0:350011bf8be7 87 }
simon 0:350011bf8be7 88
simon 0:350011bf8be7 89 /**
simon 0:350011bf8be7 90 * Sets IPv4 address for this trap destination.
simon 0:350011bf8be7 91 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
simon 0:350011bf8be7 92 * @param dst IPv4 address in host order.
simon 0:350011bf8be7 93 */
simon 0:350011bf8be7 94 void
simon 0:350011bf8be7 95 snmp_trap_dst_ip_set(u8_t dst_idx, ip_addr_t *dst)
simon 0:350011bf8be7 96 {
simon 0:350011bf8be7 97 if (dst_idx < SNMP_TRAP_DESTINATIONS)
simon 0:350011bf8be7 98 {
simon 0:350011bf8be7 99 ip_addr_set(&trap_dst[dst_idx].dip, dst);
simon 0:350011bf8be7 100 }
simon 0:350011bf8be7 101 }
simon 0:350011bf8be7 102
simon 0:350011bf8be7 103 /**
simon 0:350011bf8be7 104 * Sends a 'getresponse' message to the request originator.
simon 0:350011bf8be7 105 *
simon 0:350011bf8be7 106 * @param m_stat points to the current message request state source
simon 0:350011bf8be7 107 * @return ERR_OK when success, ERR_MEM if we're out of memory
simon 0:350011bf8be7 108 *
simon 0:350011bf8be7 109 * @note the caller is responsible for filling in outvb in the m_stat
simon 0:350011bf8be7 110 * and provide error-status and index (except for tooBig errors) ...
simon 0:350011bf8be7 111 */
simon 0:350011bf8be7 112 err_t
simon 0:350011bf8be7 113 snmp_send_response(struct snmp_msg_pstat *m_stat)
simon 0:350011bf8be7 114 {
simon 0:350011bf8be7 115 struct snmp_varbind_root emptyvb = {NULL, NULL, 0, 0, 0};
simon 0:350011bf8be7 116 struct pbuf *p;
simon 0:350011bf8be7 117 u16_t tot_len;
simon 0:350011bf8be7 118 err_t err;
simon 0:350011bf8be7 119
simon 0:350011bf8be7 120 /* pass 0, calculate length fields */
simon 0:350011bf8be7 121 tot_len = snmp_varbind_list_sum(&m_stat->outvb);
simon 0:350011bf8be7 122 tot_len = snmp_resp_header_sum(m_stat, tot_len);
simon 0:350011bf8be7 123
simon 0:350011bf8be7 124 /* try allocating pbuf(s) for complete response */
simon 0:350011bf8be7 125 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL);
simon 0:350011bf8be7 126 if (p == NULL)
simon 0:350011bf8be7 127 {
simon 0:350011bf8be7 128 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() tooBig\n"));
simon 0:350011bf8be7 129
simon 0:350011bf8be7 130 /* can't construct reply, return error-status tooBig */
simon 0:350011bf8be7 131 m_stat->error_status = SNMP_ES_TOOBIG;
simon 0:350011bf8be7 132 m_stat->error_index = 0;
simon 0:350011bf8be7 133 /* pass 0, recalculate lengths, for empty varbind-list */
simon 0:350011bf8be7 134 tot_len = snmp_varbind_list_sum(&emptyvb);
simon 0:350011bf8be7 135 tot_len = snmp_resp_header_sum(m_stat, tot_len);
simon 0:350011bf8be7 136 /* retry allocation once for header and empty varbind-list */
simon 0:350011bf8be7 137 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL);
simon 0:350011bf8be7 138 }
simon 0:350011bf8be7 139 if (p != NULL)
simon 0:350011bf8be7 140 {
simon 0:350011bf8be7 141 /* first pbuf alloc try or retry alloc success */
simon 0:350011bf8be7 142 u16_t ofs;
simon 0:350011bf8be7 143
simon 0:350011bf8be7 144 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() p != NULL\n"));
simon 0:350011bf8be7 145
simon 0:350011bf8be7 146 /* pass 1, size error, encode packet ino the pbuf(s) */
simon 0:350011bf8be7 147 ofs = snmp_resp_header_enc(m_stat, p);
simon 0:350011bf8be7 148 if (m_stat->error_status == SNMP_ES_TOOBIG)
simon 0:350011bf8be7 149 {
simon 0:350011bf8be7 150 snmp_varbind_list_enc(&emptyvb, p, ofs);
simon 0:350011bf8be7 151 }
simon 0:350011bf8be7 152 else
simon 0:350011bf8be7 153 {
simon 0:350011bf8be7 154 snmp_varbind_list_enc(&m_stat->outvb, p, ofs);
simon 0:350011bf8be7 155 }
simon 0:350011bf8be7 156
simon 0:350011bf8be7 157 switch (m_stat->error_status)
simon 0:350011bf8be7 158 {
simon 0:350011bf8be7 159 case SNMP_ES_TOOBIG:
simon 0:350011bf8be7 160 snmp_inc_snmpouttoobigs();
simon 0:350011bf8be7 161 break;
simon 0:350011bf8be7 162 case SNMP_ES_NOSUCHNAME:
simon 0:350011bf8be7 163 snmp_inc_snmpoutnosuchnames();
simon 0:350011bf8be7 164 break;
simon 0:350011bf8be7 165 case SNMP_ES_BADVALUE:
simon 0:350011bf8be7 166 snmp_inc_snmpoutbadvalues();
simon 0:350011bf8be7 167 break;
simon 0:350011bf8be7 168 case SNMP_ES_GENERROR:
simon 0:350011bf8be7 169 snmp_inc_snmpoutgenerrs();
simon 0:350011bf8be7 170 break;
simon 0:350011bf8be7 171 }
simon 0:350011bf8be7 172 snmp_inc_snmpoutgetresponses();
simon 0:350011bf8be7 173 snmp_inc_snmpoutpkts();
simon 0:350011bf8be7 174
simon 0:350011bf8be7 175 /** @todo do we need separate rx and tx pcbs for threaded case? */
simon 0:350011bf8be7 176 /** connect to the originating source */
simon 0:350011bf8be7 177 udp_connect(m_stat->pcb, &m_stat->sip, m_stat->sp);
simon 0:350011bf8be7 178 err = udp_send(m_stat->pcb, p);
simon 0:350011bf8be7 179 if (err == ERR_MEM)
simon 0:350011bf8be7 180 {
simon 0:350011bf8be7 181 /** @todo release some memory, retry and return tooBig? tooMuchHassle? */
simon 0:350011bf8be7 182 err = ERR_MEM;
simon 0:350011bf8be7 183 }
simon 0:350011bf8be7 184 else
simon 0:350011bf8be7 185 {
simon 0:350011bf8be7 186 err = ERR_OK;
simon 0:350011bf8be7 187 }
simon 0:350011bf8be7 188 /** disassociate remote address and port with this pcb */
simon 0:350011bf8be7 189 udp_disconnect(m_stat->pcb);
simon 0:350011bf8be7 190
simon 0:350011bf8be7 191 pbuf_free(p);
simon 0:350011bf8be7 192 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() done\n"));
simon 0:350011bf8be7 193 return err;
simon 0:350011bf8be7 194 }
simon 0:350011bf8be7 195 else
simon 0:350011bf8be7 196 {
simon 0:350011bf8be7 197 /* first pbuf alloc try or retry alloc failed
simon 0:350011bf8be7 198 very low on memory, couldn't return tooBig */
simon 0:350011bf8be7 199 return ERR_MEM;
simon 0:350011bf8be7 200 }
simon 0:350011bf8be7 201 }
simon 0:350011bf8be7 202
simon 0:350011bf8be7 203
simon 0:350011bf8be7 204 /**
simon 0:350011bf8be7 205 * Sends an generic or enterprise specific trap message.
simon 0:350011bf8be7 206 *
simon 0:350011bf8be7 207 * @param generic_trap is the trap code
simon 0:350011bf8be7 208 * @param eoid points to enterprise object identifier
simon 0:350011bf8be7 209 * @param specific_trap used for enterprise traps when generic_trap == 6
simon 0:350011bf8be7 210 * @return ERR_OK when success, ERR_MEM if we're out of memory
simon 0:350011bf8be7 211 *
simon 0:350011bf8be7 212 * @note the caller is responsible for filling in outvb in the trap_msg
simon 0:350011bf8be7 213 * @note the use of the enterpise identifier field
simon 0:350011bf8be7 214 * is per RFC1215.
simon 0:350011bf8be7 215 * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
simon 0:350011bf8be7 216 * and .iso.org.dod.internet.private.enterprises.yourenterprise
simon 0:350011bf8be7 217 * (sysObjectID) for specific traps.
simon 0:350011bf8be7 218 */
simon 0:350011bf8be7 219 err_t
simon 0:350011bf8be7 220 snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap)
simon 0:350011bf8be7 221 {
simon 0:350011bf8be7 222 struct snmp_trap_dst *td;
simon 0:350011bf8be7 223 struct netif *dst_if;
simon 0:350011bf8be7 224 ip_addr_t dst_ip;
simon 0:350011bf8be7 225 struct pbuf *p;
simon 0:350011bf8be7 226 u16_t i,tot_len;
simon 0:350011bf8be7 227
simon 0:350011bf8be7 228 for (i=0, td = &trap_dst[0]; i<SNMP_TRAP_DESTINATIONS; i++, td++)
simon 0:350011bf8be7 229 {
simon 0:350011bf8be7 230 if ((td->enable != 0) && !ip_addr_isany(&td->dip))
simon 0:350011bf8be7 231 {
simon 0:350011bf8be7 232 /* network order trap destination */
simon 0:350011bf8be7 233 ip_addr_copy(trap_msg.dip, td->dip);
simon 0:350011bf8be7 234 /* lookup current source address for this dst */
simon 0:350011bf8be7 235 dst_if = ip_route(&td->dip);
simon 0:350011bf8be7 236 ip_addr_copy(dst_ip, dst_if->ip_addr);
simon 0:350011bf8be7 237 /* @todo: what about IPv6? */
simon 0:350011bf8be7 238 trap_msg.sip_raw[0] = ip4_addr1(&dst_ip);
simon 0:350011bf8be7 239 trap_msg.sip_raw[1] = ip4_addr2(&dst_ip);
simon 0:350011bf8be7 240 trap_msg.sip_raw[2] = ip4_addr3(&dst_ip);
simon 0:350011bf8be7 241 trap_msg.sip_raw[3] = ip4_addr4(&dst_ip);
simon 0:350011bf8be7 242 trap_msg.gen_trap = generic_trap;
simon 0:350011bf8be7 243 trap_msg.spc_trap = specific_trap;
simon 0:350011bf8be7 244 if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC)
simon 0:350011bf8be7 245 {
simon 0:350011bf8be7 246 /* enterprise-Specific trap */
simon 0:350011bf8be7 247 trap_msg.enterprise = eoid;
simon 0:350011bf8be7 248 }
simon 0:350011bf8be7 249 else
simon 0:350011bf8be7 250 {
simon 0:350011bf8be7 251 /* generic (MIB-II) trap */
simon 0:350011bf8be7 252 snmp_get_snmpgrpid_ptr(&trap_msg.enterprise);
simon 0:350011bf8be7 253 }
simon 0:350011bf8be7 254 snmp_get_sysuptime(&trap_msg.ts);
simon 0:350011bf8be7 255
simon 0:350011bf8be7 256 /* pass 0, calculate length fields */
simon 0:350011bf8be7 257 tot_len = snmp_varbind_list_sum(&trap_msg.outvb);
simon 0:350011bf8be7 258 tot_len = snmp_trap_header_sum(&trap_msg, tot_len);
simon 0:350011bf8be7 259
simon 0:350011bf8be7 260 /* allocate pbuf(s) */
simon 0:350011bf8be7 261 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL);
simon 0:350011bf8be7 262 if (p != NULL)
simon 0:350011bf8be7 263 {
simon 0:350011bf8be7 264 u16_t ofs;
simon 0:350011bf8be7 265
simon 0:350011bf8be7 266 /* pass 1, encode packet ino the pbuf(s) */
simon 0:350011bf8be7 267 ofs = snmp_trap_header_enc(&trap_msg, p);
simon 0:350011bf8be7 268 snmp_varbind_list_enc(&trap_msg.outvb, p, ofs);
simon 0:350011bf8be7 269
simon 0:350011bf8be7 270 snmp_inc_snmpouttraps();
simon 0:350011bf8be7 271 snmp_inc_snmpoutpkts();
simon 0:350011bf8be7 272
simon 0:350011bf8be7 273 /** send to the TRAP destination */
simon 0:350011bf8be7 274 udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT);
simon 0:350011bf8be7 275
simon 0:350011bf8be7 276 pbuf_free(p);
simon 0:350011bf8be7 277 }
simon 0:350011bf8be7 278 else
simon 0:350011bf8be7 279 {
simon 0:350011bf8be7 280 return ERR_MEM;
simon 0:350011bf8be7 281 }
simon 0:350011bf8be7 282 }
simon 0:350011bf8be7 283 }
simon 0:350011bf8be7 284 return ERR_OK;
simon 0:350011bf8be7 285 }
simon 0:350011bf8be7 286
simon 0:350011bf8be7 287 void
simon 0:350011bf8be7 288 snmp_coldstart_trap(void)
simon 0:350011bf8be7 289 {
simon 0:350011bf8be7 290 trap_msg.outvb.head = NULL;
simon 0:350011bf8be7 291 trap_msg.outvb.tail = NULL;
simon 0:350011bf8be7 292 trap_msg.outvb.count = 0;
simon 0:350011bf8be7 293 snmp_send_trap(SNMP_GENTRAP_COLDSTART, NULL, 0);
simon 0:350011bf8be7 294 }
simon 0:350011bf8be7 295
simon 0:350011bf8be7 296 void
simon 0:350011bf8be7 297 snmp_authfail_trap(void)
simon 0:350011bf8be7 298 {
simon 0:350011bf8be7 299 u8_t enable;
simon 0:350011bf8be7 300 snmp_get_snmpenableauthentraps(&enable);
simon 0:350011bf8be7 301 if (enable == 1)
simon 0:350011bf8be7 302 {
simon 0:350011bf8be7 303 trap_msg.outvb.head = NULL;
simon 0:350011bf8be7 304 trap_msg.outvb.tail = NULL;
simon 0:350011bf8be7 305 trap_msg.outvb.count = 0;
simon 0:350011bf8be7 306 snmp_send_trap(SNMP_GENTRAP_AUTHFAIL, NULL, 0);
simon 0:350011bf8be7 307 }
simon 0:350011bf8be7 308 }
simon 0:350011bf8be7 309
simon 0:350011bf8be7 310 /**
simon 0:350011bf8be7 311 * Sums response header field lengths from tail to head and
simon 0:350011bf8be7 312 * returns resp_header_lengths for second encoding pass.
simon 0:350011bf8be7 313 *
simon 0:350011bf8be7 314 * @param vb_len varbind-list length
simon 0:350011bf8be7 315 * @param rhl points to returned header lengths
simon 0:350011bf8be7 316 * @return the required lenght for encoding the response header
simon 0:350011bf8be7 317 */
simon 0:350011bf8be7 318 static u16_t
simon 0:350011bf8be7 319 snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
simon 0:350011bf8be7 320 {
simon 0:350011bf8be7 321 u16_t tot_len;
simon 0:350011bf8be7 322 struct snmp_resp_header_lengths *rhl;
simon 0:350011bf8be7 323
simon 0:350011bf8be7 324 rhl = &m_stat->rhl;
simon 0:350011bf8be7 325 tot_len = vb_len;
simon 0:350011bf8be7 326 snmp_asn1_enc_s32t_cnt(m_stat->error_index, &rhl->erridxlen);
simon 0:350011bf8be7 327 snmp_asn1_enc_length_cnt(rhl->erridxlen, &rhl->erridxlenlen);
simon 0:350011bf8be7 328 tot_len += 1 + rhl->erridxlenlen + rhl->erridxlen;
simon 0:350011bf8be7 329
simon 0:350011bf8be7 330 snmp_asn1_enc_s32t_cnt(m_stat->error_status, &rhl->errstatlen);
simon 0:350011bf8be7 331 snmp_asn1_enc_length_cnt(rhl->errstatlen, &rhl->errstatlenlen);
simon 0:350011bf8be7 332 tot_len += 1 + rhl->errstatlenlen + rhl->errstatlen;
simon 0:350011bf8be7 333
simon 0:350011bf8be7 334 snmp_asn1_enc_s32t_cnt(m_stat->rid, &rhl->ridlen);
simon 0:350011bf8be7 335 snmp_asn1_enc_length_cnt(rhl->ridlen, &rhl->ridlenlen);
simon 0:350011bf8be7 336 tot_len += 1 + rhl->ridlenlen + rhl->ridlen;
simon 0:350011bf8be7 337
simon 0:350011bf8be7 338 rhl->pdulen = tot_len;
simon 0:350011bf8be7 339 snmp_asn1_enc_length_cnt(rhl->pdulen, &rhl->pdulenlen);
simon 0:350011bf8be7 340 tot_len += 1 + rhl->pdulenlen;
simon 0:350011bf8be7 341
simon 0:350011bf8be7 342 rhl->comlen = m_stat->com_strlen;
simon 0:350011bf8be7 343 snmp_asn1_enc_length_cnt(rhl->comlen, &rhl->comlenlen);
simon 0:350011bf8be7 344 tot_len += 1 + rhl->comlenlen + rhl->comlen;
simon 0:350011bf8be7 345
simon 0:350011bf8be7 346 snmp_asn1_enc_s32t_cnt(snmp_version, &rhl->verlen);
simon 0:350011bf8be7 347 snmp_asn1_enc_length_cnt(rhl->verlen, &rhl->verlenlen);
simon 0:350011bf8be7 348 tot_len += 1 + rhl->verlen + rhl->verlenlen;
simon 0:350011bf8be7 349
simon 0:350011bf8be7 350 rhl->seqlen = tot_len;
simon 0:350011bf8be7 351 snmp_asn1_enc_length_cnt(rhl->seqlen, &rhl->seqlenlen);
simon 0:350011bf8be7 352 tot_len += 1 + rhl->seqlenlen;
simon 0:350011bf8be7 353
simon 0:350011bf8be7 354 return tot_len;
simon 0:350011bf8be7 355 }
simon 0:350011bf8be7 356
simon 0:350011bf8be7 357 /**
simon 0:350011bf8be7 358 * Sums trap header field lengths from tail to head and
simon 0:350011bf8be7 359 * returns trap_header_lengths for second encoding pass.
simon 0:350011bf8be7 360 *
simon 0:350011bf8be7 361 * @param vb_len varbind-list length
simon 0:350011bf8be7 362 * @param thl points to returned header lengths
simon 0:350011bf8be7 363 * @return the required lenght for encoding the trap header
simon 0:350011bf8be7 364 */
simon 0:350011bf8be7 365 static u16_t
simon 0:350011bf8be7 366 snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len)
simon 0:350011bf8be7 367 {
simon 0:350011bf8be7 368 u16_t tot_len;
simon 0:350011bf8be7 369 struct snmp_trap_header_lengths *thl;
simon 0:350011bf8be7 370
simon 0:350011bf8be7 371 thl = &m_trap->thl;
simon 0:350011bf8be7 372 tot_len = vb_len;
simon 0:350011bf8be7 373
simon 0:350011bf8be7 374 snmp_asn1_enc_u32t_cnt(m_trap->ts, &thl->tslen);
simon 0:350011bf8be7 375 snmp_asn1_enc_length_cnt(thl->tslen, &thl->tslenlen);
simon 0:350011bf8be7 376 tot_len += 1 + thl->tslen + thl->tslenlen;
simon 0:350011bf8be7 377
simon 0:350011bf8be7 378 snmp_asn1_enc_s32t_cnt(m_trap->spc_trap, &thl->strplen);
simon 0:350011bf8be7 379 snmp_asn1_enc_length_cnt(thl->strplen, &thl->strplenlen);
simon 0:350011bf8be7 380 tot_len += 1 + thl->strplen + thl->strplenlen;
simon 0:350011bf8be7 381
simon 0:350011bf8be7 382 snmp_asn1_enc_s32t_cnt(m_trap->gen_trap, &thl->gtrplen);
simon 0:350011bf8be7 383 snmp_asn1_enc_length_cnt(thl->gtrplen, &thl->gtrplenlen);
simon 0:350011bf8be7 384 tot_len += 1 + thl->gtrplen + thl->gtrplenlen;
simon 0:350011bf8be7 385
simon 0:350011bf8be7 386 thl->aaddrlen = 4;
simon 0:350011bf8be7 387 snmp_asn1_enc_length_cnt(thl->aaddrlen, &thl->aaddrlenlen);
simon 0:350011bf8be7 388 tot_len += 1 + thl->aaddrlen + thl->aaddrlenlen;
simon 0:350011bf8be7 389
simon 0:350011bf8be7 390 snmp_asn1_enc_oid_cnt(m_trap->enterprise->len, &m_trap->enterprise->id[0], &thl->eidlen);
simon 0:350011bf8be7 391 snmp_asn1_enc_length_cnt(thl->eidlen, &thl->eidlenlen);
simon 0:350011bf8be7 392 tot_len += 1 + thl->eidlen + thl->eidlenlen;
simon 0:350011bf8be7 393
simon 0:350011bf8be7 394 thl->pdulen = tot_len;
simon 0:350011bf8be7 395 snmp_asn1_enc_length_cnt(thl->pdulen, &thl->pdulenlen);
simon 0:350011bf8be7 396 tot_len += 1 + thl->pdulenlen;
simon 0:350011bf8be7 397
simon 0:350011bf8be7 398 thl->comlen = sizeof(snmp_publiccommunity) - 1;
simon 0:350011bf8be7 399 snmp_asn1_enc_length_cnt(thl->comlen, &thl->comlenlen);
simon 0:350011bf8be7 400 tot_len += 1 + thl->comlenlen + thl->comlen;
simon 0:350011bf8be7 401
simon 0:350011bf8be7 402 snmp_asn1_enc_s32t_cnt(snmp_version, &thl->verlen);
simon 0:350011bf8be7 403 snmp_asn1_enc_length_cnt(thl->verlen, &thl->verlenlen);
simon 0:350011bf8be7 404 tot_len += 1 + thl->verlen + thl->verlenlen;
simon 0:350011bf8be7 405
simon 0:350011bf8be7 406 thl->seqlen = tot_len;
simon 0:350011bf8be7 407 snmp_asn1_enc_length_cnt(thl->seqlen, &thl->seqlenlen);
simon 0:350011bf8be7 408 tot_len += 1 + thl->seqlenlen;
simon 0:350011bf8be7 409
simon 0:350011bf8be7 410 return tot_len;
simon 0:350011bf8be7 411 }
simon 0:350011bf8be7 412
simon 0:350011bf8be7 413 /**
simon 0:350011bf8be7 414 * Sums varbind lengths from tail to head and
simon 0:350011bf8be7 415 * annotates lengths in varbind for second encoding pass.
simon 0:350011bf8be7 416 *
simon 0:350011bf8be7 417 * @param root points to the root of the variable binding list
simon 0:350011bf8be7 418 * @return the required lenght for encoding the variable bindings
simon 0:350011bf8be7 419 */
simon 0:350011bf8be7 420 static u16_t
simon 0:350011bf8be7 421 snmp_varbind_list_sum(struct snmp_varbind_root *root)
simon 0:350011bf8be7 422 {
simon 0:350011bf8be7 423 struct snmp_varbind *vb;
simon 0:350011bf8be7 424 u32_t *uint_ptr;
simon 0:350011bf8be7 425 s32_t *sint_ptr;
simon 0:350011bf8be7 426 u16_t tot_len;
simon 0:350011bf8be7 427
simon 0:350011bf8be7 428 tot_len = 0;
simon 0:350011bf8be7 429 vb = root->tail;
simon 0:350011bf8be7 430 while ( vb != NULL )
simon 0:350011bf8be7 431 {
simon 0:350011bf8be7 432 /* encoded value lenght depends on type */
simon 0:350011bf8be7 433 switch (vb->value_type)
simon 0:350011bf8be7 434 {
simon 0:350011bf8be7 435 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
simon 0:350011bf8be7 436 sint_ptr = (s32_t*)vb->value;
simon 0:350011bf8be7 437 snmp_asn1_enc_s32t_cnt(*sint_ptr, &vb->vlen);
simon 0:350011bf8be7 438 break;
simon 0:350011bf8be7 439 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
simon 0:350011bf8be7 440 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
simon 0:350011bf8be7 441 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
simon 0:350011bf8be7 442 uint_ptr = (u32_t*)vb->value;
simon 0:350011bf8be7 443 snmp_asn1_enc_u32t_cnt(*uint_ptr, &vb->vlen);
simon 0:350011bf8be7 444 break;
simon 0:350011bf8be7 445 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
simon 0:350011bf8be7 446 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
simon 0:350011bf8be7 447 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
simon 0:350011bf8be7 448 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
simon 0:350011bf8be7 449 vb->vlen = vb->value_len;
simon 0:350011bf8be7 450 break;
simon 0:350011bf8be7 451 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
simon 0:350011bf8be7 452 sint_ptr = (s32_t*)vb->value;
simon 0:350011bf8be7 453 snmp_asn1_enc_oid_cnt(vb->value_len / sizeof(s32_t), sint_ptr, &vb->vlen);
simon 0:350011bf8be7 454 break;
simon 0:350011bf8be7 455 default:
simon 0:350011bf8be7 456 /* unsupported type */
simon 0:350011bf8be7 457 vb->vlen = 0;
simon 0:350011bf8be7 458 break;
simon 0:350011bf8be7 459 };
simon 0:350011bf8be7 460 /* encoding length of value length field */
simon 0:350011bf8be7 461 snmp_asn1_enc_length_cnt(vb->vlen, &vb->vlenlen);
simon 0:350011bf8be7 462 snmp_asn1_enc_oid_cnt(vb->ident_len, vb->ident, &vb->olen);
simon 0:350011bf8be7 463 snmp_asn1_enc_length_cnt(vb->olen, &vb->olenlen);
simon 0:350011bf8be7 464
simon 0:350011bf8be7 465 vb->seqlen = 1 + vb->vlenlen + vb->vlen;
simon 0:350011bf8be7 466 vb->seqlen += 1 + vb->olenlen + vb->olen;
simon 0:350011bf8be7 467 snmp_asn1_enc_length_cnt(vb->seqlen, &vb->seqlenlen);
simon 0:350011bf8be7 468
simon 0:350011bf8be7 469 /* varbind seq */
simon 0:350011bf8be7 470 tot_len += 1 + vb->seqlenlen + vb->seqlen;
simon 0:350011bf8be7 471
simon 0:350011bf8be7 472 vb = vb->prev;
simon 0:350011bf8be7 473 }
simon 0:350011bf8be7 474
simon 0:350011bf8be7 475 /* varbind-list seq */
simon 0:350011bf8be7 476 root->seqlen = tot_len;
simon 0:350011bf8be7 477 snmp_asn1_enc_length_cnt(root->seqlen, &root->seqlenlen);
simon 0:350011bf8be7 478 tot_len += 1 + root->seqlenlen;
simon 0:350011bf8be7 479
simon 0:350011bf8be7 480 return tot_len;
simon 0:350011bf8be7 481 }
simon 0:350011bf8be7 482
simon 0:350011bf8be7 483 /**
simon 0:350011bf8be7 484 * Encodes response header from head to tail.
simon 0:350011bf8be7 485 */
simon 0:350011bf8be7 486 static u16_t
simon 0:350011bf8be7 487 snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p)
simon 0:350011bf8be7 488 {
simon 0:350011bf8be7 489 u16_t ofs;
simon 0:350011bf8be7 490
simon 0:350011bf8be7 491 ofs = 0;
simon 0:350011bf8be7 492 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
simon 0:350011bf8be7 493 ofs += 1;
simon 0:350011bf8be7 494 snmp_asn1_enc_length(p, ofs, m_stat->rhl.seqlen);
simon 0:350011bf8be7 495 ofs += m_stat->rhl.seqlenlen;
simon 0:350011bf8be7 496
simon 0:350011bf8be7 497 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 498 ofs += 1;
simon 0:350011bf8be7 499 snmp_asn1_enc_length(p, ofs, m_stat->rhl.verlen);
simon 0:350011bf8be7 500 ofs += m_stat->rhl.verlenlen;
simon 0:350011bf8be7 501 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.verlen, snmp_version);
simon 0:350011bf8be7 502 ofs += m_stat->rhl.verlen;
simon 0:350011bf8be7 503
simon 0:350011bf8be7 504 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
simon 0:350011bf8be7 505 ofs += 1;
simon 0:350011bf8be7 506 snmp_asn1_enc_length(p, ofs, m_stat->rhl.comlen);
simon 0:350011bf8be7 507 ofs += m_stat->rhl.comlenlen;
simon 0:350011bf8be7 508 snmp_asn1_enc_raw(p, ofs, m_stat->rhl.comlen, m_stat->community);
simon 0:350011bf8be7 509 ofs += m_stat->rhl.comlen;
simon 0:350011bf8be7 510
simon 0:350011bf8be7 511 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP));
simon 0:350011bf8be7 512 ofs += 1;
simon 0:350011bf8be7 513 snmp_asn1_enc_length(p, ofs, m_stat->rhl.pdulen);
simon 0:350011bf8be7 514 ofs += m_stat->rhl.pdulenlen;
simon 0:350011bf8be7 515
simon 0:350011bf8be7 516 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 517 ofs += 1;
simon 0:350011bf8be7 518 snmp_asn1_enc_length(p, ofs, m_stat->rhl.ridlen);
simon 0:350011bf8be7 519 ofs += m_stat->rhl.ridlenlen;
simon 0:350011bf8be7 520 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.ridlen, m_stat->rid);
simon 0:350011bf8be7 521 ofs += m_stat->rhl.ridlen;
simon 0:350011bf8be7 522
simon 0:350011bf8be7 523 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 524 ofs += 1;
simon 0:350011bf8be7 525 snmp_asn1_enc_length(p, ofs, m_stat->rhl.errstatlen);
simon 0:350011bf8be7 526 ofs += m_stat->rhl.errstatlenlen;
simon 0:350011bf8be7 527 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.errstatlen, m_stat->error_status);
simon 0:350011bf8be7 528 ofs += m_stat->rhl.errstatlen;
simon 0:350011bf8be7 529
simon 0:350011bf8be7 530 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 531 ofs += 1;
simon 0:350011bf8be7 532 snmp_asn1_enc_length(p, ofs, m_stat->rhl.erridxlen);
simon 0:350011bf8be7 533 ofs += m_stat->rhl.erridxlenlen;
simon 0:350011bf8be7 534 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.erridxlen, m_stat->error_index);
simon 0:350011bf8be7 535 ofs += m_stat->rhl.erridxlen;
simon 0:350011bf8be7 536
simon 0:350011bf8be7 537 return ofs;
simon 0:350011bf8be7 538 }
simon 0:350011bf8be7 539
simon 0:350011bf8be7 540 /**
simon 0:350011bf8be7 541 * Encodes trap header from head to tail.
simon 0:350011bf8be7 542 */
simon 0:350011bf8be7 543 static u16_t
simon 0:350011bf8be7 544 snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p)
simon 0:350011bf8be7 545 {
simon 0:350011bf8be7 546 u16_t ofs;
simon 0:350011bf8be7 547
simon 0:350011bf8be7 548 ofs = 0;
simon 0:350011bf8be7 549 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
simon 0:350011bf8be7 550 ofs += 1;
simon 0:350011bf8be7 551 snmp_asn1_enc_length(p, ofs, m_trap->thl.seqlen);
simon 0:350011bf8be7 552 ofs += m_trap->thl.seqlenlen;
simon 0:350011bf8be7 553
simon 0:350011bf8be7 554 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 555 ofs += 1;
simon 0:350011bf8be7 556 snmp_asn1_enc_length(p, ofs, m_trap->thl.verlen);
simon 0:350011bf8be7 557 ofs += m_trap->thl.verlenlen;
simon 0:350011bf8be7 558 snmp_asn1_enc_s32t(p, ofs, m_trap->thl.verlen, snmp_version);
simon 0:350011bf8be7 559 ofs += m_trap->thl.verlen;
simon 0:350011bf8be7 560
simon 0:350011bf8be7 561 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
simon 0:350011bf8be7 562 ofs += 1;
simon 0:350011bf8be7 563 snmp_asn1_enc_length(p, ofs, m_trap->thl.comlen);
simon 0:350011bf8be7 564 ofs += m_trap->thl.comlenlen;
simon 0:350011bf8be7 565 snmp_asn1_enc_raw(p, ofs, m_trap->thl.comlen, (u8_t *)&snmp_publiccommunity[0]);
simon 0:350011bf8be7 566 ofs += m_trap->thl.comlen;
simon 0:350011bf8be7 567
simon 0:350011bf8be7 568 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP));
simon 0:350011bf8be7 569 ofs += 1;
simon 0:350011bf8be7 570 snmp_asn1_enc_length(p, ofs, m_trap->thl.pdulen);
simon 0:350011bf8be7 571 ofs += m_trap->thl.pdulenlen;
simon 0:350011bf8be7 572
simon 0:350011bf8be7 573 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
simon 0:350011bf8be7 574 ofs += 1;
simon 0:350011bf8be7 575 snmp_asn1_enc_length(p, ofs, m_trap->thl.eidlen);
simon 0:350011bf8be7 576 ofs += m_trap->thl.eidlenlen;
simon 0:350011bf8be7 577 snmp_asn1_enc_oid(p, ofs, m_trap->enterprise->len, &m_trap->enterprise->id[0]);
simon 0:350011bf8be7 578 ofs += m_trap->thl.eidlen;
simon 0:350011bf8be7 579
simon 0:350011bf8be7 580 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR));
simon 0:350011bf8be7 581 ofs += 1;
simon 0:350011bf8be7 582 snmp_asn1_enc_length(p, ofs, m_trap->thl.aaddrlen);
simon 0:350011bf8be7 583 ofs += m_trap->thl.aaddrlenlen;
simon 0:350011bf8be7 584 snmp_asn1_enc_raw(p, ofs, m_trap->thl.aaddrlen, &m_trap->sip_raw[0]);
simon 0:350011bf8be7 585 ofs += m_trap->thl.aaddrlen;
simon 0:350011bf8be7 586
simon 0:350011bf8be7 587 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 588 ofs += 1;
simon 0:350011bf8be7 589 snmp_asn1_enc_length(p, ofs, m_trap->thl.gtrplen);
simon 0:350011bf8be7 590 ofs += m_trap->thl.gtrplenlen;
simon 0:350011bf8be7 591 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.gtrplen, m_trap->gen_trap);
simon 0:350011bf8be7 592 ofs += m_trap->thl.gtrplen;
simon 0:350011bf8be7 593
simon 0:350011bf8be7 594 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
simon 0:350011bf8be7 595 ofs += 1;
simon 0:350011bf8be7 596 snmp_asn1_enc_length(p, ofs, m_trap->thl.strplen);
simon 0:350011bf8be7 597 ofs += m_trap->thl.strplenlen;
simon 0:350011bf8be7 598 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.strplen, m_trap->spc_trap);
simon 0:350011bf8be7 599 ofs += m_trap->thl.strplen;
simon 0:350011bf8be7 600
simon 0:350011bf8be7 601 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS));
simon 0:350011bf8be7 602 ofs += 1;
simon 0:350011bf8be7 603 snmp_asn1_enc_length(p, ofs, m_trap->thl.tslen);
simon 0:350011bf8be7 604 ofs += m_trap->thl.tslenlen;
simon 0:350011bf8be7 605 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.tslen, m_trap->ts);
simon 0:350011bf8be7 606 ofs += m_trap->thl.tslen;
simon 0:350011bf8be7 607
simon 0:350011bf8be7 608 return ofs;
simon 0:350011bf8be7 609 }
simon 0:350011bf8be7 610
simon 0:350011bf8be7 611 /**
simon 0:350011bf8be7 612 * Encodes varbind list from head to tail.
simon 0:350011bf8be7 613 */
simon 0:350011bf8be7 614 static u16_t
simon 0:350011bf8be7 615 snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs)
simon 0:350011bf8be7 616 {
simon 0:350011bf8be7 617 struct snmp_varbind *vb;
simon 0:350011bf8be7 618 s32_t *sint_ptr;
simon 0:350011bf8be7 619 u32_t *uint_ptr;
simon 0:350011bf8be7 620 u8_t *raw_ptr;
simon 0:350011bf8be7 621
simon 0:350011bf8be7 622 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
simon 0:350011bf8be7 623 ofs += 1;
simon 0:350011bf8be7 624 snmp_asn1_enc_length(p, ofs, root->seqlen);
simon 0:350011bf8be7 625 ofs += root->seqlenlen;
simon 0:350011bf8be7 626
simon 0:350011bf8be7 627 vb = root->head;
simon 0:350011bf8be7 628 while ( vb != NULL )
simon 0:350011bf8be7 629 {
simon 0:350011bf8be7 630 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
simon 0:350011bf8be7 631 ofs += 1;
simon 0:350011bf8be7 632 snmp_asn1_enc_length(p, ofs, vb->seqlen);
simon 0:350011bf8be7 633 ofs += vb->seqlenlen;
simon 0:350011bf8be7 634
simon 0:350011bf8be7 635 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
simon 0:350011bf8be7 636 ofs += 1;
simon 0:350011bf8be7 637 snmp_asn1_enc_length(p, ofs, vb->olen);
simon 0:350011bf8be7 638 ofs += vb->olenlen;
simon 0:350011bf8be7 639 snmp_asn1_enc_oid(p, ofs, vb->ident_len, &vb->ident[0]);
simon 0:350011bf8be7 640 ofs += vb->olen;
simon 0:350011bf8be7 641
simon 0:350011bf8be7 642 snmp_asn1_enc_type(p, ofs, vb->value_type);
simon 0:350011bf8be7 643 ofs += 1;
simon 0:350011bf8be7 644 snmp_asn1_enc_length(p, ofs, vb->vlen);
simon 0:350011bf8be7 645 ofs += vb->vlenlen;
simon 0:350011bf8be7 646
simon 0:350011bf8be7 647 switch (vb->value_type)
simon 0:350011bf8be7 648 {
simon 0:350011bf8be7 649 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
simon 0:350011bf8be7 650 sint_ptr = (s32_t*)vb->value;
simon 0:350011bf8be7 651 snmp_asn1_enc_s32t(p, ofs, vb->vlen, *sint_ptr);
simon 0:350011bf8be7 652 break;
simon 0:350011bf8be7 653 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
simon 0:350011bf8be7 654 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
simon 0:350011bf8be7 655 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
simon 0:350011bf8be7 656 uint_ptr = (u32_t*)vb->value;
simon 0:350011bf8be7 657 snmp_asn1_enc_u32t(p, ofs, vb->vlen, *uint_ptr);
simon 0:350011bf8be7 658 break;
simon 0:350011bf8be7 659 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
simon 0:350011bf8be7 660 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
simon 0:350011bf8be7 661 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
simon 0:350011bf8be7 662 raw_ptr = (u8_t*)vb->value;
simon 0:350011bf8be7 663 snmp_asn1_enc_raw(p, ofs, vb->vlen, raw_ptr);
simon 0:350011bf8be7 664 break;
simon 0:350011bf8be7 665 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
simon 0:350011bf8be7 666 break;
simon 0:350011bf8be7 667 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
simon 0:350011bf8be7 668 sint_ptr = (s32_t*)vb->value;
simon 0:350011bf8be7 669 snmp_asn1_enc_oid(p, ofs, vb->value_len / sizeof(s32_t), sint_ptr);
simon 0:350011bf8be7 670 break;
simon 0:350011bf8be7 671 default:
simon 0:350011bf8be7 672 /* unsupported type */
simon 0:350011bf8be7 673 break;
simon 0:350011bf8be7 674 };
simon 0:350011bf8be7 675 ofs += vb->vlen;
simon 0:350011bf8be7 676 vb = vb->next;
simon 0:350011bf8be7 677 }
simon 0:350011bf8be7 678 return ofs;
simon 0:350011bf8be7 679 }
simon 0:350011bf8be7 680
simon 0:350011bf8be7 681 #endif /* LWIP_SNMP */