]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-snmp.c
Force -t on in TESTonce.
[tcpdump] / print-snmp.c
index 7e2a426ba2b86329272f111a8213ab915decec19..1b096dcfe579289b90be9521dafaad86df1e136e 100644 (file)
@@ -276,28 +276,46 @@ static struct obj {
  * Currently, this includes the prefixes for the Internet MIB, the
  * private enterprises tree, and the experimental tree.
  */
+#define OID_FIRST_OCTET(x, y)  (((x)*40) + (y))        /* X.690 8.19.4 */
+
+#ifndef NO_ABREV_MIB
+static const uint8_t mib_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 };
+#endif
+#ifndef NO_ABREV_ENTER
+static const uint8_t enterprises_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 };
+#endif
+#ifndef NO_ABREV_EXPERI
+static const uint8_t experimental_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 };
+#endif
+#ifndef NO_ABBREV_SNMPMODS
+static const uint8_t snmpModules_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 };
+#endif
+
+#define OBJ_ABBREV_ENTRY(prefix, obj) \
+       { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) }
 static const struct obj_abrev {
        const char *prefix;             /* prefix for this abrev */
        struct obj *node;               /* pointer into object table */
-       const char *oid;                /* ASN.1 encoded OID */
+       const uint8_t *oid;             /* ASN.1 encoded OID */
+       size_t oid_len;                 /* length of OID */
 } obj_abrev_list[] = {
 #ifndef NO_ABREV_MIB
        /* .iso.org.dod.internet.mgmt.mib */
-       { "",   &_mib_obj,              "\53\6\1\2\1" },
+       OBJ_ABBREV_ENTRY("",    mib),
 #endif
 #ifndef NO_ABREV_ENTER
        /* .iso.org.dod.internet.private.enterprises */
-       { "E:", &_enterprises_obj,      "\53\6\1\4\1" },
+       OBJ_ABBREV_ENTRY("E:",  enterprises),
 #endif
 #ifndef NO_ABREV_EXPERI
        /* .iso.org.dod.internet.experimental */
-       { "X:", &_experimental_obj,     "\53\6\1\3" },
+       OBJ_ABBREV_ENTRY("X:",  experimental),
 #endif
 #ifndef NO_ABBREV_SNMPMODS
        /* .iso.org.dod.internet.snmpV2.snmpModules */
-        { "S:", &_snmpModules_obj,      "\53\6\1\6\3" },
+       OBJ_ABBREV_ENTRY("S:",  snmpModules),
 #endif
-       { 0,0,0 }
+       { 0,0,0,0 }
 };
 
 /*
@@ -330,10 +348,7 @@ struct be {
                int32_t integer;
                uint32_t uns;
                const u_char *str;
-               struct {
-                       uint32_t high;
-                       uint32_t low;
-               } uns64;
+               uint64_t uns64;
        } data;
        u_short id;
        u_char form, class;             /* tag info */
@@ -504,6 +519,7 @@ asn1_parse(netdissect_options *ndo,
                ND_PRINT((ndo, "[id?%c/%s/%d]", *Form[form], Class[class].name, id));
                return -1;
        }
