]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-snmp.c
Fix a warning
[tcpdump] / print-snmp.c
index 53bc210a367e99d70668e54f92c429623b375c54..83d4090ea60a7acd90abe331ed3f87457d363dc0 100644 (file)
  #     @(#)snmp.awk.x  1.1 (LANL) 1/15/90
  */
 
-#define NETDISSECT_REWORKED
+/* \summary: Simple Network Management Protocol (SNMP) printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#ifdef HAVE_SMI_H
+#ifdef USE_LIBSMI
 #include <smi.h>
 #endif
 
-#include "interface.h"
+#include "netdissect.h"
 
 #undef OPAQUE  /* defined in <wingdi.h> */
 
@@ -325,7 +326,7 @@ static const struct obj_abrev {
 struct be {
        uint32_t asnlen;
        union {
-               caddr_t raw;
+               const uint8_t *raw;
                int32_t integer;
                uint32_t uns;
                const u_char *str;
@@ -525,7 +526,7 @@ asn1_parse(netdissect_options *ndo,
 
                        case OBJECTID:
                                elem->type = BE_OID;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                break;
 
                        case ASN_NULL:
@@ -535,7 +536,7 @@ asn1_parse(netdissect_options *ndo,
 
                        default:
                                elem->type = BE_OCTET;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                ND_PRINT((ndo, "[P/U/%s]", Class[class].Id[id]));
                                break;
                        }
@@ -545,7 +546,7 @@ asn1_parse(netdissect_options *ndo,
                        switch (id) {
                        case IPADDR:
                                elem->type = BE_INETADDR;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                break;
 
                        case COUNTER:
@@ -578,7 +579,7 @@ asn1_parse(netdissect_options *ndo,
 
                        default:
                                elem->type = BE_OCTET;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                ND_PRINT((ndo, "[P/A/%s]",
                                        Class[class].Id[id]));
                                break;
@@ -608,7 +609,7 @@ asn1_parse(netdissect_options *ndo,
                        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 = (caddr_t)p;
+                       elem->data.raw = (const uint8_t *)p;
                        break;
                }
                break;
@@ -619,12 +620,12 @@ asn1_parse(netdissect_options *ndo,
                        switch (id) {
                        case SEQUENCE:
                                elem->type = BE_SEQ;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                break;
 
                        default:
                                elem->type = BE_OCTET;
-                               elem->data.raw = (caddr_t)p;
+                               elem->data.raw = (const uint8_t *)p;
                                ND_PRINT((ndo, "C/U/%s", Class[class].Id[id]));
                                break;
                        }
@@ -632,12 +633,12 @@ asn1_parse(netdissect_options *ndo,
 
                case CONTEXT:
                        elem->type = BE_PDU;
-                       elem->data.raw = (caddr_t)p;
+                       elem->data.raw = (const uint8_t *)p;
                        break;
 
                default:
                        elem->type = BE_OCTET;
-                       elem->data.raw = (caddr_t)p;
+                       elem->data.raw = (const uint8_t *)p;
                        ND_PRINT((ndo, "C/%s/%s", Class[class].name, Class[class].Id[id]));
                        break;
                }
@@ -661,7 +662,7 @@ static int
 asn1_print(netdissect_options *ndo,
            struct be *elem)
 {
-       u_char *p = (u_char *)elem->data.raw;
+       const u_char *p = (const u_char *)elem->data.raw;
        uint32_t asnlen = elem->asnlen;
        uint32_t i;
 
@@ -677,48 +678,52 @@ asn1_print(netdissect_options *ndo,
                break;
 
        case BE_OID: {
-               int o = 0, first = -1, i = asnlen;
-
-               if (!ndo->ndo_sflag && !ndo->ndo_nflag && asnlen > 2) {
-                       const struct obj_abrev *a = &obj_abrev_list[0];
-                       size_t a_len = strlen(a->oid);
-                       for (; a->node; a++) {
-                               ND_TCHECK2(*p, a_len);
-                               if (memcmp(a->oid, (char *)p, a_len) == 0) {
-                                       objp = a->node->child;
-                                       i -= strlen(a->oid);
-                                       p += strlen(a->oid);
-                                       ND_PRINT((ndo, "%s", a->prefix));
-                                       first = 1;
-                                       break;
+               int o = 0, first = -1;
+
+               i = asnlen;
+               if (!nd_smi_module_loaded) {
+                       if (!ndo->ndo_nflag && asnlen > 2) {
+                               const struct obj_abrev *a = &obj_abrev_list[0];
+                               size_t a_len = strlen(a->oid);
+                               for (; a->node; a++) {
+                                       ND_TCHECK2(*p, a_len);
+                                       if (memcmp(a->oid, p, a_len) == 0) {
+                                               objp = a->node->child;
+                                               i -= strlen(a->oid);
+                                               p += strlen(a->oid);
+                                               ND_PRINT((ndo, "%s", a->prefix));
+                                               first = 1;
+                                               break;
+                                       }
                                }
                        }
-               }
 
-               for (; !ndo->ndo_sflag && 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;
+                       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;
                        }
-                       OBJ_PRINT(o, first);
-                       if (--first < 0)
-                               first = 0;
-                       o = 0;
                }
                break;
        }
@@ -775,7 +780,8 @@ asn1_print(netdissect_options *ndo,
 
        case BE_STR: {
                register int printable = 1, first = 1;
-               const u_char *p = elem->data.str;
+
+               p = elem->data.str;
                ND_TCHECK2(*p, asnlen);
                for (i = asnlen; printable && i-- > 0; p++)
                        printable = ND_ISPRINT(*p);
@@ -867,7 +873,7 @@ asn1_decode(u_char *p, u_int length)
 }
 #endif
 
-#ifdef LIBSMI
+#ifdef USE_LIBSMI
 
 struct smi2be {
     SmiBasetype basetype;
@@ -895,34 +901,36 @@ smi_decode_oid(netdissect_options *ndo,
                struct be *elem, unsigned int *oid,
                unsigned int oidsize, unsigned int *oidlen)
 {
-       u_char *p = (u_char *)elem->data.raw;
+       const u_char *p = (const u_char *)elem->data.raw;
        uint32_t asnlen = elem->asnlen;
        int o = 0, first = -1, i = asnlen;
        unsigned int firstval;
 
-       for (*oidlen = 0; ndo->ndo_sflag && i-- > 0; p++) {
-               ND_TCHECK(*p);
-               o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8);
-               if (*p & ASN_LONGLEN)
-                   continue;
+       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;
 
-               /*
-                * 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;
+                       /*
+                        * 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;
+                               }
+                       }
                        if (*oidlen < oidsize) {
-                           oid[(*oidlen)++] = firstval;
+                               oid[(*oidlen)++] = o;
                        }
+                       o = 0;
                }
-               if (*oidlen < oidsize) {
-                       oid[(*oidlen)++] = o;
-               }
-               o = 0;
        }
        return 0;
 
@@ -1053,7 +1061,7 @@ smi_print_variable(netdissect_options *ndo,
 
 static int
 smi_print_value(netdissect_options *ndo,
-                SmiNode *smiNode, u_char pduid, struct be *elem)
+                SmiNode *smiNode, u_short pduid, struct be *elem)
 {
        unsigned int i, oid[128], oidlen;
        SmiType *smiType;
@@ -1196,11 +1204,11 @@ smi_print_value(netdissect_options *ndo,
  */
 static void
 varbind_print(netdissect_options *ndo,
-              u_char pduid, const u_char *np, u_int length)
+              u_short pduid, const u_char *np, u_int length)
 {
        struct be elem;
        int count = 0, ind;
-#ifdef LIBSMI
+#ifdef USE_LIBSMI
        SmiNode *smiNode = NULL;
 #endif
        int status;
@@ -1217,7 +1225,7 @@ varbind_print(netdissect_options *ndo,
                ND_PRINT((ndo, "[%d extra after SEQ of varbind]", length - count));
        /* descend */
        length = elem.asnlen;
-       np = (u_char *)elem.data.raw;
+       np = (const u_char *)elem.data.raw;
 
        for (ind = 1; length > 0; ind++) {
                const u_char *vbend;
@@ -1237,7 +1245,7 @@ varbind_print(netdissect_options *ndo,
                vblength = length - count;
                /* descend */
                length = elem.asnlen;
-               np = (u_char *)elem.data.raw;
+               np = (const u_char *)elem.data.raw;
 
                /* objName (OID) */
                if ((count = asn1_parse(ndo, np, length, &elem)) < 0)
@@ -1247,7 +1255,7 @@ varbind_print(netdissect_options *ndo,
                        asn1_print(ndo, &elem);
                        return;
                }
-#ifdef LIBSMI
+#ifdef USE_LIBSMI
                smiNode = smi_print_variable(ndo, &elem, &status);
 #else
                status = asn1_print(ndo, &elem);
@@ -1273,7 +1281,7 @@ varbind_print(netdissect_options *ndo,
                        }
                } else {
                        if (elem.type != BE_NULL) {
-#ifdef LIBSMI
+#ifdef USE_LIBSMI
                                status = smi_print_value(ndo, smiNode, pduid, &elem);
 #else
                                status = asn1_print(ndo, &elem);
@@ -1296,7 +1304,7 @@ snmppdu_print(netdissect_options *ndo,
               u_short pduid, const u_char *np, u_int length)
 {
        struct be elem;
-       int count = 0, error;
+       int count = 0, error_status;
 
        /* reqId (Integer) */
        if ((count = asn1_parse(ndo, np, length, &elem)) < 0)
@@ -1319,7 +1327,7 @@ snmppdu_print(netdissect_options *ndo,
                asn1_print(ndo, &elem);
                return;
        }
-       error = 0;
+       error_status = 0;
        if ((pduid == GETREQ || pduid == GETNEXTREQ || pduid == SETREQ
            || pduid == INFORMREQ || pduid == V2TRAP || pduid == REPORT)
            && elem.data.integer != 0) {
@@ -1331,7 +1339,7 @@ snmppdu_print(netdissect_options *ndo,
        } else if (elem.data.integer != 0) {
                char errbuf[20];
                ND_PRINT((ndo, " %s", DECODE_ErrorStatus(elem.data.integer)));
-               error = elem.data.integer;
+               error_status = elem.data.integer;
        }
        length -= count;
        np += count;
@@ -1351,15 +1359,12 @@ snmppdu_print(netdissect_options *ndo,
        else if (pduid == GETBULKREQ)
                ND_PRINT((ndo, " M=%d", elem.data.integer));
        else if (elem.data.integer != 0) {
-               if (!error)
+               if (!error_status)
                        ND_PRINT((ndo, "[errorIndex(%d) w/o errorStatus]", elem.data.integer));
-               else {
+               else
                        ND_PRINT((ndo, "@%d", elem.data.integer));
-                       error = elem.data.integer;
-               }
-       } else if (error) {
+       } else if (error_status) {
                ND_PRINT((ndo, "[errorIndex==0]"));
-               error = 0;
        }
        length -= count;
        np += count;
@@ -1486,7 +1491,7 @@ pdu_print(netdissect_options *ndo,
        ND_PRINT((ndo, " "));
        /* descend into PDU */
        length = pdu.asnlen;
-       np = (u_char *)pdu.data.raw;
+       np = (const u_char *)pdu.data.raw;
 
        if (version == SNMP_VERSION_1 &&
            (pdu.id == GETBULKREQ || pdu.id == INFORMREQ ||
@@ -1540,7 +1545,7 @@ scopedpdu_print(netdissect_options *ndo,
                return;
        }
        length = elem.asnlen;
-       np = (u_char *)elem.data.raw;
+       np = (const u_char *)elem.data.raw;
 
        /* contextEngineID (OCTET STRING) */
        if ((count = asn1_parse(ndo, np, length, &elem)) < 0)
@@ -1595,7 +1600,7 @@ community_print(netdissect_options *ndo,
        }
        /* default community */
        if (!(elem.asnlen == sizeof(DEF_COMMUNITY) - 1 &&
-           strncmp((char *)elem.data.str, DEF_COMMUNITY,
+           strncmp((const char *)elem.data.str, DEF_COMMUNITY,
                    sizeof(DEF_COMMUNITY) - 1) == 0))
                /* ! "public" */
                ND_PRINT((ndo, "C=%.*s ", (int)elem.asnlen, elem.data.str));
@@ -1624,7 +1629,7 @@ usm_print(netdissect_options *ndo,
                return;
        }
        length = elem.asnlen;
-       np = (u_char *)elem.data.raw;
+       np = (const u_char *)elem.data.raw;
 
        /* msgAuthoritativeEngineID (OCTET STRING) */
        if ((count = asn1_parse(ndo, np, length, &elem)) < 0)
@@ -1725,7 +1730,7 @@ v3msg_print(netdissect_options *ndo,
                return;
        }
        length = elem.asnlen;
-       np = (u_char *)elem.data.raw;
+       np = (const u_char *)elem.data.raw;
 
        if (ndo->ndo_vflag) {
                ND_PRINT((ndo, "{ "));
@@ -1864,7 +1869,7 @@ snmp_print(netdissect_options *ndo,
                ND_PRINT((ndo, "[%d extra after iSEQ]", length - count));
        /* descend */
        length = elem.asnlen;
-       np = (u_char *)elem.data.raw;
+       np = (const u_char *)elem.data.raw;
 
        /* Version (INTEGER) */
        if ((count = asn1_parse(ndo, np, length, &elem)) < 0)