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.
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
+}
/* 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 *)
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 */
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;
}
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;
#include <fcntl.h>
#endif
-#ifdef USE_LIBSMI
-#include <smi.h>
-#endif
-
#ifdef HAVE_LIBCRYPTO
#include <openssl/crypto.h>
#endif
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':
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
(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