]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-snmp.c
CMake: Fix the comment about versions. [skip ci]
[tcpdump] / print-snmp.c
index 856309b6bfc9c649b63d0651bb2f0b004d817b42..76f0bbacaf0b4871670f6d000475c1c43a44e6de 100644 (file)
 
 /* \summary: Simple Network Management Protocol (SNMP) printer */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
 
 #include "netdissect-stdinc.h"
 
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 
 #ifdef USE_LIBSMI
 #include <smi.h>
 #endif
 
+#include "netdissect-ctype.h"
+
 #include "netdissect.h"
 #include "extract.h"
 
 #undef OPAQUE  /* defined in <wingdi.h> */
 
-static const char tstr[] = "[|snmp]";
 
 /*
  * Universal ASN.1 types
@@ -198,7 +198,7 @@ static const char *ErrorStatus[] = {
 #define DECODE_ErrorStatus(e) \
        ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
                ? ErrorStatus[e] \
-               : (nd_snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
+               : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
 
 /*
  * generic-trap values in the SNMP Trap-PDU
@@ -216,7 +216,7 @@ static const char *GenericTrap[] = {
 #define DECODE_GenericTrap(t) \
        ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
                ? GenericTrap[t] \
-               : (nd_snprintf(buf, sizeof(buf), "gt=%d", t), buf))
+               : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
 
 /*
  * ASN.1 type class table
@@ -429,7 +429,6 @@ asn1_parse(netdissect_options *ndo,
                ND_PRINT("[nothing to parse]");
                return -1;
        }
-       ND_TCHECK_1(p);
 
        /*
         * it would be nice to use a bit field, but you can't depend on them.
@@ -438,14 +437,14 @@ asn1_parse(netdissect_options *ndo,
         *  +---+---+---+---+---+---+---+---+
         *    7   6   5   4   3   2   1   0
         */
-       id = EXTRACT_U_1(p) & ASN_ID_BITS;              /* lower 5 bits, range 00-1f */
+       id = GET_U_1(p) & ASN_ID_BITS;          /* lower 5 bits, range 00-1f */
 #ifdef notdef
-       form = (EXTRACT_U_1(p) & 0xe0) >> 5;    /* move upper 3 bits to lower 3 */
+       form = (GET_U_1(p) & 0xe0) >> 5;        /* move upper 3 bits to lower 3 */
        class = form >> 1;              /* bits 7&6 -> bits 1&0, range 0-3 */
        form &= 0x1;                    /* bit 5 -> bit 0, range 0-1 */
 #else
-       form = (u_char)(EXTRACT_U_1(p) & ASN_FORM_BITS) >> ASN_FORM_SHIFT;
-       class = (u_char)(EXTRACT_U_1(p) & ASN_CLASS_BITS) >> ASN_CLASS_SHIFT;
+       form = (u_char)(GET_U_1(p) & ASN_FORM_BITS) >> ASN_FORM_SHIFT;
+       class = (u_char)(GET_U_1(p) & ASN_CLASS_BITS) >> ASN_CLASS_SHIFT;
 #endif
        elem->form = form;
        elem->class = class;
