2 * WinPcap-specific interfaces for capturing. We load WinPcap at run
3 * time, so that we only need one Wireshark binary and one Twireshark binary
4 * for Windows, regardless of whether WinPcap is installed or not.
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
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 "capture-pcap-util.h"
39 #include "capture-pcap-util-int.h"
41 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
42 #include "tools/lemon/cppmagic.h"
45 #define MAX_WIN_IF_NAME_LEN 511
48 gboolean has_wpcap = FALSE;
53 * XXX - should we require at least WinPcap 3.1 both for building an
54 * for using Wireshark?
57 static char* (*p_pcap_lookupdev) (char *);
58 static void (*p_pcap_close) (pcap_t *);
59 static int (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
60 static int (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, guchar *);
61 static int (*p_pcap_snapshot) (pcap_t *);
62 static int (*p_pcap_datalink) (pcap_t *);
63 static int (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
64 static char* (*p_pcap_geterr) (pcap_t *);
65 static int (*p_pcap_compile) (pcap_t *, struct bpf_program *, char *, int,
67 #ifdef WPCAP_CONSTIFIED
68 static int (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
70 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
72 static int (*p_pcap_lookupnet) (char *, bpf_u_int32 *, bpf_u_int32 *,
74 static pcap_t* (*p_pcap_open_live) (char *, int, int, int, char *);
76 static int (*p_pcap_loop) (pcap_t *, int, pcap_handler, guchar *);
77 static void (*p_pcap_freecode) (struct bpf_program *);
78 #ifdef HAVE_PCAP_FINDALLDEVS
79 static int (*p_pcap_findalldevs) (pcap_if_t **, char *);
80 static void (*p_pcap_freealldevs) (pcap_if_t *);
82 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
83 static int (*p_pcap_datalink_name_to_val) (const char *);
85 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
86 static const char *(*p_pcap_datalink_val_to_name) (int);
88 #ifdef HAVE_PCAP_BREAKLOOP
89 static void (*p_pcap_breakloop) (pcap_t *);
91 static const char *(*p_pcap_lib_version) (void);
92 static int (*p_pcap_setbuff) (pcap_t *, int dim);
93 static int (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
101 #define SYM(x, y) { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
107 /* These are the symbols I need or want from Wpcap */
108 static const symbol_table_t symbols[] = {
109 SYM(pcap_lookupdev, FALSE),
110 SYM(pcap_close, FALSE),
111 SYM(pcap_stats, FALSE),
112 SYM(pcap_dispatch, FALSE),
113 SYM(pcap_snapshot, FALSE),
114 SYM(pcap_datalink, FALSE),
115 SYM(pcap_setfilter, FALSE),
116 SYM(pcap_geterr, FALSE),
117 SYM(pcap_compile, FALSE),
118 SYM(pcap_lookupnet, FALSE),
119 SYM(pcap_open_live, FALSE),
120 SYM(pcap_loop, FALSE),
121 SYM(pcap_freecode, TRUE),
122 #ifdef HAVE_PCAP_FINDALLDEVS
123 SYM(pcap_findalldevs, TRUE),
124 SYM(pcap_freealldevs, TRUE),
126 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
127 SYM(pcap_datalink_name_to_val, TRUE),
129 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
130 SYM(pcap_datalink_val_to_name, TRUE),
132 #ifdef HAVE_PCAP_BREAKLOOP
134 * We don't try to work around the lack of this at
135 * run time; it's present in WinPcap 3.1, which is
136 * the version we build with and ship with.
138 SYM(pcap_breakloop, FALSE),
140 SYM(pcap_lib_version, TRUE),
141 SYM(pcap_setbuff, TRUE),
142 SYM(pcap_next_ex, TRUE),
143 { NULL, NULL, FALSE }
146 GModule *wh; /* wpcap handle */
147 const symbol_table_t *sym;
149 wh = g_module_open("wpcap", 0);
157 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
160 * We don't care if it's missing; we just
166 * We require this symbol.
179 pcap_lookupdev (char *a)
182 return p_pcap_lookupdev(a);
186 pcap_close(pcap_t *a)
193 pcap_stats(pcap_t *a, struct pcap_stat *b)
196 return p_pcap_stats(a, b);
200 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
203 return p_pcap_dispatch(a, b, c, d);
207 pcap_snapshot(pcap_t *a)
210 return p_pcap_snapshot(a);
214 pcap_datalink(pcap_t *a)
217 return p_pcap_datalink(a);
221 pcap_setfilter(pcap_t *a, struct bpf_program *b)
224 return p_pcap_setfilter(a, b);
228 pcap_geterr(pcap_t *a)
231 return p_pcap_geterr(a);
235 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
239 return p_pcap_compile(a, b, c, d, e);
243 #ifdef WPCAP_CONSTIFIED
244 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
246 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
250 return p_pcap_lookupnet(a, b, c, d);
254 #ifdef WPCAP_CONSTIFIED
255 pcap_open_live(const char *a, int b, int c, int d, char *e)
257 pcap_open_live(char *a, int b, int c, int d, char *e)
261 return p_pcap_open_live(a, b, c, d, e);
265 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
268 return p_pcap_loop(a, b, c, d);
272 pcap_freecode(struct bpf_program *a)
275 if(p_pcap_freecode) {
280 #ifdef HAVE_PCAP_FINDALLDEVS
282 pcap_findalldevs(pcap_if_t **a, char *b)
284 g_assert(has_wpcap && p_pcap_findalldevs != NULL);
285 return p_pcap_findalldevs(a, b);
289 pcap_freealldevs(pcap_if_t *a)
291 g_assert(has_wpcap && p_pcap_freealldevs != NULL);
292 p_pcap_freealldevs(a);
296 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
298 * Table of DLT_ types, names, and descriptions, for use if the version
299 * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
300 * or "pcap_datalink_val_to_name()".
304 const char *description;
308 #define DLT_CHOICE(code, description) { #code, description, code }
309 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
311 static struct dlt_choice dlt_choices[] = {
312 DLT_CHOICE(DLT_NULL, "BSD loopback"),
313 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
314 DLT_CHOICE(DLT_IEEE802, "Token ring"),
315 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
316 DLT_CHOICE(DLT_SLIP, "SLIP"),
317 DLT_CHOICE(DLT_PPP, "PPP"),
318 DLT_CHOICE(DLT_FDDI, "FDDI"),
319 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
320 DLT_CHOICE(DLT_RAW, "Raw IP"),
321 #ifdef DLT_SLIP_BSDOS
322 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
325 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
328 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
330 #ifdef DLT_PPP_SERIAL
331 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
334 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
337 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
339 #ifdef DLT_IEEE802_11
340 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
343 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
346 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
349 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
352 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
355 DLT_CHOICE(DLT_LTALK, "Localtalk"),
358 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
360 #ifdef DLT_PRISM_HEADER
361 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
363 #ifdef DLT_IP_OVER_FC
364 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
367 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
369 #ifdef DLT_IEEE802_11_RADIO
370 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
372 #ifdef DLT_ARCNET_LINUX
373 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
375 #ifdef DLT_LINUX_IRDA
376 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
378 #ifdef DLT_LINUX_LAPD
379 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
382 DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
385 DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
388 DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
392 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
394 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
396 pcap_datalink_name_to_val(const char *name)
402 if (p_pcap_datalink_name_to_val != NULL)
403 return p_pcap_datalink_name_to_val(name);
406 * We don't have it in WinPcap; do it ourselves.
408 for (i = 0; dlt_choices[i].name != NULL; i++) {
409 if (strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
411 return dlt_choices[i].dlt;
418 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
420 pcap_datalink_val_to_name(int dlt)
426 if (p_pcap_datalink_val_to_name != NULL)
427 return p_pcap_datalink_val_to_name(dlt);
430 * We don't have it in WinPcap; do it ourselves.
432 for (i = 0; dlt_choices[i].name != NULL; i++) {
433 if (dlt_choices[i].dlt == dlt)
434 return dlt_choices[i].name + sizeof("DLT_") - 1;
441 #ifdef HAVE_PCAP_BREAKLOOP
442 void pcap_breakloop(pcap_t *a)
448 /* setbuff is win32 specific! */
449 int pcap_setbuff(pcap_t *a, int b)
452 return p_pcap_setbuff(a, b);
455 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
456 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
457 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
460 return p_pcap_next_ex(a, b, c);
464 * This will use "pcap_findalldevs()" if we have it, otherwise it'll
465 * fall back on "pcap_lookupdev()".
468 get_interface_list(int *err, char *err_str)
473 char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
474 char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
477 #ifdef HAVE_PCAP_FINDALLDEVS
478 if (p_pcap_findalldevs != NULL)
479 return get_interface_list_findalldevs(err, err_str);
483 * In WinPcap, pcap_lookupdev is implemented by calling
484 * PacketGetAdapterNames. According to the documentation
487 * http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
491 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
492 * of bytes consisting of:
494 * a sequence of null-terminated ASCII strings (i.e., each
495 * one is terminated by a single 0 byte), giving the names
498 * an empty ASCII string (i.e., a single 0 byte);
500 * a sequence of null-terminated ASCII strings, giving the
501 * descriptions of the interfaces;
503 * an empty ASCII string.
505 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
506 * returns a sequence of bytes consisting of:
508 * a sequence of null-terminated double-byte Unicode strings
509 * (i.e., each one consits of a sequence of double-byte
510 * characters, terminated by a double-byte 0), giving the
511 * names of the interfaces;
513 * an empty Unicode string (i.e., a double 0 byte);
515 * a sequence of null-terminated ASCII strings, giving the
516 * descriptions of the interfaces;
518 * an empty ASCII string.
520 * The Nth string in the first sequence is the name of the Nth
521 * adapter; the Nth string in the second sequence is the
522 * description of the Nth adapter.
525 names = (wchar_t *)pcap_lookupdev(err_str);
534 * If names[0] is less than 256 it means the first
535 * byte is 0. This implies that we are using Unicode
538 while (*(names+desc_pos) || *(names+desc_pos-1))
540 desc_pos++; /* Step over the extra '\0' */
541 desc = (char*)(names + desc_pos); /* cast *after* addition */
543 while (names[i] != 0) {
545 * Copy the Unicode description to an ASCII
550 if (j < MAX_WIN_IF_NAME_LEN)
551 ascii_desc[j++] = *desc;
554 ascii_desc[j] = '\0';
558 * Copy the Unicode name to an ASCII string.
561 while (names[i] != 0) {
562 if (j < MAX_WIN_IF_NAME_LEN)
563 ascii_name[j++] = (char) names[i++];
565 ascii_name[j] = '\0';
567 il = g_list_append(il,
568 if_info_new(ascii_name, ascii_desc));
572 * Otherwise we are in Windows 95/98 and using ASCII
573 * (8-bit) characters.
575 win95names=(char *)names;
576 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
578 desc_pos++; /* Step over the extra '\0' */
579 desc = win95names + desc_pos;
581 while (win95names[i] != '\0') {
583 * "&win95names[i]" points to the current
584 * interface name, and "desc" points to
585 * that interface's description.
587 il = g_list_append(il,
588 if_info_new(&win95names[i], desc));
591 * Skip to the next description.
598 * Skip to the next name.
600 while (win95names[i] != 0)
609 * No interfaces found.
611 *err = NO_INTERFACES_FOUND;
618 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
619 * "get_interface_list()".
622 cant_get_if_list_error_message(const char *err_str)
625 * If the error message includes "Not enough storage is available
626 * to process this command" or "The operation completed successfully",
627 * suggest that they install a WinPcap version later than 3.0.
629 if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
630 strstr(err_str, "The operation completed successfully") != NULL) {
631 return g_strdup_printf("Can't get list of interfaces: %s\n"
632 "This might be a problem with WinPcap 3.0; you should try updating to\n"
633 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
636 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
640 * Append the version of WinPcap with which we were compiled to a GString.
643 get_compiled_pcap_version(GString *str)
645 g_string_append(str, "with WinPcap (version unknown)");
649 * Append the version of WinPcap with which we we're running to a GString.
652 get_runtime_pcap_version(GString *str)
655 * On Windows, we might have been compiled with WinPcap but
656 * might not have it loaded; indicate whether we have it or
657 * not and, if we have it and we have "pcap_lib_version()",
658 * what version we have.
660 GModule *handle; /* handle returned by dlopen */
661 static gchar *packetVer;
665 g_string_sprintfa(str, "with ");
666 if (p_pcap_lib_version != NULL)
667 g_string_sprintfa(str, p_pcap_lib_version());
670 * An alternative method of obtaining the version
671 * number, by using the PacketLibraryVersion
672 * string from packet.dll.
674 * Unfortunately, in WinPcap 3.0, it returns
675 * "3.0 alpha3", even in the final version of
676 * WinPcap 3.0, so if there's a blank in the
677 * string, we strip it and everything after
678 * it from the string, so we don't misleadingly
679 * report that 3.0 alpha3 is being used when
680 * the final version is being used.
682 if (packetVer == NULL) {
683 packetVer = "version unknown";
684 handle = g_module_open("Packet.dll", 0);
685 if (handle != NULL) {
686 if (g_module_symbol(handle,
687 "PacketLibraryVersion",
688 (gpointer*)&packetVer)) {
689 packetVer = g_strdup(packetVer);
690 blankp = strchr(packetVer, ' ');
694 packetVer = "version unknown";
696 g_module_close(handle);
699 g_string_sprintfa(str, "WinPcap (%s)", packetVer);
702 g_string_append(str, "without WinPcap");
703 g_string_append(str, " ");
706 #else /* HAVE_LIBPCAP */
715 * Append an indication that we were not compiled with WinPcap
719 get_compiled_pcap_version(GString *str)
721 g_string_append(str, "without WinPcap");
725 * Don't append anything, as we weren't even compiled to use WinPcap.
728 get_runtime_pcap_version(GString *str _U_)
732 #endif /* HAVE_LIBPCAP */