]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Move more libsmi stuff to netdissect.c.
authorGuy Harris <[email protected]>
Thu, 4 Aug 2016 21:35:35 +0000 (14:35 -0700)
committerGuy Harris <[email protected]>
Thu, 4 Aug 2016 21:35:35 +0000 (14:35 -0700)
Have the call to smiLoadModule() be in a nd_load_smi_module() routine.
Have it set a *global* flag indicating whether a module has been loaded;
that's not per-netdissect_options.  Use that global flag in print-snmp.c
- and don't test it once per loop iteration, it's not going to change
while the loop is running.

Have a routine to return the version of the library if we're built with
it or NULL if we're not.

That removes the last of the code that tests USE_LIBSMI or uses libsmi
from tcpdump.c.

netdissect.c
netdissect.h
print-snmp.c
tcpdump.c

index 80b6e94dcab7f214ddb71db62ae9d306f4d33f51..0c7d661d3da402b8d9f0c93a8e3abc4e48151cc8 100644 (file)
@@ -98,3 +98,47 @@ nd_cleanup(void)
        WSACleanup();
 #endif
 }
+
+int
+nd_have_smi_support(void)
+{
+#ifdef USE_LIBSMI
+       return (1);
+#else
+       return (0);
+#endif
+}
+
+/*
+ * Indicates whether an SMI module has been loaded, so that we can use
+ * libsmi to translate OIDs.
+ */
+int nd_smi_module_loaded;
+
+int
+nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
+{
+#ifdef USE_LIBSMI
+       if (smiLoadModule(module) == 0) {
+               snprintf(errbuf, errbuf_size, "could not load MIB module %s",
+                   module);
+               return (-1);
+       }
+       nd_smi_module_loaded = 1;
+       return (0);
+#else
+       snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
+           module);
+       return (-1);
+#endif
+}
+
+const char *
+nd_smi_version_string(void)
+{
+#ifdef USE_LIBSMI
+       return (smi_version_string);
+#else
+       return (NULL);
+#endif
+}
index f1e2ecb1ee14c802d8ff656e391c2d837c3623a3..902bf1b2334cdf63f3b8a354434f0ab7b372a461 100644 (file)
@@ -130,6 +130,15 @@ extern int nd_init(char *, size_t);
 /* Clean up netdissect. */
 extern void nd_cleanup(void);
 
+/* Do we have libsmi support? */
+extern int nd_have_smi_support(void);
+/* Load an SMI module. */
+extern int nd_load_smi_module(const char *, char *, size_t);
+/* Flag indicating whether an SMI module has been loaded. */
+extern int nd_smi_module_loaded;
+/* Version number of the SMI library, or NULL if we don't have libsmi support. */
+extern const char *nd_smi_version_string(void);
+
 typedef struct netdissect_options netdissect_options;
 
 #define IF_PRINTER_ARGS (netdissect_options *, const struct pcap_pkthdr *, const u_char *)
@@ -144,7 +153,6 @@ struct netdissect_options {
   int ndo_nflag;               /* leave addresses as numbers */
   int ndo_Nflag;               /* remove domains from printed host names */
   int ndo_qflag;               /* quick (shorter) output */
-  int ndo_mflag;               /* use the libsmi to translate OIDs */
   int ndo_Sflag;               /* print raw TCP sequence numbers */
   int ndo_tflag;               /* print packet arrival time */
   int ndo_uflag;               /* Print undecoded NFS handles */
index a2aa0742d201fb5b14a06d56b28d52eafa413105..12223adfd0e5c9988cab8031260ae4f2c0d943d7 100644 (file)
@@ -679,46 +679,49 @@ asn1_print(netdissect_options *ndo,
                int o = 0, first = -1;
 
                i = asnlen;
-               if (!ndo->ndo_mflag && !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;
+               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_mflag && 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;
        }
@@ -901,29 +904,31 @@ smi_decode_oid(netdissect_options *ndo,
        int o = 0, first = -1, i = asnlen;
        unsigned int firstval;
 
-       for (*oidlen = 0; ndo->ndo_mflag && 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;
 
index be14145f5688c90046e3a6bcbfe3afb3d3f622ec..657158e4e678c153f75f982656ccccbce44bd882 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -62,10 +62,6 @@ The Regents of the University of California.  All rights reserved.\n";
 #include <fcntl.h>
 #endif
 
-#ifdef USE_LIBSMI
-#include <smi.h>
-#endif
-
 #ifdef HAVE_LIBCRYPTO
 #include <openssl/crypto.h>
 #endif
@@ -1149,16 +1145,14 @@ main(int argc, char **argv)
                        break;
 
                case 'm':
-#ifdef USE_LIBSMI
-                       if (smiLoadModule(optarg) == 0) {
-                               error("could not load MIB module %s", optarg);
+                       if (nd_have_smi_support()) {
+                               if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
+                                       error("%s", ebuf);
+                       } else {
+                               (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
+                                             program_name, optarg);
+                               (void)fprintf(stderr, "(no libsmi support)\n");
                        }
-                       ndo->ndo_mflag = 1;
-#else
-                       (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
-                                     program_name, optarg);
-                       (void)fprintf(stderr, "(no libsmi support)\n");
-#endif
                        break;
 
                case 'M':
@@ -2476,6 +2470,7 @@ print_version(void)
        static char pcap_version[] = "unknown";
 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
 #endif /* HAVE_PCAP_LIB_VERSION */
+       const char *smi_version_string;
 
 #ifdef HAVE_PCAP_LIB_VERSION
 #ifdef _WIN32
@@ -2498,9 +2493,9 @@ print_version(void)
        (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
 #endif
 
-#ifdef USE_LIBSMI
-       (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
-#endif
+       smi_version_string = nd_smi_version_string();
+       if (smi_version_string != NULL)
+               (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
 }
 USES_APPLE_RST