HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / capture-wpcap.c
blob9bbfb968a429d69fab0964db0ca40fefc7048e5f
1 /* capture-wpcap.c
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.
6 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #include <stdio.h>
30 #include <glib.h>
31 #include <gmodule.h>
33 #include <epan/strutil.h>
35 #include "capture_ifinfo.h"
36 #include "capture-pcap-util.h"
37 #include "capture-pcap-util-int.h"
38 #include "capture-wpcap.h"
40 #include <wsutil/file_util.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;
50 #ifdef HAVE_LIBPCAP
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 *, const char *, int,
66 bpf_u_int32);
67 static int (*p_pcap_compile_nopcap) (int, int, struct bpf_program *, const char *, int,
68 bpf_u_int32);
69 static int (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
70 char *);
71 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
72 static int (*p_pcap_loop) (pcap_t *, int, pcap_handler, guchar *);
73 #ifdef HAVE_PCAP_OPEN_DEAD
74 static pcap_t* (*p_pcap_open_dead) (int, int);
75 #endif
76 static void (*p_pcap_freecode) (struct bpf_program *);
77 #ifdef HAVE_PCAP_FINDALLDEVS
78 static int (*p_pcap_findalldevs) (pcap_if_t **, char *);
79 static void (*p_pcap_freealldevs) (pcap_if_t *);
80 #endif
81 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
82 static int (*p_pcap_datalink_name_to_val) (const char *);
83 #endif
84 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
85 static const char *(*p_pcap_datalink_val_to_name) (int);
86 #endif
87 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
88 static const char *(*p_pcap_datalink_val_to_description) (int);
89 #endif
90 #ifdef HAVE_PCAP_BREAKLOOP
91 static void (*p_pcap_breakloop) (pcap_t *);
92 #endif
93 static const char *(*p_pcap_lib_version) (void);
94 static int (*p_pcap_setbuff) (pcap_t *, int dim);
95 static int (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
96 #ifdef HAVE_PCAP_REMOTE
97 static pcap_t* (*p_pcap_open) (const char *, int, int, int,
98 struct pcap_rmtauth *, char *);
99 static int (*p_pcap_findalldevs_ex) (char *, struct pcap_rmtauth *,
100 pcap_if_t **, char *);
101 static int (*p_pcap_createsrcstr) (char *, int, const char *, const char *,
102 const char *, char *);
103 #endif
104 #ifdef HAVE_PCAP_SETSAMPLING
105 static struct pcap_samp* (*p_pcap_setsampling)(pcap_t *);
106 #endif
108 #ifdef HAVE_PCAP_LIST_DATALINKS
109 static int (*p_pcap_list_datalinks)(pcap_t *, int **);
110 #endif
112 #ifdef HAVE_PCAP_SET_DATALINK
113 static int (*p_pcap_set_datalink)(pcap_t *, int);
114 #endif
116 #ifdef HAVE_PCAP_FREE_DATALINKS
117 static int (*p_pcap_free_datalinks)(int *);
118 #endif
120 #ifdef HAVE_BPF_IMAGE
121 static char *(*p_bpf_image) (const struct bpf_insn *, int);
122 #endif
124 typedef struct {
125 const char *name;
126 gpointer *ptr;
127 gboolean optional;
128 } symbol_table_t;
130 #define SYM(x, y) { G_STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
132 void
133 load_wpcap(void)
136 /* These are the symbols I need or want from Wpcap */
137 static const symbol_table_t symbols[] = {
138 SYM(pcap_lookupdev, FALSE),
139 SYM(pcap_close, FALSE),
140 SYM(pcap_stats, FALSE),
141 SYM(pcap_dispatch, FALSE),
142 SYM(pcap_snapshot, FALSE),
143 SYM(pcap_datalink, FALSE),
144 SYM(pcap_setfilter, FALSE),
145 SYM(pcap_geterr, FALSE),
146 SYM(pcap_compile, FALSE),
147 SYM(pcap_compile_nopcap, FALSE),
148 SYM(pcap_lookupnet, FALSE),
149 #ifdef HAVE_PCAP_REMOTE
150 SYM(pcap_open, FALSE),
151 SYM(pcap_findalldevs_ex, FALSE),
152 SYM(pcap_createsrcstr, FALSE),
153 #endif
154 SYM(pcap_open_live, FALSE),
155 #ifdef HAVE_PCAP_OPEN_DEAD
156 SYM(pcap_open_dead, FALSE),
157 #endif
158 #ifdef HAVE_PCAP_SETSAMPLING
159 SYM(pcap_setsampling, TRUE),
160 #endif
161 SYM(pcap_loop, FALSE),
162 SYM(pcap_freecode, TRUE),
163 #ifdef HAVE_PCAP_FINDALLDEVS
164 SYM(pcap_findalldevs, TRUE),
165 SYM(pcap_freealldevs, TRUE),
166 #endif
167 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
168 SYM(pcap_datalink_name_to_val, TRUE),
169 #endif
170 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
171 SYM(pcap_datalink_val_to_name, TRUE),
172 #endif
173 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
174 SYM(pcap_datalink_val_to_description, TRUE),
175 #endif
176 #ifdef HAVE_PCAP_BREAKLOOP
178 * We don't try to work around the lack of this at
179 * run time; it's present in WinPcap 3.1, which is
180 * the version we build with and ship with.
182 SYM(pcap_breakloop, FALSE),
183 #endif
184 SYM(pcap_lib_version, TRUE),
185 SYM(pcap_setbuff, TRUE),
186 SYM(pcap_next_ex, TRUE),
187 #ifdef HAVE_PCAP_LIST_DATALINKS
188 SYM(pcap_list_datalinks, FALSE),
189 #endif
190 #ifdef HAVE_PCAP_SET_DATALINK
191 SYM(pcap_set_datalink, FALSE),
192 #endif
193 #ifdef HAVE_PCAP_FREE_DATALINKS
194 SYM(pcap_free_datalinks, TRUE),
195 #endif
196 #ifdef HAVE_BPF_IMAGE
197 SYM(bpf_image, FALSE),
198 #endif
199 { NULL, NULL, FALSE }
202 GModule *wh; /* wpcap handle */
203 const symbol_table_t *sym;
205 wh = ws_module_open("wpcap.dll", 0);
207 if (!wh) {
208 return;
211 sym = symbols;
212 while (sym->name) {
213 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
214 if (sym->optional) {
216 * We don't care if it's missing; we just
217 * don't use it.
219 *sym->ptr = NULL;
220 } else {
222 * We require this symbol.
224 return;
227 sym++;
231 has_wpcap = TRUE;
235 * The official list of WinPcap mirrors is at
236 * http://www.winpcap.org/misc/mirrors.htm
238 char *
239 cant_load_winpcap_err(const char *app_name)
241 return g_strdup_printf(
242 "Unable to load WinPcap (wpcap.dll); %s will not be able to capture\n"
243 "packets.\n"
244 "\n"
245 "In order to capture packets, WinPcap must be installed; see\n"
246 "\n"
247 " http://www.winpcap.org/\n"
248 "\n"
249 "or the mirror at\n"
250 "\n"
251 " http://www.mirrors.wiretapped.net/security/packet-capture/winpcap/\n"
252 "\n"
253 "or the mirror at\n"
254 "\n"
255 " http://winpcap.cs.pu.edu.tw/\n"
256 "\n"
257 "for a downloadable version of WinPcap and for instructions on how to install\n"
258 "WinPcap.",
259 app_name);
262 char*
263 pcap_lookupdev (char *a)
265 if (!has_wpcap) {
266 return NULL;
268 return p_pcap_lookupdev(a);
271 void
272 pcap_close(pcap_t *a)
274 g_assert(has_wpcap);
275 p_pcap_close(a);
279 pcap_stats(pcap_t *a, struct pcap_stat *b)
281 g_assert(has_wpcap);
282 return p_pcap_stats(a, b);
286 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
288 g_assert(has_wpcap);
289 return p_pcap_dispatch(a, b, c, d);
293 pcap_snapshot(pcap_t *a)
295 g_assert(has_wpcap);
296 return p_pcap_snapshot(a);
300 pcap_datalink(pcap_t *a)
302 g_assert(has_wpcap);
303 return p_pcap_datalink(a);
306 #ifdef HAVE_PCAP_SET_DATALINK
308 pcap_set_datalink(pcap_t *p, int dlt)
310 g_assert(has_wpcap);
311 return p_pcap_set_datalink(p, dlt);
313 #endif
316 pcap_setfilter(pcap_t *a, struct bpf_program *b)
318 g_assert(has_wpcap);
319 return p_pcap_setfilter(a, b);
322 char*
323 pcap_geterr(pcap_t *a)
325 g_assert(has_wpcap);
326 return p_pcap_geterr(a);
330 pcap_compile(pcap_t *a, struct bpf_program *b, const char *c, int d,
331 bpf_u_int32 e)
333 g_assert(has_wpcap);
334 return p_pcap_compile(a, b, c, d, e);
338 pcap_compile_nopcap(int a, int b, struct bpf_program *c, const char *d, int e,
339 bpf_u_int32 f)
341 g_assert(has_wpcap);
342 return p_pcap_compile_nopcap(a, b, c, d, e, f);
346 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
348 g_assert(has_wpcap);
349 return p_pcap_lookupnet(a, b, c, d);
352 pcap_t*
353 pcap_open_live(const char *a, int b, int c, int d, char *e)
355 if (!has_wpcap) {
356 g_snprintf(e, PCAP_ERRBUF_SIZE,
357 "unable to load WinPcap (wpcap.dll); can't open %s to capture",
359 return NULL;
361 return p_pcap_open_live(a, b, c, d, e);
364 #ifdef HAVE_PCAP_OPEN_DEAD
365 pcap_t*
366 pcap_open_dead(int a, int b)
368 if (!has_wpcap) {
369 return NULL;
371 return p_pcap_open_dead(a, b);
373 #endif
375 #ifdef HAVE_BPF_IMAGE
376 char *
377 bpf_image(const struct bpf_insn *a, int b)
379 if (!has_wpcap) {
380 return NULL;
382 return p_bpf_image(a, b);
384 #endif
386 #ifdef HAVE_PCAP_REMOTE
387 pcap_t*
388 pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
390 if (!has_wpcap) {
391 g_snprintf(f, PCAP_ERRBUF_SIZE,
392 "unable to load WinPcap (wpcap.dll); can't open %s to capture",
394 return NULL;
396 return p_pcap_open(a, b, c, d, e, f);
400 pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
402 g_assert(has_wpcap);
403 return p_pcap_findalldevs_ex(a, b, c, d);
407 pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
408 char *f)
410 g_assert(has_wpcap);
411 return p_pcap_createsrcstr(a, b, c, d, e, f);
413 #endif
415 #ifdef HAVE_PCAP_SETSAMPLING
416 struct pcap_samp *
417 pcap_setsampling(pcap_t *a)
419 g_assert(has_wpcap);
420 if (p_pcap_setsampling != NULL) {
421 return p_pcap_setsampling(a);
423 return NULL;
425 #endif
428 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
430 g_assert(has_wpcap);
431 return p_pcap_loop(a, b, c, d);
434 void
435 pcap_freecode(struct bpf_program *a)
437 g_assert(has_wpcap);
438 if(p_pcap_freecode) {
439 p_pcap_freecode(a);
443 #ifdef HAVE_PCAP_FINDALLDEVS
445 pcap_findalldevs(pcap_if_t **a, char *b)
447 g_assert(has_wpcap && p_pcap_findalldevs != NULL);
448 return p_pcap_findalldevs(a, b);
451 void
452 pcap_freealldevs(pcap_if_t *a)
454 g_assert(has_wpcap && p_pcap_freealldevs != NULL);
455 p_pcap_freealldevs(a);
457 #endif
459 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
461 * Table of DLT_ types, names, and descriptions, for use if the version
462 * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
463 * or "pcap_datalink_val_to_name()".
465 struct dlt_choice {
466 const char *name;
467 const char *description;
468 int dlt;
471 #define DLT_CHOICE(code, description) { #code, description, code }
472 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
474 static struct dlt_choice dlt_choices[] = {
475 DLT_CHOICE(DLT_NULL, "BSD loopback"),
476 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
477 DLT_CHOICE(DLT_IEEE802, "Token ring"),
478 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
479 DLT_CHOICE(DLT_SLIP, "SLIP"),
480 DLT_CHOICE(DLT_PPP, "PPP"),
481 DLT_CHOICE(DLT_FDDI, "FDDI"),
482 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
483 DLT_CHOICE(DLT_RAW, "Raw IP"),
484 #ifdef DLT_SLIP_BSDOS
485 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
486 #endif
487 #ifdef DLT_PPP_BSDOS
488 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
489 #endif
490 #ifdef DLT_ATM_CLIP
491 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
492 #endif
493 #ifdef DLT_PPP_SERIAL
494 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
495 #endif
496 #ifdef DLT_PPP_ETHER
497 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
498 #endif
499 #ifdef DLT_C_HDLC
500 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
501 #endif
502 #ifdef DLT_IEEE802_11
503 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
504 #endif
505 #ifdef DLT_FRELAY
506 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
507 #endif
508 #ifdef DLT_LOOP
509 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
510 #endif
511 #ifdef DLT_ENC
512 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
513 #endif
514 #ifdef DLT_LINUX_SLL
515 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
516 #endif
517 #ifdef DLT_LTALK
518 DLT_CHOICE(DLT_LTALK, "Localtalk"),
519 #endif
520 #ifdef DLT_PFLOG
521 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
522 #endif
523 #ifdef DLT_PRISM_HEADER
524 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
525 #endif
526 #ifdef DLT_IP_OVER_FC
527 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
528 #endif
529 #ifdef DLT_SUNATM
530 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
531 #endif
532 #ifdef DLT_IEEE802_11_RADIO
533 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
534 #endif
535 #ifdef DLT_ARCNET_LINUX
536 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
537 #endif
538 #ifdef DLT_LINUX_IRDA
539 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
540 #endif
541 #ifdef DLT_LINUX_LAPD
542 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
543 #endif
544 #ifdef DLT_LANE8023
545 DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
546 #endif
547 #ifdef DLT_CIP
548 DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
549 #endif
550 #ifdef DLT_HDLC
551 DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
552 #endif
553 #ifdef DLT_PPI
554 DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
555 #endif
556 DLT_CHOICE_SENTINEL
558 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION */
560 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
562 pcap_datalink_name_to_val(const char *name)
564 int i;
566 g_assert(has_wpcap);
568 if (p_pcap_datalink_name_to_val != NULL)
569 return p_pcap_datalink_name_to_val(name);
570 else {
572 * We don't have it in WinPcap; do it ourselves.
574 for (i = 0; dlt_choices[i].name != NULL; i++) {
575 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
576 name) == 0)
577 return dlt_choices[i].dlt;
579 return -1;
582 #endif
584 #ifdef HAVE_PCAP_LIST_DATALINKS
586 pcap_list_datalinks(pcap_t *p, int **ddlt)
588 g_assert(has_wpcap);
589 return p_pcap_list_datalinks(p, ddlt);
591 #endif
593 #ifdef HAVE_PCAP_FREE_DATALINKS
594 void
595 pcap_free_datalinks(int *ddlt)
597 g_assert(has_wpcap);
600 * If we don't have pcap_free_datalinks() in WinPcap,
601 * we don't free the memory - we can't use free(), as
602 * we might not have been built with the same version
603 * of the C runtime library as WinPcap was, and, if we're
604 * not, free() isn't guaranteed to work on something
605 * allocated by WinPcap.
607 if (p_pcap_free_datalinks != NULL)
608 p_pcap_free_datalinks(ddlt);
610 #endif
612 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
613 const char *
614 pcap_datalink_val_to_name(int dlt)
616 int i;
618 g_assert(has_wpcap);
620 if (p_pcap_datalink_val_to_name != NULL)
621 return p_pcap_datalink_val_to_name(dlt);
622 else {
624 * We don't have it in WinPcap; do it ourselves.
626 for (i = 0; dlt_choices[i].name != NULL; i++) {
627 if (dlt_choices[i].dlt == dlt)
628 return dlt_choices[i].name + sizeof("DLT_") - 1;
630 return NULL;
633 #endif
635 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
636 const char *
637 pcap_datalink_val_to_description(int dlt)
639 int i;
641 g_assert(has_wpcap);
643 if (p_pcap_datalink_val_to_description != NULL)
644 return p_pcap_datalink_val_to_description(dlt);
645 else {
647 * We don't have it in WinPcap; do it ourselves.
649 for (i = 0; dlt_choices[i].name != NULL; i++) {
650 if (dlt_choices[i].dlt == dlt)
651 return (dlt_choices[i].description);
653 return NULL;
656 #endif
658 #ifdef HAVE_PCAP_BREAKLOOP
659 void pcap_breakloop(pcap_t *a)
661 p_pcap_breakloop(a);
663 #endif
665 /* setbuff is win32 specific! */
666 int pcap_setbuff(pcap_t *a, int b)
668 g_assert(has_wpcap);
669 return p_pcap_setbuff(a, b);
672 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
673 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
674 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
676 g_assert(has_wpcap);
677 return p_pcap_next_ex(a, b, c);
680 #ifdef HAVE_PCAP_REMOTE
681 GList *
682 get_remote_interface_list(const char *hostname, const char *port,
683 int auth_type, const char *username,
684 const char *passwd, int *err, char **err_str)
686 struct pcap_rmtauth auth;
687 char source[PCAP_BUF_SIZE];
688 char errbuf[PCAP_ERRBUF_SIZE];
689 GList *result;
691 if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
692 NULL, errbuf) == -1) {
693 *err = CANT_GET_INTERFACE_LIST;
694 if (err_str != NULL)
695 *err_str = cant_get_if_list_error_message(errbuf);
696 return NULL;
699 auth.type = auth_type;
700 auth.username = g_strdup(username);
701 auth.password = g_strdup(passwd);
703 result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
704 g_free(auth.username);
705 g_free(auth.password);
707 return result;
709 #endif
712 * This will use "pcap_findalldevs()" if we have it, otherwise it'll
713 * fall back on "pcap_lookupdev()".
715 GList *
716 get_interface_list(int *err, char **err_str)
718 GList *il = NULL;
719 wchar_t *names;
720 char *win95names;
721 char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
722 char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
723 int i, j;
724 char errbuf[PCAP_ERRBUF_SIZE];
726 if (!has_wpcap) {
728 * We don't have WinPcap, so we can't get a list of
729 * interfaces.
731 *err = DONT_HAVE_PCAP;
732 *err_str = cant_load_winpcap_err("you");
733 return NULL;
736 #ifdef HAVE_PCAP_FINDALLDEVS
737 if (p_pcap_findalldevs != NULL)
738 return get_interface_list_findalldevs(err, err_str);
739 #endif
742 * In WinPcap, pcap_lookupdev is implemented by calling
743 * PacketGetAdapterNames. According to the documentation
744 * I could find:
746 * http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
748 * this means that:
750 * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
751 * of bytes consisting of:
753 * a sequence of null-terminated ASCII strings (i.e., each
754 * one is terminated by a single 0 byte), giving the names
755 * of the interfaces;
757 * an empty ASCII string (i.e., a single 0 byte);
759 * a sequence of null-terminated ASCII strings, giving the
760 * descriptions of the interfaces;
762 * an empty ASCII string.
764 * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
765 * returns a sequence of bytes consisting of:
767 * a sequence of null-terminated double-byte Unicode strings
768 * (i.e., each one consits of a sequence of double-byte
769 * characters, terminated by a double-byte 0), giving the
770 * names of the interfaces;
772 * an empty Unicode string (i.e., a double 0 byte);
774 * a sequence of null-terminated ASCII strings, giving the
775 * descriptions of the interfaces;
777 * an empty ASCII string.
779 * The Nth string in the first sequence is the name of the Nth
780 * adapter; the Nth string in the second sequence is the
781 * description of the Nth adapter.
784 names = (wchar_t *)pcap_lookupdev(errbuf);
785 i = 0;
787 if (names) {
788 char* desc = 0;
789 int desc_pos = 0;
791 if (names[0]<256) {
793 * If names[0] is less than 256 it means the first
794 * byte is 0. This implies that we are using Unicode
795 * characters.
797 while (*(names+desc_pos) || *(names+desc_pos-1))
798 desc_pos++;
799 desc_pos++; /* Step over the extra '\0' */
800 desc = (char*)(names + desc_pos); /* cast *after* addition */
802 while (names[i] != 0) {
804 * Copy the Unicode description to an ASCII
805 * string.
807 j = 0;
808 while (*desc != 0) {
809 if (j < MAX_WIN_IF_NAME_LEN)
810 ascii_desc[j++] = *desc;
811 desc++;
813 ascii_desc[j] = '\0';
814 desc++;
817 * Copy the Unicode name to an ASCII string.
819 j = 0;
820 while (names[i] != 0) {
821 if (j < MAX_WIN_IF_NAME_LEN)
822 ascii_name[j++] = (char) names[i++];
824 ascii_name[j] = '\0';
825 i++;
826 il = g_list_append(il,
827 if_info_new(ascii_name, ascii_desc, FALSE));
829 } else {
831 * Otherwise we are in Windows 95/98 and using ASCII
832 * (8-bit) characters.
834 win95names=(char *)names;
835 while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
836 desc_pos++;
837 desc_pos++; /* Step over the extra '\0' */
838 desc = win95names + desc_pos;
840 while (win95names[i] != '\0') {
842 * "&win95names[i]" points to the current
843 * interface name, and "desc" points to
844 * that interface's description.
846 il = g_list_append(il,
847 if_info_new(&win95names[i], desc, FALSE));
850 * Skip to the next description.
852 while (*desc != 0)
853 desc++;
854 desc++;
857 * Skip to the next name.
859 while (win95names[i] != 0)
860 i++;
861 i++;
866 if (il == NULL) {
868 * No interfaces found.
870 *err = NO_INTERFACES_FOUND;
871 if (err_str != NULL)
872 *err_str = NULL;
875 return il;
879 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
880 * "get_interface_list()".
882 gchar *
883 cant_get_if_list_error_message(const char *err_str)
886 * If the error message includes "Not enough storage is available
887 * to process this command" or "The operation completed successfully",
888 * suggest that they install a WinPcap version later than 3.0.
890 if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
891 strstr(err_str, "The operation completed successfully") != NULL) {
892 return g_strdup_printf("Can't get list of interfaces: %s\n"
893 "This might be a problem with WinPcap 3.0; you should try updating to\n"
894 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
895 err_str);
897 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
901 * Append the version of WinPcap with which we were compiled to a GString.
903 void
904 get_compiled_pcap_version(GString *str)
906 g_string_append(str, "with WinPcap (" G_STRINGIFY(PCAP_VERSION) ")");
910 * Append the version of WinPcap with which we we're running to a GString.
912 void
913 get_runtime_pcap_version(GString *str)
916 * On Windows, we might have been compiled with WinPcap but
917 * might not have it loaded; indicate whether we have it or
918 * not and, if we have it and we have "pcap_lib_version()",
919 * what version we have.
921 GModule *handle; /* handle returned by ws_module_open */
922 static gchar *packetVer;
923 gchar *blankp;
925 if (has_wpcap) {
926 g_string_append_printf(str, "with ");
927 if (p_pcap_lib_version != NULL)
928 g_string_append_printf(str, p_pcap_lib_version());
929 else {
931 * An alternative method of obtaining the version
932 * number, by using the PacketLibraryVersion
933 * string from packet.dll.
935 * Unfortunately, in WinPcap 3.0, it returns
936 * "3.0 alpha3", even in the final version of
937 * WinPcap 3.0, so if there's a blank in the
938 * string, we strip it and everything after
939 * it from the string, so we don't misleadingly
940 * report that 3.0 alpha3 is being used when
941 * the final version is being used.
943 if (packetVer == NULL) {
944 packetVer = "version unknown";
945 handle = ws_module_open("packet.dll", 0);
946 if (handle != NULL) {
947 if (g_module_symbol(handle,
948 "PacketLibraryVersion",
949 (gpointer*)&packetVer)) {
950 packetVer = g_strdup(packetVer);
951 blankp = strchr(packetVer, ' ');
952 if (blankp != NULL)
953 *blankp = '\0';
954 } else {
955 packetVer = "version unknown";
957 g_module_close(handle);
960 g_string_append_printf(str, "WinPcap (%s)", packetVer);
962 } else
963 g_string_append(str, "without WinPcap");
966 #else /* HAVE_LIBPCAP */
968 void
969 load_wpcap(void)
971 return;
975 * Append an indication that we were not compiled with WinPcap
976 * to a GString.
978 void
979 get_compiled_pcap_version(GString *str)
981 g_string_append(str, "without WinPcap");
985 * Don't append anything, as we weren't even compiled to use WinPcap.
987 void
988 get_runtime_pcap_version(GString *str _U_)
992 #endif /* HAVE_LIBPCAP */