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