2 * WinPcap-specific interfaces for capturing. We load WinPcap at run
3 * time, so that we only need one Wireshark binary and one TShark 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];
476 char errbuf[PCAP_ERRBUF_SIZE];
478 #ifdef HAVE_PCAP_FINDALLDEVS
479 if (p_pcap_findalldevs != NULL)
480 return get_interface_list_findalldevs(err, err_str);
484 * In WinPcap, pcap_lookupdev is implemented by calling
485 * PacketGetAdapterNames. According to the documentation
488 * http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
492 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
493 * of bytes consisting of:
495 * a sequence of null-terminated ASCII strings (i.e., each
496 * one is terminated by a single 0 byte), giving the names
499 * an empty ASCII string (i.e., a single 0 byte);
501 * a sequence of null-terminated ASCII strings, giving the
502 * descriptions of the interfaces;
504 * an empty ASCII string.
506 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
507 * returns a sequence of bytes consisting of:
509 * a sequence of null-terminated double-byte Unicode strings
510 * (i.e., each one consits of a sequence of double-byte
511 * characters, terminated by a double-byte 0), giving the
512 * names of the interfaces;
514 * an empty Unicode string (i.e., a double 0 byte);
516 * a sequence of null-terminated ASCII strings, giving the
517 * descriptions of the interfaces;
519 * an empty ASCII string.
521 * The Nth string in the first sequence is the name of the Nth
522 * adapter; the Nth string in the second sequence is the
523 * description of the Nth adapter.
526 names = (wchar_t *)pcap_lookupdev(errbuf);
535 * If names[0] is less than 256 it means the first
536 * byte is 0. This implies that we are using Unicode
539 while (*(names+desc_pos) || *(names+desc_pos-1))
541 desc_pos++; /* Step over the extra '\0' */
542 desc = (char*)(names + desc_pos); /* cast *after* addition */
544 while (names[i] != 0) {
546 * Copy the Unicode description to an ASCII
551 if (j < MAX_WIN_IF_NAME_LEN)
552 ascii_desc[j++] = *desc;
555 ascii_desc[j] = '\0';
559 * Copy the Unicode name to an ASCII string.
562 while (names[i] != 0) {
563 if (j < MAX_WIN_IF_NAME_LEN)
564 ascii_name[j++] = (char) names[i++];
566 ascii_name[j] = '\0';
568 il = g_list_append(il,
569 if_info_new(ascii_name, ascii_desc));
573 * Otherwise we are in Windows 95/98 and using ASCII
574 * (8-bit) characters.
576 win95names=(char *)names;
577 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
579 desc_pos++; /* Step over the extra '\0' */
580 desc = win95names + desc_pos;
582 while (win95names[i] != '\0') {
584 * "&win95names[i]" points to the current
585 * interface name, and "desc" points to
586 * that interface's description.
588 il = g_list_append(il,
589 if_info_new(&win95names[i], desc));
592 * Skip to the next description.
599 * Skip to the next name.
601 while (win95names[i] != 0)
610 * No interfaces found.
612 *err = NO_INTERFACES_FOUND;
621 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
622 * "get_interface_list()".
625 cant_get_if_list_error_message(const char *err_str)
628 * If the error message includes "Not enough storage is available
629 * to process this command" or "The operation completed successfully",
630 * suggest that they install a WinPcap version later than 3.0.
632 if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
633 strstr(err_str, "The operation completed successfully") != NULL) {
634 return g_strdup_printf("Can't get list of interfaces: %s\n"
635 "This might be a problem with WinPcap 3.0; you should try updating to\n"
636 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
639 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
643 * Append the version of WinPcap with which we were compiled to a GString.
646 get_compiled_pcap_version(GString *str)
648 g_string_append(str, "with WinPcap (version unknown)");
652 * Append the version of WinPcap with which we we're running to a GString.
655 get_runtime_pcap_version(GString *str)
658 * On Windows, we might have been compiled with WinPcap but
659 * might not have it loaded; indicate whether we have it or
660 * not and, if we have it and we have "pcap_lib_version()",
661 * what version we have.
663 GModule *handle; /* handle returned by dlopen */
664 static gchar *packetVer;
668 g_string_sprintfa(str, "with ");
669 if (p_pcap_lib_version != NULL)
670 g_string_sprintfa(str, p_pcap_lib_version());
673 * An alternative method of obtaining the version
674 * number, by using the PacketLibraryVersion
675 * string from packet.dll.
677 * Unfortunately, in WinPcap 3.0, it returns
678 * "3.0 alpha3", even in the final version of
679 * WinPcap 3.0, so if there's a blank in the
680 * string, we strip it and everything after
681 * it from the string, so we don't misleadingly
682 * report that 3.0 alpha3 is being used when
683 * the final version is being used.
685 if (packetVer == NULL) {
686 packetVer = "version unknown";
687 handle = g_module_open("Packet.dll", 0);
688 if (handle != NULL) {
689 if (g_module_symbol(handle,
690 "PacketLibraryVersion",
691 (gpointer*)&packetVer)) {
692 packetVer = g_strdup(packetVer);
693 blankp = strchr(packetVer, ' ');
697 packetVer = "version unknown";
699 g_module_close(handle);
702 g_string_sprintfa(str, "WinPcap (%s)", packetVer);
705 g_string_append(str, "without WinPcap");
708 #else /* HAVE_LIBPCAP */
717 * Append an indication that we were not compiled with WinPcap
721 get_compiled_pcap_version(GString *str)
723 g_string_append(str, "without WinPcap");
727 * Don't append anything, as we weren't even compiled to use WinPcap.
730 get_runtime_pcap_version(GString *str _U_)
734 #endif /* HAVE_LIBPCAP */