2 * Copyright (c) 2006 The FreeBSD Project
5 * Author: Shteryana Shopova <syrinx@FreeBSD.org>
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
9 * the following conditions are met:
11 * 1. Redistributions of source code or documentation must retain the above
12 * copyright 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 THE 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 THE 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 * Textual conventions for OctetStrings
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
49 #include <arpa/inet.h>
50 #include <netinet/in.h>
52 #include <bsnmp/asn1.h>
53 #include <bsnmp/snmp.h>
55 #include "bsnmptools.h"
57 /* OctetString, DisplayString */
58 static char *snmp_oct2str(uint32_t, char *, char *);
59 static char *snmp_str2asn_oid(char *, struct asn_oid *);
60 static int parse_octetstring(struct snmp_value *, char *);
63 static char *snmp_octstr2date(uint32_t, char *, char *);
64 static char *snmp_date2asn_oid(char * , struct asn_oid *);
65 static int parse_dateandtime(struct snmp_value *, char *);
68 static char *snmp_oct2physAddr(uint32_t, char *, char *);
69 static char *snmp_addr2asn_oid(char *, struct asn_oid *);
70 static int parse_physaddress(struct snmp_value *, char *);
73 static char *snmp_oct2ntp_ts(uint32_t, char *, char *);
74 static char *snmp_ntp_ts2asn_oid(char *, struct asn_oid *);
75 static int parse_ntp_ts(struct snmp_value *, char *);
78 static char *snmp_oct2bridgeid(uint32_t, char *, char *);
79 static char *snmp_bridgeid2oct(char *, struct asn_oid *);
80 static int parse_bridge_id(struct snmp_value *, char *);
83 static char *snmp_oct2bport_id(uint32_t, char *, char *);
84 static char *snmp_bport_id2oct(char *, struct asn_oid *);
85 static int parse_bport_id(struct snmp_value *, char *);
88 static char *snmp_oct2inetaddr(uint32_t len, char *octets, char *buf);
89 static char *snmp_inetaddr2oct(char *str, struct asn_oid *oid);
90 static int32_t parse_inetaddr(struct snmp_value *value, char *string);
92 static char *snmp_oct2bits(uint32_t len, char *octets, char *buf);
93 static char *snmp_bits2oct(char *str, struct asn_oid *oid);
94 static int32_t parse_bits(struct snmp_value *value, char *string);
96 static struct snmp_text_conv {
100 snmp_oct2tc_f oct2tc;
101 snmp_tc2oid_f tc2oid;
102 snmp_tc2oct_f tc2oct;
104 { SNMP_STRING, "OctetString", SNMP_VAR_STRSZ,
105 snmp_oct2str, snmp_str2asn_oid, parse_octetstring },
107 { SNMP_DISPLAYSTRING, "DisplayString" , SNMP_VAR_STRSZ,
108 snmp_oct2str, snmp_str2asn_oid, parse_octetstring },
110 { SNMP_DATEANDTIME, "DateAndTime", SNMP_DATETIME_STRSZ,
111 snmp_octstr2date, snmp_date2asn_oid, parse_dateandtime },
113 { SNMP_PHYSADDR, "PhysAddress", SNMP_PHYSADDR_STRSZ,
114 snmp_oct2physAddr, snmp_addr2asn_oid, parse_physaddress },
116 { SNMP_ATMESI, "AtmESI", SNMP_PHYSADDR_STRSZ,
117 snmp_oct2physAddr, snmp_addr2asn_oid, parse_physaddress },
119 { SNMP_NTP_TIMESTAMP, "NTPTimeStamp", SNMP_NTP_TS_STRSZ,
120 snmp_oct2ntp_ts, snmp_ntp_ts2asn_oid, parse_ntp_ts },
122 { SNMP_MACADDRESS, "MacAddress", SNMP_PHYSADDR_STRSZ,
123 snmp_oct2physAddr, snmp_addr2asn_oid, parse_physaddress },
125 { SNMP_BRIDGE_ID, "BridgeId", SNMP_BRIDGEID_STRSZ,
126 snmp_oct2bridgeid, snmp_bridgeid2oct, parse_bridge_id },
128 { SNMP_BPORT_ID, "BridgePortId", SNMP_BPORT_STRSZ,
129 snmp_oct2bport_id, snmp_bport_id2oct, parse_bport_id },
131 { SNMP_INETADDRESS, "InetAddress", SNMP_INADDRS_STRSZ,
132 snmp_oct2inetaddr, snmp_inetaddr2oct, parse_inetaddr },
134 { SNMP_TC_OWN, "BITS", SNMP_VAR_STRSZ,
135 snmp_oct2bits, snmp_bits2oct, parse_bits },
137 { SNMP_UNKNOWN, "Unknown", SNMP_VAR_STRSZ, snmp_oct2str,
138 snmp_str2asn_oid, parse_octetstring } /* keep last */
143 snmp_get_tc(char *str)
146 for (i = 0; i < SNMP_UNKNOWN; i++) {
147 if (!strncmp(text_convs[i].tc_str, str,
148 strlen(text_convs[i].tc_str)))
149 return (text_convs[i].tc);
152 return (SNMP_STRING);
156 snmp_oct2tc(enum snmp_tc tc, uint32_t len, char *octets)
161 if (tc > SNMP_UNKNOWN)
164 if (text_convs[tc].len > 0)
165 tc_len = text_convs[tc].len;
167 tc_len = 2 * len + 3;
169 if ((buf = malloc(tc_len)) == NULL ) {
170 syslog(LOG_ERR, "malloc failed - %s", strerror(errno));
174 memset(buf, 0, tc_len);
175 if (text_convs[tc].oct2tc(len, octets, buf) == NULL) {
184 snmp_tc2oid(enum snmp_tc tc, char *str, struct asn_oid *oid)
186 if (tc > SNMP_UNKNOWN)
189 return (text_convs[tc].tc2oid(str, oid));
193 snmp_tc2oct(enum snmp_tc tc, struct snmp_value *value, char *string)
195 if (tc > SNMP_UNKNOWN)
198 return (text_convs[tc].tc2oct(value, string));
201 /*****************************************************
202 * Basic OctetString type.
205 snmp_oct2str(uint32_t len, char *octets, char *buf)
211 if (len > MAX_OCTSTRING_LEN || octets == NULL || buf == NULL)
214 for (ptr = buf, i = 0; i < len; i++)
215 if (!isprint(octets[i])) {
217 buf += sprintf(buf, "0x");
221 for (ptr = buf, i = 0; i < len; i++)
223 ptr += sprintf(ptr, "%c", octets[i]);
225 ptr += sprintf(ptr, "%2.2x", (u_char)octets[i]);
231 snmp_str2asn_oid(char *str, struct asn_oid *oid)
236 * OctetStrings are allowed max length of ASN_MAXOCTETSTRING,
237 * but trying to index an entry with such a long OctetString
240 for (len = 0; len < ASN_MAXOIDLEN; len++) {
241 if (strchr(",]", *(str + len)) != NULL)
245 if (len >= ASN_MAXOIDLEN)
248 if (snmp_suboid_append(oid, (asn_subid_t) len) < 0)
251 for (i = 0; i < len; i++)
252 if (snmp_suboid_append(oid, (asn_subid_t) *(str + i)) < 0)
259 parse_octetstring(struct snmp_value *value, char *val)
263 if ((len = strlen(val)) >= MAX_OCTSTRING_LEN) {
264 warnx("Octetstring too long - %d is max allowed",
265 MAX_OCTSTRING_LEN - 1);
269 if ((value->v.octetstring.octets = malloc(len)) == NULL) {
270 value->v.octetstring.len = 0;
271 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
275 value->v.octetstring.len = len;
276 memcpy(value->v.octetstring.octets, val, len);
277 value->syntax = SNMP_SYNTAX_OCTETSTRING;
282 /*************************************************************
284 *************************************************************
285 * rfc 2579 specification:
286 * DateAndTime ::= TEXTUAL-CONVENTION
287 * DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
290 * "A date-time specification.
292 * field octets contents range
293 * ----- ------ -------- -----
294 * 1 1-2 year* 0..65536
300 * (use 60 for leap-second)
301 * 7 8 deci-seconds 0..9
302 * 8 9 direction from UTC '+' / '-'
303 * 9 10 hours from UTC* 0..13
304 * 10 11 minutes from UTC 0..59
307 * - the value of year is in network-byte order
308 * - daylight saving time in New Zealand is +13
310 * For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be
313 * 1992-5-26,13:30:15.0,-4:0
316 snmp_octstr2date(uint32_t len, char *octets, char *buf)
321 if (len != SNMP_DATETIME_OCTETS || octets == NULL || buf == NULL)
325 year = (octets[0] << 8);
329 ptr += sprintf(ptr, "%4.4d-%.2d-%.2d, ", year, octets[2],octets[3]);
330 ptr += sprintf(ptr, "%2.2d:%2.2d:%2.2d.%.2d, ", octets[4],octets[5],
331 octets[6],octets[7]);
332 ptr += sprintf(ptr, "%c%.2d:%.2d", octets[8],octets[9],octets[10]);
338 snmp_date2asn_oid(char *str, struct asn_oid *oid)
341 static const char UTC[3] = "UTC";
345 if (snmp_suboid_append(oid, (asn_subid_t) SNMP_DATETIME_OCTETS) < 0)
348 /* Read 'YYYY-' and write it in two subs. */
352 v = strtoul(ptr, &endptr, 10);
359 if (snmp_suboid_append(oid, (asn_subid_t) ((v & 0xff00) >> 8)) < 0)
361 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xff)) < 0)
368 v = strtoul(ptr, &endptr, 10);
375 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
382 v = strtoul(ptr, &endptr, 10);
389 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
396 v = strtoul(ptr, &endptr, 10);
403 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
410 v = strtoul(ptr, &endptr, 10);
417 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
424 v = strtoul(ptr, &endptr, 10);
431 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
438 v = strtoul(ptr, &endptr, 10);
445 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
448 /* 'UTC' - optional */
450 if (strncmp(ptr, UTC, sizeof(UTC)) == 0)
454 if (*ptr == '-' || *ptr == '+') {
455 if (snmp_suboid_append(oid, (asn_subid_t) (*ptr)) < 0)
464 v = strtoul(ptr, &endptr, 10);
471 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
474 /* 'MM' - last one - ignore endptr here. */
478 v = strtoul(ptr, &endptr, 10);
483 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
491 warnx("Date value %s not supported", str);
495 /* Read a DateAndTime string eg. 1992-5-26,13:30:15.0,-4:0. */
497 parse_dateandtime(struct snmp_value *sv, char *val)
501 uint8_t date[SNMP_DATETIME_OCTETS];
504 v = strtoul(val, &endptr, 10);
505 if (v > 0xffff || *endptr != '-')
507 date[0] = ((v & 0xff00) >> 8);
508 date[1] = (v & 0xff);
512 v = strtoul(val, &endptr, 10);
513 if (v == 0 || v > 12 || *endptr != '-')
519 v = strtoul(val, &endptr, 10);
520 if (v == 0 || v > 31 || *endptr != ',')
526 v = strtoul(val, &endptr, 10);
527 if (v > 23 || *endptr != ':')
533 v = strtoul(val, &endptr, 10);
534 if (v > 59 || *endptr != ':')
540 v = strtoul(val, &endptr, 10);
541 if (v > 60 || *endptr != '.')
547 v = strtoul(val, &endptr, 10);
548 if (v > 9 || *endptr != ',')
554 if (*val != '-' && *val != '+')
556 date[8] = (uint8_t) *val;
559 /* 'HH:' - offset from UTC */
560 v = strtoul(val, &endptr, 10);
561 if (v > 13 || *endptr != ':')
566 /* 'MM'\0'' offset from UTC */
567 v = strtoul(val, &endptr, 10);
568 if (v > 59 || *endptr != '\0')
572 if ((sv->v.octetstring.octets = malloc(SNMP_DATETIME_OCTETS)) == NULL) {
573 warn("malloc() failed");
577 sv->v.octetstring.len = SNMP_DATETIME_OCTETS;
578 memcpy(sv->v.octetstring.octets, date, SNMP_DATETIME_OCTETS);
579 sv->syntax = SNMP_SYNTAX_OCTETSTRING;
583 warnx("Date value %s not supported", val);
587 /**************************************************************
591 snmp_oct2physAddr(uint32_t len, char *octets, char *buf)
596 if (len != SNMP_PHYSADDR_OCTETS || octets == NULL || buf == NULL)
602 ptr += sprintf(ptr, "%2.2x", octets[0]);
603 for (i = 1; i < 6; i++)
604 ptr += sprintf(ptr, ":%2.2x", octets[i]);
610 snmp_addr2asn_oid(char *str, struct asn_oid *oid)
616 if (snmp_suboid_append(oid, (asn_subid_t) SNMP_PHYSADDR_OCTETS) < 0)
620 for (i = 0; i < 5; i++) {
622 v = strtoul(ptr, &endptr, 16);
625 warnx("Integer value %s not supported", str);
628 if (*endptr != ':') {
629 warnx("Failed adding oid - %s", str);
632 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
637 /* The last one - don't check the ending char here. */
639 v = strtoul(ptr, &endptr, 16);
642 warnx("Integer value %s not supported", str);
645 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
652 parse_physaddress(struct snmp_value *sv, char *val)
657 uint8_t phys_addr[SNMP_PHYSADDR_OCTETS];
659 for (i = 0; i < 5; i++) {
660 v = strtoul(val, &endptr, 16);
662 warnx("Integer value %s not supported", val);
666 warnx("Failed reading octet - %s", val);
673 /* The last one - don't check the ending char here. */
674 v = strtoul(val, &endptr, 16);
676 warnx("Integer value %s not supported", val);
681 if ((sv->v.octetstring.octets = malloc(SNMP_PHYSADDR_OCTETS)) == NULL) {
682 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
686 sv->v.octetstring.len = SNMP_PHYSADDR_OCTETS;
687 memcpy(sv->v.octetstring.octets, phys_addr, SNMP_PHYSADDR_OCTETS);
688 sv->syntax = SNMP_SYNTAX_OCTETSTRING;
692 /**************************************************************
694 **************************************************************
695 * NTP MIB, Revision 0.2, 7/25/97:
696 * NTPTimeStamp ::= TEXTUAL-CONVENTION
697 * DISPLAY-HINT "4x.4x"
701 * SYNTAX OCTET STRING (SIZE(8))
704 snmp_oct2ntp_ts(uint32_t len, char *octets, char *buf)
709 if (len != SNMP_NTP_TS_OCTETS || octets == NULL || buf == NULL)
715 i = octets[0] * 1000 + octets[1] * 100 + octets[2] * 10 + octets[3];
716 ptr += sprintf(ptr, "%4.4d", i);
717 i = octets[4] * 1000 + octets[5] * 100 + octets[6] * 10 + octets[7];
718 ptr += sprintf(ptr, ".%4.4d", i);
724 snmp_ntp_ts2asn_oid(char *str, struct asn_oid *oid)
728 struct asn_oid suboid;
731 if (snmp_suboid_append(oid, (asn_subid_t) SNMP_NTP_TS_OCTETS) < 0)
737 v = strtoul(ptr, &endptr, 10);
738 if (errno != 0 || (v / 1000) > 9) {
739 warnx("Integer value %s not supported", str);
745 if (*endptr != '.') {
746 warnx("Failed adding oid - %s", str);
750 memset(&suboid, 0, sizeof(struct asn_oid));
751 suboid.len = SNMP_NTP_TS_OCTETS;
753 for (i = 0, d = 1000; i < 4; i++) {
754 suboid.subs[i] = v / d;
762 v = strtoul(ptr, &endptr, 10);
763 if (errno != 0 || (v / 1000) > 9) {
764 warnx("Integer value %s not supported", str);
770 for (i = 0, d = 1000; i < 4; i++) {
771 suboid.subs[i + 4] = v / d;
776 asn_append_oid(oid, &suboid);
781 parse_ntp_ts(struct snmp_value *sv, char *val)
784 int32_t i, d, saved_errno;
786 uint8_t ntp_ts[SNMP_NTP_TS_OCTETS];
790 v = strtoul(val, &endptr, 10);
791 if (errno != 0 || (v / 1000) > 9) {
793 warnx("Integer value %s not supported", val);
798 if (*endptr != '.') {
799 warnx("Failed reading octet - %s", val);
803 for (i = 0, d = 1000; i < 4; i++) {
812 v = strtoul(val, &endptr, 10);
813 if (errno != 0 || (v / 1000) > 9) {
815 warnx("Integer value %s not supported", val);
820 for (i = 0, d = 1000; i < 4; i++) {
821 ntp_ts[i + 4] = v / d;
826 if ((sv->v.octetstring.octets = malloc(SNMP_NTP_TS_OCTETS)) == NULL) {
827 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
831 sv->v.octetstring.len = SNMP_NTP_TS_OCTETS;
832 memcpy(sv->v.octetstring.octets, ntp_ts, SNMP_NTP_TS_OCTETS);
833 sv->syntax = SNMP_SYNTAX_OCTETSTRING;
837 /**************************************************************
839 **************************************************************
840 * BRIDGE-MIB, REVISION "200509190000Z"
841 * BridgeId ::= TEXTUAL-CONVENTION
844 * "The Bridge-Identifier, as used in the Spanning Tree
845 * Protocol, to uniquely identify a bridge. Its first two
846 * octets (in network byte order) contain a priority value,
847 * and its last 6 octets contain the MAC address used to
848 * refer to a bridge in a unique fashion (typically, the
849 * numerically smallest MAC address of all ports on the
851 * SYNTAX OCTET STRING (SIZE (8))
854 snmp_oct2bridgeid(uint32_t len, char *octets, char *buf)
857 uint32_t i, priority;
859 if (len != SNMP_BRIDGEID_OCTETS || octets == NULL || buf == NULL)
865 priority = octets[0] << 8;
866 priority += octets[1];
867 if (priority > SNMP_MAX_BRIDGE_PRIORITY) {
868 warnx("Invalid bridge priority %d", priority);
871 ptr += sprintf(ptr, "%d.", octets[0]);
873 ptr += sprintf(ptr, "%2.2x", octets[2]);
875 for (i = 1; i < 6; i++)
876 ptr += sprintf(ptr, ":%2.2x", octets[i + 2]);
882 snmp_bridgeid2oct(char *str, struct asn_oid *oid)
888 if (snmp_suboid_append(oid, (asn_subid_t) SNMP_BRIDGEID_OCTETS) < 0)
892 /* Read the priority. */
895 v = strtoul(ptr, &endptr, 10);
897 if (v > SNMP_MAX_BRIDGE_PRIORITY || errno != 0 || *endptr != '.') {
899 warnx("Bad bridge priority value %d", v);
903 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xff00)) < 0)
906 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xff)) < 0)
910 for (i = 0; i < 5; i++) {
913 v = strtoul(ptr, &endptr, 16);
916 warnx("Integer value %s not supported", str);
919 if (*endptr != ':') {
920 warnx("Failed adding oid - %s",str);
923 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
928 /* The last one - don't check the ending char here. */
931 v = strtoul(ptr, &endptr, 16);
934 warnx("Integer value %s not supported", str);
937 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
944 parse_bridge_id(struct snmp_value *sv, char *string)
947 int32_t i, saved_errno;
949 uint8_t bridge_id[SNMP_BRIDGEID_OCTETS];
951 /* Read the priority. */
954 v = strtoul(string, &endptr, 10);
956 if (v > SNMP_MAX_BRIDGE_PRIORITY || errno != 0 || *endptr != '.') {
958 warnx("Bad bridge priority value %d", v);
962 bridge_id[0] = (v & 0xff00);
963 bridge_id[1] = (v & 0xff);
967 for (i = 0; i < 5; i++) {
968 v = strtoul(string, &endptr, 16);
970 warnx("Integer value %s not supported", string);
974 warnx("Failed reading octet - %s", string);
977 bridge_id[i + 2] = v;
981 /* The last one - don't check the ending char here. */
982 v = strtoul(string, &endptr, 16);
984 warnx("Integer value %s not supported", string);
989 if ((sv->v.octetstring.octets = malloc(SNMP_BRIDGEID_OCTETS)) == NULL) {
990 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
994 sv->v.octetstring.len = SNMP_BRIDGEID_OCTETS;
995 memcpy(sv->v.octetstring.octets, bridge_id, SNMP_BRIDGEID_OCTETS);
996 sv->syntax = SNMP_SYNTAX_OCTETSTRING;
1000 /**************************************************************
1002 **************************************************************
1003 * BEGEMOT-BRIDGE-MIB, LAST-UPDATED "200608100000Z"
1004 * BridgePortId ::= TEXTUAL-CONVENTION
1005 * DISPLAY-HINT "1x.1x"
1008 * "A port identifier that contains a bridge port's STP priority
1009 * in the first octet and the port number in the second octet."
1010 * SYNTAX OCTET STRING (SIZE(2))
1013 snmp_oct2bport_id(uint32_t len, char *octets, char *buf)
1017 if (len != SNMP_BPORT_OCTETS || octets == NULL || buf == NULL)
1023 ptr += sprintf(ptr, "%d.", octets[0]);
1024 ptr += sprintf(ptr, "%d", octets[1]);
1030 snmp_bport_id2oct(char *str, struct asn_oid *oid)
1036 if (snmp_suboid_append(oid, (asn_subid_t) SNMP_BPORT_OCTETS) < 0)
1040 /* Read the priority. */
1041 saved_errno = errno;
1043 v = strtoul(ptr, &endptr, 10);
1045 if (v > SNMP_MAX_BPORT_PRIORITY || errno != 0 || *endptr != '.') {
1046 errno = saved_errno;
1047 warnx("Bad bridge port priority value %d", v);
1051 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1054 saved_errno = errno;
1056 v = strtoul(ptr, &endptr, 16);
1057 errno = saved_errno;
1060 warnx("Bad port number - %d", v);
1064 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1071 parse_bport_id(struct snmp_value *value, char *string)
1076 uint8_t bport_id[SNMP_BPORT_OCTETS];
1078 /* Read the priority. */
1079 saved_errno = errno;
1081 v = strtoul(string, &endptr, 10);
1083 if (v > SNMP_MAX_BPORT_PRIORITY || errno != 0 || *endptr != '.') {
1084 errno = saved_errno;
1085 warnx("Bad bridge port priority value %d", v);
1091 string = endptr + 1;
1092 v = strtoul(string, &endptr, 16);
1094 warnx("Bad port number - %d", v);
1100 if ((value->v.octetstring.octets = malloc(SNMP_BPORT_OCTETS)) == NULL) {
1101 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
1105 value->v.octetstring.len = SNMP_BPORT_OCTETS;
1106 memcpy(value->v.octetstring.octets, bport_id, SNMP_BPORT_OCTETS);
1107 value->syntax = SNMP_SYNTAX_OCTETSTRING;
1110 /**************************************************************
1112 **************************************************************
1113 * INET-ADDRESS-MIB, REVISION "200502040000Z"
1114 * InetAddress ::= TEXTUAL-CONVENTION
1117 * "Denotes a generic Internet address.
1119 * An InetAddress value is always interpreted within the context
1120 * of an InetAddressType value. Every usage of the InetAddress
1121 * textual convention is required to specify the InetAddressType
1122 * object that provides the context. It is suggested that the
1123 * InetAddressType object be logically registered before the
1124 * object(s) that use the InetAddress textual convention, if
1125 * they appear in the same logical row.
1127 * The value of an InetAddress object must always be
1128 * consistent with the value of the associated InetAddressType
1129 * object. Attempts to set an InetAddress object to a value
1130 * inconsistent with the associated InetAddressType
1131 * must fail with an inconsistentValue error.
1133 * When this textual convention is used as the syntax of an
1134 * index object, there may be issues with the limit of 128
1135 * sub-identifiers specified in SMIv2, STD 58. In this case,
1136 * the object definition MUST include a 'SIZE' clause to
1137 * limit the number of potential instance sub-identifiers;
1138 * otherwise the applicable constraints MUST be stated in
1139 * the appropriate conceptual row DESCRIPTION clauses, or
1140 * in the surrounding documentation if there is no single
1141 * DESCRIPTION clause that is appropriate."
1142 * SYNTAX OCTET STRING (SIZE (0..255))
1143 **************************************************************
1144 * TODO: FIXME!!! syrinx: Since we do not support checking the
1145 * consistency of a varbinding based on the value of a previous
1146 * one, try to guess the type of address based on the
1147 * OctetString SIZE - 4 for IPv4, 16 for IPv6, others currently
1151 snmp_oct2inetaddr(uint32_t len, char *octets, char *buf)
1155 struct in_addr ipv4;
1156 struct in6_addr ipv6;
1158 if (len > MAX_OCTSTRING_LEN || octets == NULL || buf == NULL)
1162 /* XXX: FIXME - IPv4*/
1164 memcpy(&ipv4.s_addr, octets, sizeof(ipv4.s_addr));
1169 /* XXX: FIXME - IPv4*/
1171 memcpy(ipv6.s6_addr, octets, sizeof(ipv6.s6_addr));
1180 if (inet_ntop(af, ip, buf, SNMP_INADDRS_STRSZ) == NULL) {
1181 warn("inet_ntop failed");
1189 snmp_inetaddr2oct(char *str __unused, struct asn_oid *oid __unused)
1195 parse_inetaddr(struct snmp_value *value __unused, char *string __unused)
1200 /**************************************************************
1201 * SNMP BITS type - XXX: FIXME
1202 **************************************************************/
1204 snmp_oct2bits(uint32_t len, char *octets, char *buf)
1209 if (len > sizeof(value) || octets == NULL || buf == NULL)
1212 for (i = len, value = 0, bits = 0; i > 0; i--, bits += 8)
1213 value += octets[i] << bits;
1216 sprintf(buf, "0x%llx.",(long long unsigned) value);
1222 snmp_bits2oct(char *str, struct asn_oid *oid)
1225 int i, size, bits, saved_errno;
1226 uint64_t v, mask = 0xFF00000000000000;
1228 saved_errno = errno;
1231 v = strtoull(str, &endptr, 16);
1233 warn("Bad BITS value %s", str);
1234 errno = saved_errno;
1239 /* Determine length - up to 8 octets supported so far. */
1240 for (size = sizeof(v); size > 0; size--) {
1241 if ((v & mask) != 0)
1243 mask = mask >> bits;
1249 if (snmp_suboid_append(oid, (asn_subid_t) size) < 0)
1252 for (i = 0, bits = 0; i < size; i++, bits += 8)
1253 if (snmp_suboid_append(oid,
1254 (asn_subid_t)((v & mask) >> bits)) < 0)
1261 parse_bits(struct snmp_value *value, char *string)
1264 int i, size, bits, saved_errno;
1265 uint64_t v, mask = 0xFF00000000000000;
1267 saved_errno = errno;
1270 v = strtoull(string, &endptr, 16);
1273 warn("Bad BITS value %s", string);
1274 errno = saved_errno;
1279 /* Determine length - up to 8 octets supported so far. */
1280 for (size = sizeof(v); size > 0; size--) {
1281 if ((v & mask) != 0)
1283 mask = mask >> bits;
1289 if ((value->v.octetstring.octets = malloc(size)) == NULL) {
1290 syslog(LOG_ERR, "malloc failed: %s", strerror(errno));
1294 value->v.octetstring.len = size;
1295 for (i = 0, bits = 0; i < size; i++, bits += 8)
1296 value->v.octetstring.octets[i] = (v & mask) >> bits;
1297 value->syntax = SNMP_SYNTAX_OCTETSTRING;