SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
Child:
2:25e12a7fe0aa
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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