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>
50 #include "lwip/netif.h"
52 #include "lwip/snmp.h"
53 #include "lwip/snmp_asn1.h"
54 #include "lwip/snmp_msg.h"
58 /* destination IP address in network order */
60 /* set to 0 when disabled, >0 when enabled */
63 #if (SNMP_TRAP_DESTINATIONS == 0)
64 #error "need at least one trap destination"
66 struct snmp_trap_dst trap_dst
[SNMP_TRAP_DESTINATIONS
];
68 /** TRAP message structure */
69 struct snmp_msg_trap trap_msg
;
71 static u16_t
snmp_resp_header_sum(struct snmp_msg_pstat
*m_stat
, u16_t vb_len
);
72 static u16_t
snmp_trap_header_sum(struct snmp_msg_trap
*m_trap
, u16_t vb_len
);
73 static u16_t
snmp_varbind_list_sum(struct snmp_varbind_root
*root
);
75 static u16_t
snmp_resp_header_enc(struct snmp_msg_pstat
*m_stat
, struct pbuf
*p
);
76 static u16_t
snmp_trap_header_enc(struct snmp_msg_trap
*m_trap
, struct pbuf
*p
);
77 static u16_t
snmp_varbind_list_enc(struct snmp_varbind_root
*root
, struct pbuf
*p
, u16_t ofs
);
80 * Sets enable switch for this trap destination.
81 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
82 * @param enable switch if 0 destination is disabled >0 enabled.
85 snmp_trap_dst_enable(u8_t dst_idx
, u8_t enable
)
87 if (dst_idx
< SNMP_TRAP_DESTINATIONS
)
89 trap_dst
[dst_idx
].enable
= enable
;
94 * Sets IPv4 address for this trap destination.
95 * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
96 * @param dst IPv4 address in host order.
99 snmp_trap_dst_ip_set(u8_t dst_idx
, struct ip_addr
*dst
)
101 if (dst_idx
< SNMP_TRAP_DESTINATIONS
)
103 trap_dst
[dst_idx
].dip
.addr
= htonl(dst
->addr
);
108 * Sends a 'getresponse' message to the request originator.
110 * @param m_stat points to the current message request state source
111 * @return ERR_OK when success, ERR_MEM if we're out of memory
113 * @note the caller is responsible for filling in outvb in the m_stat
114 * and provide error-status and index (except for tooBig errors) ...
117 snmp_send_response(struct snmp_msg_pstat
*m_stat
)
119 struct snmp_varbind_root emptyvb
= {NULL
, NULL
, 0, 0, 0};
124 /* pass 0, calculate length fields */
125 tot_len
= snmp_varbind_list_sum(&m_stat
->outvb
);
126 tot_len
= snmp_resp_header_sum(m_stat
, tot_len
);
128 /* try allocating pbuf(s) for complete response */
129 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
132 LWIP_DEBUGF(SNMP_MSG_DEBUG
, ("snmp_snd_response() tooBig\n"));
134 /* can't construct reply, return error-status tooBig */
135 m_stat
->error_status
= SNMP_ES_TOOBIG
;
136 m_stat
->error_index
= 0;
137 /* pass 0, recalculate lengths, for empty varbind-list */
138 tot_len
= snmp_varbind_list_sum(&emptyvb
);
139 tot_len
= snmp_resp_header_sum(m_stat
, tot_len
);
140 /* retry allocation once for header and empty varbind-list */
141 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
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 if (m_stat
->error_status
== SNMP_ES_TOOBIG
)
154 snmp_varbind_list_enc(&emptyvb
, p
, ofs
);
158 snmp_varbind_list_enc(&m_stat
->outvb
, p
, ofs
);
161 switch (m_stat
->error_status
)
164 snmp_inc_snmpouttoobigs();
166 case SNMP_ES_NOSUCHNAME
:
167 snmp_inc_snmpoutnosuchnames();
169 case SNMP_ES_BADVALUE
:
170 snmp_inc_snmpoutbadvalues();
172 case SNMP_ES_GENERROR
:
173 snmp_inc_snmpoutgenerrs();
176 snmp_inc_snmpoutgetresponses();
177 snmp_inc_snmpoutpkts();
179 /** @todo do we need separate rx and tx pcbs for threaded case? */
180 /** connect to the originating source */
181 udp_connect(m_stat
->pcb
, &m_stat
->sip
, m_stat
->sp
);
182 err
= udp_send(m_stat
->pcb
, p
);
185 /** @todo release some memory, retry and return tooBig? tooMuchHassle? */
192 /** disassociate remote address and port with this pcb */
193 udp_disconnect(m_stat
->pcb
);
196 LWIP_DEBUGF(SNMP_MSG_DEBUG
, ("snmp_snd_response() done\n"));
201 /* first pbuf alloc try or retry alloc failed
202 very low on memory, couldn't return tooBig */
209 * Sends an generic or enterprise specific trap message.
211 * @param generic_trap is the trap code
212 * @param eoid points to enterprise object identifier
213 * @param specific_trap used for enterprise traps when generic_trap == 6
214 * @return ERR_OK when success, ERR_MEM if we're out of memory
216 * @note the caller is responsible for filling in outvb in the trap_msg
217 * @note the use of the enterpise identifier field
219 * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
220 * and .iso.org.dod.internet.private.enterprises.yourenterprise
221 * (sysObjectID) for specific traps.
224 snmp_send_trap(s8_t generic_trap
, struct snmp_obj_id
*eoid
, s32_t specific_trap
)
226 struct snmp_trap_dst
*td
;
227 struct netif
*dst_if
;
228 struct ip_addr dst_ip
;
232 for (i
=0, td
= &trap_dst
[0]; i
<SNMP_TRAP_DESTINATIONS
; i
++, td
++)
234 if ((td
->enable
!= 0) && (td
->dip
.addr
!= 0))
236 /* network order trap destination */
237 trap_msg
.dip
.addr
= td
->dip
.addr
;
238 /* lookup current source address for this dst */
239 dst_if
= ip_route(&td
->dip
);
240 dst_ip
.addr
= ntohl(dst_if
->ip_addr
.addr
);
241 trap_msg
.sip_raw
[0] = dst_ip
.addr
>> 24;
242 trap_msg
.sip_raw
[1] = dst_ip
.addr
>> 16;
243 trap_msg
.sip_raw
[2] = dst_ip
.addr
>> 8;
244 trap_msg
.sip_raw
[3] = dst_ip
.addr
;
245 trap_msg
.gen_trap
= generic_trap
;
246 trap_msg
.spc_trap
= specific_trap
;
247 if (generic_trap
== SNMP_GENTRAP_ENTERPRISESPC
)
249 /* enterprise-Specific trap */
250 trap_msg
.enterprise
= eoid
;
254 /* generic (MIB-II) trap */
255 snmp_get_snmpgrpid_ptr(&trap_msg
.enterprise
);
257 snmp_get_sysuptime(&trap_msg
.ts
);
259 /* pass 0, calculate length fields */
260 tot_len
= snmp_varbind_list_sum(&trap_msg
.outvb
);
261 tot_len
= snmp_trap_header_sum(&trap_msg
, tot_len
);
263 /* allocate pbuf(s) */
264 p
= pbuf_alloc(PBUF_TRANSPORT
, tot_len
, PBUF_POOL
);
269 /* pass 1, encode packet ino the pbuf(s) */
270 ofs
= snmp_trap_header_enc(&trap_msg
, p
);
271 snmp_varbind_list_enc(&trap_msg
.outvb
, p
, ofs
);
273 snmp_inc_snmpouttraps();
274 snmp_inc_snmpoutpkts();
276 /** connect to the TRAP destination */
277 udp_connect(trap_msg
.pcb
, &trap_msg
.dip
, SNMP_TRAP_PORT
);
278 udp_send(trap_msg
.pcb
, p
);
279 /** disassociate remote address and port with this pcb */
280 udp_disconnect(trap_msg
.pcb
);
294 snmp_coldstart_trap(void)
296 trap_msg
.outvb
.head
= NULL
;
297 trap_msg
.outvb
.tail
= NULL
;
298 trap_msg
.outvb
.count
= 0;
299 snmp_send_trap(SNMP_GENTRAP_COLDSTART
, NULL
, 0);
303 snmp_authfail_trap(void)
306 snmp_get_snmpenableauthentraps(&enable
);
309 trap_msg
.outvb
.head
= NULL
;
310 trap_msg
.outvb
.tail
= NULL
;
311 trap_msg
.outvb
.count
= 0;
312 snmp_send_trap(SNMP_GENTRAP_AUTHFAIL
, NULL
, 0);
317 * Sums response header field lengths from tail to head and
318 * returns resp_header_lengths for second encoding pass.
320 * @param vb_len varbind-list length
321 * @param rhl points to returned header lengths
322 * @return the required lenght for encoding the response header
325 snmp_resp_header_sum(struct snmp_msg_pstat
*m_stat
, u16_t vb_len
)
328 struct snmp_resp_header_lengths
*rhl
;
332 snmp_asn1_enc_s32t_cnt(m_stat
->error_index
, &rhl
->erridxlen
);
333 snmp_asn1_enc_length_cnt(rhl
->erridxlen
, &rhl
->erridxlenlen
);
334 tot_len
+= 1 + rhl
->erridxlenlen
+ rhl
->erridxlen
;
336 snmp_asn1_enc_s32t_cnt(m_stat
->error_status
, &rhl
->errstatlen
);
337 snmp_asn1_enc_length_cnt(rhl
->errstatlen
, &rhl
->errstatlenlen
);
338 tot_len
+= 1 + rhl
->errstatlenlen
+ rhl
->errstatlen
;
340 snmp_asn1_enc_s32t_cnt(m_stat
->rid
, &rhl
->ridlen
);
341 snmp_asn1_enc_length_cnt(rhl
->ridlen
, &rhl
->ridlenlen
);
342 tot_len
+= 1 + rhl
->ridlenlen
+ rhl
->ridlen
;
344 rhl
->pdulen
= tot_len
;
345 snmp_asn1_enc_length_cnt(rhl
->pdulen
, &rhl
->pdulenlen
);
346 tot_len
+= 1 + rhl
->pdulenlen
;
348 rhl
->comlen
= m_stat
->com_strlen
;
349 snmp_asn1_enc_length_cnt(rhl
->comlen
, &rhl
->comlenlen
);
350 tot_len
+= 1 + rhl
->comlenlen
+ rhl
->comlen
;
352 snmp_asn1_enc_s32t_cnt(snmp_version
, &rhl
->verlen
);
353 snmp_asn1_enc_length_cnt(rhl
->verlen
, &rhl
->verlenlen
);
354 tot_len
+= 1 + rhl
->verlen
+ rhl
->verlenlen
;
356 rhl
->seqlen
= tot_len
;
357 snmp_asn1_enc_length_cnt(rhl
->seqlen
, &rhl
->seqlenlen
);
358 tot_len
+= 1 + rhl
->seqlenlen
;
364 * Sums trap header field lengths from tail to head and
365 * returns trap_header_lengths for second encoding pass.
367 * @param vb_len varbind-list length
368 * @param thl points to returned header lengths
369 * @return the required lenght for encoding the trap header
372 snmp_trap_header_sum(struct snmp_msg_trap
*m_trap
, u16_t vb_len
)
375 struct snmp_trap_header_lengths
*thl
;
380 snmp_asn1_enc_u32t_cnt(m_trap
->ts
, &thl
->tslen
);
381 snmp_asn1_enc_length_cnt(thl
->tslen
, &thl
->tslenlen
);
382 tot_len
+= 1 + thl
->tslen
+ thl
->tslenlen
;
384 snmp_asn1_enc_s32t_cnt(m_trap
->spc_trap
, &thl
->strplen
);
385 snmp_asn1_enc_length_cnt(thl
->strplen
, &thl
->strplenlen
);
386 tot_len
+= 1 + thl
->strplen
+ thl
->strplenlen
;
388 snmp_asn1_enc_s32t_cnt(m_trap
->gen_trap
, &thl
->gtrplen
);
389 snmp_asn1_enc_length_cnt(thl
->gtrplen
, &thl
->gtrplenlen
);
390 tot_len
+= 1 + thl
->gtrplen
+ thl
->gtrplenlen
;
393 snmp_asn1_enc_length_cnt(thl
->aaddrlen
, &thl
->aaddrlenlen
);
394 tot_len
+= 1 + thl
->aaddrlen
+ thl
->aaddrlenlen
;
396 snmp_asn1_enc_oid_cnt(m_trap
->enterprise
->len
, &m_trap
->enterprise
->id
[0], &thl
->eidlen
);
397 snmp_asn1_enc_length_cnt(thl
->eidlen
, &thl
->eidlenlen
);
398 tot_len
+= 1 + thl
->eidlen
+ thl
->eidlenlen
;
400 thl
->pdulen
= tot_len
;
401 snmp_asn1_enc_length_cnt(thl
->pdulen
, &thl
->pdulenlen
);
402 tot_len
+= 1 + thl
->pdulenlen
;
404 thl
->comlen
= sizeof(snmp_publiccommunity
) - 1;
405 snmp_asn1_enc_length_cnt(thl
->comlen
, &thl
->comlenlen
);
406 tot_len
+= 1 + thl
->comlenlen
+ thl
->comlen
;
408 snmp_asn1_enc_s32t_cnt(snmp_version
, &thl
->verlen
);
409 snmp_asn1_enc_length_cnt(thl
->verlen
, &thl
->verlenlen
);
410 tot_len
+= 1 + thl
->verlen
+ thl
->verlenlen
;
412 thl
->seqlen
= tot_len
;
413 snmp_asn1_enc_length_cnt(thl
->seqlen
, &thl
->seqlenlen
);
414 tot_len
+= 1 + thl
->seqlenlen
;
420 * Sums varbind lengths from tail to head and
421 * annotates lengths in varbind for second encoding pass.
423 * @param root points to the root of the variable binding list
424 * @return the required lenght for encoding the variable bindings
427 snmp_varbind_list_sum(struct snmp_varbind_root
*root
)
429 struct snmp_varbind
*vb
;
438 /* encoded value lenght depends on type */
439 switch (vb
->value_type
)
441 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
):
442 sint_ptr
= vb
->value
;
443 snmp_asn1_enc_s32t_cnt(*sint_ptr
, &vb
->vlen
);
445 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_COUNTER
):
446 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_GAUGE
):
447 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
):
448 uint_ptr
= vb
->value
;
449 snmp_asn1_enc_u32t_cnt(*uint_ptr
, &vb
->vlen
);
451 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
):
452 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_NUL
):
453 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
):
454 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OPAQUE
):
455 vb
->vlen
= vb
->value_len
;
457 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
):
458 sint_ptr
= vb
->value
;
459 snmp_asn1_enc_oid_cnt(vb
->value_len
/ sizeof(s32_t
), sint_ptr
, &vb
->vlen
);
462 /* unsupported type */
466 /* encoding length of value length field */
467 snmp_asn1_enc_length_cnt(vb
->vlen
, &vb
->vlenlen
);
468 snmp_asn1_enc_oid_cnt(vb
->ident_len
, vb
->ident
, &vb
->olen
);
469 snmp_asn1_enc_length_cnt(vb
->olen
, &vb
->olenlen
);
471 vb
->seqlen
= 1 + vb
->vlenlen
+ vb
->vlen
;
472 vb
->seqlen
+= 1 + vb
->olenlen
+ vb
->olen
;
473 snmp_asn1_enc_length_cnt(vb
->seqlen
, &vb
->seqlenlen
);
476 tot_len
+= 1 + vb
->seqlenlen
+ vb
->seqlen
;
481 /* varbind-list seq */
482 root
->seqlen
= tot_len
;
483 snmp_asn1_enc_length_cnt(root
->seqlen
, &root
->seqlenlen
);
484 tot_len
+= 1 + root
->seqlenlen
;
490 * Encodes response header from head to tail.
493 snmp_resp_header_enc(struct snmp_msg_pstat
*m_stat
, struct pbuf
*p
)
498 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
500 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.seqlen
);
501 ofs
+= m_stat
->rhl
.seqlenlen
;
503 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
505 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.verlen
);
506 ofs
+= m_stat
->rhl
.verlenlen
;
507 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.verlen
, snmp_version
);
508 ofs
+= m_stat
->rhl
.verlen
;
510 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
));
512 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.comlen
);
513 ofs
+= m_stat
->rhl
.comlenlen
;
514 snmp_asn1_enc_raw(p
, ofs
, m_stat
->rhl
.comlen
, m_stat
->community
);
515 ofs
+= m_stat
->rhl
.comlen
;
517 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_CONTXT
| SNMP_ASN1_CONSTR
| SNMP_ASN1_PDU_GET_RESP
));
519 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.pdulen
);
520 ofs
+= m_stat
->rhl
.pdulenlen
;
522 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
524 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.ridlen
);
525 ofs
+= m_stat
->rhl
.ridlenlen
;
526 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.ridlen
, m_stat
->rid
);
527 ofs
+= m_stat
->rhl
.ridlen
;
529 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
531 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.errstatlen
);
532 ofs
+= m_stat
->rhl
.errstatlenlen
;
533 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.errstatlen
, m_stat
->error_status
);
534 ofs
+= m_stat
->rhl
.errstatlen
;
536 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
538 snmp_asn1_enc_length(p
, ofs
, m_stat
->rhl
.erridxlen
);
539 ofs
+= m_stat
->rhl
.erridxlenlen
;
540 snmp_asn1_enc_s32t(p
, ofs
, m_stat
->rhl
.erridxlen
, m_stat
->error_index
);
541 ofs
+= m_stat
->rhl
.erridxlen
;
547 * Encodes trap header from head to tail.
550 snmp_trap_header_enc(struct snmp_msg_trap
*m_trap
, struct pbuf
*p
)
555 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
557 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.seqlen
);
558 ofs
+= m_trap
->thl
.seqlenlen
;
560 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
562 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.verlen
);
563 ofs
+= m_trap
->thl
.verlenlen
;
564 snmp_asn1_enc_s32t(p
, ofs
, m_trap
->thl
.verlen
, snmp_version
);
565 ofs
+= m_trap
->thl
.verlen
;
567 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
));
569 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.comlen
);
570 ofs
+= m_trap
->thl
.comlenlen
;
571 snmp_asn1_enc_raw(p
, ofs
, m_trap
->thl
.comlen
, (u8_t
*)&snmp_publiccommunity
[0]);
572 ofs
+= m_trap
->thl
.comlen
;
574 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_CONTXT
| SNMP_ASN1_CONSTR
| SNMP_ASN1_PDU_TRAP
));
576 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.pdulen
);
577 ofs
+= m_trap
->thl
.pdulenlen
;
579 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
));
581 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.eidlen
);
582 ofs
+= m_trap
->thl
.eidlenlen
;
583 snmp_asn1_enc_oid(p
, ofs
, m_trap
->enterprise
->len
, &m_trap
->enterprise
->id
[0]);
584 ofs
+= m_trap
->thl
.eidlen
;
586 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
));
588 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.aaddrlen
);
589 ofs
+= m_trap
->thl
.aaddrlenlen
;
590 snmp_asn1_enc_raw(p
, ofs
, m_trap
->thl
.aaddrlen
, &m_trap
->sip_raw
[0]);
591 ofs
+= m_trap
->thl
.aaddrlen
;
593 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
595 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.gtrplen
);
596 ofs
+= m_trap
->thl
.gtrplenlen
;
597 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.gtrplen
, m_trap
->gen_trap
);
598 ofs
+= m_trap
->thl
.gtrplen
;
600 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
));
602 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.strplen
);
603 ofs
+= m_trap
->thl
.strplenlen
;
604 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.strplen
, m_trap
->spc_trap
);
605 ofs
+= m_trap
->thl
.strplen
;
607 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
));
609 snmp_asn1_enc_length(p
, ofs
, m_trap
->thl
.tslen
);
610 ofs
+= m_trap
->thl
.tslenlen
;
611 snmp_asn1_enc_u32t(p
, ofs
, m_trap
->thl
.tslen
, m_trap
->ts
);
612 ofs
+= m_trap
->thl
.tslen
;
618 * Encodes varbind list from head to tail.
621 snmp_varbind_list_enc(struct snmp_varbind_root
*root
, struct pbuf
*p
, u16_t ofs
)
623 struct snmp_varbind
*vb
;
628 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
630 snmp_asn1_enc_length(p
, ofs
, root
->seqlen
);
631 ofs
+= root
->seqlenlen
;
636 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_CONSTR
| SNMP_ASN1_SEQ
));
638 snmp_asn1_enc_length(p
, ofs
, vb
->seqlen
);
639 ofs
+= vb
->seqlenlen
;
641 snmp_asn1_enc_type(p
, ofs
, (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
));
643 snmp_asn1_enc_length(p
, ofs
, vb
->olen
);
645 snmp_asn1_enc_oid(p
, ofs
, vb
->ident_len
, &vb
->ident
[0]);
648 snmp_asn1_enc_type(p
, ofs
, vb
->value_type
);
650 snmp_asn1_enc_length(p
, ofs
, vb
->vlen
);
653 switch (vb
->value_type
)
655 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_INTEG
):
656 sint_ptr
= vb
->value
;
657 snmp_asn1_enc_s32t(p
, ofs
, vb
->vlen
, *sint_ptr
);
659 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_COUNTER
):
660 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_GAUGE
):
661 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_TIMETICKS
):
662 uint_ptr
= vb
->value
;
663 snmp_asn1_enc_u32t(p
, ofs
, vb
->vlen
, *uint_ptr
);
665 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OC_STR
):
666 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_IPADDR
):
667 case (SNMP_ASN1_APPLIC
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OPAQUE
):
669 snmp_asn1_enc_raw(p
, ofs
, vb
->vlen
, raw_ptr
);
671 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_NUL
):
673 case (SNMP_ASN1_UNIV
| SNMP_ASN1_PRIMIT
| SNMP_ASN1_OBJ_ID
):
674 sint_ptr
= vb
->value
;
675 snmp_asn1_enc_oid(p
, ofs
, vb
->value_len
/ sizeof(s32_t
), sint_ptr
);
678 /* unsupported type */
687 #endif /* LWIP_SNMP */