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_asn1.h"
53 #include "lwip/snmp_msg.h"
57 /* destination IP address in network order */
59 /* set to 0 when disabled, >0 when enabled */
62 struct snmp_trap_dst trap_dst
[SNMP_TRAP_DESTINATIONS
];
64 /** TRAP message structure */
65 struct snmp_msg_trap trap_msg
;
67 static u16_t
snmp_resp_header_sum(struct snmp_msg_pstat
*m_stat
, u16_t vb_len
);
68 static u16_t
snmp_trap_header_sum(struct snmp_msg_trap
*m_trap
, u16_t vb_len
);
69 static u16_t
snmp_varbind_list_sum(struct snmp_varbind_root
*root
);
71 static u16_t
snmp_resp_header_enc(struct snmp_msg_pstat
*m_stat
, struct pbuf
*p
);
72 static u16_t
snmp_trap_header_enc(struct snmp_msg_trap
*m_trap
, struct pbuf
*p
);
73 static u16_t
snmp_varbind_list_enc(struct snmp_varbind_root
*root
, struct pbuf
*p
, u16_t ofs
);
76 * Sets enable switch for this trap destination.
77 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
78 * @param enable switch if 0 destination is disabled >0 enabled.
81 snmp_trap_dst_enable(u8_t dst_idx
, u8_t enable
)
83 if (dst_idx
< SNMP_TRAP_DESTINATIONS
)
85 trap_dst
[dst_idx
].enable
= enable
;
90 * Sets IPv4 address for this trap destination.
91 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
92 * @param dst IPv4 address in host order.
95 snmp_trap_dst_ip_set(u8_t dst_idx
, ip_addr_t
*dst
)
97 if (dst_idx
< SNMP_TRAP_DESTINATIONS
)
99 ip_addr_set(&trap_dst
[dst_idx
].dip
, dst
);
104 * Sends a 'getresponse' message to the request originator.
106 * @param m_stat points to the current message request state source
107 * @return ERR_OK when success, ERR_MEM if we're out of memory
109 * @note the caller is responsible for filling in outvb in the m_stat
110 * and provide error-status and index (except for tooBig errors) ...
113 snmp_send_response(struct snmp_msg_pstat
*m_stat
)
115 struct snmp_varbind_root emptyvb
= {NULL
, NULL
, 0, 0, 0};
120 /* pass 0, calculate length fields */
121 tot_len
= snmp_varbind_list_sum(&m_stat
->outvb
);
122 tot_len
= snmp_resp_header_sum(m_stat
, tot_len
);
124 /* try allocating pbuf(s) for complete response */
125 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
128 LWIP_DEBUGF(SNMP_MSG_DEBUG
, ("snmp_snd_response() tooBig\n"));
130 /* can't construct reply, return error-status tooBig */
131 m_stat
->error_status
= SNMP_ES_TOOBIG
;
132 m_stat
->error_index
= 0;
133 /* pass 0, recalculate lengths, for empty varbind-list */
134 tot_len
= snmp_varbind_list_sum(&emptyvb
);
135 tot_len
= snmp_resp_header_sum(m_stat
, tot_len
);
136 /* retry allocation once for header and empty varbind-list */
137 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
141 /* first pbuf alloc try or retry alloc success */
144 LWIP_DEBUGF(SNMP_MSG_DEBUG
, ("snmp_snd_response() p != NULL\n"));
146 /* pass 1, size error, encode packet ino the pbuf(s) */
147 ofs
= snmp_resp_header_enc(m_stat
, p
);
148 if (m_stat
->error_status
== SNMP_ES_TOOBIG
)
150 snmp_varbind_list_enc(&emptyvb
, p
, ofs
);
154 snmp_varbind_list_enc(&m_stat
->outvb
, p
, ofs
);
157 switch (m_stat
->error_status
)
160 snmp_inc_snmpouttoobigs();
162 case SNMP_ES_NOSUCHNAME
:
163 snmp_inc_snmpoutnosuchnames();
165 case SNMP_ES_BADVALUE
:
166 snmp_inc_snmpoutbadvalues();
168 case SNMP_ES_GENERROR
:
169 snmp_inc_snmpoutgenerrs();
172 snmp_inc_snmpoutgetresponses();
173 snmp_inc_snmpoutpkts();
175 /** @todo do we need separate rx and tx pcbs for threaded case? */
176 /** connect to the originating source */
177 udp_connect(m_stat
->pcb
, &m_stat
->sip
, m_stat
->sp
);
178 err
= udp_send(m_stat
->pcb
, p
);
181 /** @todo release some memory, retry and return tooBig? tooMuchHassle? */
188 /** disassociate remote address and port with this pcb */
189 udp_disconnect(m_stat
->pcb
);
192 LWIP_DEBUGF(SNMP_MSG_DEBUG
, ("snmp_snd_response() done\n"));
197 /* first pbuf alloc try or retry alloc failed
198 very low on memory, couldn't return tooBig */
205 * Sends an generic or enterprise specific trap message.
207 * @param generic_trap is the trap code
208 * @param eoid points to enterprise object identifier
209 * @param specific_trap used for enterprise traps when generic_trap == 6
210 * @return ERR_OK when success, ERR_MEM if we're out of memory
212 * @note the caller is responsible for filling in outvb in the trap_msg
213 * @note the use of the enterpise identifier field
215 * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
216 * and .iso.org.dod.internet.private.enterprises.yourenterprise
217 * (sysObjectID) for specific traps.
220 snmp_send_trap(s8_t generic_trap
, struct snmp_obj_id
*eoid
, s32_t specific_trap
)
222 struct snmp_trap_dst
*td
;
223 struct netif
*dst_if
;
228 for (i
=0, td
= &trap_dst
[0]; i
<SNMP_TRAP_DESTINATIONS
; i
++, td
++)
230 if ((td
->enable
!= 0) && !ip_addr_isany(&td
->dip
))
232 /* network order trap destination */
233 ip_addr_copy(trap_msg
.dip
, td
->dip
);
234 /* lookup current source address for this dst */
235 dst_if
= ip_route(&td
->dip
);
236 ip_addr_copy(dst_ip
, dst_if
->ip_addr
);
237 /* @todo: what about IPv6? */
238 trap_msg
.sip_raw
[0] = ip4_addr1(&dst_ip
);
239 trap_msg
.sip_raw
[1] = ip4_addr2(&dst_ip
);
240 trap_msg
.sip_raw
[2] = ip4_addr3(&dst_ip
);
241 trap_msg
.sip_raw
[3] = ip4_addr4(&dst_ip
);
242 trap_msg
.gen_trap
= generic_trap
;
243 trap_msg
.spc_trap
= specific_trap
;
244 if (generic_trap
== SNMP_GENTRAP_ENTERPRISESPC
)
246 /* enterprise-Specific trap */
247 trap_msg
.enterprise
= eoid
;
251 /* generic (MIB-II) trap */
252 snmp_get_snmpgrpid_ptr(&trap_msg
.enterprise
);
254 snmp_get_sysuptime(&trap_msg
.ts
);
256 /* pass 0, calculate length fields */
257 tot_len
= snmp_varbind_list_sum(&trap_msg
.outvb
);
258 tot_len
= snmp_trap_header_sum(&trap_msg
, tot_len
);
260 /* allocate pbuf(s) */
261 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
266 /* pass 1, encode packet ino the pbuf(s) */
267 ofs
= snmp_trap_header_enc(&trap_msg
, p
);
268 snmp_varbind_list_enc(&trap_msg
.outvb
, p
, ofs
);
270 snmp_inc_snmpouttraps();
271 snmp_inc_snmpoutpkts();
273 /** send to the TRAP destination */
274 udp_sendto(trap_msg
.pcb
, p
, &trap_msg
.dip
, SNMP_TRAP_PORT
);
288 snmp_coldstart_trap(void)
290 trap_msg
.outvb
.head
= NULL
;
291 trap_msg
.outvb
.tail
= NULL
;
292 trap_msg
.outvb
.count
= 0;
293 snmp_send_trap(SNMP_GENTRAP_COLDSTART
, NULL
, 0);
297 snmp_authfail_trap(void)
300 snmp_get_snmpenableauthentraps(&enable
);
303 trap_msg
.outvb
.head
= NULL
;
304 trap_msg
.outvb
.tail
= NULL
;
305 trap_msg
.outvb
.count
= 0;
306 snmp_send_trap(SNMP_GENTRAP_AUTHFAIL
, NULL
, 0);
311 * Sums response header field lengths from tail to head and
312 * returns resp_header_lengths for second encoding pass.
314 * @param vb_len varbind-list length
315 * @param rhl points to returned header lengths
316 * @return the required lenght for encoding the response header
319 snmp_resp_header_sum(struct snmp_msg_pstat
*m_stat
, u16_t vb_len
)
322 struct snmp_resp_header_lengths
*rhl
;
326 snmp_asn1_enc_s32t_cnt(m_stat
->error_index
, &rhl
->erridxlen
);
327 snmp_asn1_enc_length_cnt(rhl
->erridxlen
, &rhl
->erridxlenlen
);
328 tot_len
+= 1 + rhl
->erridxlenlen
+ rhl
->erridxlen
;
330 snmp_asn1_enc_s32t_cnt(m_stat
->error_status
, &rhl
->errstatlen
);
331 snmp_asn1_enc_length_cnt(rhl
->errstatlen
, &rhl
->errstatlenlen
);
332 tot_len
+= 1 + rhl
->errstatlenlen
+ rhl
->errstatlen
;
334 snmp_asn1_enc_s32t_cnt(m_stat
->rid
, &rhl
->ridlen
);
335 snmp_asn1_enc_length_cnt(rhl
->ridlen
, &rhl
->ridlenlen
);
336 tot_len
+= 1 + rhl
->ridlenlen
+ rhl
->ridlen
;
338 rhl
->pdulen
= tot_len
;
339 snmp_asn1_enc_length_cnt(rhl
->pdulen
, &rhl
->pdulenlen
);
340 tot_len
+= 1 + rhl
->pdulenlen
;
342 rhl
->comlen
= m_stat
->com_strlen
;
343 snmp_asn1_enc_length_cnt(rhl
->comlen
, &rhl
->comlenlen
);
344 tot_len
+= 1 + rhl
->comlenlen
+ rhl
->comlen
;
346 snmp_asn1_enc_s32t_cnt(snmp_version
, &rhl
->verlen
);
347 snmp_asn1_enc_length_cnt(rhl
->verlen
, &rhl
->verlenlen
);
348 tot_len
+= 1 + rhl
->verlen
+ rhl
->verlenlen
;
350 rhl
->seqlen
= tot_len
;
351 snmp_asn1_enc_length_cnt(rhl
->seqlen
, &rhl
->seqlenlen
);
352 tot_len
+= 1 + rhl
->seqlenlen
;
358 * Sums trap header field lengths from tail to head and
359 * returns trap_header_lengths for second encoding pass.
361 * @param vb_len varbind-list length
362 * @param thl points to returned header lengths
363 * @return the required lenght for encoding the trap header
366 snmp_trap_header_sum(struct snmp_msg_trap
*m_trap
, u16_t vb_len
)
369 struct snmp_trap_header_lengths
*thl
;
374 snmp_asn1_enc_u32t_cnt(m_trap
->ts
, &thl
->tslen
);
375 snmp_asn1_enc_length_cnt(thl
->tslen
, &thl
->tslenlen
);
376 tot_len
+= 1 + thl
->tslen
+ thl
->tslenlen
;
378 snmp_asn1_enc_s32t_cnt(m_trap
->spc_trap
, &thl
->strplen
);
379 snmp_asn1_enc_length_cnt(thl
->strplen
, &thl
->strplenlen
);
380 tot_len
+= 1 + thl
->strplen
+ thl
->strplenlen
;
382 snmp_asn1_enc_s32t_cnt(m_trap
->gen_trap
, &thl
->gtrplen
);
383 snmp_asn1_enc_length_cnt(thl
->gtrplen
, &thl
->gtrplenlen
);
384 tot_len
+= 1 + thl
->gtrplen
+ thl
->gtrplenlen
;
387 snmp_asn1_enc_length_cnt(thl
->aaddrlen
, &thl
->aaddrlenlen
);
388 tot_len
+= 1 + thl
->aaddrlen
+ thl
->aaddrlenlen
;
390 snmp_asn1_enc_oid_cnt(m_trap
->enterprise
->len
, &m_trap
->enterprise
->id
[0], &thl
->eidlen
);
391 snmp_asn1_enc_length_cnt(thl
->eidlen
, &thl
->eidlenlen
);
392 tot_len
+= 1 + thl
->eidlen
+ thl
->eidlenlen
;
394 thl
->pdulen
= tot_len
;
395 snmp_asn1_enc_length_cnt(thl
->pdulen
, &thl
->pdulenlen
);
396 tot_len
+= 1 + thl
->pdulenlen
;
398 thl
->comlen
= sizeof(snmp_publiccommunity
) - 1;
399 snmp_asn1_enc_length_cnt(thl
->comlen
, &thl
->comlenlen
);
400 tot_len
+= 1 + thl
->comlenlen
+ thl
->comlen
;
402 snmp_asn1_enc_s32t_cnt(snmp_version
, &thl
->verlen
);
403 snmp_asn1_enc_length_cnt(thl
->verlen
, &thl
->verlenlen
);
404 tot_len
+= 1 + thl
->verlen
+ thl
->verlenlen
;
406 thl
->seqlen
= tot_len
;
407 snmp_asn1_enc_length_cnt(thl
->seqlen
, &thl
->seqlenlen
);
408 tot_len
+= 1 + thl
->seqlenlen
;
414 * Sums varbind lengths from tail to head and
415 * annotates lengths in varbind for second encoding pass.
417 * @param root points to the root of the variable binding list
418 * @return the required lenght for encoding the variable bindings
421 snmp_varbind_list_sum(struct snmp_varbind_root
*root
)
423 struct snmp_varbind
*vb
;
432 /* encoded value lenght depends on type */
433 switch (vb
->value_type
)
435 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
):
436 sint_ptr
= (s32_t
*)vb
->value
;
437 snmp_asn1_enc_s32t_cnt(*sint_ptr
, &vb
->vlen
);
439 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_COUNTER
):
440 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_GAUGE
):
441 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
):
442 uint_ptr
= (u32_t
*)vb
->value
;
443 snmp_asn1_enc_u32t_cnt(*uint_ptr
, &vb
->vlen
);
445 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
):
446 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_NUL
):
447 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
):
448 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OPAQUE
):
449 vb
->vlen
= vb
->value_len
;
451 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
):
452 sint_ptr
= (s32_t
*)vb
->value
;
453 snmp_asn1_enc_oid_cnt(vb
->value_len
/ sizeof(s32_t
), sint_ptr
, &vb
->vlen
);
456 /* unsupported type */
460 /* encoding length of value length field */
461 snmp_asn1_enc_length_cnt(vb
->vlen
, &vb
->vlenlen
);
462 snmp_asn1_enc_oid_cnt(vb
->ident_len
, vb
->ident
, &vb
->olen
);
463 snmp_asn1_enc_length_cnt(vb
->olen
, &vb
->olenlen
);
465 vb
->seqlen
= 1 + vb
->vlenlen
+ vb
->vlen
;
466 vb
->seqlen
+= 1 + vb
->olenlen
+ vb
->olen
;
467 snmp_asn1_enc_length_cnt(vb
->seqlen
, &vb
->seqlenlen
);
470 tot_len
+= 1 + vb
->seqlenlen
+ vb
->seqlen
;
475 /* varbind-list seq */
476 root
->seqlen
= tot_len
;
477 snmp_asn1_enc_length_cnt(root
->seqlen
, &root
->seqlenlen
);
478 tot_len
+= 1 + root
->seqlenlen
;
484 * Encodes response header from head to tail.
487 snmp_resp_header_enc(struct snmp_msg_pstat
*m_stat
, struct pbuf
*p
)
492 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
494 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.seqlen
);
495 ofs
+= m_stat
->rhl
.seqlenlen
;
497 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
499 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.verlen
);
500 ofs
+= m_stat
->rhl
.verlenlen
;
501 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.verlen
, snmp_version
);
502 ofs
+= m_stat
->rhl
.verlen
;
504 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
));
506 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.comlen
);
507 ofs
+= m_stat
->rhl
.comlenlen
;
508 snmp_asn1_enc_raw(p
, ofs
, m_stat
->rhl
.comlen
, m_stat
->community
);
509 ofs
+= m_stat
->rhl
.comlen
;
511 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_CONTXT
| SNMP_ASN1_CONSTR
| SNMP_ASN1_PDU_GET_RESP
));
513 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.pdulen
);
514 ofs
+= m_stat
->rhl
.pdulenlen
;
516 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
518 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.ridlen
);
519 ofs
+= m_stat
->rhl
.ridlenlen
;
520 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.ridlen
, m_stat
->rid
);
521 ofs
+= m_stat
->rhl
.ridlen
;
523 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
525 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.errstatlen
);
526 ofs
+= m_stat
->rhl
.errstatlenlen
;
527 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.errstatlen
, m_stat
->error_status
);
528 ofs
+= m_stat
->rhl
.errstatlen
;
530 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
532 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.erridxlen
);
533 ofs
+= m_stat
->rhl
.erridxlenlen
;
534 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.erridxlen
, m_stat
->error_index
);
535 ofs
+= m_stat
->rhl
.erridxlen
;
541 * Encodes trap header from head to tail.
544 snmp_trap_header_enc(struct snmp_msg_trap
*m_trap
, struct pbuf
*p
)
549 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
551 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.seqlen
);
552 ofs
+= m_trap
->thl
.seqlenlen
;
554 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
556 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.verlen
);
557 ofs
+= m_trap
->thl
.verlenlen
;
558 snmp_asn1_enc_s32t(p
, ofs
, m_trap
->thl
.verlen
, snmp_version
);
559 ofs
+= m_trap
->thl
.verlen
;
561 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
));
563 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.comlen
);
564 ofs
+= m_trap
->thl
.comlenlen
;
565 snmp_asn1_enc_raw(p
, ofs
, m_trap
->thl
.comlen
, (u8_t
*)&snmp_publiccommunity
[0]);
566 ofs
+= m_trap
->thl
.comlen
;
568 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_CONTXT
| SNMP_ASN1_CONSTR
| SNMP_ASN1_PDU_TRAP
));
570 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.pdulen
);
571 ofs
+= m_trap
->thl
.pdulenlen
;
573 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
));
575 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.eidlen
);
576 ofs
+= m_trap
->thl
.eidlenlen
;
577 snmp_asn1_enc_oid(p
, ofs
, m_trap
->enterprise
->len
, &m_trap
->enterprise
->id
[0]);
578 ofs
+= m_trap
->thl
.eidlen
;
580 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
));
582 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.aaddrlen
);
583 ofs
+= m_trap
->thl
.aaddrlenlen
;
584 snmp_asn1_enc_raw(p
, ofs
, m_trap
->thl
.aaddrlen
, &m_trap
->sip_raw
[0]);
585 ofs
+= m_trap
->thl
.aaddrlen
;
587 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
589 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.gtrplen
);
590 ofs
+= m_trap
->thl
.gtrplenlen
;
591 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.gtrplen
, m_trap
->gen_trap
);
592 ofs
+= m_trap
->thl
.gtrplen
;
594 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
596 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.strplen
);
597 ofs
+= m_trap
->thl
.strplenlen
;
598 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.strplen
, m_trap
->spc_trap
);
599 ofs
+= m_trap
->thl
.strplen
;
601 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
));
603 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.tslen
);
604 ofs
+= m_trap
->thl
.tslenlen
;
605 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.tslen
, m_trap
->ts
);
606 ofs
+= m_trap
->thl
.tslen
;
612 * Encodes varbind list from head to tail.
615 snmp_varbind_list_enc(struct snmp_varbind_root
*root
, struct pbuf
*p
, u16_t ofs
)
617 struct snmp_varbind
*vb
;
622 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
624 snmp_asn1_enc_length(p
, ofs
, root
->seqlen
);
625 ofs
+= root
->seqlenlen
;
630 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
632 snmp_asn1_enc_length(p
, ofs
, vb
->seqlen
);
633 ofs
+= vb
->seqlenlen
;
635 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
));
637 snmp_asn1_enc_length(p
, ofs
, vb
->olen
);
639 snmp_asn1_enc_oid(p
, ofs
, vb
->ident_len
, &vb
->ident
[0]);
642 snmp_asn1_enc_type(p
, ofs
, vb
->value_type
);
644 snmp_asn1_enc_length(p
, ofs
, vb
->vlen
);
647 switch (vb
->value_type
)
649 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
):
650 sint_ptr
= (s32_t
*)vb
->value
;
651 snmp_asn1_enc_s32t(p
, ofs
, vb
->vlen
, *sint_ptr
);
653 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_COUNTER
):
654 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_GAUGE
):
655 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
):
656 uint_ptr
= (u32_t
*)vb
->value
;
657 snmp_asn1_enc_u32t(p
, ofs
, vb
->vlen
, *uint_ptr
);
659 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
):
660 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
):
661 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OPAQUE
):
662 raw_ptr
= (u8_t
*)vb
->value
;
663 snmp_asn1_enc_raw(p
, ofs
, vb
->vlen
, raw_ptr
);
665 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_NUL
):
667 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
):
668 sint_ptr
= (s32_t
*)vb
->value
;
669 snmp_asn1_enc_oid(p
, ofs
, vb
->value_len
/ sizeof(s32_t
), sint_ptr
);
672 /* unsupported type */
681 #endif /* LWIP_SNMP */