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