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