@@ -464,24 +463,21 @@ asn1_parse(netdissect_options *ndo,
                 * that won't fit in 32 bits.
                 */
                id = 0;
-               ND_TCHECK_1(p);
-               while (EXTRACT_U_1(p) & ASN_BIT8) {
+               while (GET_U_1(p) & ASN_BIT8) {
                        if (len < 1) {
                                ND_PRINT("[Xtagfield?]");
                                return -1;
                        }
-                       id = (id << 7) | (EXTRACT_U_1(p) & ~ASN_BIT8);
+                       id = (id << 7) | (GET_U_1(p) & ~ASN_BIT8);
                        len--;
                        hdr++;
                        p++;
-                       ND_TCHECK_1(p);
                }
                if (len < 1) {
                        ND_PRINT("[Xtagfield?]");
                        return -1;
                }
-               ND_TCHECK_1(p);
-               elem->id = id = (id << 7) | EXTRACT_U_1(p);
+               elem->id = id = (id << 7) | GET_U_1(p);
                --len;
                ++hdr;
                ++p;
@@ -490,8 +486,7 @@ asn1_parse(netdissect_options *ndo,
                ND_PRINT("[no asnlen]");
                return -1;
        }
-       ND_TCHECK_1(p);
-       elem->asnlen = EXTRACT_U_1(p);
+       elem->asnlen = GET_U_1(p);
        p++; len--; hdr++;
        if (elem->asnlen & ASN_BIT8) {
                uint32_t noct = elem->asnlen % ASN_BIT8;
@@ -501,8 +496,8 @@ asn1_parse(netdissect_options *ndo,
                        return -1;
                }
                ND_TCHECK_LEN(p, noct);
-               for (; noct-- > 0; len--, hdr++) {
-                       elem->asnlen = (elem->asnlen << ASN_SHIFT8) | EXTRACT_U_1(p);
+               for (; noct != 0; len--, hdr++, noct--) {
+                       elem->asnlen = (elem->asnlen << ASN_SHIFT8) | GET_U_1(p);
                        p++;
                }
        }
@@ -535,7 +530,7 @@ asn1_parse(netdissect_options *ndo,
                                break;
 
                        case INTEGER: {
-                               int32_t data;
+                               uint32_t data;
                                elem->type = BE_INT;
                                data = 0;
 
@@ -543,10 +538,10 @@ asn1_parse(netdissect_options *ndo,
                                        ND_PRINT("[asnlen=0]");
                                        return -1;
                                }
-                               if (EXTRACT_U_1(p) & ASN_BIT8)  /* negative */
-                                       data = -1;
+                               if (GET_U_1(p) & ASN_BIT8)      /* negative */
+                                       data = UINT_MAX;
                                for (i = elem->asnlen; i != 0; p++, i--)
-                                       data = (data << ASN_SHIFT8) | EXTRACT_U_1(p);
+                                       data = (data << ASN_SHIFT8) | GET_U_1(p);
                                elem->data.integer = data;
                                break;
                        }
@@ -583,7 +578,7 @@ asn1_parse(netdissect_options *ndo,
                                elem->type = BE_UNS;
                                data = 0;
                                for (i = elem->asnlen; i != 0; p++, i--)
-                                       data = (data << 8) + EXTRACT_U_1(p);
+                                       data = (data << 8) + GET_U_1(p);
                                elem->data.uns = data;
                                break;
                        }
@@ -593,7 +588,7 @@ asn1_parse(netdissect_options *ndo,
                                elem->type = BE_UNS64;
                                data64 = 0;
                                for (i = elem->asnlen; i != 0; p++, i--)
-                                       data64 = (data64 << 8) + EXTRACT_U_1(p);
+                                       data64 = (data64 << 8) + GET_U_1(p);
                                elem->data.uns64 = data64;
                                break;
                        }
@@ -669,7 +664,7 @@ asn1_parse(netdissect_options *ndo,
        return elem->asnlen + hdr;
 
 trunc:
-       ND_PRINT("%s", tstr);
+       nd_print_trunc(ndo);
        return -1;
 }
 
@@ -682,11 +677,11 @@ asn1_print_octets(netdissect_options *ndo, struct be *elem)
 
        ND_TCHECK_LEN(p, asnlen);
        for (i = asnlen; i != 0; p++, i--)
-               ND_PRINT("_%.2x", EXTRACT_U_1(p));
+               ND_PRINT("_%.2x", GET_U_1(p));
        return 0;
 
 trunc:
-       ND_PRINT("%s", tstr);
+       nd_print_trunc(ndo);
        return -1;
 }
 
@@ -701,25 +696,25 @@ asn1_print_string(netdissect_options *ndo, struct be *elem)
        p = elem->data.str;
        ND_TCHECK_LEN(p, asnlen);
        for (i = asnlen; printable && i != 0; p++, i--)
-               printable = ND_ISPRINT(EXTRACT_U_1(p));
+               printable = ND_ASCII_ISPRINT(GET_U_1(p));
        p = elem->data.str;
        if (printable) {
                ND_PRINT("\"");
-               if (fn_printn(ndo, p, asnlen, ndo->ndo_snapend)) {
+               if (nd_printn(ndo, p, asnlen, ndo->ndo_snapend)) {
                        ND_PRINT("\"");
                        goto trunc;
                }
                ND_PRINT("\"");
        } else {
                for (i = asnlen; i != 0; p++, i--) {
-                       ND_PRINT(first ? "%.2x" : "_%.2x", EXTRACT_U_1(p));
+                       ND_PRINT(first ? "%.2x" : "_%.2x", GET_U_1(p));
                        first = 0;
                }
        }
        return 0;
 
 trunc:
-       ND_PRINT("%s", tstr);
+       nd_print_trunc(ndo);
        return -1;
 }
 
@@ -747,7 +742,8 @@ asn1_print(netdissect_options *ndo,
                break;
 
        case BE_OID: {
-               int o = 0, first = -1;
+               int first = -1;
+               uint32_t o = 0;
 
                p = (const u_char *)elem->data.raw;
                i = asnlen;
@@ -770,9 +766,8 @@ asn1_print(netdissect_options *ndo,
                }
 
                for (; i != 0; p++, i--) {
-                       ND_TCHECK_1(p);
-                       o = (o << ASN_SHIFT7) + (EXTRACT_U_1(p) & ~ASN_BIT8);
-                       if (EXTRACT_U_1(p) & ASN_LONGLEN)
+                       o = (o << ASN_SHIFT7) + (GET_U_1(p) & ~ASN_BIT8);
+                       if (GET_U_1(p) & ASN_LONGLEN)
                                continue;
 
                        /*
@@ -825,7 +820,7 @@ asn1_print(netdissect_options *ndo,
                p = (const u_char *)elem->data.raw;
                ND_TCHECK_LEN(p, asnlen);
                for (i = asnlen; i != 0; p++, i--) {
-                       ND_PRINT((i == asnlen) ? "%u" : ".%u", EXTRACT_U_1(p));
+                       ND_PRINT((i == asnlen) ? "%u" : ".%u", GET_U_1(p));
                }
                break;
 
@@ -850,7 +845,7 @@ asn1_print(netdissect_options *ndo,
        return 0;
 
 trunc:
-       ND_PRINT("%s", tstr);
+       nd_print_trunc(ndo);
        return -1;
 }
 
@@ -923,9 +918,8 @@ smi_decode_oid(netdissect_options *ndo,
        unsigned int firstval;
 
        for (*oidlen = 0; i != 0; p++, i--) {
-               ND_TCHECK_1(p);
-               o = (o << ASN_SHIFT7) + (EXTRACT_U_1(p) & ~ASN_BIT8);
-               if (EXTRACT_U_1(p) & ASN_LONGLEN)
+               o = (o << ASN_SHIFT7) + (GET_U_1(p) & ~ASN_BIT8);
+               if (GET_U_1(p) & ASN_LONGLEN)
                    continue;
 
                /*
@@ -933,7 +927,7 @@ smi_decode_oid(netdissect_options *ndo,
                 * (see X.690:1997 clause 8.19 for the details)
                 */
                if (first < 0) {
-                       first = 0;
+                       first = 0;
                        firstval = o / OIDMUX;
                        if (firstval > 2) firstval = 2;
                        o -= firstval * OIDMUX;
@@ -947,10 +941,6 @@ smi_decode_oid(netdissect_options *ndo,
                o = 0;
        }
        return 0;
-
-trunc:
-       ND_PRINT("%s", tstr);
-       return -1;
 }
 
 static int smi_check_type(SmiBasetype basetype, int be)
@@ -1098,7 +1088,7 @@ smi_print_value(netdissect_options *ndo,
        }
 
        if (NOTIFY_CLASS(pduid) && smiNode->access < SMI_ACCESS_NOTIFY) {
-           ND_PRINT("[notNotifyable]");
+           ND_PRINT("[notNotifiable]");
        }
 
        if (READ_CLASS(pduid) && smiNode->access < SMI_ACCESS_READ_ONLY) {
@@ -1227,7 +1217,7 @@ varbind_print(netdissect_options *ndo,
               u_short pduid, const u_char *np, u_int length)
 {
        struct be elem;
-       int count = 0, ind;
+       int count = 0;
 #ifdef USE_LIBSMI
        SmiNode *smiNode = NULL;
 #endif
@@ -1247,7 +1237,7 @@ varbind_print(netdissect_options *ndo,
        length = elem.asnlen;
        np = (const u_char *)elem.data.raw;
 
-       for (ind = 1; length > 0; ind++) {
+       while (length) {
                const u_char *vbend;
                u_int vblength;
 
@@ -1390,7 +1380,6 @@ snmppdu_print(netdissect_options *ndo,
        np += count;
 
        varbind_print(ndo, pduid, np, length);
-       return;
 }
 
 /*
@@ -1481,7 +1470,6 @@ trappdu_print(netdissect_options *ndo,
        np += count;
 
        varbind_print(ndo, TRAP, np, length);
-       return;
 }
 
 /*
@@ -1799,7 +1787,7 @@ v3msg_print(netdissect_options *ndo,
                ND_PRINT("[msgFlags size %d]", elem.asnlen);
                return;
        }
-       flags = EXTRACT_U_1(elem.data.str);
+       flags = GET_U_1(elem.data.str);
        if (flags != 0x00 && flags != 0x01 && flags != 0x03
            && flags != 0x04 && flags != 0x05 && flags != 0x07) {
                ND_PRINT("[msgFlags=0x%02X]", flags);