Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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