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