On Windows, if "get_interface_list()" returns CANT_GET_INTERFACE_LIST
[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: capture-wpcap.c,v 1.10 2004/06/12 07:47:12 guy Exp $
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 static const char *(*p_pcap_lib_version) (void);
73 static int     (*p_pcap_setbuff) (pcap_t *, int dim);
74
75 typedef struct {
76         const char      *name;
77         gpointer        *ptr;
78         gboolean        optional;
79 } symbol_table_t;
80
81 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
82
83 void
84 load_wpcap(void)
85 {
86
87         /* These are the symbols I need or want from Wpcap */
88         static const symbol_table_t     symbols[] = {
89                 SYM(pcap_lookupdev, FALSE),
90                 SYM(pcap_close, FALSE),
91                 SYM(pcap_stats, FALSE),
92                 SYM(pcap_dispatch, FALSE),
93                 SYM(pcap_snapshot, FALSE),
94                 SYM(pcap_datalink, FALSE),
95                 SYM(pcap_setfilter, FALSE),
96                 SYM(pcap_geterr, FALSE),
97                 SYM(pcap_compile, FALSE),
98                 SYM(pcap_lookupnet, FALSE),
99                 SYM(pcap_open_live, FALSE),
100                 SYM(pcap_loop, FALSE),
101 #ifdef HAVE_PCAP_FINDALLDEVS
102                 SYM(pcap_findalldevs, TRUE),
103                 SYM(pcap_freealldevs, TRUE),
104 #endif
105                 SYM(pcap_lib_version, TRUE),
106                 SYM(pcap_setbuff, TRUE),
107                 { NULL, NULL, FALSE }
108         };
109
110         GModule         *wh; /* wpcap handle */
111         const symbol_table_t    *sym;
112
113         wh = g_module_open("wpcap", 0);
114
115         if (!wh) {
116                 return;
117         }
118
119         sym = symbols;
120         while (sym->name) {
121                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
122                         if (sym->optional) {
123                                 /*
124                                  * We don't care if it's missing; we just
125                                  * don't use it.
126                                  */
127                                 *sym->ptr = NULL;
128                         } else {
129                                 /*
130                                  * We require this symbol.
131                                  */
132                                 return;
133                         }
134                 }
135                 sym++;
136         }
137
138
139         has_wpcap = TRUE;
140 }
141
142 char*
143 pcap_lookupdev (char *a)
144 {
145         g_assert(has_wpcap);
146         return p_pcap_lookupdev(a);
147 }
148
149 void
150 pcap_close(pcap_t *a)
151 {
152         g_assert(has_wpcap);
153         p_pcap_close(a);
154 }
155
156 int
157 pcap_stats(pcap_t *a, struct pcap_stat *b)
158 {
159         g_assert(has_wpcap);
160         return p_pcap_stats(a, b);
161 }
162
163 int
164 pcap_dispatch(pcap_t *a, int b, pcap_handler c, guchar *d)
165 {
166         g_assert(has_wpcap);
167         return p_pcap_dispatch(a, b, c, d);
168 }
169
170
171 int
172 pcap_snapshot(pcap_t *a)
173 {
174         g_assert(has_wpcap);
175         return p_pcap_snapshot(a);
176 }
177
178
179 int
180 pcap_datalink(pcap_t *a)
181 {
182         g_assert(has_wpcap);
183         return p_pcap_datalink(a);
184 }
185
186 int
187 pcap_setfilter(pcap_t *a, struct bpf_program *b)
188 {
189         g_assert(has_wpcap);
190         return p_pcap_setfilter(a, b);
191 }
192
193 char*
194 pcap_geterr(pcap_t *a)
195 {
196         g_assert(has_wpcap);
197         return p_pcap_geterr(a);
198 }
199
200 int
201 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
202             bpf_u_int32 e)
203 {
204         g_assert(has_wpcap);
205         return p_pcap_compile(a, b, c, d, e);
206 }
207
208 int
209 #ifdef WPCAP_CONSTIFIED
210 pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
211 #else
212 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
213 #endif
214 {
215         g_assert(has_wpcap);
216         return p_pcap_lookupnet(a, b, c, d);
217 }
218
219 pcap_t*
220 #ifdef WPCAP_CONSTIFIED
221 pcap_open_live(const char *a, int b, int c, int d, char *e)
222 #else
223 pcap_open_live(char *a, int b, int c, int d, char *e)
224 #endif
225 {
226         g_assert(has_wpcap);
227         return p_pcap_open_live(a, b, c, d, e);
228 }
229
230 int
231 pcap_loop(pcap_t *a, int b, pcap_handler c, guchar *d)
232 {
233         g_assert(has_wpcap);
234         return p_pcap_loop(a, b, c, d);
235 }
236
237 #ifdef HAVE_PCAP_FINDALLDEVS
238 int
239 pcap_findalldevs(pcap_if_t **a, char *b)
240 {
241         g_assert(has_wpcap && p_pcap_findalldevs != NULL);
242         return p_pcap_findalldevs(a, b);
243 }
244
245 void
246 pcap_freealldevs(pcap_if_t *a)
247 {
248         g_assert(has_wpcap && p_pcap_freealldevs != NULL);
249         p_pcap_freealldevs(a);
250 }
251 #endif
252
253 /* setbuff is win32 specific! */
254 int pcap_setbuff(pcap_t *a, int b)
255 {
256         g_assert(has_wpcap);
257         return p_pcap_setbuff(a, b);
258 }
259
260 /*
261  * This will use "pcap_findalldevs()" if we have it, otherwise it'll
262  * fall back on "pcap_lookupdev()".
263  */
264 GList *
265 get_interface_list(int *err, char *err_str)
266 {
267         GList  *il = NULL;
268         wchar_t *names;
269         char *win95names;
270         char ascii_name[MAX_WIN_IF_NAME_LEN + 1];
271         char ascii_desc[MAX_WIN_IF_NAME_LEN + 1];
272         int i, j;
273
274 #ifdef HAVE_PCAP_FINDALLDEVS
275         if (p_pcap_findalldevs != NULL)
276                 return get_interface_list_findalldevs(err, err_str);
277 #endif
278
279         /*
280          * In WinPcap, pcap_lookupdev is implemented by calling
281          * PacketGetAdapterNames.  According to the documentation
282          * I could find:
283          *
284          *      http://winpcap.polito.it/docs/man/html/Packet32_8c.html#a43
285          *
286          * this means that:
287          *
288          * On Windows OT (95, 98, Me), pcap_lookupdev returns a sequence
289          * of bytes consisting of:
290          *
291          *      a sequence of null-terminated ASCII strings (i.e., each
292          *      one is terminated by a single 0 byte), giving the names
293          *      of the interfaces;
294          *
295          *      an empty ASCII string (i.e., a single 0 byte);
296          *
297          *      a sequence of null-terminated ASCII strings, giving the
298          *      descriptions of the interfaces;
299          *
300          *      an empty ASCII string.
301          *
302          * On Windows NT (NT 4.0, W2K, WXP, W2K3, etc.), pcap_lookupdev
303          * returns a sequence of bytes consisting of:
304          *
305          *      a sequence of null-terminated double-byte Unicode strings
306          *      (i.e., each one consits of a sequence of double-byte
307          *      characters, terminated by a double-byte 0), giving the
308          *      names of the interfaces;
309          *
310          *      an empty Unicode string (i.e., a double 0 byte);
311          *
312          *      a sequence of null-terminated ASCII strings, giving the
313          *      descriptions of the interfaces;
314          *
315          *      an empty ASCII string.
316          *
317          * The Nth string in the first sequence is the name of the Nth
318          * adapter; the Nth string in the second sequence is the
319          * description of the Nth adapter.
320          */
321
322         names = (wchar_t *)pcap_lookupdev(err_str);
323         i = 0;
324
325         if (names) {
326                 char* desc = 0;
327                 int desc_pos = 0;
328
329                 if (names[0]<256) {
330                         /*
331                          * If names[0] is less than 256 it means the first
332                          * byte is 0.  This implies that we are using Unicode
333                          * characters.
334                          */
335                         while (*(names+desc_pos) || *(names+desc_pos-1))
336                                 desc_pos++;
337                         desc_pos++;     /* Step over the extra '\0' */
338                         desc = (char*)(names + desc_pos); /* cast *after* addition */
339
340                         while (names[i] != 0) {
341                                 /*
342                                  * Copy the Unicode description to an ASCII
343                                  * string.
344                                  */
345                                 j = 0;
346                                 while (*desc != 0) {
347                                         if (j < MAX_WIN_IF_NAME_LEN)
348                                                 ascii_desc[j++] = *desc;
349                                         desc++;
350                                 }
351                                 ascii_desc[j] = '\0';
352                                 desc++;
353
354                                 /*
355                                  * Copy the Unicode name to an ASCII string.
356                                  */
357                                 j = 0;
358                                 while (names[i] != 0) {
359                                         if (j < MAX_WIN_IF_NAME_LEN)
360                                         ascii_name[j++] = (char) names[i++];
361                                 }
362                                 ascii_name[j] = '\0';
363                                 i++;
364                                 il = g_list_append(il,
365                                     if_info_new(ascii_name, ascii_desc));
366                         }
367                 } else {
368                         /*
369                          * Otherwise we are in Windows 95/98 and using ASCII
370                          * (8-bit) characters.
371                          */
372                         win95names=(char *)names;
373                         while (*(win95names+desc_pos) || *(win95names+desc_pos-1))
374                                 desc_pos++;
375                         desc_pos++;     /* Step over the extra '\0' */
376                         desc = win95names + desc_pos;
377
378                         while (win95names[i] != '\0') {
379                                 /*
380                                  * "&win95names[i]" points to the current
381                                  * interface name, and "desc" points to
382                                  * that interface's description.
383                                  */
384                                 il = g_list_append(il,
385                                     if_info_new(&win95names[i], desc));
386
387                                 /*
388                                  * Skip to the next description.
389                                  */
390                                 while (*desc != 0)
391                                         desc++;
392                                 desc++;
393
394                                 /*
395                                  * Skip to the next name.
396                                  */
397                                 while (win95names[i] != 0)
398                                         i++;
399                                 i++;
400                         }
401                 }
402         }
403
404         if (il == NULL) {
405                 /*
406                  * No interfaces found.
407                  */
408                 *err = NO_INTERFACES_FOUND;
409         }
410
411         return il;
412 }
413
414 /*
415  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
416  * "get_interface_list()".
417  */
418 gchar *
419 cant_get_if_list_error_message(const char *err_str)
420 {
421         /*
422          * If the error message includes "Not enough storage is available
423          * to process this command" or "The operation completed successfully",
424          * suggest that they install a WinPcap version later than 3.0.
425          */
426         if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
427             strstr(err_str, "The operation completed successfully") != NULL) {
428                 return g_strdup_printf("Can't get list of interfaces: %s\n"
429 "This might be a problem with WinPcap 3.0; you should try updating to\n"
430 "a later version of WinPcap - see the WinPcap site at winpcap.polito.it",
431                     err_str);
432         }
433         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
434 }
435
436 /*
437  * Append the version of WinPcap with which we were compiled to a GString.
438  */
439 void
440 get_compiled_pcap_version(GString *str)
441 {
442         g_string_append(str, "with WinPcap (version unknown)");
443 }
444
445 /*
446  * Append the version of WinPcap with which we we're running to a GString.
447  */
448 void
449 get_runtime_pcap_version(GString *str)
450 {
451         /*
452          * On Windows, we might have been compiled with WinPcap but
453          * might not have it loaded; indicate whether we have it or
454          * not and, if we have it and we have "pcap_lib_version()",
455          * what version we have.
456          */
457         if (has_wpcap) {
458                 g_string_sprintfa(str, "with ");
459                 if (p_pcap_lib_version != NULL)
460                         g_string_sprintfa(str, p_pcap_lib_version());
461                 else
462                         g_string_append(str, "WinPcap (version unknown)");
463         } else
464                 g_string_append(str, "without WinPcap");
465         g_string_append(str, " ");
466 }
467
468 #else /* HAVE_LIBPCAP */
469
470 void
471 load_wpcap(void)
472 {
473         return;
474 }
475
476 /*
477  * Append an indication that we were not compiled with WinPcap
478  * to a GString.
479  */
480 void
481 get_compiled_pcap_version(GString *str)
482 {
483         g_string_append(str, "without WinPcap");
484 }
485
486 /*
487  * Don't append anything, as we weren't even compiled to use WinPcap.
488  */
489 void
490 get_runtime_pcap_version(GString *str _U_)
491 {
492 }
493
494 #endif /* HAVE_LIBPCAP */