Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependencies:   mbed-rtos

Dependents:   IMU_ethernet

Fork of F7_Ethernet by Dieter Graef

Committer:
rctaduio
Date:
Thu Oct 06 16:55:16 2016 +0000
Revision:
2:e0a4035b5cd1
Parent:
0:d26c1b55cfca
Ethernet library for F7

Who changed what in which revision?

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