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