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