HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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