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.
39 #include "capture-pcap-util.h"
40 #include "capture-pcap-util-int.h"
42 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
43 #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);
94 #ifdef HAVE_PCAP_REMOTE
95 static pcap_t* (*p_pcap_open) (const char *, int, int, int,
96 struct pcap_rmtauth *, char *);
97 static int (*p_pcap_findalldevs_ex) (char *, struct pcap_rmtauth *,
98 pcap_if_t **, char *);
99 static int (*p_pcap_createsrcstr) (char *, int, const char *, const char *,
100 const char *, char *);
102 #ifdef HAVE_PCAP_SETSAMPLING
103 static struct pcap_samp* (*p_pcap_setsampling)(pcap_t *);
106 #ifdef HAVE_PCAP_LIST_DATALINKS
107 static int (*p_pcap_list_datalinks)(pcap_t *, int **);
110 #ifdef HAVE_PCAP_SET_DATALINK
111 static int (*p_pcap_set_datalink)(pcap_t *, int);
114 #ifdef HAVE_FREE_DATALINKS
115 static int (*p_pcap_free_datalinks)(int *);
124 #define SYM(x, y) { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
130 /* These are the symbols I need or want from Wpcap */
131 static const symbol_table_t symbols[] = {
132 SYM(pcap_lookupdev, FALSE),
133 SYM(pcap_close, FALSE),
134 SYM(pcap_stats, FALSE),
135 SYM(pcap_dispatch, FALSE),
136 SYM(pcap_snapshot, FALSE),
137 SYM(pcap_datalink, FALSE),
138 SYM(pcap_setfilter, FALSE),
139 SYM(pcap_geterr, FALSE),
140 SYM(pcap_compile, FALSE),
141 SYM(pcap_lookupnet, FALSE),
142 #ifdef HAVE_PCAP_REMOTE
143 SYM(pcap_open, FALSE),
144 SYM(pcap_findalldevs_ex, FALSE),
145 SYM(pcap_createsrcstr, FALSE),
147 SYM(pcap_open_live, FALSE),
149 #ifdef HAVE_PCAP_SETSAMPLING
150 SYM(pcap_setsampling, TRUE),
152 SYM(pcap_loop, FALSE),
153 SYM(pcap_freecode, TRUE),
154 #ifdef HAVE_PCAP_FINDALLDEVS
155 SYM(pcap_findalldevs, TRUE),
156 SYM(pcap_freealldevs, TRUE),
158 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
159 SYM(pcap_datalink_name_to_val, TRUE),
161 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
162 SYM(pcap_datalink_val_to_name, TRUE),
164 #ifdef HAVE_PCAP_BREAKLOOP
166 * We don't try to work around the lack of this at
167 * run time; it's present in WinPcap 3.1, which is
168 * the version we build with and ship with.
170 SYM(pcap_breakloop, FALSE),
172 SYM(pcap_lib_version, TRUE),
173 SYM(pcap_setbuff, TRUE),
174 SYM(pcap_next_ex, TRUE),
175 #ifdef HAVE_PCAP_LIST_DATALINKS
176 SYM(pcap_list_datalinks, FALSE),
178 #ifdef HAVE_PCAP_SET_DATALINK
179 SYM(pcap_set_datalink, FALSE),
181 #ifdef HAVE_PCAP_FREE_DATALINKS
182 SYM(pcap_free_datalinks, TRUE),
184 { NULL, NULL, FALSE }
187 GModule *wh; /* wpcap handle */
188 const symbol_table_t *sym;
190 wh = g_module_open("wpcap", 0);
198 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
201 * We don't care if it's missing; we just
207 * We require this symbol.
220 pcap_lookupdev (char *a)
223 return p_pcap_lookupdev(a);
227 pcap_close(pcap_t *a)
234 pcap_stats(pcap_t *a, struct pcap_stat *b)
237 return p_pcap_stats(a, b);
241 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
244 return p_pcap_dispatch(a, b, c, d);
248 pcap_snapshot(pcap_t *a)
251 return p_pcap_snapshot(a);
255 pcap_datalink(pcap_t *a)
258 return p_pcap_datalink(a);
261 #ifdef HAVE_PCAP_SET_DATALINK
263 pcap_set_datalink(pcap_t *p, int dlt)
266 return p_pcap_set_datalink(p, dlt);
271 pcap_setfilter(pcap_t *a, struct bpf_program *b)
274 return p_pcap_setfilter(a, b);
278 pcap_geterr(pcap_t *a)
281 return p_pcap_geterr(a);
285 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
289 return p_pcap_compile(a, b, c, d, e);
293 #ifdef WPCAP_CONSTIFIED
294 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
296 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
300 return p_pcap_lookupnet(a, b, c, d);
304 #ifdef WPCAP_CONSTIFIED
305 pcap_open_live(const char *a, int b, int c, int d, char *e)
307 pcap_open_live(char *a, int b, int c, int d, char *e)
311 return p_pcap_open_live(a, b, c, d, e);
314 #ifdef HAVE_PCAP_REMOTE
316 pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
319 return p_pcap_open(a, b, c, d, e, f);
323 pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
326 return p_pcap_findalldevs_ex(a, b, c, d);
330 pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
334 return p_pcap_createsrcstr(a, b, c, d, e, f);
338 #ifdef HAVE_PCAP_SETSAMPLING
340 pcap_setsampling(pcap_t *a)
343 if (p_pcap_setsampling != NULL) {
344 return p_pcap_setsampling(a);
351 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
354 return p_pcap_loop(a, b, c, d);
358 pcap_freecode(struct bpf_program *a)
361 if(p_pcap_freecode) {
366 #ifdef HAVE_PCAP_FINDALLDEVS
368 pcap_findalldevs(pcap_if_t **a, char *b)
370 g_assert(has_wpcap && p_pcap_findalldevs != NULL);
371 return p_pcap_findalldevs(a, b);
375 pcap_freealldevs(pcap_if_t *a)
377 g_assert(has_wpcap && p_pcap_freealldevs != NULL);
378 p_pcap_freealldevs(a);
382 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
384 * Table of DLT_ types, names, and descriptions, for use if the version
385 * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
386 * or "pcap_datalink_val_to_name()".
390 const char *description;
394 #define DLT_CHOICE(code, description) { #code, description, code }
395 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
397 static struct dlt_choice dlt_choices[] = {
398 DLT_CHOICE(DLT_NULL, "BSD loopback"),
399 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
400 DLT_CHOICE(DLT_IEEE802, "Token ring"),
401 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
402 DLT_CHOICE(DLT_SLIP, "SLIP"),
403 DLT_CHOICE(DLT_PPP, "PPP"),
404 DLT_CHOICE(DLT_FDDI, "FDDI"),
405 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
406 DLT_CHOICE(DLT_RAW, "Raw IP"),
407 #ifdef DLT_SLIP_BSDOS
408 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
411 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
414 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
416 #ifdef DLT_PPP_SERIAL
417 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
420 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
423 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
425 #ifdef DLT_IEEE802_11
426 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
429 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
432 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
435 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
438 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
441 DLT_CHOICE(DLT_LTALK, "Localtalk"),
444 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
446 #ifdef DLT_PRISM_HEADER
447 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
449 #ifdef DLT_IP_OVER_FC
450 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
453 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
455 #ifdef DLT_IEEE802_11_RADIO
456 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
458 #ifdef DLT_ARCNET_LINUX
459 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
461 #ifdef DLT_LINUX_IRDA
462 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
464 #ifdef DLT_LINUX_LAPD
465 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
468 DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
471 DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
474 DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
478 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
480 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
482 pcap_datalink_name_to_val(const char *name)
488 if (p_pcap_datalink_name_to_val != NULL)
489 return p_pcap_datalink_name_to_val(name);
492 * We don't have it in WinPcap; do it ourselves.
494 for (i = 0; dlt_choices[i].name != NULL; i++) {
495 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
497 return dlt_choices[i].dlt;
504 #ifdef HAVE_PCAP_LIST_DATALINKS
506 pcap_list_datalinks(pcap_t *p, int **ddlt)
509 return p_pcap_list_datalinks(p, ddlt);
513 #ifdef HAVE_PCAP_FREE_DATALINKS
515 pcap_free_datalinks(int *ddlt)
520 * If we don't have pcap_free_datalinks() in WinPcap,
521 * we don't free the memory - we can't use free(), as
522 * we might not have been built with the same version
523 * of the C runtime library as WinPcap was, and, if we're
524 * not, free() isn't guaranteed to work on something
525 * allocated by WinPcap.
527 if (p_pcap_free_datalinks != NULL)
528 p_pcap_free_datalinks(ddlt);
532 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
534 pcap_datalink_val_to_name(int dlt)
540 if (p_pcap_datalink_val_to_name != NULL)
541 return p_pcap_datalink_val_to_name(dlt);
544 * We don't have it in WinPcap; do it ourselves.
546 for (i = 0; dlt_choices[i].name != NULL; i++) {
547 if (dlt_choices[i].dlt == dlt)
548 return dlt_choices[i].name + sizeof("DLT_") - 1;
555 #ifdef HAVE_PCAP_BREAKLOOP
556 void pcap_breakloop(pcap_t *a)
562 /* setbuff is win32 specific! */
563 int pcap_setbuff(pcap_t *a, int b)
566 return p_pcap_setbuff(a, b);
569 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
570 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
571 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
574 return p_pcap_next_ex(a, b, c);
577 #ifdef HAVE_PCAP_REMOTE
579 get_remote_interface_list(const char *hostname, const char *port,
580 int auth_type, const char *username,
581 const char *passwd, int *err, char **err_str)
583 struct pcap_rmtauth auth;
584 char source[PCAP_BUF_SIZE];
585 char errbuf[PCAP_ERRBUF_SIZE];
588 if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
589 NULL, errbuf) == -1) {
590 *err = CANT_GET_INTERFACE_LIST;
592 *err_str = cant_get_if_list_error_message(errbuf);
596 auth.type = auth_type;
597 auth.username = g_strdup(username);
598 auth.password = g_strdup(passwd);
600 result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
601 g_free(auth.username);
602 g_free(auth.password);
609 * This will use "pcap_findalldevs()" if we have it, otherwise it'll
610 * fall back on "pcap_lookupdev()".
613 get_interface_list(int *err, char **err_str)
618 char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
619 char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
621 char errbuf[PCAP_ERRBUF_SIZE];
623 #ifdef HAVE_PCAP_FINDALLDEVS
624 if (p_pcap_findalldevs != NULL)
625 return get_interface_list_findalldevs(err, err_str);
629 * In WinPcap, pcap_lookupdev is implemented by calling
630 * PacketGetAdapterNames. According to the documentation
633 * http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
637 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
638 * of bytes consisting of:
640 * a sequence of null-terminated ASCII strings (i.e., each
641 * one is terminated by a single 0 byte), giving the names
644 * an empty ASCII string (i.e., a single 0 byte);
646 * a sequence of null-terminated ASCII strings, giving the
647 * descriptions of the interfaces;
649 * an empty ASCII string.
651 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
652 * returns a sequence of bytes consisting of:
654 * a sequence of null-terminated double-byte Unicode strings
655 * (i.e., each one consits of a sequence of double-byte
656 * characters, terminated by a double-byte 0), giving the
657 * names of the interfaces;
659 * an empty Unicode string (i.e., a double 0 byte);
661 * a sequence of null-terminated ASCII strings, giving the
662 * descriptions of the interfaces;
664 * an empty ASCII string.
666 * The Nth string in the first sequence is the name of the Nth
667 * adapter; the Nth string in the second sequence is the
668 * description of the Nth adapter.
671 names = (wchar_t *)pcap_lookupdev(errbuf);
680 * If names[0] is less than 256 it means the first
681 * byte is 0. This implies that we are using Unicode
684 while (*(names+desc_pos) || *(names+desc_pos-1))
686 desc_pos++; /* Step over the extra '\0' */
687 desc = (char*)(names + desc_pos); /* cast *after* addition */
689 while (names[i] != 0) {
691 * Copy the Unicode description to an ASCII
696 if (j < MAX_WIN_IF_NAME_LEN)
697 ascii_desc[j++] = *desc;
700 ascii_desc[j] = '\0';
704 * Copy the Unicode name to an ASCII string.
707 while (names[i] != 0) {
708 if (j < MAX_WIN_IF_NAME_LEN)
709 ascii_name[j++] = (char) names[i++];
711 ascii_name[j] = '\0';
713 il = g_list_append(il,
714 if_info_new(ascii_name, ascii_desc));
718 * Otherwise we are in Windows 95/98 and using ASCII
719 * (8-bit) characters.
721 win95names=(char *)names;
722 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
724 desc_pos++; /* Step over the extra '\0' */
725 desc = win95names + desc_pos;
727 while (win95names[i] != '\0') {
729 * "&win95names[i]" points to the current
730 * interface name, and "desc" points to
731 * that interface's description.
733 il = g_list_append(il,
734 if_info_new(&win95names[i], desc));
737 * Skip to the next description.
744 * Skip to the next name.
746 while (win95names[i] != 0)
755 * No interfaces found.
757 *err = NO_INTERFACES_FOUND;
766 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
767 * "get_interface_list()".
770 cant_get_if_list_error_message(const char *err_str)
773 * If the error message includes "Not enough storage is available
774 * to process this command" or "The operation completed successfully",
775 * suggest that they install a WinPcap version later than 3.0.
777 if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
778 strstr(err_str, "The operation completed successfully") != NULL) {
779 return g_strdup_printf("Can't get list of interfaces: %s\n"
780 "This might be a problem with WinPcap 3.0; you should try updating to\n"
781 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
784 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
788 * Append the version of WinPcap with which we were compiled to a GString.
791 get_compiled_pcap_version(GString *str)
793 g_string_append(str, "with WinPcap (version unknown)");
797 * Append the version of WinPcap with which we we're running to a GString.
800 get_runtime_pcap_version(GString *str)
803 * On Windows, we might have been compiled with WinPcap but
804 * might not have it loaded; indicate whether we have it or
805 * not and, if we have it and we have "pcap_lib_version()",
806 * what version we have.
808 GModule *handle; /* handle returned by dlopen */
809 static gchar *packetVer;
813 g_string_append_printf(str, "with ");
814 if (p_pcap_lib_version != NULL)
815 g_string_append_printf(str, p_pcap_lib_version());
818 * An alternative method of obtaining the version
819 * number, by using the PacketLibraryVersion
820 * string from packet.dll.
822 * Unfortunately, in WinPcap 3.0, it returns
823 * "3.0 alpha3", even in the final version of
824 * WinPcap 3.0, so if there's a blank in the
825 * string, we strip it and everything after
826 * it from the string, so we don't misleadingly
827 * report that 3.0 alpha3 is being used when
828 * the final version is being used.
830 if (packetVer == NULL) {
831 packetVer = "version unknown";
832 handle = g_module_open("Packet.dll", 0);
833 if (handle != NULL) {
834 if (g_module_symbol(handle,
835 "PacketLibraryVersion",
836 (gpointer*)&packetVer)) {
837 packetVer = g_strdup(packetVer);
838 blankp = strchr(packetVer, ' ');
842 packetVer = "version unknown";
844 g_module_close(handle);
847 g_string_append_printf(str, "WinPcap (%s)", packetVer);
850 g_string_append(str, "without WinPcap");
853 #else /* HAVE_LIBPCAP */
862 * Append an indication that we were not compiled with WinPcap
866 get_compiled_pcap_version(GString *str)
868 g_string_append(str, "without WinPcap");
872 * Don't append anything, as we weren't even compiled to use WinPcap.
875 get_runtime_pcap_version(GString *str _U_)
879 #endif /* HAVE_LIBPCAP */