+       ND_TCHECK2(*p, elem->asnlen);
 
        switch (form) {
        case PRIMITIVE:
@@ -524,7 +540,6 @@ asn1_parse(netdissect_options *ndo,
                                        ND_PRINT((ndo, "[asnlen=0]"));
                                        return -1;
                                }
-                               ND_TCHECK2(*p, elem->asnlen);
                                if (*p & ASN_BIT8)      /* negative */
                                        data = -1;
                                for (i = elem->asnlen; i-- > 0; p++)
@@ -562,7 +577,6 @@ asn1_parse(netdissect_options *ndo,
                        case GAUGE:
                        case TIMETICKS: {
                                register uint32_t data;
-                               ND_TCHECK2(*p, elem->asnlen);
                                elem->type = BE_UNS;
                                data = 0;
                                for (i = elem->asnlen; i-- > 0; p++)
@@ -572,17 +586,12 @@ asn1_parse(netdissect_options *ndo,
                        }
 
                        case COUNTER64: {
-                               register uint32_t high, low;
-                               ND_TCHECK2(*p, elem->asnlen);
+                               register uint64_t data64;
                                elem->type = BE_UNS64;
-                               high = 0, low = 0;
-                               for (i = elem->asnlen; i-- > 0; p++) {
-                                       high = (high << 8) |
-                                           ((low & 0xFF000000) >> 24);
-                                       low = (low << 8) | *p;
-                               }
-                               elem->data.uns64.high = high;
-                               elem->data.uns64.low = low;
+                               data64 = 0;
+                               for (i = elem->asnlen; i-- > 0; p++)
+                                       data64 = (data64 << 8) + *p;
+                               elem->data.uns64 = data64;
                                break;
                        }
 
@@ -616,7 +625,6 @@ asn1_parse(netdissect_options *ndo,
 
                default:
                        ND_PRINT((ndo, "[P/%s/%s]", Class[class].name, Class[class].Id[id]));
-                       ND_TCHECK2(*p, elem->asnlen);
                        elem->type = BE_OCTET;
                        elem->data.raw = (const uint8_t *)p;
                        break;
@@ -740,51 +748,49 @@ asn1_print(netdissect_options *ndo,
 
                p = (const u_char *)elem->data.raw;
                i = asnlen;
-               if (!nd_smi_module_loaded) {
-                       if (!ndo->ndo_nflag&& asnlen > 2) {
-                               const struct obj_abrev *a = &obj_abrev_list[0];
-                               for (; a->node; a++) {
-                                       size_t a_len = strlen(a->oid);
-                                       ND_TCHECK2(*p, a_len);
-                                       if (memcmp(a->oid, p, a_len) == 0) {
-                                               objp = a->node->child;
-                                               if (i < a_len)
-                                                       goto trunc;
-                                               i -= a_len;
-                                               p += a_len;
-                                               ND_PRINT((ndo, "%s", a->prefix));
-                                               first = 1;
-                                               break;
-                                       }
+               if (!ndo->ndo_nflag && asnlen > 2) {
+                       const struct obj_abrev *a = &obj_abrev_list[0];
+                       for (; a->node; a++) {
+                               if (i < a->oid_len)
+                                       continue;
+                               if (!ND_TTEST2(*p, a->oid_len))
+                                       continue;
+                               if (memcmp(a->oid, p, a->oid_len) == 0) {
+                                       objp = a->node->child;
+                                       i -= a->oid_len;
+                                       p += a->oid_len;
+                                       ND_PRINT((ndo, "%s", a->prefix));
+                                       first = 1;
+                                       break;
                                }
                        }
+               }
 
-                       for (; i-- > 0; p++) {
-                               ND_TCHECK(*p);
-                               o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8);
-                               if (*p & ASN_LONGLEN)
-                                       continue;
-
-                               /*
-                                * first subitem encodes two items with
-                                * 1st*OIDMUX+2nd
-                                * (see X.690:1997 clause 8.19 for the details)
-                                */
-                               if (first < 0) {
-                                       int s;
-                                       if (!ndo->ndo_nflag)
-                                               objp = mibroot;
-                                       first = 0;
-                                       s = o / OIDMUX;
-                                       if (s > 2) s = 2;
-                                       OBJ_PRINT(s, first);
-                                       o -= s * OIDMUX;
-                               }
-                               OBJ_PRINT(o, first);
-                               if (--first < 0)
-                                       first = 0;
-                               o = 0;
+               for (; i-- > 0; p++) {
+                       ND_TCHECK(*p);
+                       o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8);
+                       if (*p & ASN_LONGLEN)
+                               continue;
+
+                       /*
+                        * first subitem encodes two items with
+                        * 1st*OIDMUX+2nd
+                        * (see X.690:1997 clause 8.19 for the details)
+                        */
+                       if (first < 0) {
+                               int s;
+                               if (!ndo->ndo_nflag)
+                                       objp = mibroot;
+                               first = 0;
+                               s = o / OIDMUX;
+                               if (s > 2) s = 2;
+                               OBJ_PRINT(s, first);
+                               o -= s * OIDMUX;
                        }
+                       OBJ_PRINT(o, first);
+                       if (--first < 0)
+                               first = 0;
+                       o = 0;
                }
                break;
        }
@@ -797,47 +803,9 @@ asn1_print(netdissect_options *ndo,
                ND_PRINT((ndo, "%u", elem->data.uns));
                break;
 
-       case BE_UNS64: {        /* idea borrowed from by Marshall Rose */
-               double d;
-               int j, carry;
-               char *cpf, *cpl, last[6], first[30];
-               if (elem->data.uns64.high == 0) {
-                       ND_PRINT((ndo, "%u", elem->data.uns64.low));
-                       break;
-               }
-               d = elem->data.uns64.high * 4294967296.0;       /* 2^32 */
-               if (elem->data.uns64.high <= 0x1fffff) {
-                       d += elem->data.uns64.low;
-#if 0 /*is looks illegal, but what is the intention?*/
-                       ND_PRINT((ndo, "%.f", d));
-#else
-                       ND_PRINT((ndo, "%f", d));
-#endif
-                       break;
-               }
-               d += (elem->data.uns64.low & 0xfffff000);
-#if 0 /*is looks illegal, but what is the intention?*/
-               snprintf(first, sizeof(first), "%.f", d);
-#else
-               snprintf(first, sizeof(first), "%f", d);
-#endif
-               snprintf(last, sizeof(last), "%5.5d",
-                   elem->data.uns64.low & 0xfff);
-               for (carry = 0, cpf = first+strlen(first)-1, cpl = last+4;
-                    cpl >= last;
-                    cpf--, cpl--) {
-                       j = carry + (*cpf - '0') + (*cpl - '0');
-                       if (j > 9) {
-                               j -= 10;
-                               carry = 1;
-                       } else {
-                               carry = 0;
-                       }
-                       *cpf = j + '0';
-               }
-               ND_PRINT((ndo, "%s", first));
+       case BE_UNS64:
+               ND_PRINT((ndo, "%" PRIu64, elem->data.uns64));
                break;
-       }
 
        case BE_STR:
                if (asn1_print_string(ndo, elem) == -1)
@@ -950,31 +918,29 @@ smi_decode_oid(netdissect_options *ndo,
        int o = 0, first = -1, i = asnlen;
        unsigned int firstval;
 
-       if (nd_smi_module_loaded) {
-               for (*oidlen = 0; i-- > 0; p++) {
-                       ND_TCHECK(*p);
-                       o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8);
-                       if (*p & ASN_LONGLEN)
-                           continue;
+       for (*oidlen = 0; i-- > 0; p++) {
+               ND_TCHECK(*p);
+               o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8);
+               if (*p & ASN_LONGLEN)
+                   continue;
 
-                       /*
-                        * first subitem encodes two items with 1st*OIDMUX+2nd
-                        * (see X.690:1997 clause 8.19 for the details)
-                        */
-                       if (first < 0) {
-                               first = 0;
-                               firstval = o / OIDMUX;
-                               if (firstval > 2) firstval = 2;
-                               o -= firstval * OIDMUX;
-                               if (*oidlen < oidsize) {
-                                   oid[(*oidlen)++] = firstval;
-                               }
-                       }
+               /*
+                * first subitem encodes two items with 1st*OIDMUX+2nd
+                * (see X.690:1997 clause 8.19 for the details)
+                */
+               if (first < 0) {
+                       first = 0;
+                       firstval = o / OIDMUX;
+                       if (firstval > 2) firstval = 2;
+                       o -= firstval * OIDMUX;
                        if (*oidlen < oidsize) {
-                               oid[(*oidlen)++] = o;
+                           oid[(*oidlen)++] = firstval;
                        }
-                       o = 0;
                }
+               if (*oidlen < oidsize) {
+                       oid[(*oidlen)++] = o;
+               }
+               o = 0;
        }
        return 0;
 
@@ -1081,6 +1047,10 @@ smi_print_variable(netdissect_options *ndo,
        SmiNode *smiNode = NULL;
        unsigned int i;
 
+       if (!nd_smi_module_loaded) {
+               *status = asn1_print(ndo, elem);
+               return NULL;
+       }
        *status = smi_decode_oid(ndo, elem, oid, sizeof(oid) / sizeof(unsigned int),
            &oidlen);
        if (*status < 0)
@@ -1166,22 +1136,24 @@ smi_print_value(netdissect_options *ndo,
                if (smiType->basetype == SMI_BASETYPE_BITS) {
                        /* print bit labels */
                } else {
-                       smi_decode_oid(ndo, elem, oid,
-                                      sizeof(oid)/sizeof(unsigned int),
-                                      &oidlen);
-                       smiNode = smiGetNodeByOID(oidlen, oid);
-                       if (smiNode) {
-                               if (ndo->ndo_vflag) {
-                                       ND_PRINT((ndo, "%s::", smiGetNodeModule(smiNode)->name));
-                               }
-                               ND_PRINT((ndo, "%s", smiNode->name));
-                               if (smiNode->oidlen < oidlen) {
-                                       for (i = smiNode->oidlen;
-                                            i < oidlen; i++) {
-                                               ND_PRINT((ndo, ".%u", oid[i]));
+                       if (nd_smi_module_loaded &&
+                           smi_decode_oid(ndo, elem, oid,
+                                          sizeof(oid)/sizeof(unsigned int),
+                                          &oidlen) == 0) {
+                               smiNode = smiGetNodeByOID(oidlen, oid);
+                               if (smiNode) {
+                                       if (ndo->ndo_vflag) {
+                                               ND_PRINT((ndo, "%s::", smiGetNodeModule(smiNode)->name));
+                                       }
+                                       ND_PRINT((ndo, "%s", smiNode->name));
+                                       if (smiNode->oidlen < oidlen) {
+                                               for (i = smiNode->oidlen;
+                                                    i < oidlen; i++) {
+                                                       ND_PRINT((ndo, ".%u", oid[i]));
+                                               }
                                        }
+                                       done++;
                                }
-                               done++;
                        }
                }
                break;