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