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