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