delta param

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

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