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