The hash table merely associates data structures with conversations,
[obnox/wireshark/wip.git] / capture-wpcap.c
1 /* capture-wpcap.c
2  * Try to load WinPcap DLL at run-time.
3  *
4  * $Id: capture-wpcap.c,v 1.1 2001/04/03 05:26:26 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 2001 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <gmodule.h>
32
33 #ifdef HAVE_LIBPCAP
34 #include <pcap.h>
35 #endif
36
37 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
38 #include "tools/lemon/cppmagic.h"
39
40 gboolean has_wpcap = FALSE;
41
42 #ifdef HAVE_LIBPCAP
43
44
45 static char*   (*p_pcap_lookupdev) (char *);
46 static void    (*p_pcap_close) (pcap_t *);
47 static int     (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
48 static int     (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, u_char *);
49 static int     (*p_pcap_snapshot) (pcap_t *);
50 static int     (*p_pcap_datalink) (pcap_t *);
51 static int     (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
52 static char*   (*p_pcap_geterr) (pcap_t *);
53 static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, char *, int,
54                         bpf_u_int32);
55 static int     (*p_pcap_lookupnet) (char *, bpf_u_int32 *, bpf_u_int32 *,
56                         char *);
57 static pcap_t* (*p_pcap_open_live) (char *, int, int, int, char *);
58 static int     (*p_pcap_loop) (pcap_t *, int, pcap_handler, u_char *);
59
60 typedef struct {
61         const char      *name;
62         gpointer        *ptr;
63 } symbol_table_t;
64
65 #define SYM(x)  STRINGIFY(x) , (gpointer) &CONCAT(p_,x)
66
67 void
68 load_wpcap(void)
69 {
70
71         /* These are the symbols I need from Wpcap */
72         symbol_table_t  symbols[] = {
73                 SYM(pcap_lookupdev),
74                 SYM(pcap_close),
75                 SYM(pcap_stats),
76                 SYM(pcap_dispatch),
77                 SYM(pcap_snapshot),
78                 SYM(pcap_datalink),
79                 SYM(pcap_setfilter),
80                 SYM(pcap_geterr),
81                 SYM(pcap_compile),
82                 SYM(pcap_lookupnet),
83                 SYM(pcap_open_live),
84                 SYM(pcap_loop),
85                 NULL, NULL
86         };
87
88         GModule         *wh; /* wpcap handle */
89         symbol_table_t  *sym;
90
91         wh = g_module_open("wpcap", 0);
92
93         if (!wh) {
94                 return;
95         }
96
97         sym = symbols;
98         while (sym && sym->name) {
99                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
100                         return;
101                 }
102                 sym++;
103         }
104
105
106         has_wpcap = TRUE;
107 }
108
109 char*
110 pcap_lookupdev (char *a)
111 {
112         g_assert(has_wpcap);
113         return p_pcap_lookupdev(a);
114 }
115
116 void
117 pcap_close(pcap_t *a)
118 {
119         g_assert(has_wpcap);
120         p_pcap_close(a);
121 }
122
123 int
124 pcap_stats(pcap_t *a, struct pcap_stat *b)
125 {
126         g_assert(has_wpcap);
127         return p_pcap_stats(a, b);
128 }
129
130 int
131 pcap_dispatch(pcap_t *a, int b, pcap_handler c, u_char *d)
132 {
133         g_assert(has_wpcap);
134         return p_pcap_dispatch(a, b, c, d);
135 }
136
137
138 int
139 pcap_snapshot(pcap_t *a)
140 {
141         g_assert(has_wpcap);
142         return p_pcap_snapshot(a);
143 }
144
145
146 int
147 pcap_datalink(pcap_t *a)
148 {
149         g_assert(has_wpcap);
150         return p_pcap_datalink(a);
151 }
152
153 int
154 pcap_setfilter(pcap_t *a, struct bpf_program *b)
155 {
156         g_assert(has_wpcap);
157         return p_pcap_setfilter(a, b);
158 }
159
160 char*
161 pcap_geterr(pcap_t *a)
162 {
163         g_assert(has_wpcap);
164         return p_pcap_geterr(a);
165 }
166
167 int
168 pcap_compile(pcap_t *a, struct bpf_program *b, char *c, int d,
169             bpf_u_int32 e)
170 {
171         g_assert(has_wpcap);
172         return p_pcap_compile(a, b, c, d, e);
173 }
174
175 int
176 pcap_lookupnet(char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *d)
177 {
178         g_assert(has_wpcap);
179         return p_pcap_lookupnet(a, b, c, d);
180 }
181
182 pcap_t*
183 pcap_open_live(char *a, int b, int c, int d, char *e)
184 {
185         g_assert(has_wpcap);
186         return p_pcap_open_live(a, b, c, d, e);
187 }
188
189 int
190 pcap_loop(pcap_t *a, int b, pcap_handler c, u_char *d)
191 {
192         g_assert(has_wpcap);
193         return p_pcap_loop(a, b, c, d);
194 }
195
196 #else /* HAVE_LIBPCAP */
197
198 void
199 load_wpcap(void)
200 {
201         return;
202 }
203
204
205 #endif /* HAVE_LIBPCAP */