If WinPcap isn't loaded, return NULL from pcap_lookupdev() instead
[metze/wireshark/wip.git] / capture-wpcap.c
1 /* capture-wpcap.c
2  * WinPcap-specific interfaces for capturing.  We load WinPcap at run
3  * time, so that we only need one Wireshark binary and one TShark binary
4  * for Windows, regardless of whether WinPcap is installed or not.
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2001 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include <gmodule.h>
34
35 #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         g_assert(has_wpcap);
299         return p_pcap_open_live(a, b, c, d, e);
300 }
301
302 #ifdef HAVE_PCAP_REMOTE
303 pcap_t*
304 pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *f)
305 {
306     g_assert(has_wpcap);
307     return p_pcap_open(a, b, c, d, e, f);
308 }
309
310 int
311 pcap_findalldevs_ex(char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *d)
312 {
313     g_assert(has_wpcap);
314     return p_pcap_findalldevs_ex(a, b, c, d);
315 }
316
317 int
318 pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
319                   char *f)
320 {
321     g_assert(has_wpcap);
322     return p_pcap_createsrcstr(a, b, c, d, e, f);
323 }
324 #endif
325
326 #ifdef HAVE_PCAP_SETSAMPLING
327 struct pcap_samp *
328 pcap_setsampling(pcap_t *a)
329 {
330     g_assert(has_wpcap);
331     if (p_pcap_setsampling != NULL) {
332         return p_pcap_setsampling(a);
333     }
334     return NULL;
335 }
336 #endif
337
338 int
339 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
340 {
341         g_assert(has_wpcap);
342         return p_pcap_loop(a, b, c, d);
343 }
344
345 void
346 pcap_freecode(struct bpf_program *a)
347 {
348         g_assert(has_wpcap);
349     if(p_pcap_freecode) {
350             p_pcap_freecode(a);
351     }
352 }
353
354 #ifdef HAVE_PCAP_FINDALLDEVS
355 int
356 pcap_findalldevs(pcap_if_t **a, char *b)
357 {
358         g_assert(has_wpcap && p_pcap_findalldevs != NULL);
359         return p_pcap_findalldevs(a, b);
360 }
361
362 void
363 pcap_freealldevs(pcap_if_t *a)
364 {
365         g_assert(has_wpcap && p_pcap_freealldevs != NULL);
366         p_pcap_freealldevs(a);
367 }
368 #endif
369
370 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
371 /*
372  * Table of DLT_ types, names, and descriptions, for use if the version
373  * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
374  * or "pcap_datalink_val_to_name()".
375  */
376 struct dlt_choice {
377         const char *name;
378         const char *description;
379         int     dlt;
380 };
381
382 #define DLT_CHOICE(code, description) { #code, description, code }
383 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
384
385 static struct dlt_choice dlt_choices[] = {
386         DLT_CHOICE(DLT_NULL, "BSD loopback"),
387         DLT_CHOICE(DLT_EN10MB, "Ethernet"),
388         DLT_CHOICE(DLT_IEEE802, "Token ring"),
389         DLT_CHOICE(DLT_ARCNET, "ARCNET"),
390         DLT_CHOICE(DLT_SLIP, "SLIP"),
391         DLT_CHOICE(DLT_PPP, "PPP"),
392         DLT_CHOICE(DLT_FDDI, "FDDI"),
393         DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
394         DLT_CHOICE(DLT_RAW, "Raw IP"),
395 #ifdef DLT_SLIP_BSDOS
396         DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
397 #endif
398 #ifdef DLT_PPP_BSDOS
399         DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
400 #endif
401 #ifdef DLT_ATM_CLIP
402         DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
403 #endif
404 #ifdef DLT_PPP_SERIAL
405         DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
406 #endif
407 #ifdef DLT_PPP_ETHER
408         DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
409 #endif
410 #ifdef DLT_C_HDLC
411         DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
412 #endif
413 #ifdef DLT_IEEE802_11
414         DLT_CHOICE(DLT_IEEE802_11, "802.11"),
415 #endif
416 #ifdef DLT_FRELAY
417         DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
418 #endif
419 #ifdef DLT_LOOP
420         DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
421 #endif
422 #ifdef DLT_ENC
423         DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
424 #endif
425 #ifdef DLT_LINUX_SLL
426         DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
427 #endif
428 #ifdef DLT_LTALK
429         DLT_CHOICE(DLT_LTALK, "Localtalk"),
430 #endif
431 #ifdef DLT_PFLOG
432         DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
433 #endif
434 #ifdef DLT_PRISM_HEADER
435         DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
436 #endif
437 #ifdef DLT_IP_OVER_FC
438         DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
439 #endif
440 #ifdef DLT_SUNATM
441         DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
442 #endif
443 #ifdef DLT_IEEE802_11_RADIO
444         DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
445 #endif
446 #ifdef DLT_ARCNET_LINUX
447         DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
448 #endif
449 #ifdef DLT_LINUX_IRDA
450         DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
451 #endif
452 #ifdef DLT_LINUX_LAPD
453         DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
454 #endif
455 #ifdef DLT_LANE8023
456         DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
457 #endif
458 #ifdef DLT_CIP
459         DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
460 #endif
461 #ifdef DLT_HDLC
462         DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
463 #endif
464         DLT_CHOICE_SENTINEL
465 };
466 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
467
468 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
469 int
470 pcap_datalink_name_to_val(const char *name)
471 {
472         int i;
473
474         g_assert(has_wpcap);
475
476         if (p_pcap_datalink_name_to_val != NULL)
477                 return p_pcap_datalink_name_to_val(name);
478         else {
479                 /*
480                  * We don't have it in WinPcap; do it ourselves.
481                  */
482                 for (i = 0; dlt_choices[i].name != NULL; i++) {
483                         if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
484                             name) == 0)
485                                 return dlt_choices[i].dlt;
486                 }
487                 return -1;
488         }
489 }
490 #endif
491
492 #ifdef HAVE_PCAP_LIST_DATALINKS
493 int
494 pcap_list_datalinks(pcap_t *p, int **ddlt)
495 {
496         g_assert(has_wpcap);
497         return p_pcap_list_datalinks(p, ddlt);
498 }
499 #endif
500
501 #ifdef HAVE_PCAP_FREE_DATALINKS
502 void
503 pcap_free_datalinks(int *ddlt)
504 {
505         g_assert(has_wpcap);
506
507         /*
508          * If we don't have pcap_free_datalinks() in WinPcap,
509          * we don't free the memory - we can't use free(), as
510          * we might not have been built with the same version
511          * of the C runtime library as WinPcap was, and, if we're
512          * not, free() isn't guaranteed to work on something
513          * allocated by WinPcap.
514          */
515         if (p_pcap_free_datalinks != NULL)
516                 p_pcap_free_datalinks(ddlt);
517 }
518 #endif
519
520 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
521 const char *
522 pcap_datalink_val_to_name(int dlt)
523 {
524         int i;
525
526         g_assert(has_wpcap);
527
528         if (p_pcap_datalink_val_to_name != NULL)
529                 return p_pcap_datalink_val_to_name(dlt);
530         else {
531                 /*
532                  * We don't have it in WinPcap; do it ourselves.
533                  */
534                 for (i = 0; dlt_choices[i].name != NULL; i++) {
535                         if (dlt_choices[i].dlt == dlt)
536                                 return dlt_choices[i].name + sizeof("DLT_") - 1;
537                 }
538                 return NULL;
539         }
540 }
541 #endif
542
543 #ifdef HAVE_PCAP_BREAKLOOP
544 void pcap_breakloop(pcap_t *a)
545 {
546         p_pcap_breakloop(a);
547 }
548 #endif
549
550 /* setbuff is win32 specific! */
551 int pcap_setbuff(pcap_t *a, int b)
552 {
553         g_assert(has_wpcap);
554         return p_pcap_setbuff(a, b);
555 }
556
557 /* pcap_next_ex is available since libpcap 0.8 / WinPcap 3.0! */
558 /* (if you get a declaration warning here, try to update to at least WinPcap 3.1b4 develpack) */
559 int pcap_next_ex (pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
560 {
561         g_assert(has_wpcap);
562         return p_pcap_next_ex(a, b, c);
563 }
564
565 #ifdef HAVE_PCAP_REMOTE
566 GList *
567 get_remote_interface_list(const char *hostname, const char *port,
568                           int auth_type, const char *username,
569                           const char *passwd, int *err, char **err_str)
570 {
571     struct pcap_rmtauth auth;
572     char source[PCAP_BUF_SIZE];
573     char errbuf[PCAP_ERRBUF_SIZE];
574     GList *result;
575
576     if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
577                           NULL, errbuf) == -1) {
578         *err = CANT_GET_INTERFACE_LIST;
579         if (err_str != NULL)
580             *err_str = cant_get_if_list_error_message(errbuf);
581         return NULL;
582     }
583
584     auth.type = auth_type;
585     auth.username = g_strdup(username);
586     auth.password = g_strdup(passwd);
587
588     result = get_interface_list_findalldevs_ex(source, &auth, err, err_str);
589     g_free(auth.username);
590     g_free(auth.password);
591
592     return result;
593 }
594 #endif
595
596 /*
597  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
598  * fall back on "pcap_lookupdev()".
599  */
600 GList *
601 get_interface_list(int *err, char **err_str)
602 {
603         GList  *il = NULL;
604         wchar_t *names;
605         char *win95names;
606         char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
607         char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
608         int i, j;
609         char errbuf[PCAP_ERRBUF_SIZE];
610
611 #ifdef HAVE_PCAP_FINDALLDEVS
612         if (p_pcap_findalldevs != NULL)
613                 return get_interface_list_findalldevs(err, err_str);
614 #endif
615
616         /*
617          * In WinPcap, pcap_lookupdev is implemented by calling
618          * PacketGetAdapterNames.  According to the documentation
619          * I could find:
620          *
621          *      http://www.winpcap.org/docs/man/html/Packet32_8c.html#a43
622          *
623          * this means that:
624          *
625          * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
626          * of bytes consisting of:
627          *
628          *      a sequence of null-terminated ASCII strings (i.e., each
629          *      one is terminated by a single 0 byte), giving the names
630          *      of the interfaces;
631          *
632          *      an empty ASCII string (i.e., a single 0 byte);
633          *
634          *      a sequence of null-terminated ASCII strings, giving the
635          *      descriptions of the interfaces;
636          *
637          *      an empty ASCII string.
638          *
639          * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
640          * returns a sequence of bytes consisting of:
641          *
642          *      a sequence of null-terminated double-byte Unicode strings
643          *      (i.e., each one consits of a sequence of double-byte
644          *      characters, terminated by a double-byte 0), giving the
645          *      names of the interfaces;
646          *
647          *      an empty Unicode string (i.e., a double 0 byte);
648          *
649          *      a sequence of null-terminated ASCII strings, giving the
650          *      descriptions of the interfaces;
651          *
652          *      an empty ASCII string.
653          *
654          * The Nth string in the first sequence is the name of the Nth
655          * adapter; the Nth string in the second sequence is the
656          * description of the Nth adapter.
657          */
658
659         names = (wchar_t *)pcap_lookupdev(errbuf);
660         i = 0;
661
662         if (names) {
663                 char* desc = 0;
664                 int desc_pos = 0;
665
666                 if (names[0]<256) {
667                         /*
668                          * If names[0] is less than 256 it means the first
669                          * byte is 0.  This implies that we are using Unicode
670                          * characters.
671                          */
672                         while (*(names+desc_pos) || *(names+desc_pos-1))
673                                 desc_pos++;
674                         desc_pos++;     /* Step over the extra '\0' */
675                         desc = (char*)(names + desc_pos); /* cast *after* addition */
676
677                         while (names[i] != 0) {
678                                 /*
679                                  * Copy the Unicode description to an ASCII
680                                  * string.
681                                  */
682                                 j = 0;
683                                 while (*desc != 0) {
684                                         if (j < MAX_WIN_IF_NAME_LEN)
685                                                 ascii_desc[j++] = *desc;
686                                         desc++;
687                                 }
688                                 ascii_desc[j] = '\0';
689                                 desc++;
690
691                                 /*
692                                  * Copy the Unicode name to an ASCII string.
693                                  */
694                                 j = 0;
695                                 while (names[i] != 0) {
696                                         if (j < MAX_WIN_IF_NAME_LEN)
697                                         ascii_name[j++] = (char) names[i++];
698                                 }
699                                 ascii_name[j] = '\0';
700                                 i++;
701                                 il = g_list_append(il,
702                                     if_info_new(ascii_name, ascii_desc));
703                         }
704                 } else {
705                         /*
706                          * Otherwise we are in Windows 95/98 and using ASCII
707                          * (8-bit) characters.
708                          */
709                         win95names=(char *)names;
710                         while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
711                                 desc_pos++;
712                         desc_pos++;     /* Step over the extra '\0' */
713                         desc = win95names + desc_pos;
714
715                         while (win95names[i] != '\0') {
716                                 /*
717                                  * "&win95names[i]" points to the current
718                                  * interface name, and "desc" points to
719                                  * that interface's description.
720                                  */
721                                 il = g_list_append(il,
722                                     if_info_new(&win95names[i], desc));
723
724                                 /*
725                                  * Skip to the next description.
726                                  */
727                                 while (*desc != 0)
728                                         desc++;
729                                 desc++;
730
731                                 /*
732                                  * Skip to the next name.
733                                  */
734                                 while (win95names[i] != 0)
735                                         i++;
736                                 i++;
737                         }
738                 }
739         }
740
741         if (il == NULL) {
742                 /*
743                  * No interfaces found.
744                  */
745                 *err = NO_INTERFACES_FOUND;
746                 if (err_str != NULL)
747                         *err_str = NULL;
748         }
749
750         return il;
751 }
752
753 /*
754  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
755  * "get_interface_list()".
756  */
757 gchar *
758 cant_get_if_list_error_message(const char *err_str)
759 {
760         /*
761          * If the error message includes "Not enough storage is available
762          * to process this command" or "The operation completed successfully",
763          * suggest that they install a WinPcap version later than 3.0.
764          */
765         if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
766             strstr(err_str, "The operation completed successfully") != NULL) {
767                 return g_strdup_printf("Can't get list of interfaces: %s\n"
768 "This might be a problem with WinPcap 3.0; you should try updating to\n"
769 "a later version of WinPcap - see the WinPcap site at www.winpcap.org",
770                     err_str);
771         }
772         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
773 }
774
775 /*
776  * Append the version of WinPcap with which we were compiled to a GString.
777  */
778 void
779 get_compiled_pcap_version(GString *str)
780 {
781         g_string_append(str, "with WinPcap (version unknown)");
782 }
783
784 /*
785  * Append the version of WinPcap with which we we're running to a GString.
786  */
787 void
788 get_runtime_pcap_version(GString *str)
789 {
790         /*
791          * On Windows, we might have been compiled with WinPcap but
792          * might not have it loaded; indicate whether we have it or
793          * not and, if we have it and we have "pcap_lib_version()",
794          * what version we have.
795          */
796         GModule *handle;                /* handle returned by dlopen */
797         static gchar *packetVer;
798         gchar *blankp;
799
800         if (has_wpcap) {
801                 g_string_append_printf(str, "with ");
802                 if (p_pcap_lib_version != NULL)
803                         g_string_append_printf(str, p_pcap_lib_version());
804                 else {
805                         /*
806                          * An alternative method of obtaining the version
807                          * number, by using the PacketLibraryVersion
808                          * string from packet.dll.
809                          *
810                          * Unfortunately, in WinPcap 3.0, it returns
811                          * "3.0 alpha3", even in the final version of
812                          * WinPcap 3.0, so if there's a blank in the
813                          * string, we strip it and everything after
814                          * it from the string, so we don't misleadingly
815                          * report that 3.0 alpha3 is being used when
816                          * the final version is being used.
817                          */
818                         if (packetVer == NULL) {
819                                 packetVer = "version unknown";
820                                 handle = g_module_open("Packet.dll", 0);
821                                 if (handle != NULL) {
822                                         if (g_module_symbol(handle,
823                                             "PacketLibraryVersion",
824                                             (gpointer*)&packetVer)) {
825                                                 packetVer = g_strdup(packetVer);
826                                                 blankp = strchr(packetVer, ' ');
827                                                 if (blankp != NULL)
828                                                         *blankp = '\0';
829                                         } else {
830                                                 packetVer = "version unknown";
831                                         }
832                                         g_module_close(handle);
833                                 }
834                         }
835                         g_string_append_printf(str, "WinPcap (%s)", packetVer);
836                 }
837         } else
838                 g_string_append(str, "without WinPcap");
839 }
840
841 #else /* HAVE_LIBPCAP */
842
843 void
844 load_wpcap(void)
845 {
846         return;
847 }
848
849 /*
850  * Append an indication that we were not compiled with WinPcap
851  * to a GString.
852  */
853 void
854 get_compiled_pcap_version(GString *str)
855 {
856         g_string_append(str, "without WinPcap");
857 }
858
859 /*
860  * Don't append anything, as we weren't even compiled to use WinPcap.
861  */
862 void
863 get_runtime_pcap_version(GString *str _U_)
864 {
865 }
866
867 #endif /* HAVE_LIBPCAP */