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