3 * SNMP output message processing (RFC1157).
5 * Output responses and traps are build in two passes:
7 * Pass 0: iterate over the output message backwards to determine encoding lengths
8 * Pass 1: the actual forward encoding of internal form into ASN1
10 * The single-pass encoding method described by Comer & Stevens
11 * requires extra buffer space and copying for reversal of the packet.
12 * The buffer requirement can be prohibitively large for big payloads
13 * (>= 484) therefore we use the two encoding passes.
17 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
23 * 1. Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
34 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
42 * Author: Christiaan Simons <christiaan.simons@axon.tv>
47 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
50 #include "lwip/netif.h"
51 #include "lwip/snmp.h"
52 #include "lwip/snmp_mib2.h"
53 #include "lwip/snmp_asn1.h"
54 #include "lwip/snmp_msg.h"
59 #if !SNMP_COMMUNITY_EXT
60 #define snmp_community_trap snmp_community
65 /* destination IP address in network order */
67 /* set to 0 when disabled, >0 when enabled */
70 struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS];
72 /** TRAP message structure */
73 struct snmp_msg_trap trap_msg;
75 static u16_t snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len);
76 static u16_t snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len);
77 static u16_t snmp_varbind_list_sum(struct snmp_varbind_root *root);
79 static u16_t snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p);
80 static u16_t snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p);
81 static u16_t snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs);
84 * Sets enable switch for this trap destination.
85 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
86 * @param enable switch if 0 destination is disabled >0 enabled.
89 snmp_trap_dst_enable(u8_t dst_idx, u8_t enable)
91 if (dst_idx < SNMP_TRAP_DESTINATIONS) {
92 trap_dst[dst_idx].enable = enable;
97 * Sets IPv4 address for this trap destination.
98 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
99 * @param dst IPv4 address in host order.
102 snmp_trap_dst_ip_set(u8_t dst_idx, const ip_addr_t *dst)
104 if (dst_idx < SNMP_TRAP_DESTINATIONS) {
105 ip_addr_set(&trap_dst[dst_idx].dip, dst);
110 * Sends a 'getresponse' message to the request originator.
112 * @param m_stat points to the current message request state source
113 * @return ERR_OK when success, ERR_MEM if we're out of memory
115 * @note the caller is responsible for filling in outvb in the m_stat
116 * and provide error-status and index (except for tooBig errors) ...
119 snmp_send_response(struct snmp_msg_pstat *m_stat)
121 struct snmp_varbind_root emptyvb = {NULL, NULL, 0, 0, 0};
126 /* pass 0, calculate length fields */
127 tot_len = snmp_varbind_list_sum(&m_stat->outvb);
128 tot_len = snmp_resp_header_sum(m_stat, tot_len);
130 /* try allocating pbuf(s) for complete response */
131 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
133 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() tooBig\n"));
135 /* can't construct reply, return error-status tooBig */
136 m_stat->error_status = SNMP_ES_TOOBIG;
137 m_stat->error_index = 0;
138 /* pass 0, recalculate lengths, for empty varbind-list */
139 tot_len = snmp_varbind_list_sum(&emptyvb);
140 tot_len = snmp_resp_header_sum(m_stat, tot_len);
141 /* retry allocation once for header and empty varbind-list */
142 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
145 /* first pbuf alloc try or retry alloc success */
148 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() p != NULL\n"));
150 /* pass 1, size error, encode packet ino the pbuf(s) */
151 ofs = snmp_resp_header_enc(m_stat, p);
152 snmp_varbind_list_enc(&m_stat->outvb, p, ofs);
154 switch (m_stat->error_status) {
155 case SNMP_ES_NOERROR:
159 mib2_inc_snmpouttoobigs();
161 case SNMP_ES_NOSUCHNAME:
162 mib2_inc_snmpoutnosuchnames();
164 case SNMP_ES_BADVALUE:
165 mib2_inc_snmpoutbadvalues();
167 case SNMP_ES_GENERROR:
168 mib2_inc_snmpoutgenerrs();
171 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_send_response(): unknown error_status: %d\n", (int)m_stat->error_status));
174 mib2_inc_snmpoutgetresponses();
175 mib2_inc_snmpoutpkts();
177 /** @todo do we need separate rx and tx pcbs for threaded case? */
178 /** connect to the originating source */
179 udp_connect(m_stat->pcb, &m_stat->sip, m_stat->sp);
180 err = udp_send(m_stat->pcb, p);
181 if (err == ERR_MEM) {
182 /** @todo release some memory, retry and return tooBig? tooMuchHassle? */
187 /** disassociate remote address and port with this pcb */
188 udp_disconnect(m_stat->pcb);
191 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() done\n"));
194 /* first pbuf alloc try or retry alloc failed
195 very low on memory, couldn't return tooBig */
202 * Sends an generic or enterprise specific trap message.
204 * @param generic_trap is the trap code
205 * @param eoid points to enterprise object identifier
206 * @param specific_trap used for enterprise traps when generic_trap == 6
207 * @return ERR_OK when success, ERR_MEM if we're out of memory
209 * @note the caller is responsible for filling in outvb in the trap_msg
210 * @note the use of the enterprise identifier field
212 * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
213 * and .iso.org.dod.internet.private.enterprises.yourenterprise
214 * (sysObjectID) for specific traps.
217 snmp_send_trap(s8_t generic_trap, const struct snmp_obj_id *eoid, s32_t specific_trap)
219 struct snmp_trap_dst *td;
220 struct netif *dst_if;
221 const ip_addr_t* dst_ip;
226 for (i = 0, td = &trap_dst[0]; i < SNMP_TRAP_DESTINATIONS; i++, td++) {
227 if ((td->enable != 0) && !ip_addr_isany(&td->dip)) {
228 /* network order trap destination */
229 ip_addr_copy(trap_msg.dip, td->dip);
230 /* lookup current source address for this dst */
231 ip_route_get_local_ip(PCB_ISIPV6(trap_msg.pcb), &trap_msg.pcb->local_ip,
232 &td->dip, dst_if, dst_ip);
233 if ((dst_if != NULL) && (dst_ip != NULL)) {
234 trap_msg.sip_raw_len = (IP_IS_V6_VAL(*dst_ip) ? 16 : 4);
235 memcpy(trap_msg.sip_raw, dst_ip, trap_msg.sip_raw_len);
236 trap_msg.gen_trap = generic_trap;
237 trap_msg.spc_trap = specific_trap;
238 if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC) {
239 /* enterprise-Specific trap */
240 trap_msg.enterprise = eoid;
242 /* generic (MIB-II) trap */
243 mib2_get_snmpgrpid_ptr(&trap_msg.enterprise);
246 MIB2_COPY_SYSUPTIME_TO(&trap_msg.ts);
248 /* pass 0, calculate length fields */
249 tot_len = snmp_varbind_list_sum(&trap_msg.outvb);
250 tot_len = snmp_trap_header_sum(&trap_msg, tot_len);
252 /* allocate pbuf(s) */
253 p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
257 /* pass 1, encode packet ino the pbuf(s) */
258 ofs = snmp_trap_header_enc(&trap_msg, p);
259 snmp_varbind_list_enc(&trap_msg.outvb, p, ofs);
261 mib2_inc_snmpouttraps();
262 mib2_inc_snmpoutpkts();
264 /** send to the TRAP destination */
265 udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT);
281 snmp_coldstart_trap(void)
283 trap_msg.outvb.head = NULL;
284 trap_msg.outvb.tail = NULL;
285 trap_msg.outvb.count = 0;
286 snmp_send_trap(SNMP_GENTRAP_COLDSTART, NULL, 0);
290 snmp_authfail_trap(void)
293 mib2_get_snmpenableauthentraps(&enable);
295 trap_msg.outvb.head = NULL;
296 trap_msg.outvb.tail = NULL;
297 trap_msg.outvb.count = 0;
298 snmp_send_trap(SNMP_GENTRAP_AUTHFAIL, NULL, 0);
303 * Sums response header field lengths from tail to head and
304 * returns resp_header_lengths for second encoding pass.
306 * @param vb_len varbind-list length
307 * @param rhl points to returned header lengths
308 * @return the required length for encoding the response header
311 snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
315 struct snmp_resp_header_lengths *rhl;
319 snmp_asn1_enc_s32t_cnt(m_stat->error_index, &rhl->erridxlen);
320 snmp_asn1_enc_length_cnt(rhl->erridxlen, &rhl->erridxlenlen);
321 tot_len += 1 + rhl->erridxlenlen + rhl->erridxlen;
323 snmp_asn1_enc_s32t_cnt(m_stat->error_status, &rhl->errstatlen);
324 snmp_asn1_enc_length_cnt(rhl->errstatlen, &rhl->errstatlenlen);
325 tot_len += 1 + rhl->errstatlenlen + rhl->errstatlen;
327 snmp_asn1_enc_s32t_cnt(m_stat->rid, &rhl->ridlen);
328 snmp_asn1_enc_length_cnt(rhl->ridlen, &rhl->ridlenlen);
329 tot_len += 1 + rhl->ridlenlen + rhl->ridlen;
331 rhl->pdulen = tot_len;
332 snmp_asn1_enc_length_cnt(rhl->pdulen, &rhl->pdulenlen);
333 tot_len += 1 + rhl->pdulenlen;
335 rhl->comlen = m_stat->com_strlen;
336 snmp_asn1_enc_length_cnt(rhl->comlen, &rhl->comlenlen);
337 tot_len += 1 + rhl->comlenlen + rhl->comlen;
339 snmp_req_ver = m_stat->version;
340 snmp_asn1_enc_s32t_cnt(snmp_req_ver, &rhl->verlen);
341 snmp_asn1_enc_length_cnt(rhl->verlen, &rhl->verlenlen);
342 tot_len += 1 + rhl->verlen + rhl->verlenlen;
344 rhl->seqlen = tot_len;
345 snmp_asn1_enc_length_cnt(rhl->seqlen, &rhl->seqlenlen);
346 tot_len += 1 + rhl->seqlenlen;
352 * Sums trap header field lengths from tail to head and
353 * returns trap_header_lengths for second encoding pass.
355 * @param vb_len varbind-list length
356 * @param thl points to returned header lengths
357 * @return the required length for encoding the trap header
360 snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len)
363 struct snmp_trap_header_lengths *thl;
368 snmp_asn1_enc_u32t_cnt(m_trap->ts, &thl->tslen);
369 snmp_asn1_enc_length_cnt(thl->tslen, &thl->tslenlen);
370 tot_len += 1 + thl->tslen + thl->tslenlen;
372 snmp_asn1_enc_s32t_cnt(m_trap->spc_trap, &thl->strplen);
373 snmp_asn1_enc_length_cnt(thl->strplen, &thl->strplenlen);
374 tot_len += 1 + thl->strplen + thl->strplenlen;
376 snmp_asn1_enc_s32t_cnt(m_trap->gen_trap, &thl->gtrplen);
377 snmp_asn1_enc_length_cnt(thl->gtrplen, &thl->gtrplenlen);
378 tot_len += 1 + thl->gtrplen + thl->gtrplenlen;
380 thl->aaddrlen = m_trap->sip_raw_len;
381 snmp_asn1_enc_length_cnt(thl->aaddrlen, &thl->aaddrlenlen);
382 tot_len += 1 + thl->aaddrlen + thl->aaddrlenlen;
384 snmp_asn1_enc_oid_cnt(m_trap->enterprise->len, &m_trap->enterprise->id[0], &thl->eidlen);
385 snmp_asn1_enc_length_cnt(thl->eidlen, &thl->eidlenlen);
386 tot_len += 1 + thl->eidlen + thl->eidlenlen;
388 thl->pdulen = tot_len;
389 snmp_asn1_enc_length_cnt(thl->pdulen, &thl->pdulenlen);
390 tot_len += 1 + thl->pdulenlen;
392 thl->comlen = (u16_t)strlen(snmp_community_trap);
393 snmp_asn1_enc_length_cnt(thl->comlen, &thl->comlenlen);
394 tot_len += 1 + thl->comlenlen + thl->comlen;
396 snmp_asn1_enc_s32t_cnt(snmp_version, &thl->verlen);
397 snmp_asn1_enc_length_cnt(thl->verlen, &thl->verlenlen);
398 tot_len += 1 + thl->verlen + thl->verlenlen;
400 thl->seqlen = tot_len;
401 snmp_asn1_enc_length_cnt(thl->seqlen, &thl->seqlenlen);
402 tot_len += 1 + thl->seqlenlen;
408 * Sums varbind lengths from tail to head and
409 * annotates lengths in varbind for second encoding pass.
411 * @param root points to the root of the variable binding list
412 * @return the required length for encoding the variable bindings
415 snmp_varbind_list_sum(struct snmp_varbind_root *root)
417 struct snmp_varbind *vb;
425 /* encoded value lenght depends on type */
426 switch (vb->value_type) {
427 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
428 sint_ptr = (s32_t*)vb->value;
429 snmp_asn1_enc_s32t_cnt(*sint_ptr, &vb->vlen);
431 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
432 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
433 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
434 uint_ptr = (u32_t*)vb->value;
435 snmp_asn1_enc_u32t_cnt(*uint_ptr, &vb->vlen);
437 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
438 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
439 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
440 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
441 vb->vlen = vb->value_len;
443 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
444 sint_ptr = (s32_t*)vb->value;
445 snmp_asn1_enc_oid_cnt(vb->value_len / sizeof(s32_t), sint_ptr, &vb->vlen);
448 /* unsupported type */
452 /* encoding length of value length field */
453 snmp_asn1_enc_length_cnt(vb->vlen, &vb->vlenlen);
454 snmp_asn1_enc_oid_cnt(vb->ident_len, vb->ident, &vb->olen);
455 snmp_asn1_enc_length_cnt(vb->olen, &vb->olenlen);
457 vb->seqlen = 1 + vb->vlenlen + vb->vlen;
458 vb->seqlen += 1 + vb->olenlen + vb->olen;
459 snmp_asn1_enc_length_cnt(vb->seqlen, &vb->seqlenlen);
462 tot_len += 1 + vb->seqlenlen + vb->seqlen;
467 /* varbind-list seq */
468 root->seqlen = tot_len;
469 snmp_asn1_enc_length_cnt(root->seqlen, &root->seqlenlen);
470 tot_len += 1 + root->seqlenlen;
476 * Encodes response header from head to tail.
479 snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p)
485 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
487 snmp_asn1_enc_length(p, ofs, m_stat->rhl.seqlen);
488 ofs += m_stat->rhl.seqlenlen;
490 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
492 snmp_asn1_enc_length(p, ofs, m_stat->rhl.verlen);
493 ofs += m_stat->rhl.verlenlen;
494 snmp_req_ver = m_stat->version;
495 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.verlen, snmp_req_ver);
496 ofs += m_stat->rhl.verlen;
498 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
500 snmp_asn1_enc_length(p, ofs, m_stat->rhl.comlen);
501 ofs += m_stat->rhl.comlenlen;
502 snmp_asn1_enc_raw(p, ofs, m_stat->rhl.comlen, m_stat->community);
503 ofs += m_stat->rhl.comlen;
505 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP));
507 snmp_asn1_enc_length(p, ofs, m_stat->rhl.pdulen);
508 ofs += m_stat->rhl.pdulenlen;
510 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
512 snmp_asn1_enc_length(p, ofs, m_stat->rhl.ridlen);
513 ofs += m_stat->rhl.ridlenlen;
514 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.ridlen, m_stat->rid);
515 ofs += m_stat->rhl.ridlen;
517 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
519 snmp_asn1_enc_length(p, ofs, m_stat->rhl.errstatlen);
520 ofs += m_stat->rhl.errstatlenlen;
521 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.errstatlen, m_stat->error_status);
522 ofs += m_stat->rhl.errstatlen;
524 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
526 snmp_asn1_enc_length(p, ofs, m_stat->rhl.erridxlen);
527 ofs += m_stat->rhl.erridxlenlen;
528 snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.erridxlen, m_stat->error_index);
529 ofs += m_stat->rhl.erridxlen;
535 * Encodes trap header from head to tail.
538 snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p)
543 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
545 snmp_asn1_enc_length(p, ofs, m_trap->thl.seqlen);
546 ofs += m_trap->thl.seqlenlen;
548 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
550 snmp_asn1_enc_length(p, ofs, m_trap->thl.verlen);
551 ofs += m_trap->thl.verlenlen;
552 snmp_asn1_enc_s32t(p, ofs, m_trap->thl.verlen, snmp_version);
553 ofs += m_trap->thl.verlen;
555 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
557 snmp_asn1_enc_length(p, ofs, m_trap->thl.comlen);
558 ofs += m_trap->thl.comlenlen;
559 snmp_asn1_enc_raw(p, ofs, m_trap->thl.comlen, (const u8_t *)&snmp_community_trap[0]);
560 ofs += m_trap->thl.comlen;
562 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP));
564 snmp_asn1_enc_length(p, ofs, m_trap->thl.pdulen);
565 ofs += m_trap->thl.pdulenlen;
567 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
569 snmp_asn1_enc_length(p, ofs, m_trap->thl.eidlen);
570 ofs += m_trap->thl.eidlenlen;
571 snmp_asn1_enc_oid(p, ofs, m_trap->enterprise->len, &m_trap->enterprise->id[0]);
572 ofs += m_trap->thl.eidlen;
574 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR));
576 snmp_asn1_enc_length(p, ofs, m_trap->thl.aaddrlen);
577 ofs += m_trap->thl.aaddrlenlen;
578 snmp_asn1_enc_raw(p, ofs, m_trap->thl.aaddrlen, &m_trap->sip_raw[0]);
579 ofs += m_trap->thl.aaddrlen;
581 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
583 snmp_asn1_enc_length(p, ofs, m_trap->thl.gtrplen);
584 ofs += m_trap->thl.gtrplenlen;
585 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.gtrplen, m_trap->gen_trap);
586 ofs += m_trap->thl.gtrplen;
588 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
590 snmp_asn1_enc_length(p, ofs, m_trap->thl.strplen);
591 ofs += m_trap->thl.strplenlen;
592 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.strplen, m_trap->spc_trap);
593 ofs += m_trap->thl.strplen;
595 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS));
597 snmp_asn1_enc_length(p, ofs, m_trap->thl.tslen);
598 ofs += m_trap->thl.tslenlen;
599 snmp_asn1_enc_u32t(p, ofs, m_trap->thl.tslen, m_trap->ts);
600 ofs += m_trap->thl.tslen;
606 * Encodes varbind list from head to tail.
609 snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs)
611 struct snmp_varbind *vb;
616 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
618 snmp_asn1_enc_length(p, ofs, root->seqlen);
619 ofs += root->seqlenlen;
623 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
625 snmp_asn1_enc_length(p, ofs, vb->seqlen);
626 ofs += vb->seqlenlen;
628 snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
630 snmp_asn1_enc_length(p, ofs, vb->olen);
632 snmp_asn1_enc_oid(p, ofs, vb->ident_len, &vb->ident[0]);
635 snmp_asn1_enc_type(p, ofs, vb->value_type);
637 snmp_asn1_enc_length(p, ofs, vb->vlen);
640 switch (vb->value_type) {
641 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
642 sint_ptr = (s32_t*)vb->value;
643 snmp_asn1_enc_s32t(p, ofs, vb->vlen, *sint_ptr);
645 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
646 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
647 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
648 uint_ptr = (u32_t*)vb->value;
649 snmp_asn1_enc_u32t(p, ofs, vb->vlen, *uint_ptr);
651 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
652 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
653 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
654 raw_ptr = (u8_t*)vb->value;
655 snmp_asn1_enc_raw(p, ofs, vb->vlen, raw_ptr);
657 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
659 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
660 sint_ptr = (s32_t*)vb->value;
661 snmp_asn1_enc_oid(p, ofs, vb->value_len / sizeof(s32_t), sint_ptr);
664 /* unsupported type */
673 #endif /* LWIP_SNMP */