xref: /netbsd-src/external/bsd/tcpdump/dist/netdissect.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*
2  * Copyright (c) 1988-1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1998-2012  Michael Richardson <mcr@tcpdump.org>
6  *      The TCPDUMP project
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that: (1) source code distributions
10  * retain the above copyright notice and this paragraph in its entirety, (2)
11  * distributions including binary code include the above copyright notice and
12  * this paragraph in its entirety in the documentation or other materials
13  * provided with the distribution, and (3) all advertising materials mentioning
14  * features or use of this software display the following acknowledgement:
15  * ``This product includes software developed by the University of California,
16  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17  * the University nor the names of its contributors may be used to endorse
18  * or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23  */
24 
25 #include <sys/cdefs.h>
26 #ifndef lint
27 __RCSID("$NetBSD: netdissect.c,v 1.2 2017/02/05 04:05:05 spz Exp $");
28 #endif
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include <netdissect-stdinc.h>
35 #include "netdissect.h"
36 #include <string.h>
37 #include <stdio.h>
38 
39 #ifdef USE_LIBSMI
40 #include <smi.h>
41 #endif
42 
43 /*
44  * Initialize anything that must be initialized before dissecting
45  * packets.
46  *
47  * This should be called at the beginning of the program; it does
48  * not need to be called, and should not be called, for every
49  * netdissect_options structure.
50  */
51 int
52 nd_init(char *errbuf, size_t errbuf_size)
53 {
54 #ifdef _WIN32
55 	WORD wVersionRequested;
56 	WSADATA wsaData;
57 	int err;
58 
59 	/*
60 	 * Request Winsock 2.2; we expect Winsock 2.
61 	 */
62 	wVersionRequested = MAKEWORD(2, 2);
63 	err = WSAStartup(wVersionRequested, &wsaData);
64 	if (err != 0) {
65 		strlcpy(errbuf, "Attempting to initialize Winsock failed",
66 		    errbuf_size);
67 		return (-1);
68 	}
69 #endif /* _WIN32 */
70 
71 #ifdef USE_LIBSMI
72 	/*
73 	 * XXX - should we just fail if this fails?  Some of the
74 	 * libsmi calls may fail.
75 	 */
76 	smiInit("tcpdump");
77 #endif
78 
79 	/*
80 	 * Clears the error buffer, and uses it so we don't get
81 	 * "unused argument" warnings at compile time.
82 	 */
83 	strlcpy(errbuf, "", errbuf_size);
84 	return (0);
85 }
86 
87 /*
88  * Clean up anything that ndo_init() did.
89  */
90 void
91 nd_cleanup(void)
92 {
93 #ifdef USE_LIBSMI
94 	/*
95 	 * This appears, in libsmi 0.4.8, to do nothing if smiInit()
96 	 * wasn't done or failed, so we call it unconditionally.
97 	 */
98 	smiExit();
99 #endif
100 
101 #ifdef _WIN32
102 	/*
103 	 * Undo the WSAStartup() call above.
104 	 */
105 	WSACleanup();
106 #endif
107 }
108 
109 int
110 nd_have_smi_support(void)
111 {
112 #ifdef USE_LIBSMI
113 	return (1);
114 #else
115 	return (0);
116 #endif
117 }
118 
119 /*
120  * Indicates whether an SMI module has been loaded, so that we can use
121  * libsmi to translate OIDs.
122  */
123 int nd_smi_module_loaded;
124 
125 int
126 nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
127 {
128 #ifdef USE_LIBSMI
129 	if (smiLoadModule(module) == 0) {
130 		snprintf(errbuf, errbuf_size, "could not load MIB module %s",
131 		    module);
132 		return (-1);
133 	}
134 	nd_smi_module_loaded = 1;
135 	return (0);
136 #else
137 	snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
138 	    module);
139 	return (-1);
140 #endif
141 }
142 
143 const char *
144 nd_smi_version_string(void)
145 {
146 #ifdef USE_LIBSMI
147 	return (smi_version_string);
148 #else
149 	return (NULL);
150 #endif
151 }
152