Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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