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