I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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