2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/lib/snmp.c,v 1.38 2004/08/06 08:46:53 brandt Exp $
33 #include <sys/types.h>
34 #include <sys/socket.h>
49 static void snmp_error_func(const char *, ...);
50 static void snmp_printf_func(const char *, ...);
52 void (*snmp_error)(const char *, ...) = snmp_error_func;
53 void (*snmp_printf)(const char *, ...) = snmp_printf_func;
56 * Get the next variable binding from the list.
57 * ASN errors on the sequence or the OID are always fatal.
60 get_var_binding(struct asn_buf *b, struct snmp_value *binding)
63 asn_len_t len, trailer;
66 if (asn_get_sequence(b, &len) != ASN_ERR_OK) {
67 snmp_error("cannot parse varbind header");
68 return (ASN_ERR_FAILED);
71 /* temporary truncate the length so that the parser does not
72 * eat up bytes behind the sequence in the case the encoding is
73 * wrong of inner elements. */
74 trailer = b->asn_len - len;
77 if (asn_get_objid(b, &binding->var) != ASN_ERR_OK) {
78 snmp_error("cannot parse binding objid");
79 return (ASN_ERR_FAILED);
81 if (asn_get_header(b, &type, &len) != ASN_ERR_OK) {
82 snmp_error("cannot parse binding value header");
83 return (ASN_ERR_FAILED);
89 binding->syntax = SNMP_SYNTAX_NULL;
90 err = asn_get_null_raw(b, len);
93 case ASN_TYPE_INTEGER:
94 binding->syntax = SNMP_SYNTAX_INTEGER;
95 err = asn_get_integer_raw(b, len, &binding->v.integer);
98 case ASN_TYPE_OCTETSTRING:
99 binding->syntax = SNMP_SYNTAX_OCTETSTRING;
100 binding->v.octetstring.octets = malloc(len);
101 if (binding->v.octetstring.octets == NULL) {
102 snmp_error("%s", strerror(errno));
103 return (ASN_ERR_FAILED);
105 binding->v.octetstring.len = len;
106 err = asn_get_octetstring_raw(b, len,
107 binding->v.octetstring.octets,
108 &binding->v.octetstring.len);
109 if (ASN_ERR_STOPPED(err)) {
110 free(binding->v.octetstring.octets);
111 binding->v.octetstring.octets = NULL;
116 binding->syntax = SNMP_SYNTAX_OID;
117 err = asn_get_objid_raw(b, len, &binding->v.oid);
120 case ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS:
121 binding->syntax = SNMP_SYNTAX_IPADDRESS;
122 err = asn_get_ipaddress_raw(b, len, binding->v.ipaddress);
125 case ASN_CLASS_APPLICATION|ASN_APP_TIMETICKS:
126 binding->syntax = SNMP_SYNTAX_TIMETICKS;
127 err = asn_get_uint32_raw(b, len, &binding->v.uint32);
130 case ASN_CLASS_APPLICATION|ASN_APP_COUNTER:
131 binding->syntax = SNMP_SYNTAX_COUNTER;
132 err = asn_get_uint32_raw(b, len, &binding->v.uint32);
135 case ASN_CLASS_APPLICATION|ASN_APP_GAUGE:
136 binding->syntax = SNMP_SYNTAX_GAUGE;
137 err = asn_get_uint32_raw(b, len, &binding->v.uint32);
140 case ASN_CLASS_APPLICATION|ASN_APP_COUNTER64:
141 binding->syntax = SNMP_SYNTAX_COUNTER64;
142 err = asn_get_counter64_raw(b, len, &binding->v.counter64);
145 case ASN_CLASS_CONTEXT | ASN_EXCEPT_NOSUCHOBJECT:
146 binding->syntax = SNMP_SYNTAX_NOSUCHOBJECT;
147 err = asn_get_null_raw(b, len);
150 case ASN_CLASS_CONTEXT | ASN_EXCEPT_NOSUCHINSTANCE:
151 binding->syntax = SNMP_SYNTAX_NOSUCHINSTANCE;
152 err = asn_get_null_raw(b, len);
155 case ASN_CLASS_CONTEXT | ASN_EXCEPT_ENDOFMIBVIEW:
156 binding->syntax = SNMP_SYNTAX_ENDOFMIBVIEW;
157 err = asn_get_null_raw(b, len);
161 if ((err = asn_skip(b, len)) == ASN_ERR_OK)
163 snmp_error("bad binding value type 0x%x", type);
167 if (ASN_ERR_STOPPED(err)) {
168 snmp_error("cannot parse binding value");
173 snmp_error("ignoring junk at end of binding");
175 b->asn_len = trailer;
181 * Parse the different PDUs contents. Any ASN error in the outer components
182 * are fatal. Only errors in variable values may be tolerated. If all
183 * components can be parsed it returns either ASN_ERR_OK or the first
184 * error that was found.
187 snmp_parse_pdus_hdr(struct asn_buf *b, struct snmp_pdu *pdu, asn_len_t *lenp)
189 if (pdu->type == SNMP_PDU_TRAP) {
190 if (asn_get_objid(b, &pdu->enterprise) != ASN_ERR_OK) {
191 snmp_error("cannot parse trap enterprise");
192 return (ASN_ERR_FAILED);
194 if (asn_get_ipaddress(b, pdu->agent_addr) != ASN_ERR_OK) {
195 snmp_error("cannot parse trap agent address");
196 return (ASN_ERR_FAILED);
198 if (asn_get_integer(b, &pdu->generic_trap) != ASN_ERR_OK) {
199 snmp_error("cannot parse 'generic-trap'");
200 return (ASN_ERR_FAILED);
202 if (asn_get_integer(b, &pdu->specific_trap) != ASN_ERR_OK) {
203 snmp_error("cannot parse 'specific-trap'");
204 return (ASN_ERR_FAILED);
206 if (asn_get_timeticks(b, &pdu->time_stamp) != ASN_ERR_OK) {
207 snmp_error("cannot parse trap 'time-stamp'");
208 return (ASN_ERR_FAILED);
211 if (asn_get_integer(b, &pdu->request_id) != ASN_ERR_OK) {
212 snmp_error("cannot parse 'request-id'");
213 return (ASN_ERR_FAILED);
215 if (asn_get_integer(b, &pdu->error_status) != ASN_ERR_OK) {
216 snmp_error("cannot parse 'error_status'");
217 return (ASN_ERR_FAILED);
219 if (asn_get_integer(b, &pdu->error_index) != ASN_ERR_OK) {
220 snmp_error("cannot parse 'error_index'");
221 return (ASN_ERR_FAILED);
225 if (asn_get_sequence(b, lenp) != ASN_ERR_OK) {
226 snmp_error("cannot get varlist header");
227 return (ASN_ERR_FAILED);
234 parse_pdus(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip)
236 asn_len_t len, trailer;
237 struct snmp_value *v;
238 enum asn_err err, err1;
240 err = snmp_parse_pdus_hdr(b, pdu, &len);
241 if (ASN_ERR_STOPPED(err))
244 trailer = b->asn_len - len;
248 while (b->asn_len != 0) {
249 if (pdu->nbindings == SNMP_MAX_BINDINGS) {
250 snmp_error("too many bindings (> %u) in PDU",
252 return (ASN_ERR_FAILED);
254 err1 = get_var_binding(b, v);
255 if (ASN_ERR_STOPPED(err1))
256 return (ASN_ERR_FAILED);
257 if (err1 != ASN_ERR_OK && err == ASN_ERR_OK) {
259 *ip = pdu->nbindings + 1;
265 b->asn_len = trailer;
271 * Parse the outer SEQUENCE value. ASN_ERR_TAG means 'bad version'.
274 snmp_parse_message_hdr(struct asn_buf *b, struct snmp_pdu *pdu, asn_len_t *lenp)
280 if (asn_get_integer(b, &version) != ASN_ERR_OK) {
281 snmp_error("cannot decode version");
282 return (ASN_ERR_FAILED);
286 pdu->version = SNMP_V1;
287 } else if (version == 1) {
288 pdu->version = SNMP_V2c;
290 pdu->version = SNMP_Verr;
291 snmp_error("unsupported SNMP version");
292 return (ASN_ERR_TAG);
295 comm_len = SNMP_COMMUNITY_MAXLEN;
296 if (asn_get_octetstring(b, (u_char *)pdu->community,
297 &comm_len) != ASN_ERR_OK) {
298 snmp_error("cannot decode community");
299 return (ASN_ERR_FAILED);
301 pdu->community[comm_len] = '\0';
303 if (asn_get_header(b, &type, lenp) != ASN_ERR_OK) {
304 snmp_error("cannot get pdu header");
305 return (ASN_ERR_FAILED);
307 if ((type & ~ASN_TYPE_MASK) !=
308 (ASN_TYPE_CONSTRUCTED | ASN_CLASS_CONTEXT)) {
309 snmp_error("bad pdu header tag");
310 return (ASN_ERR_FAILED);
312 pdu->type = type & ASN_TYPE_MASK;
317 case SNMP_PDU_GETNEXT:
318 case SNMP_PDU_RESPONSE:
323 if (pdu->version != SNMP_V1) {
324 snmp_error("bad pdu type %u", pdu->type);
325 return (ASN_ERR_FAILED);
329 case SNMP_PDU_GETBULK:
330 case SNMP_PDU_INFORM:
332 case SNMP_PDU_REPORT:
333 if (pdu->version == SNMP_V1) {
334 snmp_error("bad pdu type %u", pdu->type);
335 return (ASN_ERR_FAILED);
340 snmp_error("bad pdu type %u", pdu->type);
341 return (ASN_ERR_FAILED);
345 if (*lenp > b->asn_len) {
346 snmp_error("pdu length too long");
347 return (ASN_ERR_FAILED);
354 parse_message(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip)
357 asn_len_t len, trailer;
359 err = snmp_parse_message_hdr(b, pdu, &len);
360 if (ASN_ERR_STOPPED(err))
363 trailer = b->asn_len - len;
366 err = parse_pdus(b, pdu, ip);
367 if (ASN_ERR_STOPPED(err))
368 return (ASN_ERR_FAILED);
371 snmp_error("ignoring trailing junk after pdu");
373 b->asn_len = trailer;
379 * Decode the PDU except for the variable bindings itself.
380 * If decoding fails because of a bad binding, but the rest can be
381 * decoded, ip points to the index of the failed variable (errors
382 * OORANGE, BADLEN or BADVERS).
385 snmp_pdu_decode(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip)
389 memset(pdu, 0, sizeof(*pdu));
391 if (asn_get_sequence(b, &len) != ASN_ERR_OK) {
392 snmp_error("cannot decode pdu header");
393 return (SNMP_CODE_FAILED);
395 if (b->asn_len < len) {
396 snmp_error("outer sequence value too short");
397 return (SNMP_CODE_FAILED);
399 if (b->asn_len != len) {
400 snmp_error("ignoring trailing junk in message");
404 switch (parse_message(b, pdu, ip)) {
407 return (SNMP_CODE_OK);
412 return (SNMP_CODE_FAILED);
415 return (SNMP_CODE_BADLEN);
418 return (SNMP_CODE_OORANGE);
421 if (pdu->version == SNMP_Verr)
422 return (SNMP_CODE_BADVERS);
424 return (SNMP_CODE_BADENC);
427 return (SNMP_CODE_OK);
431 * Check whether what we have is the complete PDU by snooping at the
432 * enclosing structure header. This returns:
433 * -1 if there are ASN.1 errors
434 * 0 if we need more data
435 * > 0 the length of this PDU
438 snmp_pdu_snoop(const struct asn_buf *b0)
442 struct asn_buf b = *b0;
444 /* <0x10|0x20> <len> <data...> */
448 if (b.asn_cptr[0] != (ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED)) {
449 asn_error(&b, "bad sequence type %u", b.asn_cptr[0]);
458 if (*b.asn_cptr & 0x80) {
460 length = *b.asn_cptr++ & 0x7f;
463 asn_error(&b, "indefinite length not supported");
466 if (length > ASN_MAXLENLEN) {
467 asn_error(&b, "long length too long (%u)", length);
470 if (length > b.asn_len)
474 len = (len << 8) | *b.asn_cptr++;
485 return (len + b.asn_cptr - b0->asn_cptr);
489 * Encode the SNMP PDU without the variable bindings field.
490 * We do this the rather uneffective way by
491 * moving things around and assuming that the length field will never
492 * use more than 2 bytes.
493 * We need a number of pointers to apply the fixes afterwards.
496 snmp_pdu_encode_header(struct asn_buf *b, struct snmp_pdu *pdu)
500 if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED),
501 &pdu->outer_ptr) != ASN_ERR_OK)
502 return (SNMP_CODE_FAILED);
504 if (pdu->version == SNMP_V1)
505 err = asn_put_integer(b, 0);
506 else if (pdu->version == SNMP_V2c)
507 err = asn_put_integer(b, 1);
509 return (SNMP_CODE_BADVERS);
510 if (err != ASN_ERR_OK)
511 return (SNMP_CODE_FAILED);
513 if (asn_put_octetstring(b, (u_char *)pdu->community,
514 strlen(pdu->community)) != ASN_ERR_OK)
515 return (SNMP_CODE_FAILED);
517 if (asn_put_temp_header(b, (ASN_TYPE_CONSTRUCTED | ASN_CLASS_CONTEXT |
518 pdu->type), &pdu->pdu_ptr) != ASN_ERR_OK)
519 return (SNMP_CODE_FAILED);
521 if (pdu->type == SNMP_PDU_TRAP) {
522 if (pdu->version != SNMP_V1 ||
523 asn_put_objid(b, &pdu->enterprise) != ASN_ERR_OK ||
524 asn_put_ipaddress(b, pdu->agent_addr) != ASN_ERR_OK ||
525 asn_put_integer(b, pdu->generic_trap) != ASN_ERR_OK ||
526 asn_put_integer(b, pdu->specific_trap) != ASN_ERR_OK ||
527 asn_put_timeticks(b, pdu->time_stamp) != ASN_ERR_OK)
528 return (SNMP_CODE_FAILED);
530 if (pdu->version == SNMP_V1 && (pdu->type == SNMP_PDU_GETBULK ||
531 pdu->type == SNMP_PDU_INFORM ||
532 pdu->type == SNMP_PDU_TRAP2 ||
533 pdu->type == SNMP_PDU_REPORT))
534 return (SNMP_CODE_FAILED);
536 if (asn_put_integer(b, pdu->request_id) != ASN_ERR_OK ||
537 asn_put_integer(b, pdu->error_status) != ASN_ERR_OK ||
538 asn_put_integer(b, pdu->error_index) != ASN_ERR_OK)
539 return (SNMP_CODE_FAILED);
542 if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED),
543 &pdu->vars_ptr) != ASN_ERR_OK)
544 return (SNMP_CODE_FAILED);
546 return (SNMP_CODE_OK);
550 snmp_fix_encoding(struct asn_buf *b, const struct snmp_pdu *pdu)
552 if (asn_commit_header(b, pdu->vars_ptr) != ASN_ERR_OK ||
553 asn_commit_header(b, pdu->pdu_ptr) != ASN_ERR_OK ||
554 asn_commit_header(b, pdu->outer_ptr) != ASN_ERR_OK)
555 return (SNMP_CODE_FAILED);
556 return (SNMP_CODE_OK);
560 * Encode a binding. Caller must ensure, that the syntax is ok for that version.
561 * Be sure not to cobber b, when something fails.
564 snmp_binding_encode(struct asn_buf *b, const struct snmp_value *binding)
568 struct asn_buf save = *b;
570 if ((err = asn_put_temp_header(b, (ASN_TYPE_SEQUENCE |
571 ASN_TYPE_CONSTRUCTED), &ptr)) != ASN_ERR_OK) {
576 if ((err = asn_put_objid(b, &binding->var)) != ASN_ERR_OK) {
581 switch (binding->syntax) {
583 case SNMP_SYNTAX_NULL:
584 err = asn_put_null(b);
587 case SNMP_SYNTAX_INTEGER:
588 err = asn_put_integer(b, binding->v.integer);
591 case SNMP_SYNTAX_OCTETSTRING:
592 err = asn_put_octetstring(b, binding->v.octetstring.octets,
593 binding->v.octetstring.len);
596 case SNMP_SYNTAX_OID:
597 err = asn_put_objid(b, &binding->v.oid);
600 case SNMP_SYNTAX_IPADDRESS:
601 err = asn_put_ipaddress(b, binding->v.ipaddress);
604 case SNMP_SYNTAX_TIMETICKS:
605 err = asn_put_uint32(b, ASN_APP_TIMETICKS, binding->v.uint32);
608 case SNMP_SYNTAX_COUNTER:
609 err = asn_put_uint32(b, ASN_APP_COUNTER, binding->v.uint32);
612 case SNMP_SYNTAX_GAUGE:
613 err = asn_put_uint32(b, ASN_APP_GAUGE, binding->v.uint32);
616 case SNMP_SYNTAX_COUNTER64:
617 err = asn_put_counter64(b, binding->v.counter64);
620 case SNMP_SYNTAX_NOSUCHOBJECT:
621 err = asn_put_exception(b, ASN_EXCEPT_NOSUCHOBJECT);
624 case SNMP_SYNTAX_NOSUCHINSTANCE:
625 err = asn_put_exception(b, ASN_EXCEPT_NOSUCHINSTANCE);
628 case SNMP_SYNTAX_ENDOFMIBVIEW:
629 err = asn_put_exception(b, ASN_EXCEPT_ENDOFMIBVIEW);
633 if (err != ASN_ERR_OK) {
638 err = asn_commit_header(b, ptr);
639 if (err != ASN_ERR_OK) {
651 snmp_pdu_encode(struct snmp_pdu *pdu, struct asn_buf *resp_b)
656 if ((err = snmp_pdu_encode_header(resp_b, pdu)) != SNMP_CODE_OK)
658 for (idx = 0; idx < pdu->nbindings; idx++)
659 if ((err = snmp_binding_encode(resp_b, &pdu->bindings[idx]))
661 return (SNMP_CODE_FAILED);
663 return (snmp_fix_encoding(resp_b, pdu));
667 dump_binding(const struct snmp_value *b)
670 char buf[ASN_OIDSTRLEN];
672 snmp_printf("%s=", asn_oid2str_r(&b->var, buf));
675 case SNMP_SYNTAX_NULL:
679 case SNMP_SYNTAX_INTEGER:
680 snmp_printf("INTEGER %d", b->v.integer);
683 case SNMP_SYNTAX_OCTETSTRING:
684 snmp_printf("OCTET STRING %lu:", b->v.octetstring.len);
685 for (i = 0; i < b->v.octetstring.len; i++)
686 snmp_printf(" %02x", b->v.octetstring.octets[i]);
689 case SNMP_SYNTAX_OID:
690 snmp_printf("OID %s", asn_oid2str_r(&b->v.oid, buf));
693 case SNMP_SYNTAX_IPADDRESS:
694 snmp_printf("IPADDRESS %u.%u.%u.%u", b->v.ipaddress[0],
695 b->v.ipaddress[1], b->v.ipaddress[2], b->v.ipaddress[3]);
698 case SNMP_SYNTAX_COUNTER:
699 snmp_printf("COUNTER %u", b->v.uint32);
702 case SNMP_SYNTAX_GAUGE:
703 snmp_printf("GAUGE %u", b->v.uint32);
706 case SNMP_SYNTAX_TIMETICKS:
707 snmp_printf("TIMETICKS %u", b->v.uint32);
710 case SNMP_SYNTAX_COUNTER64:
711 snmp_printf("COUNTER64 %lld", b->v.counter64);
714 case SNMP_SYNTAX_NOSUCHOBJECT:
715 snmp_printf("NoSuchObject");
718 case SNMP_SYNTAX_NOSUCHINSTANCE:
719 snmp_printf("NoSuchInstance");
722 case SNMP_SYNTAX_ENDOFMIBVIEW:
723 snmp_printf("EndOfMibView");
727 snmp_printf("UNKNOWN SYNTAX %u", b->syntax);
733 dump_bindings(const struct snmp_pdu *pdu)
737 for (i = 0; i < pdu->nbindings; i++) {
738 snmp_printf(" [%u]: ", i);
739 dump_binding(&pdu->bindings[i]);
745 dump_notrap(const struct snmp_pdu *pdu)
747 snmp_printf(" request_id=%d", pdu->request_id);
748 snmp_printf(" error_status=%d", pdu->error_status);
749 snmp_printf(" error_index=%d\n", pdu->error_index);
754 snmp_pdu_dump(const struct snmp_pdu *pdu)
756 char buf[ASN_OIDSTRLEN];
758 static const char *types[] = {
759 [SNMP_PDU_GET] = "GET",
760 [SNMP_PDU_GETNEXT] = "GETNEXT",
761 [SNMP_PDU_RESPONSE] = "RESPONSE",
762 [SNMP_PDU_SET] = "SET",
763 [SNMP_PDU_TRAP] = "TRAPv1",
764 [SNMP_PDU_GETBULK] = "GETBULK",
765 [SNMP_PDU_INFORM] = "INFORM",
766 [SNMP_PDU_TRAP2] = "TRAPv2",
767 [SNMP_PDU_REPORT] = "REPORT",
770 if (pdu->version == SNMP_V1)
772 else if (pdu->version == SNMP_V2c)
779 snmp_printf("%s %s '%s'", types[pdu->type], vers, pdu->community);
780 snmp_printf(" enterprise=%s", asn_oid2str_r(&pdu->enterprise, buf));
781 snmp_printf(" agent_addr=%u.%u.%u.%u", pdu->agent_addr[0],
782 pdu->agent_addr[1], pdu->agent_addr[2], pdu->agent_addr[3]);
783 snmp_printf(" generic_trap=%d", pdu->generic_trap);
784 snmp_printf(" specific_trap=%d", pdu->specific_trap);
785 snmp_printf(" time-stamp=%u\n", pdu->time_stamp);
790 case SNMP_PDU_GETNEXT:
791 case SNMP_PDU_RESPONSE:
793 case SNMP_PDU_GETBULK:
794 case SNMP_PDU_INFORM:
796 case SNMP_PDU_REPORT:
797 snmp_printf("%s %s '%s'", types[pdu->type], vers, pdu->community);
802 snmp_printf("bad pdu type %u\n", pdu->type);
808 snmp_value_free(struct snmp_value *value)
810 if (value->syntax == SNMP_SYNTAX_OCTETSTRING)
811 free(value->v.octetstring.octets);
812 value->syntax = SNMP_SYNTAX_NULL;
816 snmp_value_copy(struct snmp_value *to, const struct snmp_value *from)
819 to->syntax = from->syntax;
821 if (from->syntax == SNMP_SYNTAX_OCTETSTRING) {
822 if ((to->v.octetstring.len = from->v.octetstring.len) == 0)
823 to->v.octetstring.octets = NULL;
825 to->v.octetstring.octets = malloc(to->v.octetstring.len);
826 if (to->v.octetstring.octets == NULL)
828 (void)memcpy(to->v.octetstring.octets,
829 from->v.octetstring.octets, to->v.octetstring.len);
837 snmp_pdu_free(struct snmp_pdu *pdu)
841 for (i = 0; i < pdu->nbindings; i++)
842 snmp_value_free(&pdu->bindings[i]);
846 * Parse an ASCII SNMP value into the binary form
849 snmp_value_parse(const char *str, enum snmp_syntax syntax, union snmp_values *v)
855 case SNMP_SYNTAX_NULL:
856 case SNMP_SYNTAX_NOSUCHOBJECT:
857 case SNMP_SYNTAX_NOSUCHINSTANCE:
858 case SNMP_SYNTAX_ENDOFMIBVIEW:
863 case SNMP_SYNTAX_INTEGER:
864 v->integer = strtoll(str, &end, 0);
869 case SNMP_SYNTAX_OCTETSTRING:
871 u_long len; /* actual length of string */
872 u_long alloc; /* allocate length of string */
873 u_char *octs; /* actual octets */
874 u_long oct; /* actual octet */
875 u_char *nocts; /* to avoid memory leak */
876 u_char c; /* actual character */
879 if (alloc == len) { \
881 if ((nocts = realloc(octs, alloc)) == NULL) { \
894 while((c = *str++) != '\0') {
903 switch (c = *str++) {
934 else if (isupper(*str))
935 c = *str++ - 'A' + 10;
937 c = *str++ - 'a' + 10;
942 else if (isupper(*str))
943 c += *str++ - 'A' + 10;
945 c += *str++ - 'a' + 10;
947 case '0': case '1': case '2':
948 case '3': case '4': case '5':
951 if (*str < '0' || *str > '7')
954 if (*str < '0' || *str > '7')
965 while (*str != '\0') {
966 oct = strtoul(str, &end, 16);
975 else if(*str != '\0') {
981 v->octetstring.octets = octs;
982 v->octetstring.len = len;
987 case SNMP_SYNTAX_OID:
994 if (v->oid.len == ASN_MAXOIDLEN)
996 subid = strtoul(str, &end, 10);
998 if (subid > ASN_MAXID)
1000 v->oid.subs[v->oid.len++] = (asn_subid_t)subid;
1010 case SNMP_SYNTAX_IPADDRESS:
1016 if (sscanf(str, "%lu.%lu.%lu.%lu%n", &ip[0], &ip[1], &ip[2],
1017 &ip[3], &n) == 4 && (size_t)n == strlen(str) &&
1018 ip[0] <= 0xff && ip[1] <= 0xff &&
1019 ip[2] <= 0xff && ip[3] <= 0xff) {
1020 v->ipaddress[0] = (u_char)ip[0];
1021 v->ipaddress[1] = (u_char)ip[1];
1022 v->ipaddress[2] = (u_char)ip[2];
1023 v->ipaddress[3] = (u_char)ip[3];
1027 if ((he = gethostbyname(str)) == NULL)
1029 if (he->h_addrtype != AF_INET)
1032 v->ipaddress[0] = he->h_addr[0];
1033 v->ipaddress[1] = he->h_addr[1];
1034 v->ipaddress[2] = he->h_addr[2];
1035 v->ipaddress[3] = he->h_addr[3];
1039 case SNMP_SYNTAX_COUNTER:
1040 case SNMP_SYNTAX_GAUGE:
1041 case SNMP_SYNTAX_TIMETICKS:
1045 sub = strtoull(str, &end, 0);
1046 if (*end != '\0' || sub > 0xffffffff)
1048 v->uint32 = (uint32_t)sub;
1052 case SNMP_SYNTAX_COUNTER64:
1053 v->counter64 = strtoull(str, &end, 0);
1062 snmp_error_func(const char *fmt, ...)
1067 fprintf(stderr, "SNMP: ");
1068 vfprintf(stderr, fmt, ap);
1069 fprintf(stderr, "\n");
1074 snmp_printf_func(const char *fmt, ...)
1079 vfprintf(stderr, fmt, ap);