Merge commit '37e84ab74e939caf52150fc3352081786ecc0c29' into merges
[unleashed.git] / contrib / tcpdump / netdissect.c
blob0215c83b7f23ab3483e08d357f3ae7b19c4d7b38
1 /*
2 * Copyright (c) 1988-1997
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1998-2012 Michael Richardson <mcr@tcpdump.org>
6 * The TCPDUMP project
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.
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <netdissect-stdinc.h>
30 #include "netdissect.h"
31 #include <string.h>
32 #include <stdio.h>
34 #ifdef USE_LIBSMI
35 #include <smi.h>
36 #endif
39 * Initialize anything that must be initialized before dissecting
40 * packets.
42 * This should be called at the beginning of the program; it does
43 * not need to be called, and should not be called, for every
44 * netdissect_options structure.
46 int
47 nd_init(char *errbuf, size_t errbuf_size)
49 #ifdef _WIN32
50 WORD wVersionRequested;
51 WSADATA wsaData;
52 int err;
55 * Request Winsock 2.2; we expect Winsock 2.
57 wVersionRequested = MAKEWORD(2, 2);
58 err = WSAStartup(wVersionRequested, &wsaData);
59 if (err != 0) {
60 strlcpy(errbuf, "Attempting to initialize Winsock failed",
61 errbuf_size);
62 return (-1);
64 #endif /* _WIN32 */
66 #ifdef USE_LIBSMI
68 * XXX - should we just fail if this fails? Some of the
69 * libsmi calls may fail.
71 smiInit("tcpdump");
72 #endif
75 * Clears the error buffer, and uses it so we don't get
76 * "unused argument" warnings at compile time.
78 strlcpy(errbuf, "", errbuf_size);
79 return (0);
83 * Clean up anything that ndo_init() did.
85 void
86 nd_cleanup(void)
88 #ifdef USE_LIBSMI
90 * This appears, in libsmi 0.4.8, to do nothing if smiInit()
91 * wasn't done or failed, so we call it unconditionally.
93 smiExit();
94 #endif
96 #ifdef _WIN32
98 * Undo the WSAStartup() call above.
100 WSACleanup();
101 #endif
105 nd_have_smi_support(void)
107 #ifdef USE_LIBSMI
108 return (1);
109 #else
110 return (0);
111 #endif
115 * Indicates whether an SMI module has been loaded, so that we can use
116 * libsmi to translate OIDs.
118 int nd_smi_module_loaded;
121 nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
123 #ifdef USE_LIBSMI
124 if (smiLoadModule(module) == 0) {
125 snprintf(errbuf, errbuf_size, "could not load MIB module %s",
126 module);
127 return (-1);
129 nd_smi_module_loaded = 1;
130 return (0);
131 #else
132 snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
133 module);
134 return (-1);
135 #endif
138 const char *
139 nd_smi_version_string(void)
141 #ifdef USE_LIBSMI
142 return (smi_version_string);
143 #else
144 return (NULL);
145 #endif