cd ../gtk ==> cd ../ui/gtk
[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 "capture_ifinfo.h"
36 #include "capture-pcap-util.h"
37 #include "capture-pcap-util-int.h"
38 #include "capture-wpcap.h"
39
40 #include <wsutil/file_util.h>
41
42 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
43 #include "tools/lemon/cppmagic.h"
44
45 #define MAX_WIN_IF_NAME_LEN 511
46
47
48 gboolean has_wpcap = FALSE;
49
50 #ifdef HAVE_LIBPCAP
51
52 /*
53  * XXX - should we require at least WinPcap 3.1 both for building an
54  * for using Wireshark?
55  */
56
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
107
108 #ifdef HAVE_PCAP_LIST_DATALINKS
109 static int      (*p_pcap_list_datalinks)(pcap_t *, int **);
110 #endif
111
112 #ifdef HAVE_PCAP_SET_DATALINK
113 static int      (*p_pcap_set_datalink)(pcap_t *, int);
114 #endif
115
116 #ifdef HAVE_PCAP_FREE_DATALINKS
117 static int      (*p_pcap_free_datalinks)(int *);
118 #endif
119
120 #ifdef HAVE_BPF_IMAGE
121 static char     *(*p_bpf_image) (const struct bpf_insn *, int);
122 #endif
123
124 typedef struct {
125         const char      *name;
126         gpointer        *ptr;
127         gboolean        optional;
128 } symbol_table_t;
129
130 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
131
132 void
133 load_wpcap(void)
134 {
135
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
177                 /*
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.
181                  */
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 }
200         };
201
202         GModule         *wh; /* wpcap handle */
203         const symbol_table_t    *sym;
204
205         wh = ws_module_open("wpcap.dll", 0);
206
207         if (!wh) {
208                 return;
209         }
210
211         sym = symbols;
212         while (sym->name) {
213                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
214                         if (sym->optional) {
215                                 /*
216                                  * We don't care if it's missing; we just
217                                  * don't use it.
218                                  */
219                                 *sym->ptr = NULL;
220                         } else {
221                                 /*
222                                  * We require this symbol.
223                                  */
224                                 return;
225                         }
226                 }
227                 sym++;
228         }
229
230
231         has_wpcap = TRUE;
232 }
233
234 /*
235  * The official list of WinPcap mirrors is at
236  * http://www.winpcap.org/misc/mirrors.htm
237  */
238 char *
239 cant_load_winpcap_err(const char *app_name)
240 {
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);
260 }
261
262 char*
263 pcap_lookupdev (char *a)
264 {
265         if (!has_wpcap) {
266                 return NULL;
267         }
268         return p_pcap_lookupdev(a);
269 }
270
271 void
272 pcap_close(pcap_t *a)
273 {
274         g_assert(has_wpcap);
275         p_pcap_close(a);
276 }
277
278 int
279 pcap_stats(pcap_t *a, struct pcap_stat *b)
280 {
281         g_assert(has_wpcap);
282         return p_pcap_stats(a, b);
283 }
284
285 int
286 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
287 {
288         g_assert(has_wpcap);
289         return p_pcap_dispatch(a, b, c, d);
290 }
291
292 int
293 pcap_snapshot(pcap_t *a)
294 {
295         g_assert(has_wpcap);
296         return p_pcap_snapshot(a);
297 }
298
299 int
300 pcap_datalink(pcap_t *a)
301 {
302         g_assert(has_wpcap);
303         return p_pcap_datalink(a);
304 }
305
306 #ifdef HAVE_PCAP_SET_DATALINK
307 int
308 pcap_set_datalink(pcap_t *p, int dlt)
309 {
310         g_assert(has_wpcap);
311         return p_pcap_set_datalink(p, dlt);
312 }
313 #endif
314
315 int
316 pcap_setfilter(pcap_t *a, struct bpf_program *b)
317 {
318         g_assert(has_wpcap);
319         return p_pcap_setfilter(a, b);
320 }
321
322 char*
323 pcap_geterr(pcap_t *a)
324 {
325         g_assert(has_wpcap);
326         return p_pcap_geterr(a);
327 }
328
329 int
330 pcap_compile(pcap_t *a, struct bpf_program *b, const char *c, int d,
331             bpf_u_int32 e)
332 {
333         g_assert(has_wpcap);
334         return p_pcap_compile(a, b, c, d, e);
335 }
336
337 int
338 pcap_compile_nopcap(int a, int b, struct bpf_program *c, const char *d, int e,
339             bpf_u_int32 f)
340 {
341         g_assert(has_wpcap);
342         return p_pcap_compile_nopcap(a, b, c, d, e, f);
343 }
344
345 int
346 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
347 {
348         g_assert(has_wpcap);
349         return p_pcap_lookupnet(a, b, c, d);
350 }
351
352 pcap_t*
353 pcap_open_live(const char *a, int b, int c, int d, char *e)
354 {
355     if (!has_wpcap) {
356         g_snprintf(e, PCAP_ERRBUF_SIZE,
357                    "unable to load WinPcap (wpcap.dll); can't open %s to capture",
358                    a);
359         return NULL;
360     }
361     return p_pcap_open_live(a, b, c, d, e);
362 }
363
364 #ifdef HAVE_PCAP_OPEN_DEAD
365 pcap_t*
366 pcap_open_dead(int a, int b)
367 {
368     if (!has_wpcap) {
369         return NULL;
370     }
371     return p_pcap_open_dead(a, b);
372 }
373 #endif
374
375 #ifdef HAVE_BPF_IMAGE
376 char *
377 bpf_image(const struct bpf_insn *a, int b)
378 {
379     if (!has_wpcap) {
380         return NULL;
381     }
382     return p_bpf_image(a, b);
383 }
384 #endif
385
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)
389 {
390     if (!has_wpcap) {
391         g_snprintf(f, PCAP_ERRBUF_SIZE,
392                    "unable to load WinPcap (wpcap.dll); can't open %s to capture",
393                    a);
394         return NULL;
395     }
396     return p_pcap_open(a, b, c, d, e, f);
397 }
398
399 int
400 pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
401 {
402     g_assert(has_wpcap);
403     return p_pcap_findalldevs_ex(a, b, c, d);
404 }
405
406 int
407 pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
408                   char *f)
409 {
410     g_assert(has_wpcap);
411     return p_pcap_createsrcstr(a, b, c, d, e, f);
412 }
413 #endif
414
415 #ifdef HAVE_PCAP_SETSAMPLING
416 struct pcap_samp *
417 pcap_setsampling(pcap_t *a)
418 {
419     g_assert(has_wpcap);
420     if (p_pcap_setsampling != NULL) {
421         return p_pcap_setsampling(a);
422     }
423     return NULL;
424 }
425 #endif
426
427 int
428 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
429 {
430         g_assert(has_wpcap);
431         return p_pcap_loop(a, b, c, d);
432 }
433
434 void
435 pcap_freecode(struct bpf_program *a)
436 {
437         g_assert(has_wpcap);
438     if(p_pcap_freecode) {
439             p_pcap_freecode(a);
440     }
441 }
442
443 #ifdef HAVE_PCAP_FINDALLDEVS
444 int
445 pcap_findalldevs(pcap_if_t **a, char *b)
446 {
447         g_assert(has_wpcap && p_pcap_findalldevs != NULL);
448         return p_pcap_findalldevs(a, b);
449 }
450
451 void
452 pcap_freealldevs(pcap_if_t *a)
453 {
454         g_assert(has_wpcap && p_pcap_freealldevs != NULL);
455         p_pcap_freealldevs(a);
456 }
457 #endif
458
459 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
460 /*
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()".
464  */
465 struct dlt_choice {
466         const char *name;
467         const char *description;
468         int     dlt;
469 };
470
471 #define DLT_CHOICE(code, description) { #code, description, code }
472 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
473
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
557 };
558 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION */
559
560 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
561 int
562 pcap_datalink_name_to_val(const char *name)
563 {
564         int i;
565
566         g_assert(has_wpcap);
567
568         if (p_pcap_datalink_name_to_val != NULL)
569                 return p_pcap_datalink_name_to_val(name);
570         else {
571                 /*
572                  * We don't have it in WinPcap; do it ourselves.
573                  */
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;
578                 }
579                 return -1;
580         }
581 }
582 #endif
583
584 #ifdef HAVE_PCAP_LIST_DATALINKS
585 int
586 pcap_list_datalinks(pcap_t *p, int **ddlt)
587 {
588         g_assert(has_wpcap);
589         return p_pcap_list_datalinks(p, ddlt);
590 }
591 #endif
592
593 #ifdef HAVE_PCAP_FREE_DATALINKS
594 void
595 pcap_free_datalinks(int *ddlt)
596 {
597         g_assert(has_wpcap);
598
599         /*
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.
606          */
607         if (p_pcap_free_datalinks != NULL)
608                 p_pcap_free_datalinks(ddlt);
609 }
610 #endif
611
612 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
613 const char *
614 pcap_datalink_val_to_name(int dlt)
615 {
616         int i;
617
618         g_assert(has_wpcap);
619
620         if (p_pcap_datalink_val_to_name != NULL)
621                 return p_pcap_datalink_val_to_name(dlt);
622         else {
623                 /*
624                  * We don't have it in WinPcap; do it ourselves.
625                  */
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;
629                 }
630                 return NULL;
631         }
632 }
633 #endif
634
635 #ifdef HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION
636 const char *
637 pcap_datalink_val_to_description(int dlt)
638 {
639         int i;
640
641         g_assert(has_wpcap);
642
643         if (p_pcap_datalink_val_to_description != NULL)
644                 return p_pcap_datalink_val_to_description(dlt);
645         else {
646                 /*
647                  * We don't have it in WinPcap; do it ourselves.
648                  */
649                 for (i = 0; dlt_choices[i].name != NULL; i++) {
650                         if (dlt_choices[i].dlt == dlt)
651                                 return (dlt_choices[i].description);
652                 }
653                 return NULL;
654         }
655 }
656 #endif
657
658 #ifdef HAVE_PCAP_BREAKLOOP
659 void pcap_breakloop(pcap_t *a)
660 {
661         p_pcap_breakloop(a);
662 }
663 #endif
664
665 /* setbuff is win32 specific! */
666 int pcap_setbuff(pcap_t *a, int b)
667 {
668         g_assert(has_wpcap);
669         return p_pcap_setbuff(a, b);
670 }
671
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)
675 {
676         g_assert(has_wpcap);
677         return p_pcap_next_ex(a, b, c);
678 }
679
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)
685 {
686     struct pcap_rmtauth auth;
687     char source[PCAP_BUF_SIZE];
688     char errbuf[PCAP_ERRBUF_SIZE];
689     GList *result;
690
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;
697     }
698
699     auth.type = auth_type;
700     auth.username = g_strdup(username);
701     auth.password = g_strdup(passwd);
702
703     result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
704     g_free(auth.username);
705     g_free(auth.password);
706
707     return result;
708 }
709 #endif
710
711 /*
712  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
713  * fall back on "pcap_lookupdev()".
714  */
715 GList *
716 get_interface_list(int *err, char **err_str)
717 {
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];
725
726         if (!has_wpcap) {
727                 /*
728                  * We don't have WinPcap, so we can't get a list of
729                  * interfaces.
730                  */
731                 *err = DONT_HAVE_PCAP;
732                 *err_str = cant_load_winpcap_err("you");
733                 return NULL;
734         }
735
736 #ifdef HAVE_PCAP_FINDALLDEVS
737         if (p_pcap_findalldevs != NULL)
738                 return get_interface_list_findalldevs(err, err_str);
739 #endif
740
741         /*
742          * In WinPcap, pcap_lookupdev is implemented by calling
743          * PacketGetAdapterNames.  According to the documentation
744          * I could find:
745          *
746          *      http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
747          *
748          * this means that:
749          *
750          * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
751          * of bytes consisting of:
752          *
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;
756          *
757          *      an empty ASCII string (i.e., a single 0 byte);
758          *
759          *      a sequence of null-terminated ASCII strings, giving the
760          *      descriptions of the interfaces;
761          *
762          *      an empty ASCII string.
763          *
764          * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
765          * returns a sequence of bytes consisting of:
766          *
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;
771          *
772          *      an empty Unicode string (i.e., a double 0 byte);
773          *
774          *      a sequence of null-terminated ASCII strings, giving the
775          *      descriptions of the interfaces;
776          *
777          *      an empty ASCII string.
778          *
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.
782          */
783
784         names = (wchar_t *)pcap_lookupdev(errbuf);
785         i = 0;
786
787         if (names) {
788                 char* desc = 0;
789                 int desc_pos = 0;
790
791                 if (names[0]<256) {
792                         /*
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.
796                          */
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 */
801
802                         while (names[i] != 0) {
803                                 /*
804                                  * Copy the Unicode description to an ASCII
805                                  * string.
806                                  */
807                                 j = 0;
808                                 while (*desc != 0) {
809                                         if (j < MAX_WIN_IF_NAME_LEN)
810                                                 ascii_desc[j++] = *desc;
811                                         desc++;
812                                 }
813                                 ascii_desc[j] = '\0';
814                                 desc++;
815
816                                 /*
817                                  * Copy the Unicode name to an ASCII string.
818                                  */
819                                 j = 0;
820                                 while (names[i] != 0) {
821                                         if (j < MAX_WIN_IF_NAME_LEN)
822                                         ascii_name[j++] = (char) names[i++];
823                                 }
824                                 ascii_name[j] = '\0';
825                                 i++;
826                                 il = g_list_append(il,
827                                     if_info_new(ascii_name, ascii_desc));
828                         }
829                 } else {
830                         /*
831                          * Otherwise we are in Windows 95/98 and using ASCII
832                          * (8-bit) characters.
833                          */
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;
839
840                         while (win95names[i] != '\0') {
841                                 /*
842                                  * "&win95names[i]" points to the current
843                                  * interface name, and "desc" points to
844                                  * that interface's description.
845                                  */
846                                 il = g_list_append(il,
847                                     if_info_new(&win95names[i], desc));
848
849                                 /*
850                                  * Skip to the next description.
851                                  */
852                                 while (*desc != 0)
853                                         desc++;
854                                 desc++;
855
856                                 /*
857                                  * Skip to the next name.
858                                  */
859                                 while (win95names[i] != 0)
860                                         i++;
861                                 i++;
862                         }
863                 }
864         }
865
866         if (il == NULL) {
867                 /*
868                  * No interfaces found.
869                  */
870                 *err = NO_INTERFACES_FOUND;
871                 if (err_str != NULL)
872                         *err_str = NULL;
873         }
874
875         return il;
876 }
877
878 /*
879  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
880  * "get_interface_list()".
881  */
882 gchar *
883 cant_get_if_list_error_message(const char *err_str)
884 {
885         /*
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.
889          */
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);
896         }
897         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
898 }
899
900 /*
901  * Append the version of WinPcap with which we were compiled to a GString.
902  */
903 void
904 get_compiled_pcap_version(GString *str)
905 {
906         g_string_append(str, "with WinPcap (version unknown)");
907 }
908
909 /*
910  * Append the version of WinPcap with which we we're running to a GString.
911  */
912 void
913 get_runtime_pcap_version(GString *str)
914 {
915         /*
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.
920          */
921         GModule *handle;                /* handle returned by ws_module_open */
922         static gchar *packetVer;
923         gchar *blankp;
924
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 {
930                         /*
931                          * An alternative method of obtaining the version
932                          * number, by using the PacketLibraryVersion
933                          * string from packet.dll.
934                          *
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.
942                          */
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";
956                                         }
957                                         g_module_close(handle);
958                                 }
959                         }
960                         g_string_append_printf(str, "WinPcap (%s)", packetVer);
961                 }
962         } else
963                 g_string_append(str, "without WinPcap");
964 }
965
966 #else /* HAVE_LIBPCAP */
967
968 void
969 load_wpcap(void)
970 {
971         return;
972 }
973
974 /*
975  * Append an indication that we were not compiled with WinPcap
976  * to a GString.
977  */
978 void
979 get_compiled_pcap_version(GString *str)
980 {
981         g_string_append(str, "without WinPcap");
982 }
983
984 /*
985  * Don't append anything, as we weren't even compiled to use WinPcap.
986  */
987 void
988 get_runtime_pcap_version(GString *str _U_)
989 {
990 }
991
992 #endif /* HAVE_LIBPCAP */