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.
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 static void (*p_pcap_freecode) (struct bpf_program *);
69 #ifdef HAVE_PCAP_FINDALLDEVS
70 static int (*p_pcap_findalldevs) (pcap_if_t **, char *);
71 static void (*p_pcap_freealldevs) (pcap_if_t *);
73 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
74 static int (*p_pcap_datalink_name_to_val) (const char *);
76 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
77 static const char *(*p_pcap_datalink_val_to_name) (int);
79 static const char *(*p_pcap_lib_version) (void);
80 static int (*p_pcap_setbuff) (pcap_t *, int dim);
81 static int (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
89 #define SYM(x, y) { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
95 /* These are the symbols I need or want from Wpcap */
96 static const symbol_table_t symbols[] = {
97 SYM(pcap_lookupdev, FALSE),
98 SYM(pcap_close, FALSE),
99 SYM(pcap_stats, FALSE),
100 SYM(pcap_dispatch, FALSE),
101 SYM(pcap_snapshot, FALSE),
102 SYM(pcap_datalink, FALSE),
103 SYM(pcap_setfilter, FALSE),
104 SYM(pcap_geterr, FALSE),
105 SYM(pcap_compile, FALSE),
106 SYM(pcap_lookupnet, FALSE),
107 SYM(pcap_open_live, FALSE),
108 SYM(pcap_loop, FALSE),
109 SYM(pcap_freecode, TRUE),
110 #ifdef HAVE_PCAP_FINDALLDEVS
111 SYM(pcap_findalldevs, TRUE),
112 SYM(pcap_freealldevs, TRUE),
114 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
115 SYM(pcap_datalink_name_to_val, TRUE),
117 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
118 SYM(pcap_datalink_val_to_name, TRUE),
120 SYM(pcap_lib_version, TRUE),
121 SYM(pcap_setbuff, TRUE),
122 SYM(pcap_next_ex, TRUE),
123 { NULL, NULL, FALSE }
126 GModule *wh; /* wpcap handle */
127 const symbol_table_t *sym;
129 wh = g_module_open("wpcap", 0);
137 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
140 * We don't care if it's missing; we just
146 * We require this symbol.
159 pcap_lookupdev (char *a)
162 return p_pcap_lookupdev(a);
166 pcap_close(pcap_t *a)
173 pcap_stats(pcap_t *a, struct pcap_stat *b)
176 return p_pcap_stats(a, b);
180 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
183 return p_pcap_dispatch(a, b, c, d);
187 pcap_snapshot(pcap_t *a)
190 return p_pcap_snapshot(a);
194 pcap_datalink(pcap_t *a)
197 return p_pcap_datalink(a);
201 pcap_setfilter(pcap_t *a, struct bpf_program *b)
204 return p_pcap_setfilter(a, b);
208 pcap_geterr(pcap_t *a)
211 return p_pcap_geterr(a);
215 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
219 return p_pcap_compile(a, b, c, d, e);
223 #ifdef WPCAP_CONSTIFIED
224 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
226 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
230 return p_pcap_lookupnet(a, b, c, d);
234 #ifdef WPCAP_CONSTIFIED
235 pcap_open_live(const char *a, int b, int c, int d, char *e)
237 pcap_open_live(char *a, int b, int c, int d, char *e)
241 return p_pcap_open_live(a, b, c, d, e);
245 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
248 return p_pcap_loop(a, b, c, d);
252 pcap_freecode(struct bpf_program *a)
255 if(p_pcap_freecode) {
260 #ifdef HAVE_PCAP_FINDALLDEVS
262 pcap_findalldevs(pcap_if_t **a, char *b)
264 g_assert(has_wpcap && p_pcap_findalldevs != NULL);
265 return p_pcap_findalldevs(a, b);
269 pcap_freealldevs(pcap_if_t *a)
271 g_assert(has_wpcap && p_pcap_freealldevs != NULL);
272 p_pcap_freealldevs(a);
276 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
278 * Table of DLT_ types, names, and descriptions, for use if the version
279 * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
280 * or "pcap_datalink_val_to_name()".
284 const char *description;
288 #define DLT_CHOICE(code, description) { #code, description, code }
289 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
291 static struct dlt_choice dlt_choices[] = {
292 DLT_CHOICE(DLT_NULL, "BSD loopback"),
293 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
294 DLT_CHOICE(DLT_IEEE802, "Token ring"),
295 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
296 DLT_CHOICE(DLT_SLIP, "SLIP"),
297 DLT_CHOICE(DLT_PPP, "PPP"),
298 DLT_CHOICE(DLT_FDDI, "FDDI"),
299 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
300 DLT_CHOICE(DLT_RAW, "Raw IP"),
301 #ifdef DLT_SLIP_BSDOS
302 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
305 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
308 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
310 #ifdef DLT_PPP_SERIAL
311 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
314 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
317 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
319 #ifdef DLT_IEEE802_11
320 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
323 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
326 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
329 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
332 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
335 DLT_CHOICE(DLT_LTALK, "Localtalk"),
338 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
340 #ifdef DLT_PRISM_HEADER
341 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
343 #ifdef DLT_IP_OVER_FC
344 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
347 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
349 #ifdef DLT_IEEE802_11_RADIO
350 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
352 #ifdef DLT_ARCNET_LINUX
353 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
355 #ifdef DLT_LINUX_IRDA
356 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
359 DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
362 DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
365 DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
369 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
371 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
373 pcap_datalink_name_to_val(const char *name)
379 if (p_pcap_datalink_name_to_val != NULL)
380 return p_pcap_datalink_name_to_val(name);
383 * We don't have it in WinPcap; do it ourselves.
385 for (i = 0; dlt_choices[i].name != NULL; i++) {
386 if (strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
388 return dlt_choices[i].dlt;
395 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
397 pcap_datalink_val_to_name(int dlt)
403 if (p_pcap_datalink_val_to_name != NULL)
404 return p_pcap_datalink_val_to_name(dlt);
407 * We don't have it in WinPcap; do it ourselves.
409 for (i = 0; dlt_choices[i].name != NULL; i++) {
410 if (dlt_choices[i].dlt == dlt)
411 return dlt_choices[i].name + sizeof("DLT_") - 1;
418 /* setbuff is win32 specific! */
419 int pcap_setbuff(pcap_t *a, int b)
422 return p_pcap_setbuff(a, b);
425 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
426 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
427 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
430 return p_pcap_next_ex(a, b, c);
434 * This will use "pcap_findalldevs()" if we have it, otherwise it'll
435 * fall back on "pcap_lookupdev()".
438 get_interface_list(int *err, char *err_str)
443 char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
444 char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
447 #ifdef HAVE_PCAP_FINDALLDEVS
448 if (p_pcap_findalldevs != NULL)
449 return get_interface_list_findalldevs(err, err_str);
453 * In WinPcap, pcap_lookupdev is implemented by calling
454 * PacketGetAdapterNames. According to the documentation
457 * http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
461 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
462 * of bytes consisting of:
464 * a sequence of null-terminated ASCII strings (i.e., each
465 * one is terminated by a single 0 byte), giving the names
468 * an empty ASCII string (i.e., a single 0 byte);
470 * a sequence of null-terminated ASCII strings, giving the
471 * descriptions of the interfaces;
473 * an empty ASCII string.
475 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
476 * returns a sequence of bytes consisting of:
478 * a sequence of null-terminated double-byte Unicode strings
479 * (i.e., each one consits of a sequence of double-byte
480 * characters, terminated by a double-byte 0), giving the
481 * names of the interfaces;
483 * an empty Unicode string (i.e., a double 0 byte);
485 * a sequence of null-terminated ASCII strings, giving the
486 * descriptions of the interfaces;
488 * an empty ASCII string.
490 * The Nth string in the first sequence is the name of the Nth
491 * adapter; the Nth string in the second sequence is the
492 * description of the Nth adapter.
495 names = (wchar_t *)pcap_lookupdev(err_str);
504 * If names[0] is less than 256 it means the first
505 * byte is 0. This implies that we are using Unicode
508 while (*(names+desc_pos) || *(names+desc_pos-1))
510 desc_pos++; /* Step over the extra '\0' */
511 desc = (char*)(names + desc_pos); /* cast *after* addition */
513 while (names[i] != 0) {
515 * Copy the Unicode description to an ASCII
520 if (j < MAX_WIN_IF_NAME_LEN)
521 ascii_desc[j++] = *desc;
524 ascii_desc[j] = '\0';
528 * Copy the Unicode name to an ASCII string.
531 while (names[i] != 0) {
532 if (j < MAX_WIN_IF_NAME_LEN)
533 ascii_name[j++] = (char) names[i++];
535 ascii_name[j] = '\0';
537 il = g_list_append(il,
538 if_info_new(ascii_name, ascii_desc));
542 * Otherwise we are in Windows 95/98 and using ASCII
543 * (8-bit) characters.
545 win95names=(char *)names;
546 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
548 desc_pos++; /* Step over the extra '\0' */
549 desc = win95names + desc_pos;
551 while (win95names[i] != '\0') {
553 * "&win95names[i]" points to the current
554 * interface name, and "desc" points to
555 * that interface's description.
557 il = g_list_append(il,
558 if_info_new(&win95names[i], desc));
561 * Skip to the next description.
568 * Skip to the next name.
570 while (win95names[i] != 0)
579 * No interfaces found.
581 *err = NO_INTERFACES_FOUND;
588 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
589 * "get_interface_list()".
592 cant_get_if_list_error_message(const char *err_str)
595 * If the error message includes "Not enough storage is available
596 * to process this command" or "The operation completed successfully",
597 * suggest that they install a WinPcap version later than 3.0.
599 if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
600 strstr(err_str, "The operation completed successfully") != NULL) {
601 return g_strdup_printf("Can't get list of interfaces: %s\n"
602 "This might be a problem with WinPcap 3.0; you should try updating to\n"
603 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
606 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
610 * Append the version of WinPcap with which we were compiled to a GString.
613 get_compiled_pcap_version(GString *str)
615 g_string_append(str, "with WinPcap (version unknown)");
619 * Append the version of WinPcap with which we we're running to a GString.
622 get_runtime_pcap_version(GString *str)
625 * On Windows, we might have been compiled with WinPcap but
626 * might not have it loaded; indicate whether we have it or
627 * not and, if we have it and we have "pcap_lib_version()",
628 * what version we have.
630 GModule *handle; /* handle returned by dlopen */
631 static gchar *packetVer;
635 g_string_sprintfa(str, "with ");
636 if (p_pcap_lib_version != NULL)
637 g_string_sprintfa(str, p_pcap_lib_version());
640 * An alternative method of obtaining the version
641 * number, by using the PacketLibraryVersion"
642 * string from packet.dll.
644 * Unfortunately, in WinPcap 3.0, it returns
645 * "3.0 alpha3", even in the final version of
646 * WinPcap 3.0, so if there's a blank in the
647 * string, we strip it and everything after
648 * it from the string, so we don't misleadingly
649 * report that 3.0 alpha3 is being used when
650 * the final version is being used.
652 if (packetVer == NULL) {
653 packetVer = "version unknown";
654 handle = g_module_open("Packet.dll", 0);
655 if (handle != NULL) {
656 if (g_module_symbol(handle,
657 "PacketLibraryVersion",
658 (gpointer*)&packetVer)) {
659 packetVer = g_strdup(packetVer);
660 blankp = strchr(packetVer, ' ');
664 packetVer = "version unknown";
666 g_module_close(handle);
669 g_string_sprintfa(str, "WinPcap (%s)", packetVer);
672 g_string_append(str, "without WinPcap");
673 g_string_append(str, " ");
676 #else /* HAVE_LIBPCAP */
685 * Append an indication that we were not compiled with WinPcap
689 get_compiled_pcap_version(GString *str)
691 g_string_append(str, "without WinPcap");
695 * Don't append anything, as we weren't even compiled to use WinPcap.
698 get_runtime_pcap_version(GString *str _U_)
702 #endif /* HAVE_LIBPCAP */