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