2 * WinPcap-specific interfaces for capturing. We load WinPcap at run
3 * time, so that we only need one Ethereal binary and one Tethereal binary
4 * for Windows, regardless of whether WinPcap is installed or not.
6 * $Id: capture-wpcap.c,v 1.7 2003/12/21 12:18:59 ulfl Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 2001 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include "pcap-util.h"
39 #include "pcap-util-int.h"
41 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
42 #include "tools/lemon/cppmagic.h"
44 gboolean has_wpcap = FALSE;
48 static char* (*p_pcap_lookupdev) (char *);
49 static void (*p_pcap_close) (pcap_t *);
50 static int (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
51 static int (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, guchar *);
52 static int (*p_pcap_snapshot) (pcap_t *);
53 static int (*p_pcap_datalink) (pcap_t *);
54 static int (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
55 static char* (*p_pcap_geterr) (pcap_t *);
56 static int (*p_pcap_compile) (pcap_t *, struct bpf_program *, char *, int,
58 #ifdef WPCAP_CONSTIFIED
59 static int (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
61 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
63 static int (*p_pcap_lookupnet) (char *, bpf_u_int32 *, bpf_u_int32 *,
65 static pcap_t* (*p_pcap_open_live) (char *, int, int, int, char *);
67 static int (*p_pcap_loop) (pcap_t *, int, pcap_handler, guchar *);
68 #ifdef HAVE_PCAP_FINDALLDEVS
69 static int (*p_pcap_findalldevs) (pcap_if_t **, char *);
70 static void (*p_pcap_freealldevs) (pcap_if_t *);
72 static const char *(*p_pcap_lib_version) (void);
80 #define SYM(x, y) { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
86 /* These are the symbols I need or want from Wpcap */
87 static const symbol_table_t symbols[] = {
88 SYM(pcap_lookupdev, FALSE),
89 SYM(pcap_close, FALSE),
90 SYM(pcap_stats, FALSE),
91 SYM(pcap_dispatch, FALSE),
92 SYM(pcap_snapshot, FALSE),
93 SYM(pcap_datalink, FALSE),
94 SYM(pcap_setfilter, FALSE),
95 SYM(pcap_geterr, FALSE),
96 SYM(pcap_compile, FALSE),
97 SYM(pcap_lookupnet, FALSE),
98 SYM(pcap_open_live, FALSE),
99 SYM(pcap_loop, FALSE),
100 #ifdef HAVE_PCAP_FINDALLDEVS
101 SYM(pcap_findalldevs, TRUE),
102 SYM(pcap_freealldevs, TRUE),
104 SYM(pcap_lib_version, TRUE),
105 { NULL, NULL, FALSE }
108 GModule *wh; /* wpcap handle */
109 const symbol_table_t *sym;
111 wh = g_module_open("wpcap", 0);
119 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
122 * We don't care if it's missing; we just
128 * We require this symbol.
141 pcap_lookupdev (char *a)
144 return p_pcap_lookupdev(a);
148 pcap_close(pcap_t *a)
155 pcap_stats(pcap_t *a, struct pcap_stat *b)
158 return p_pcap_stats(a, b);
162 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
165 return p_pcap_dispatch(a, b, c, d);
170 pcap_snapshot(pcap_t *a)
173 return p_pcap_snapshot(a);
178 pcap_datalink(pcap_t *a)
181 return p_pcap_datalink(a);
185 pcap_setfilter(pcap_t *a, struct bpf_program *b)
188 return p_pcap_setfilter(a, b);
192 pcap_geterr(pcap_t *a)
195 return p_pcap_geterr(a);
199 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
203 return p_pcap_compile(a, b, c, d, e);
207 #ifdef WPCAP_CONSTIFIED
208 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
210 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
214 return p_pcap_lookupnet(a, b, c, d);
218 #ifdef WPCAP_CONSTIFIED
219 pcap_open_live(const char *a, int b, int c, int d, char *e)
221 pcap_open_live(char *a, int b, int c, int d, char *e)
225 return p_pcap_open_live(a, b, c, d, e);
229 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
232 return p_pcap_loop(a, b, c, d);
235 #ifdef HAVE_PCAP_FINDALLDEVS
237 pcap_findalldevs(pcap_if_t **a, char *b)
239 g_assert(has_wpcap && p_pcap_findalldevs != NULL);
240 return p_pcap_findalldevs(a, b);
244 pcap_freealldevs(pcap_if_t *a)
246 g_assert(has_wpcap && p_pcap_freealldevs != NULL);
247 p_pcap_freealldevs(a);
252 * This will use "pcap_findalldevs()" if we have it, otherwise it'll
253 * fall back on "pcap_lookupdev()".
256 get_interface_list(int *err, char *err_str)
261 char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
262 char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
265 #ifdef HAVE_PCAP_FINDALLDEVS
266 if (p_pcap_findalldevs != NULL)
267 return get_interface_list_findalldevs(err, err_str);
271 * In WinPcap, pcap_lookupdev is implemented by calling
272 * PacketGetAdapterNames. According to the documentation
275 * http://winpcap.polito.it/docs/man/html/Packet32_8c.html#a43
279 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
280 * of bytes consisting of:
282 * a sequence of null-terminated ASCII strings (i.e., each
283 * one is terminated by a single 0 byte), giving the names
286 * an empty ASCII string (i.e., a single 0 byte);
288 * a sequence of null-terminated ASCII strings, giving the
289 * descriptions of the interfaces;
291 * an empty ASCII string.
293 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
294 * returns a sequence of bytes consisting of:
296 * a sequence of null-terminated double-byte Unicode strings
297 * (i.e., each one consits of a sequence of double-byte
298 * characters, terminated by a double-byte 0), giving the
299 * names of the interfaces;
301 * an empty Unicode string (i.e., a double 0 byte);
303 * a sequence of null-terminated ASCII strings, giving the
304 * descriptions of the interfaces;
306 * an empty ASCII string.
308 * The Nth string in the first sequence is the name of the Nth
309 * adapter; the Nth string in the second sequence is the
310 * description of the Nth adapter.
313 names = (wchar_t *)pcap_lookupdev(err_str);
322 * If names[0] is less than 256 it means the first
323 * byte is 0. This implies that we are using Unicode
326 while (*(names+desc_pos) || *(names+desc_pos-1))
328 desc_pos++; /* Step over the extra '\0' */
329 desc = (char*)(names + desc_pos); /* cast *after* addition */
331 while (names[i] != 0) {
333 * Copy the Unicode description to an ASCII
338 if (j < MAX_WIN_IF_NAME_LEN)
339 ascii_desc[j++] = *desc;
342 ascii_desc[j] = '\0';
346 * Copy the Unicode name to an ASCII string.
349 while (names[i] != 0) {
350 if (j < MAX_WIN_IF_NAME_LEN)
351 ascii_name[j++] = names[i++];
353 ascii_name[j] = '\0';
355 il = g_list_append(il,
356 if_info_new(ascii_name, ascii_desc));
360 * Otherwise we are in Windows 95/98 and using ASCII
361 * (8-bit) characters.
363 win95names=(char *)names;
364 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
366 desc_pos++; /* Step over the extra '\0' */
367 desc = win95names + desc_pos;
369 while (win95names[i] != '\0') {
371 * "&win95names[i]" points to the current
372 * interface name, and "desc" points to
373 * that interface's description.
375 il = g_list_append(il,
376 if_info_new(&win95names[i], desc));
379 * Skip to the next description.
386 * Skip to the next name.
388 while (win95names[i] != 0)
397 * No interfaces found.
399 *err = NO_INTERFACES_FOUND;
406 * Append the version of WinPcap with which we were compiled to a GString.
409 get_compiled_pcap_version(GString *str)
411 g_string_append(str, "with WinPcap (version unknown)");
415 * Append the version of WinPcap with which we we're running to a GString.
418 get_runtime_pcap_version(GString *str)
421 * On Windows, we might have been compiled with WinPcap but
422 * might not have it loaded; indicate whether we have it or
423 * not and, if we have it and we have "pcap_lib_version()",
424 * what version we have.
427 g_string_sprintfa(str, "with ");
428 if (p_pcap_lib_version != NULL)
429 g_string_sprintfa(str, p_pcap_lib_version());
431 g_string_append(str, "WinPcap (version unknown)");
433 g_string_append(str, "without WinPcap");
434 g_string_append(str, " ");
437 #else /* HAVE_LIBPCAP */
446 * Append an indication that we were not compiled with WinPcap
450 get_compiled_pcap_version(GString *str)
452 g_string_append(str, "without WinPcap");
456 * Don't append anything, as we weren't even compiled to use WinPcap.
459 get_runtime_pcap_version(GString *str _U_)
463 #endif /* HAVE_LIBPCAP */