From Michael Mann:
[obnox/wireshark/wip.git] / capture-wpcap.c
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.
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2001 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include <gmodule.h>
34
35 #include <epan/strutil.h>
36
37 #include "capture_ifinfo.h"
38 #include "capture-pcap-util.h"
39 #include "capture-pcap-util-int.h"
40 #include "capture-wpcap.h"
41
42 #include <wsutil/file_util.h>
43
44 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
45 #include "tools/lemon/cppmagic.h"
46
47 #define MAX_WIN_IF_NAME_LEN 511
48
49
50 gboolean has_wpcap = FALSE;
51
52 #ifdef HAVE_LIBPCAP
53
54 /*
55  * XXX - should we require at least WinPcap 3.1 both for building an
56  * for using Wireshark?
57  */
58
59 static char*   (*p_pcap_lookupdev) (char *);
60 static void    (*p_pcap_close) (pcap_t *);
61 static int     (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
62 static int     (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, guchar *);
63 static int     (*p_pcap_snapshot) (pcap_t *);
64 static int     (*p_pcap_datalink) (pcap_t *);
65 static int     (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
66 static char*   (*p_pcap_geterr) (pcap_t *);
67 static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, const char *, int,
68                         bpf_u_int32);
69 static int     (*p_pcap_compile_nopcap) (int, int, struct bpf_program *, const char *, int,
70                         bpf_u_int32);
71 static int     (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
72                         char *);
73 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
74 static int     (*p_pcap_loop) (pcap_t *, int, pcap_handler, guchar *);
75 #ifdef HAVE_PCAP_OPEN_DEAD
76 static pcap_t* (*p_pcap_open_dead) (int, int);
77 #endif
78 static void    (*p_pcap_freecode) (struct bpf_program *);
79 #ifdef HAVE_PCAP_FINDALLDEVS
80 static int     (*p_pcap_findalldevs) (pcap_if_t **, char *);
81 static void    (*p_pcap_freealldevs) (pcap_if_t *);
82 #endif
83 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
84 static int (*p_pcap_datalink_name_to_val) (const char *);
85 #endif
86 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
87 static const char *(*p_pcap_datalink_val_to_name) (int);
88 #endif
89 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
90 static const char *(*p_pcap_datalink_val_to_description) (int);
91 #endif
92 #ifdef HAVE_PCAP_BREAKLOOP
93 static void    (*p_pcap_breakloop) (pcap_t *);
94 #endif
95 static const char *(*p_pcap_lib_version) (void);
96 static int     (*p_pcap_setbuff) (pcap_t *, int dim);
97 static int     (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
98 #ifdef HAVE_PCAP_REMOTE
99 static pcap_t* (*p_pcap_open) (const char *, int, int, int,
100                                struct pcap_rmtauth *, char *);
101 static int     (*p_pcap_findalldevs_ex) (char *, struct pcap_rmtauth *,
102                                          pcap_if_t **, char *);
103 static int     (*p_pcap_createsrcstr) (char *, int, const char *, const char *,
104                                        const char *, char *);
105 #endif
106 #ifdef HAVE_PCAP_SETSAMPLING
107 static struct pcap_samp* (*p_pcap_setsampling)(pcap_t *);
108 #endif
109
110 #ifdef HAVE_PCAP_LIST_DATALINKS
111 static int      (*p_pcap_list_datalinks)(pcap_t *, int **);
112 #endif
113
114 #ifdef HAVE_PCAP_SET_DATALINK
115 static int      (*p_pcap_set_datalink)(pcap_t *, int);
116 #endif
117
118 #ifdef HAVE_PCAP_FREE_DATALINKS
119 static int      (*p_pcap_free_datalinks)(int *);
120 #endif
121
122 #ifdef HAVE_BPF_IMAGE
123 static char     *(*p_bpf_image) (const struct bpf_insn *, int);
124 #endif
125
126 typedef struct {
127         const char      *name;
128         gpointer        *ptr;
129         gboolean        optional;
130 } symbol_table_t;
131
132 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
133
134 void
135 load_wpcap(void)
136 {
137
138         /* These are the symbols I need or want from Wpcap */
139         static const symbol_table_t     symbols[] = {
140                 SYM(pcap_lookupdev, FALSE),
141                 SYM(pcap_close, FALSE),
142                 SYM(pcap_stats, FALSE),
143                 SYM(pcap_dispatch, FALSE),
144                 SYM(pcap_snapshot, FALSE),
145                 SYM(pcap_datalink, FALSE),
146                 SYM(pcap_setfilter, FALSE),
147                 SYM(pcap_geterr, FALSE),
148                 SYM(pcap_compile, FALSE),
149                 SYM(pcap_compile_nopcap, FALSE),
150                 SYM(pcap_lookupnet, FALSE),
151 #ifdef HAVE_PCAP_REMOTE
152                 SYM(pcap_open, FALSE),
153                 SYM(pcap_findalldevs_ex, FALSE),
154                 SYM(pcap_createsrcstr, FALSE),
155 #endif
156                 SYM(pcap_open_live, FALSE),
157 #ifdef HAVE_PCAP_OPEN_DEAD
158                 SYM(pcap_open_dead, FALSE),
159 #endif
160 #ifdef HAVE_PCAP_SETSAMPLING
161                 SYM(pcap_setsampling, TRUE),
162 #endif
163                 SYM(pcap_loop, FALSE),
164                 SYM(pcap_freecode, TRUE),
165 #ifdef HAVE_PCAP_FINDALLDEVS
166                 SYM(pcap_findalldevs, TRUE),
167                 SYM(pcap_freealldevs, TRUE),
168 #endif
169 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
170                 SYM(pcap_datalink_name_to_val, TRUE),
171 #endif
172 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
173                 SYM(pcap_datalink_val_to_name, TRUE),
174 #endif
175 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
176                 SYM(pcap_datalink_val_to_description, TRUE),
177 #endif
178 #ifdef HAVE_PCAP_BREAKLOOP
179                 /*
180                  * We don't try to work around the lack of this at
181                  * run time; it's present in WinPcap 3.1, which is
182                  * the version we build with and ship with.
183                  */
184                 SYM(pcap_breakloop, FALSE),
185 #endif
186                 SYM(pcap_lib_version, TRUE),
187                 SYM(pcap_setbuff, TRUE),
188                 SYM(pcap_next_ex, TRUE),
189 #ifdef HAVE_PCAP_LIST_DATALINKS
190                 SYM(pcap_list_datalinks, FALSE),
191 #endif
192 #ifdef HAVE_PCAP_SET_DATALINK
193                 SYM(pcap_set_datalink, FALSE),
194 #endif
195 #ifdef HAVE_PCAP_FREE_DATALINKS
196                 SYM(pcap_free_datalinks, TRUE),
197 #endif
198 #ifdef HAVE_BPF_IMAGE
199                 SYM(bpf_image, FALSE),
200 #endif
201                 { NULL, NULL, FALSE }
202         };
203
204         GModule         *wh; /* wpcap handle */
205         const symbol_table_t    *sym;
206
207         wh = ws_module_open("wpcap.dll", 0);
208
209         if (!wh) {
210                 return;
211         }
212
213         sym = symbols;
214         while (sym->name) {
215                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
216                         if (sym->optional) {
217                                 /*
218                                  * We don't care if it's missing; we just
219                                  * don't use it.
220                                  */
221                                 *sym->ptr = NULL;
222                         } else {
223                                 /*
224                                  * We require this symbol.
225                                  */
226                                 return;
227                         }
228                 }
229                 sym++;
230         }
231
232
233         has_wpcap = TRUE;
234 }
235
236 /*
237  * The official list of WinPcap mirrors is at
238  * http://www.winpcap.org/misc/mirrors.htm
239  */
240 char *
241 cant_load_winpcap_err(const char *app_name)
242 {
243         return g_strdup_printf(
244 "Unable to load WinPcap (wpcap.dll); %s will not be able to capture\n"
245 "packets.\n"
246 "\n"
247 "In order to capture packets, WinPcap must be installed; see\n"
248 "\n"
249 "        http://www.winpcap.org/\n"
250 "\n"
251 "or the mirror at\n"
252 "\n"
253 "        http://www.mirrors.wiretapped.net/security/packet-capture/winpcap/\n"
254 "\n"
255 "or the mirror at\n"
256 "\n"
257 "        http://winpcap.cs.pu.edu.tw/\n"
258 "\n"
259 "for a downloadable version of WinPcap and for instructions on how to install\n"
260 "WinPcap.",
261             app_name);
262 }
263
264 char*
265 pcap_lookupdev (char *a)
266 {
267         if (!has_wpcap) {
268                 return NULL;
269         }
270         return p_pcap_lookupdev(a);
271 }
272
273 void
274 pcap_close(pcap_t *a)
275 {
276         g_assert(has_wpcap);
277         p_pcap_close(a);
278 }
279
280 int
281 pcap_stats(pcap_t *a, struct pcap_stat *b)
282 {
283         g_assert(has_wpcap);
284         return p_pcap_stats(a, b);
285 }
286
287 int
288 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
289 {
290         g_assert(has_wpcap);
291         return p_pcap_dispatch(a, b, c, d);
292 }
293
294 int
295 pcap_snapshot(pcap_t *a)
296 {
297         g_assert(has_wpcap);
298         return p_pcap_snapshot(a);
299 }
300
301 int
302 pcap_datalink(pcap_t *a)
303 {
304         g_assert(has_wpcap);
305         return p_pcap_datalink(a);
306 }
307
308 #ifdef HAVE_PCAP_SET_DATALINK
309 int
310 pcap_set_datalink(pcap_t *p, int dlt)
311 {
312         g_assert(has_wpcap);
313         return p_pcap_set_datalink(p, dlt);
314 }
315 #endif
316
317 int
318 pcap_setfilter(pcap_t *a, struct bpf_program *b)
319 {
320         g_assert(has_wpcap);
321         return p_pcap_setfilter(a, b);
322 }
323
324 char*
325 pcap_geterr(pcap_t *a)
326 {
327         g_assert(has_wpcap);
328         return p_pcap_geterr(a);
329 }
330
331 int
332 pcap_compile(pcap_t *a, struct bpf_program *b, const char *c, int d,
333             bpf_u_int32 e)
334 {
335         g_assert(has_wpcap);
336         return p_pcap_compile(a, b, c, d, e);
337 }
338
339 int
340 pcap_compile_nopcap(int a, int b, struct bpf_program *c, const char *d, int e,
341             bpf_u_int32 f)
342 {
343         g_assert(has_wpcap);
344         return p_pcap_compile_nopcap(a, b, c, d, e, f);
345 }
346
347 int
348 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
349 {
350         g_assert(has_wpcap);
351         return p_pcap_lookupnet(a, b, c, d);
352 }
353
354 pcap_t*
355 pcap_open_live(const char *a, int b, int c, int d, char *e)
356 {
357     if (!has_wpcap) {
358         g_snprintf(e, PCAP_ERRBUF_SIZE,
359                    "unable to load WinPcap (wpcap.dll); can't open %s to capture",
360                    a);
361         return NULL;
362     }
363     return p_pcap_open_live(a, b, c, d, e);
364 }
365
366 #ifdef HAVE_PCAP_OPEN_DEAD
367 pcap_t*
368 pcap_open_dead(int a, int b)
369 {
370     if (!has_wpcap) {
371         return NULL;
372     }
373     return p_pcap_open_dead(a, b);
374 }
375 #endif
376
377 #ifdef HAVE_BPF_IMAGE
378 char *
379 bpf_image(const struct bpf_insn *a, int b)
380 {
381     if (!has_wpcap) {
382         return NULL;
383     }
384     return p_bpf_image(a, b);
385 }
386 #endif
387
388 #ifdef HAVE_PCAP_REMOTE
389 pcap_t*
390 pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
391 {
392     if (!has_wpcap) {
393         g_snprintf(f, PCAP_ERRBUF_SIZE,
394                    "unable to load WinPcap (wpcap.dll); can't open %s to capture",
395                    a);
396         return NULL;
397     }
398     return p_pcap_open(a, b, c, d, e, f);
399 }
400
401 int
402 pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
403 {
404     g_assert(has_wpcap);
405     return p_pcap_findalldevs_ex(a, b, c, d);
406 }
407
408 int
409 pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
410                   char *f)
411 {
412     g_assert(has_wpcap);
413     return p_pcap_createsrcstr(a, b, c, d, e, f);
414 }
415 #endif
416
417 #ifdef HAVE_PCAP_SETSAMPLING
418 struct pcap_samp *
419 pcap_setsampling(pcap_t *a)
420 {
421     g_assert(has_wpcap);
422     if (p_pcap_setsampling != NULL) {
423         return p_pcap_setsampling(a);
424     }
425     return NULL;
426 }
427 #endif
428
429 int
430 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
431 {
432         g_assert(has_wpcap);
433         return p_pcap_loop(a, b, c, d);
434 }
435
436 void
437 pcap_freecode(struct bpf_program *a)
438 {
439         g_assert(has_wpcap);
440     if(p_pcap_freecode) {
441             p_pcap_freecode(a);
442     }
443 }
444
445 #ifdef HAVE_PCAP_FINDALLDEVS
446 int
447 pcap_findalldevs(pcap_if_t **a, char *b)
448 {
449         g_assert(has_wpcap && p_pcap_findalldevs != NULL);
450         return p_pcap_findalldevs(a, b);
451 }
452
453 void
454 pcap_freealldevs(pcap_if_t *a)
455 {
456         g_assert(has_wpcap && p_pcap_freealldevs != NULL);
457         p_pcap_freealldevs(a);
458 }
459 #endif
460
461 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
462 /*
463  * Table of DLT_ types, names, and descriptions, for use if the version
464  * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
465  * or "pcap_datalink_val_to_name()".
466  */
467 struct dlt_choice {
468         const char *name;
469         const char *description;
470         int     dlt;
471 };
472
473 #define DLT_CHOICE(code, description) { #code, description, code }
474 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
475
476 static struct dlt_choice dlt_choices[] = {
477         DLT_CHOICE(DLT_NULL, "BSD loopback"),
478         DLT_CHOICE(DLT_EN10MB, "Ethernet"),
479         DLT_CHOICE(DLT_IEEE802, "Token ring"),
480         DLT_CHOICE(DLT_ARCNET, "ARCNET"),
481         DLT_CHOICE(DLT_SLIP, "SLIP"),
482         DLT_CHOICE(DLT_PPP, "PPP"),
483         DLT_CHOICE(DLT_FDDI, "FDDI"),
484         DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
485         DLT_CHOICE(DLT_RAW, "Raw IP"),
486 #ifdef DLT_SLIP_BSDOS
487         DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
488 #endif
489 #ifdef DLT_PPP_BSDOS
490         DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
491 #endif
492 #ifdef DLT_ATM_CLIP
493         DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
494 #endif
495 #ifdef DLT_PPP_SERIAL
496         DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
497 #endif
498 #ifdef DLT_PPP_ETHER
499         DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
500 #endif
501 #ifdef DLT_C_HDLC
502         DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
503 #endif
504 #ifdef DLT_IEEE802_11
505         DLT_CHOICE(DLT_IEEE802_11, "802.11"),
506 #endif
507 #ifdef DLT_FRELAY
508         DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
509 #endif
510 #ifdef DLT_LOOP
511         DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
512 #endif
513 #ifdef DLT_ENC
514         DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
515 #endif
516 #ifdef DLT_LINUX_SLL
517         DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
518 #endif
519 #ifdef DLT_LTALK
520         DLT_CHOICE(DLT_LTALK, "Localtalk"),
521 #endif
522 #ifdef DLT_PFLOG
523         DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
524 #endif
525 #ifdef DLT_PRISM_HEADER
526         DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
527 #endif
528 #ifdef DLT_IP_OVER_FC
529         DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
530 #endif
531 #ifdef DLT_SUNATM
532         DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
533 #endif
534 #ifdef DLT_IEEE802_11_RADIO
535         DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
536 #endif
537 #ifdef DLT_ARCNET_LINUX
538         DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
539 #endif
540 #ifdef DLT_LINUX_IRDA
541         DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
542 #endif
543 #ifdef DLT_LINUX_LAPD
544         DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
545 #endif
546 #ifdef DLT_LANE8023
547         DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
548 #endif
549 #ifdef DLT_CIP
550         DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
551 #endif
552 #ifdef DLT_HDLC
553         DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
554 #endif
555 #ifdef DLT_PPI
556         DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
557 #endif
558         DLT_CHOICE_SENTINEL
559 };
560 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION */
561
562 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
563 int
564 pcap_datalink_name_to_val(const char *name)
565 {
566         int i;
567
568         g_assert(has_wpcap);
569
570         if (p_pcap_datalink_name_to_val != NULL)
571                 return p_pcap_datalink_name_to_val(name);
572         else {
573                 /*
574                  * We don't have it in WinPcap; do it ourselves.
575                  */
576                 for (i = 0; dlt_choices[i].name != NULL; i++) {
577                         if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
578                             name) == 0)
579                                 return dlt_choices[i].dlt;
580                 }
581                 return -1;
582         }
583 }
584 #endif
585
586 #ifdef HAVE_PCAP_LIST_DATALINKS
587 int
588 pcap_list_datalinks(pcap_t *p, int **ddlt)
589 {
590         g_assert(has_wpcap);
591         return p_pcap_list_datalinks(p, ddlt);
592 }
593 #endif
594
595 #ifdef HAVE_PCAP_FREE_DATALINKS
596 void
597 pcap_free_datalinks(int *ddlt)
598 {
599         g_assert(has_wpcap);
600
601         /*
602          * If we don't have pcap_free_datalinks() in WinPcap,
603          * we don't free the memory - we can't use free(), as
604          * we might not have been built with the same version
605          * of the C runtime library as WinPcap was, and, if we're
606          * not, free() isn't guaranteed to work on something
607          * allocated by WinPcap.
608          */
609         if (p_pcap_free_datalinks != NULL)
610                 p_pcap_free_datalinks(ddlt);
611 }
612 #endif
613
614 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
615 const char *
616 pcap_datalink_val_to_name(int dlt)
617 {
618         int i;
619
620         g_assert(has_wpcap);
621
622         if (p_pcap_datalink_val_to_name != NULL)
623                 return p_pcap_datalink_val_to_name(dlt);
624         else {
625                 /*
626                  * We don't have it in WinPcap; do it ourselves.
627                  */
628                 for (i = 0; dlt_choices[i].name != NULL; i++) {
629                         if (dlt_choices[i].dlt == dlt)
630                                 return dlt_choices[i].name + sizeof("DLT_") - 1;
631                 }
632                 return NULL;
633         }
634 }
635 #endif
636
637 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
638 const char *
639 pcap_datalink_val_to_description(int dlt)
640 {
641         int i;
642
643         g_assert(has_wpcap);
644
645         if (p_pcap_datalink_val_to_description != NULL)
646                 return p_pcap_datalink_val_to_description(dlt);
647         else {
648                 /*
649                  * We don't have it in WinPcap; do it ourselves.
650                  */
651                 for (i = 0; dlt_choices[i].name != NULL; i++) {
652                         if (dlt_choices[i].dlt == dlt)
653                                 return (dlt_choices[i].description);
654                 }
655                 return NULL;
656         }
657 }
658 #endif
659
660 #ifdef HAVE_PCAP_BREAKLOOP
661 void pcap_breakloop(pcap_t *a)
662 {
663         p_pcap_breakloop(a);
664 }
665 #endif
666
667 /* setbuff is win32 specific! */
668 int pcap_setbuff(pcap_t *a, int b)
669 {
670         g_assert(has_wpcap);
671         return p_pcap_setbuff(a, b);
672 }
673
674 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
675 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
676 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
677 {
678         g_assert(has_wpcap);
679         return p_pcap_next_ex(a, b, c);
680 }
681
682 #ifdef HAVE_PCAP_REMOTE
683 GList *
684 get_remote_interface_list(const char *hostname, const char *port,
685                           int auth_type, const char *username,
686                           const char *passwd, int *err, char **err_str)
687 {
688     struct pcap_rmtauth auth;
689     char source[PCAP_BUF_SIZE];
690     char errbuf[PCAP_ERRBUF_SIZE];
691     GList *result;
692
693     if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
694                           NULL, errbuf) == -1) {
695         *err = CANT_GET_INTERFACE_LIST;
696         if (err_str != NULL)
697             *err_str = cant_get_if_list_error_message(errbuf);
698         return NULL;
699     }
700
701     auth.type = auth_type;
702     auth.username = g_strdup(username);
703     auth.password = g_strdup(passwd);
704
705     result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
706     g_free(auth.username);
707     g_free(auth.password);
708
709     return result;
710 }
711 #endif
712
713 /*
714  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
715  * fall back on "pcap_lookupdev()".
716  */
717 GList *
718 get_interface_list(int *err, char **err_str)
719 {
720         GList  *il = NULL;
721         wchar_t *names;
722         char *win95names;
723         char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
724         char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
725         int i, j;
726         char errbuf[PCAP_ERRBUF_SIZE];
727
728         if (!has_wpcap) {
729                 /*
730                  * We don't have WinPcap, so we can't get a list of
731                  * interfaces.
732                  */
733                 *err = DONT_HAVE_PCAP;
734                 *err_str = cant_load_winpcap_err("you");
735                 return NULL;
736         }
737
738 #ifdef HAVE_PCAP_FINDALLDEVS
739         if (p_pcap_findalldevs != NULL)
740                 return get_interface_list_findalldevs(err, err_str);
741 #endif
742
743         /*
744          * In WinPcap, pcap_lookupdev is implemented by calling
745          * PacketGetAdapterNames.  According to the documentation
746          * I could find:
747          *
748          *      http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
749          *
750          * this means that:
751          *
752          * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
753          * of bytes consisting of:
754          *
755          *      a sequence of null-terminated ASCII strings (i.e., each
756          *      one is terminated by a single 0 byte), giving the names
757          *      of the interfaces;
758          *
759          *      an empty ASCII string (i.e., a single 0 byte);
760          *
761          *      a sequence of null-terminated ASCII strings, giving the
762          *      descriptions of the interfaces;
763          *
764          *      an empty ASCII string.
765          *
766          * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
767          * returns a sequence of bytes consisting of:
768          *
769          *      a sequence of null-terminated double-byte Unicode strings
770          *      (i.e., each one consits of a sequence of double-byte
771          *      characters, terminated by a double-byte 0), giving the
772          *      names of the interfaces;
773          *
774          *      an empty Unicode string (i.e., a double 0 byte);
775          *
776          *      a sequence of null-terminated ASCII strings, giving the
777          *      descriptions of the interfaces;
778          *
779          *      an empty ASCII string.
780          *
781          * The Nth string in the first sequence is the name of the Nth
782          * adapter; the Nth string in the second sequence is the
783          * description of the Nth adapter.
784          */
785
786         names = (wchar_t *)pcap_lookupdev(errbuf);
787         i = 0;
788
789         if (names) {
790                 char* desc = 0;
791                 int desc_pos = 0;
792
793                 if (names[0]<256) {
794                         /*
795                          * If names[0] is less than 256 it means the first
796                          * byte is 0.  This implies that we are using Unicode
797                          * characters.
798                          */
799                         while (*(names+desc_pos) || *(names+desc_pos-1))
800                                 desc_pos++;
801                         desc_pos++;     /* Step over the extra '\0' */
802                         desc = (char*)(names + desc_pos); /* cast *after* addition */
803
804                         while (names[i] != 0) {
805                                 /*
806                                  * Copy the Unicode description to an ASCII
807                                  * string.
808                                  */
809                                 j = 0;
810                                 while (*desc != 0) {
811                                         if (j < MAX_WIN_IF_NAME_LEN)
812                                                 ascii_desc[j++] = *desc;
813                                         desc++;
814                                 }
815                                 ascii_desc[j] = '\0';
816                                 desc++;
817
818                                 /*
819                                  * Copy the Unicode name to an ASCII string.
820                                  */
821                                 j = 0;
822                                 while (names[i] != 0) {
823                                         if (j < MAX_WIN_IF_NAME_LEN)
824                                         ascii_name[j++] = (char) names[i++];
825                                 }
826                                 ascii_name[j] = '\0';
827                                 i++;
828                                 il = g_list_append(il,
829                                     if_info_new(ascii_name, ascii_desc));
830                         }
831                 } else {
832                         /*
833                          * Otherwise we are in Windows 95/98 and using ASCII
834                          * (8-bit) characters.
835                          */
836                         win95names=(char *)names;
837                         while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
838                                 desc_pos++;
839                         desc_pos++;     /* Step over the extra '\0' */
840                         desc = win95names + desc_pos;
841
842                         while (win95names[i] != '\0') {
843                                 /*
844                                  * "&win95names[i]" points to the current
845                                  * interface name, and "desc" points to
846                                  * that interface's description.
847                                  */
848                                 il = g_list_append(il,
849                                     if_info_new(&win95names[i], desc));
850
851                                 /*
852                                  * Skip to the next description.
853                                  */
854                                 while (*desc != 0)
855                                         desc++;
856                                 desc++;
857
858                                 /*
859                                  * Skip to the next name.
860                                  */
861                                 while (win95names[i] != 0)
862                                         i++;
863                                 i++;
864                         }
865                 }
866         }
867
868         if (il == NULL) {
869                 /*
870                  * No interfaces found.
871                  */
872                 *err = NO_INTERFACES_FOUND;
873                 if (err_str != NULL)
874                         *err_str = NULL;
875         }
876
877         return il;
878 }
879
880 /*
881  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
882  * "get_interface_list()".
883  */
884 gchar *
885 cant_get_if_list_error_message(const char *err_str)
886 {
887         /*
888          * If the error message includes "Not enough storage is available
889          * to process this command" or "The operation completed successfully",
890          * suggest that they install a WinPcap version later than 3.0.
891          */
892         if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
893             strstr(err_str, "The operation completed successfully") != NULL) {
894                 return g_strdup_printf("Can't get list of interfaces: %s\n"
895 "This might be a problem with WinPcap 3.0; you should try updating to\n"
896 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
897                     err_str);
898         }
899         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
900 }
901
902 /*
903  * Append the version of WinPcap with which we were compiled to a GString.
904  */
905 void
906 get_compiled_pcap_version(GString *str)
907 {
908         g_string_append(str, "with WinPcap (" STRINGIFY(PCAP_VERSION) ")");
909 }
910
911 /*
912  * Append the version of WinPcap with which we we're running to a GString.
913  */
914 void
915 get_runtime_pcap_version(GString *str)
916 {
917         /*
918          * On Windows, we might have been compiled with WinPcap but
919          * might not have it loaded; indicate whether we have it or
920          * not and, if we have it and we have "pcap_lib_version()",
921          * what version we have.
922          */
923         GModule *handle;                /* handle returned by ws_module_open */
924         static gchar *packetVer;
925         gchar *blankp;
926
927         if (has_wpcap) {
928                 g_string_append_printf(str, "with ");
929                 if (p_pcap_lib_version != NULL)
930                         g_string_append_printf(str, p_pcap_lib_version());
931                 else {
932                         /*
933                          * An alternative method of obtaining the version
934                          * number, by using the PacketLibraryVersion
935                          * string from packet.dll.
936                          *
937                          * Unfortunately, in WinPcap 3.0, it returns
938                          * "3.0 alpha3", even in the final version of
939                          * WinPcap 3.0, so if there's a blank in the
940                          * string, we strip it and everything after
941                          * it from the string, so we don't misleadingly
942                          * report that 3.0 alpha3 is being used when
943                          * the final version is being used.
944                          */
945                         if (packetVer == NULL) {
946                                 packetVer = "version unknown";
947                                 handle = ws_module_open("packet.dll", 0);
948                                 if (handle != NULL) {
949                                         if (g_module_symbol(handle,
950                                             "PacketLibraryVersion",
951                                             (gpointer*)&packetVer)) {
952                                                 packetVer = g_strdup(packetVer);
953                                                 blankp = strchr(packetVer, ' ');
954                                                 if (blankp != NULL)
955                                                         *blankp = '\0';
956                                         } else {
957                                                 packetVer = "version unknown";
958                                         }
959                                         g_module_close(handle);
960                                 }
961                         }
962                         g_string_append_printf(str, "WinPcap (%s)", packetVer);
963                 }
964         } else
965                 g_string_append(str, "without WinPcap");
966 }
967
968 #else /* HAVE_LIBPCAP */
969
970 void
971 load_wpcap(void)
972 {
973         return;
974 }
975
976 /*
977  * Append an indication that we were not compiled with WinPcap
978  * to a GString.
979  */
980 void
981 get_compiled_pcap_version(GString *str)
982 {
983         g_string_append(str, "without WinPcap");
984 }
985
986 /*
987  * Don't append anything, as we weren't even compiled to use WinPcap.
988  */
989 void
990 get_runtime_pcap_version(GString *str _U_)
991 {
992 }
993
994 #endif /* HAVE_LIBPCAP */