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