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