SNMP contains quite a few enumerations, which are annoying to read, even
if you have the MIB at hand. This diff adds support for integer
enumerations. I've only implemented TruthValue for now, but others
should be easy to add. I went for a normal linear search since most
enums don't even hit 10 elements, comparison is int-based and thus the
added complexity of something like RB_* seems like enormous overkill.

I've removed BER_TYPE_BOOLEAN, since it's not part of the SNMP-spec and
thus dead code. I added it originally because snmpd's smi.c contained
it.

I should probably rewrite smi_print_element at some point, but I'm not
confident enough in its intricacies to make that jump just yet. One
thing at a time.

OK?

martijn@

Index: mib.h
===================================================================
RCS file: /cvs/src/usr.bin/snmp/mib.h,v
retrieving revision 1.8
diff -u -p -r1.8 mib.h
--- mib.h       12 Sep 2020 18:11:43 -0000      1.8
+++ mib.h       20 Sep 2020 15:23:48 -0000
@@ -1034,8 +1034,8 @@
        { MIBDECL(ifHCOutBroadcastPkts) },              \
        { MIBDECL(ifLinkUpDownTrapEnable) },            \
        { MIBDECL(ifHighSpeed) },                       \
-       { MIBDECL(ifPromiscuousMode) },                 \
-       { MIBDECL(ifConnectorPresent) },                \
+       { MIBDECL(ifPromiscuousMode), "TruthValue" },   \
+       { MIBDECL(ifConnectorPresent), "TruthValue" },  \
        { MIBDECL(ifAlias) },                           \
        { MIBDECL(ifCounterDiscontinuityTime) },        \
        { MIBDECL(ifStackTable) },                      \
@@ -1460,10 +1460,15 @@
        { MIBEND }                                      \
 }
 
-#define TEXTCONV_TREE {                                        \
-       { "SnmpAdminString", "255t", BER_TYPE_OCTETSTRING }, \
-       { "DisplayString", "255a", BER_TYPE_OCTETSTRING }, \
-       { NULL, NULL }                                  \
+#define TEXTCONV_TREE {                                                        
\
+       { "SnmpAdminString", BER_TYPE_OCTETSTRING, "255t" },            \
+       { "DisplayString", BER_TYPE_OCTETSTRING, "255a" },              \
+       { "TruthValue", BER_TYPE_INTEGER, NULL, (struct textconv_enum[]){\
+               { 1, "true" },                                          \
+               { 2, "false" },                                         \
+               {}                                                      \
+       }},                                                             \
+       {  }                                                            \
 }
 
 void    mib_init(void);
Index: smi.c
===================================================================
RCS file: /cvs/src/usr.bin/snmp/smi.c,v
retrieving revision 1.12
diff -u -p -r1.12 smi.c
--- smi.c       8 Aug 2020 13:02:54 -0000       1.12
+++ smi.c       20 Sep 2020 15:23:48 -0000
@@ -39,6 +39,7 @@
 #define MINIMUM(a, b)  (((a) < (b)) ? (a) : (b))
 
 char *smi_displayhint_os(struct textconv *, int, const char *, size_t, int);
+char *smi_displayhint_int(struct textconv*, int, long long);
 
 int smi_oid_cmp(struct oid *, struct oid *);
 int smi_key_cmp(struct oid *, struct oid *);
@@ -254,7 +255,6 @@ smi_print_element(struct ber_oid *oid, s
        struct textconv  tckey;
        size_t           len, i, slen;
        long long        v, ticks;
-       int              d;
        int              is_hex = 0, ret;
        struct ber_oid   o;
        char             strbuf[BUFSIZ];
@@ -276,17 +276,6 @@ smi_print_element(struct ber_oid *oid, s
        }
 
        switch (root->be_encoding) {
-       case BER_TYPE_BOOLEAN:
-               if (ober_get_boolean(root, &d) == -1)
-                       goto fail;
-               if (print_hint) {
-                       if (asprintf(&str, "INTEGER: %s(%d)",
-                           d ? "true" : "false", d) == -1)
-                               goto fail;
-               } else
-                       if (asprintf(&str, "%s", d ? "true" : "false") == -1)
-                               goto fail;
-               break;
        case BER_TYPE_INTEGER:
        case BER_TYPE_ENUMERATED:
                if (ober_get_integer(root, &v) == -1)
@@ -345,6 +334,10 @@ smi_print_element(struct ber_oid *oid, s
                        break;
                }
                hint = "INTEGER: ";
+               if (object != NULL && object->o_textconv != NULL &&
+                   object->o_textconv->tc_syntax == root->be_encoding)
+                       return smi_displayhint_int(object->o_textconv,
+                           print_hint, v);
                if (root->be_class == BER_CLASS_APPLICATION) {
                        if (root->be_type == SNMP_T_COUNTER32)
                                hint = "Counter32: ";
@@ -628,6 +621,31 @@ smi_foreach(struct oid *oid)
        if (oid == NULL)
                return RB_MIN(oidtree, &smi_oidtree);
        return RB_NEXT(oidtree, &smi_oidtree, oid);
+}
+
+char *
+smi_displayhint_int(struct textconv *tc, int print_hint, long long v)
+{
+       size_t i;
+       char *rbuf;
+
+       for (i = 0; tc->tc_enum[i].tce_name != NULL; i++) {
+               if (tc->tc_enum[i].tce_number == v) {
+                       if (print_hint) {
+                               if (asprintf(&rbuf, "INTEGER: %s(%lld)",
+                                   tc->tc_enum[i].tce_name, v) == -1)
+                                       return NULL;
+                       } else {
+                               if (asprintf(&rbuf, "%s",
+                                   tc->tc_enum[i].tce_name) == -1)
+                                       return NULL;
+                       }
+                       return rbuf;
+               }
+       }
+       if (asprintf(&rbuf, "%s%lld", print_hint ? "INTEGER: " : "", v) == -1)
+               return NULL;
+       return rbuf;
 }
 
 #define REPLACEMENT "\357\277\275"
Index: smi.h
===================================================================
RCS file: /cvs/src/usr.bin/snmp/smi.h,v
retrieving revision 1.3
diff -u -p -r1.3 smi.h
--- smi.h       3 Aug 2020 14:45:54 -0000       1.3
+++ smi.h       20 Sep 2020 15:23:48 -0000
@@ -67,10 +67,16 @@ struct oid {
        RB_ENTRY(oid)            o_keyword;
 };
 
+struct textconv_enum {
+       uint32_t                 tce_number;
+       const char              *tce_name;
+};
+
 struct textconv {
        const char              *tc_name;
-       const char              *tc_display_hint;
        unsigned int             tc_syntax;
+       const char              *tc_display_hint;
+       struct textconv_enum    *tc_enum;
        RB_ENTRY(textconv)       tc_entry;
 };
 

Reply via email to