It doesn't matter whether, when building with WinPcap, we have
[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 Ethereal binary and one Tethereal binary
4  * for Windows, regardless of whether WinPcap is installed or not.
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_LIBPCAP
32 #include <pcap.h>
33 #endif
34
35 #include <glib.h>
36 #include <gmodule.h>
37
38 #include "pcap-util.h"
39 #include "pcap-util-int.h"
40
41 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
42 #include "tools/lemon/cppmagic.h"
43
44 gboolean has_wpcap = FALSE;
45
46 #ifdef HAVE_LIBPCAP
47
48 static char*   (*p_pcap_lookupdev) (char *);
49 static void    (*p_pcap_close) (pcap_t *);
50 static int     (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
51 static int     (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, guchar *);
52 static int     (*p_pcap_snapshot) (pcap_t *);
53 static int     (*p_pcap_datalink) (pcap_t *);
54 static int     (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
55 static char*   (*p_pcap_geterr) (pcap_t *);
56 static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, char *, int,
57                         bpf_u_int32);
58 #ifdef WPCAP_CONSTIFIED
59 static int     (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
60                         char *);
61 static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
62 #else
63 static int     (*p_pcap_lookupnet) (char *, bpf_u_int32 *, bpf_u_int32 *,
64                         char *);
65 static pcap_t* (*p_pcap_open_live) (char *, int, int, int, char *);
66 #endif
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 static const char *(*p_pcap_lib_version) (void);
80 static int     (*p_pcap_setbuff) (pcap_t *, int dim);
81
82 typedef struct {
83         const char      *name;
84         gpointer        *ptr;
85         gboolean        optional;
86 } symbol_table_t;
87
88 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
89
90 void
91 load_wpcap(void)
92 {
93
94         /* These are the symbols I need or want from Wpcap */
95         static const symbol_table_t     symbols[] = {
96                 SYM(pcap_lookupdev, FALSE),
97                 SYM(pcap_close, FALSE),
98                 SYM(pcap_stats, FALSE),
99                 SYM(pcap_dispatch, FALSE),
100                 SYM(pcap_snapshot, FALSE),
101                 SYM(pcap_datalink, FALSE),
102                 SYM(pcap_setfilter, FALSE),
103                 SYM(pcap_geterr, FALSE),
104                 SYM(pcap_compile, FALSE),
105                 SYM(pcap_lookupnet, FALSE),
106                 SYM(pcap_open_live, FALSE),
107                 SYM(pcap_loop, FALSE),
108                 SYM(pcap_freecode, FALSE),
109 #ifdef HAVE_PCAP_FINDALLDEVS
110                 SYM(pcap_findalldevs, TRUE),
111                 SYM(pcap_freealldevs, TRUE),
112 #endif
113 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
114                 SYM(pcap_datalink_name_to_val, TRUE),
115 #endif
116 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
117                 SYM(pcap_datalink_val_to_name, TRUE),
118 #endif
119                 SYM(pcap_lib_version, TRUE),
120                 SYM(pcap_setbuff, TRUE),
121                 { NULL, NULL, FALSE }
122         };
123
124         GModule         *wh; /* wpcap handle */
125         const symbol_table_t    *sym;
126
127         wh = g_module_open("wpcap", 0);
128
129         if (!wh) {
130                 return;
131         }
132
133         sym = symbols;
134         while (sym->name) {
135                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
136                         if (sym->optional) {
137                                 /*
138                                  * We don't care if it's missing; we just
139                                  * don't use it.
140                                  */
141                                 *sym->ptr = NULL;
142                         } else {
143                                 /*
144                                  * We require this symbol.
145                                  */
146                                 return;
147                         }
148                 }
149                 sym++;
150         }
151
152
153         has_wpcap = TRUE;
154 }
155
156 char*
157 pcap_lookupdev (char *a)
158 {
159         g_assert(has_wpcap);
160         return p_pcap_lookupdev(a);
161 }
162
163 void
164 pcap_close(pcap_t *a)
165 {
166         g_assert(has_wpcap);
167         p_pcap_close(a);
168 }
169
170 int
171 pcap_stats(pcap_t *a, struct pcap_stat *b)
172 {
173         g_assert(has_wpcap);
174         return p_pcap_stats(a, b);
175 }
176
177 int
178 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
179 {
180         g_assert(has_wpcap);
181         return p_pcap_dispatch(a, b, c, d);
182 }
183
184 int
185 pcap_snapshot(pcap_t *a)
186 {
187         g_assert(has_wpcap);
188         return p_pcap_snapshot(a);
189 }
190
191 int
192 pcap_datalink(pcap_t *a)
193 {
194         g_assert(has_wpcap);
195         return p_pcap_datalink(a);
196 }
197
198 int
199 pcap_setfilter(pcap_t *a, struct bpf_program *b)
200 {
201         g_assert(has_wpcap);
202         return p_pcap_setfilter(a, b);
203 }
204
205 char*
206 pcap_geterr(pcap_t *a)
207 {
208         g_assert(has_wpcap);
209         return p_pcap_geterr(a);
210 }
211
212 int
213 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
214             bpf_u_int32 e)
215 {
216         g_assert(has_wpcap);
217         return p_pcap_compile(a, b, c, d, e);
218 }
219
220 int
221 #ifdef WPCAP_CONSTIFIED
222 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
223 #else
224 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
225 #endif
226 {
227         g_assert(has_wpcap);
228         return p_pcap_lookupnet(a, b, c, d);
229 }
230
231 pcap_t*
232 #ifdef WPCAP_CONSTIFIED
233 pcap_open_live(const char *a, int b, int c, int d, char *e)
234 #else
235 pcap_open_live(char *a, int b, int c, int d, char *e)
236 #endif
237 {
238         g_assert(has_wpcap);
239         return p_pcap_open_live(a, b, c, d, e);
240 }
241
242 int
243 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
244 {
245         g_assert(has_wpcap);
246         return p_pcap_loop(a, b, c, d);
247 }
248
249 void
250 pcap_freecode(struct bpf_program *a)
251 {
252         g_assert(has_wpcap);
253         p_pcap_freecode(a);
254 }
255
256 #ifdef HAVE_PCAP_FINDALLDEVS
257 int
258 pcap_findalldevs(pcap_if_t **a, char *b)
259 {
260         g_assert(has_wpcap && p_pcap_findalldevs != NULL);
261         return p_pcap_findalldevs(a, b);
262 }
263
264 void
265 pcap_freealldevs(pcap_if_t *a)
266 {
267         g_assert(has_wpcap && p_pcap_freealldevs != NULL);
268         p_pcap_freealldevs(a);
269 }
270 #endif
271
272 #if defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
273 /*
274  * Table of DLT_ types, names, and descriptions, for use if the version
275  * of WinPcap we have installed lacks "pcap_datalink_name_to_val()"
276  * or "pcap_datalink_val_to_name()".
277  */
278 struct dlt_choice {
279         const char *name;
280         const char *description;
281         int     dlt;
282 };
283
284 #define DLT_CHOICE(code, description) { #code, description, code }
285 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
286
287 static struct dlt_choice dlt_choices[] = {
288         DLT_CHOICE(DLT_NULL, "BSD loopback"),
289         DLT_CHOICE(DLT_EN10MB, "Ethernet"),
290         DLT_CHOICE(DLT_IEEE802, "Token ring"),
291         DLT_CHOICE(DLT_ARCNET, "ARCNET"),
292         DLT_CHOICE(DLT_SLIP, "SLIP"),
293         DLT_CHOICE(DLT_PPP, "PPP"),
294         DLT_CHOICE(DLT_FDDI, "FDDI"),
295         DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
296         DLT_CHOICE(DLT_RAW, "Raw IP"),
297 #ifdef DLT_SLIP_BSDOS
298         DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
299 #endif
300 #ifdef DLT_PPP_BSDOS
301         DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
302 #endif
303 #ifdef DLT_ATM_CLIP
304         DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
305 #endif
306 #ifdef DLT_PPP_SERIAL
307         DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
308 #endif
309 #ifdef DLT_PPP_ETHER
310         DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
311 #endif
312 #ifdef DLT_C_HDLC
313         DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
314 #endif
315 #ifdef DLT_IEEE802_11
316         DLT_CHOICE(DLT_IEEE802_11, "802.11"),
317 #endif
318 #ifdef DLT_FRELAY
319         DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
320 #endif
321 #ifdef DLT_LOOP
322         DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
323 #endif
324 #ifdef DLT_ENC
325         DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
326 #endif
327 #ifdef DLT_LINUX_SLL
328         DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
329 #endif
330 #ifdef DLT_LTALK
331         DLT_CHOICE(DLT_LTALK, "Localtalk"),
332 #endif
333 #ifdef DLT_PFLOG
334         DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
335 #endif
336 #ifdef DLT_PRISM_HEADER
337         DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
338 #endif
339 #ifdef DLT_IP_OVER_FC
340         DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
341 #endif
342 #ifdef DLT_SUNATM
343         DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
344 #endif
345 #ifdef DLT_IEEE802_11_RADIO
346         DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"),
347 #endif
348 #ifdef DLT_ARCNET_LINUX
349         DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
350 #endif
351 #ifdef DLT_LINUX_IRDA
352         DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
353 #endif
354 #ifdef DLT_LANE8023
355         DLT_CHOICE(DLT_LANE8023, "Linux 802.3 LANE"),
356 #endif
357 #ifdef DLT_CIP
358         DLT_CHOICE(DLT_CIP, "Linux Classical IP-over-ATM"),
359 #endif
360 #ifdef DLT_HDLC
361         DLT_CHOICE(DLT_HDLC, "Cisco HDLC"),
362 #endif
363         DLT_CHOICE_SENTINEL
364 };
365 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
366
367 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
368 int
369 pcap_datalink_name_to_val(const char *name)
370 {
371         int i;
372
373         g_assert(has_wpcap);
374
375         if (p_pcap_datalink_name_to_val != NULL)
376                 return p_pcap_datalink_name_to_val(name);
377         else {
378                 /*
379                  * We don't have it in WinPcap; do it ourselves.
380                  */
381                 for (i = 0; dlt_choices[i].name != NULL; i++) {
382                         if (strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
383                             name) == 0)
384                                 return dlt_choices[i].dlt;
385                 }
386                 return -1;
387         }
388 }
389 #endif
390
391 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
392 const char *
393 pcap_datalink_val_to_name(int dlt)
394 {
395         int i;
396
397         g_assert(has_wpcap);
398
399         if (p_pcap_datalink_val_to_name != NULL)
400                 return p_pcap_datalink_val_to_name(dlt);
401         else {
402                 /*
403                  * We don't have it in WinPcap; do it ourselves.
404                  */
405                 for (i = 0; dlt_choices[i].name != NULL; i++) {
406                         if (dlt_choices[i].dlt == dlt)
407                                 return dlt_choices[i].name + sizeof("DLT_") - 1;
408                 }
409                 return NULL;
410         }
411 }
412 #endif
413
414 /* setbuff is win32 specific! */
415 int pcap_setbuff(pcap_t *a, int b)
416 {
417         g_assert(has_wpcap);
418         return p_pcap_setbuff(a, b);
419 }
420
421 /*
422  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
423  * fall back on "pcap_lookupdev()".
424  */
425 GList *
426 get_interface_list(int *err, char *err_str)
427 {
428         GList  *il = NULL;
429         wchar_t *names;
430         char *win95names;
431         char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
432         char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
433         int i, j;
434
435 #ifdef HAVE_PCAP_FINDALLDEVS
436         if (p_pcap_findalldevs != NULL)
437                 return get_interface_list_findalldevs(err, err_str);
438 #endif
439
440         /*
441          * In WinPcap, pcap_lookupdev is implemented by calling
442          * PacketGetAdapterNames.  According to the documentation
443          * I could find:
444          *
445          *      http://winpcap.polito.it/docs/man/html/Packet32_8c.html#a43
446          *
447          * this means that:
448          *
449          * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
450          * of bytes consisting of:
451          *
452          *      a sequence of null-terminated ASCII strings (i.e., each
453          *      one is terminated by a single 0 byte), giving the names
454          *      of the interfaces;
455          *
456          *      an empty ASCII string (i.e., a single 0 byte);
457          *
458          *      a sequence of null-terminated ASCII strings, giving the
459          *      descriptions of the interfaces;
460          *
461          *      an empty ASCII string.
462          *
463          * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
464          * returns a sequence of bytes consisting of:
465          *
466          *      a sequence of null-terminated double-byte Unicode strings
467          *      (i.e., each one consits of a sequence of double-byte
468          *      characters, terminated by a double-byte 0), giving the
469          *      names of the interfaces;
470          *
471          *      an empty Unicode string (i.e., a double 0 byte);
472          *
473          *      a sequence of null-terminated ASCII strings, giving the
474          *      descriptions of the interfaces;
475          *
476          *      an empty ASCII string.
477          *
478          * The Nth string in the first sequence is the name of the Nth
479          * adapter; the Nth string in the second sequence is the
480          * description of the Nth adapter.
481          */
482
483         names = (wchar_t *)pcap_lookupdev(err_str);
484         i = 0;
485
486         if (names) {
487                 char* desc = 0;
488                 int desc_pos = 0;
489
490                 if (names[0]<256) {
491                         /*
492                          * If names[0] is less than 256 it means the first
493                          * byte is 0.  This implies that we are using Unicode
494                          * characters.
495                          */
496                         while (*(names+desc_pos) || *(names+desc_pos-1))
497                                 desc_pos++;
498                         desc_pos++;     /* Step over the extra '\0' */
499                         desc = (char*)(names + desc_pos); /* cast *after* addition */
500
501                         while (names[i] != 0) {
502                                 /*
503                                  * Copy the Unicode description to an ASCII
504                                  * string.
505                                  */
506                                 j = 0;
507                                 while (*desc != 0) {
508                                         if (j < MAX_WIN_IF_NAME_LEN)
509                                                 ascii_desc[j++] = *desc;
510                                         desc++;
511                                 }
512                                 ascii_desc[j] = '\0';
513                                 desc++;
514
515                                 /*
516                                  * Copy the Unicode name to an ASCII string.
517                                  */
518                                 j = 0;
519                                 while (names[i] != 0) {
520                                         if (j < MAX_WIN_IF_NAME_LEN)
521                                         ascii_name[j++] = (char) names[i++];
522                                 }
523                                 ascii_name[j] = '\0';
524                                 i++;
525                                 il = g_list_append(il,
526                                     if_info_new(ascii_name, ascii_desc));
527                         }
528                 } else {
529                         /*
530                          * Otherwise we are in Windows 95/98 and using ASCII
531                          * (8-bit) characters.
532                          */
533                         win95names=(char *)names;
534                         while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
535                                 desc_pos++;
536                         desc_pos++;     /* Step over the extra '\0' */
537                         desc = win95names + desc_pos;
538
539                         while (win95names[i] != '\0') {
540                                 /*
541                                  * "&win95names[i]" points to the current
542                                  * interface name, and "desc" points to
543                                  * that interface's description.
544                                  */
545                                 il = g_list_append(il,
546                                     if_info_new(&win95names[i], desc));
547
548                                 /*
549                                  * Skip to the next description.
550                                  */
551                                 while (*desc != 0)
552                                         desc++;
553                                 desc++;
554
555                                 /*
556                                  * Skip to the next name.
557                                  */
558                                 while (win95names[i] != 0)
559                                         i++;
560                                 i++;
561                         }
562                 }
563         }
564
565         if (il == NULL) {
566                 /*
567                  * No interfaces found.
568                  */
569                 *err = NO_INTERFACES_FOUND;
570         }
571
572         return il;
573 }
574
575 /*
576  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
577  * "get_interface_list()".
578  */
579 gchar *
580 cant_get_if_list_error_message(const char *err_str)
581 {
582         /*
583          * If the error message includes "Not enough storage is available
584          * to process this command" or "The operation completed successfully",
585          * suggest that they install a WinPcap version later than 3.0.
586          */
587         if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
588             strstr(err_str, "The operation completed successfully") != NULL) {
589                 return g_strdup_printf("Can't get list of interfaces: %s\n"
590 "This might be a problem with WinPcap 3.0; you should try updating to\n"
591 "a later version of WinPcap - see the WinPcap site at winpcap.polito.it",
592                     err_str);
593         }
594         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
595 }
596
597 /*
598  * Append the version of WinPcap with which we were compiled to a GString.
599  */
600 void
601 get_compiled_pcap_version(GString *str)
602 {
603         g_string_append(str, "with WinPcap (version unknown)");
604 }
605
606 /*
607  * Append the version of WinPcap with which we we're running to a GString.
608  */
609 void
610 get_runtime_pcap_version(GString *str)
611 {
612         /*
613          * On Windows, we might have been compiled with WinPcap but
614          * might not have it loaded; indicate whether we have it or
615          * not and, if we have it and we have "pcap_lib_version()",
616          * what version we have.
617          */
618         GModule *handle;                /* handle returned by dlopen */
619         gchar *packetVer = NULL;
620
621         if (has_wpcap) {
622                 /* An alternative method of obtaining the version number */
623                 if ((handle = g_module_open("Packet.dll", 0)) != NULL) {
624                         if (g_module_symbol(handle, "PacketLibraryVersion",
625                             (gpointer*)&packetVer) == FALSE)
626                                 packetVer = NULL;
627                         g_module_close(handle);
628                 }
629
630                 g_string_sprintfa(str, "with ");
631                 if (p_pcap_lib_version != NULL)
632                         g_string_sprintfa(str, p_pcap_lib_version());
633                 else if (packetVer != NULL)
634                         g_string_sprintfa(str, "WinPcap (%s)", packetVer);
635                 else
636                         g_string_append(str, "WinPcap (version unknown)");
637         } else
638                 g_string_append(str, "without WinPcap");
639         g_string_append(str, " ");
640 }
641
642 #else /* HAVE_LIBPCAP */
643
644 void
645 load_wpcap(void)
646 {
647         return;
648 }
649
650 /*
651  * Append an indication that we were not compiled with WinPcap
652  * to a GString.
653  */
654 void
655 get_compiled_pcap_version(GString *str)
656 {
657         g_string_append(str, "without WinPcap");
658 }
659
660 /*
661  * Don't append anything, as we weren't even compiled to use WinPcap.
662  */
663 void
664 get_runtime_pcap_version(GString *str _U_)
665 {
666 }
667
668 #endif /* HAVE_LIBPCAP */