From Alexis La Goutte:
[obnox/wireshark/wip.git] / capture_wpcap_packet.c
1 /* capture_wpcap_packet.c
2  * WinPcap-specific interfaces for low-level information (packet.dll).
3  * We load WinPcap at run
4  * time, so that we only need one Wireshark binary and one TShark binary
5  * for Windows, regardless of whether WinPcap is installed or not.
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 2001 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #if defined HAVE_LIBPCAP && defined _WIN32
33
34 #include <glib.h>
35 #include <gmodule.h>
36
37 #include <pcap.h>
38
39 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
40 #include "tools/lemon/cppmagic.h"
41
42 #include <epan/value_string.h>
43
44 #include <winsock2.h>    /* Needed here to force a definition of WINVER           */
45                          /* for some (all ?) Microsoft compilers newer than vc6.  */
46                          /* (If windows.h were used instead, there might be       */
47                          /*  issues re winsock.h included before winsock2.h )     */
48 #include <windowsx.h>
49 #include <Ntddndis.h>
50
51 #include "capture_wpcap_packet.h"
52 #include <wsutil/file_util.h>
53
54 /* packet32.h requires sockaddr_storage
55  * whether sockaddr_storage is defined or not depends on the Platform SDK
56  * version installed. The only one not defining it is the SDK that comes
57  * with MSVC 6.0 (WINVER 0x0400).
58  *
59  * copied from RFC2553 (and slightly modified because of datatypes) ...
60  * XXX - defined more than once, move this to a header file */
61 #ifndef WINVER
62 #error WINVER not defined ....
63 #endif
64 #if (WINVER <= 0x0400) && defined(_MSC_VER)
65 typedef unsigned short eth_sa_family_t;
66
67 /*
68  * Desired design of maximum size and alignment
69  */
70 #define ETH_SS_MAXSIZE    128  /* Implementation specific max size */
71 #define ETH_SS_ALIGNSIZE  (sizeof (gint64 /*int64_t*/))
72                          /* Implementation specific desired alignment */
73 /*
74  * Definitions used for sockaddr_storage structure paddings design.
75  */
76 #define ETH_SS_PAD1SIZE   (ETH_SS_ALIGNSIZE - sizeof (eth_sa_family_t))
77 #define ETH_SS_PAD2SIZE   (ETH_SS_MAXSIZE - (sizeof (eth_sa_family_t) + \
78                               ETH_SS_PAD1SIZE + ETH_SS_ALIGNSIZE))
79
80 struct sockaddr_storage {
81     eth_sa_family_t  __ss_family;     /* address family */
82     /* Following fields are implementation specific */
83     char      __ss_pad1[ETH_SS_PAD1SIZE];
84               /* 6 byte pad, this is to make implementation */
85               /* specific pad up to alignment field that */
86               /* follows explicit in the data structure */
87     gint64 /*int64_t*/   __ss_align;     /* field to force desired structure */
88                /* storage alignment */
89     char      __ss_pad2[ETH_SS_PAD2SIZE];
90               /* 112 byte pad to achieve desired size, */
91               /* _SS_MAXSIZE value minus size of ss_family */
92               /* __ss_pad1, __ss_align fields is 112 */
93 };
94 /* ... copied from RFC2553 */
95 #endif /* WINVER */
96
97 #include <Packet32.h>
98
99 gboolean has_wpacket = FALSE;
100
101
102 /* This module will use the PacketRequest function in packet.dll (coming with WinPcap) to "directly" access
103  * the Win32 NDIS network driver(s) and ask for various values (status, statistics, ...).
104  *
105  * Unfortunately, the definitions required for this are not available through the usual windows header files,
106  * but require the Windows "Device Driver Kit" which is not available for free :-(
107  *
108  * Fortunately, the definitions needed to access the various NDIS values are available from various OSS projects:
109  * - WinPcap in Ntddndis.h
110  * - Ndiswrapper in driver/ndis.h and driver/iw_ndis.h
111  * - cygwin (MingW?) in usr/include/w32api/ddk/ndis.h and ntddndis.h
112  * - FreeBSD (netperf)
113  */
114
115 /* The MSDN description of the NDIS driver API is available at:
116 /* MSDN Home >  MSDN Library >  Win32 and COM Development >  Driver Development Kit >  Network Devices and Protocols >  Reference */
117 /* "NDIS Objects" */
118 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/network/hh/network/21oidovw_d55042e5-0b8a-4439-8ef2-be7331e98464.xml.asp */
119
120 /* Some more interesting links:
121  * http://sourceforge.net/projects/ndiswrapper/
122  * http://www.osronline.com/lists_archive/windbg/thread521.html
123  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ndis.h?view=markup
124  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ntddndis.h?view=markup
125  */
126
127
128
129 /******************************************************************************************************************************/
130 /* stuff to load WinPcap's packet.dll and the functions required from it */
131
132 static PCHAR     (*p_PacketGetVersion) (void);
133 static LPADAPTER (*p_PacketOpenAdapter) (char *adaptername);
134 static void      (*p_PacketCloseAdapter) (LPADAPTER);
135 static int       (*p_PacketRequest) (LPADAPTER, int, void *);
136
137 typedef struct {
138     const char  *name;
139     gpointer    *ptr;
140     gboolean    optional;
141 } symbol_table_t;
142
143 #define SYM(x, y)   { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
144
145 void
146 wpcap_packet_load(void)
147 {
148
149     /* These are the symbols I need or want from packet.dll */
150     static const symbol_table_t symbols[] = {
151         SYM(PacketGetVersion, FALSE),
152         SYM(PacketOpenAdapter, FALSE),
153         SYM(PacketCloseAdapter, FALSE),
154         SYM(PacketRequest, FALSE),
155         { NULL, NULL, FALSE }
156     };
157
158     GModule     *wh; /* wpcap handle */
159     const symbol_table_t    *sym;
160
161     wh = ws_module_open("packet.dll", 0);
162
163     if (!wh) {
164         return;
165     }
166
167     sym = symbols;
168     while (sym->name) {
169         if (!g_module_symbol(wh, sym->name, sym->ptr)) {
170             if (sym->optional) {
171                 /*
172                  * We don't care if it's missing; we just
173                  * don't use it.
174                  */
175                 *sym->ptr = NULL;
176             } else {
177                 /*
178                  * We require this symbol.
179                  */
180                 return;
181             }
182         }
183         sym++;
184     }
185
186     has_wpacket = TRUE;
187 }
188
189
190
191 /******************************************************************************************************************************/
192 /* functions to access the NDIS driver values */
193
194
195 /* get dll version */
196 char *
197 wpcap_packet_get_version(void)
198 {
199     if(!has_wpacket) {
200         return NULL;
201     }
202     return p_PacketGetVersion();
203 }
204
205
206 /* open the interface */
207 void *
208 wpcap_packet_open(char *if_name)
209 {
210     LPADAPTER   adapter;
211
212     g_assert(has_wpacket);
213     adapter = p_PacketOpenAdapter(if_name);
214
215     return adapter;
216 }
217
218
219 /* close the interface */
220 void
221 wpcap_packet_close(void *adapter)
222 {
223
224     g_assert(has_wpacket);
225     p_PacketCloseAdapter(adapter);
226 }
227
228
229 /* do a packet request call */
230 int
231 wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned int *length)
232 {
233     BOOLEAN    Status;
234     ULONG      IoCtlBufferLength=(sizeof(PACKET_OID_DATA) + (*length) - 1);
235     PPACKET_OID_DATA  OidData;
236
237
238     g_assert(has_wpacket);
239
240     if(p_PacketRequest == NULL) {
241         g_warning("packet_request not available\n");
242         return 0;
243     }
244
245     /* get a buffer suitable for PacketRequest() */
246     OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,IoCtlBufferLength);
247     if (OidData == NULL) {
248         g_warning("GlobalAllocPtr failed for %u\n", IoCtlBufferLength);
249         return 0;
250     }
251
252     OidData->Oid = Oid;
253     OidData->Length = *length;
254     memcpy(OidData->Data, value, *length);
255
256     Status = p_PacketRequest(adapter, set, OidData);
257
258     if(Status) {
259         if(OidData->Length <= *length) {
260             /* copy value from driver */
261             memcpy(value, OidData->Data, OidData->Length);
262             *length = OidData->Length;
263         } else {
264             /* the driver returned a value that is longer than expected (and longer than the given buffer) */
265             g_warning("returned oid too long, Oid: 0x%x OidLen:%u MaxLen:%u", Oid, OidData->Length, *length);
266             Status = FALSE;
267         }
268     }
269
270     GlobalFreePtr (OidData);
271
272     if(Status) {
273         return 1;
274     } else {
275         return 0;
276     }
277 }
278
279
280 /* get an UINT value using the packet request call */
281 int
282 wpcap_packet_request_uint(void *adapter, ULONG Oid, UINT *value)
283 {
284     BOOLEAN     Status;
285     int         length = sizeof(UINT);
286
287
288     Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
289     if(Status && length == sizeof(UINT)) {
290         return 1;
291     } else {
292         return 0;
293     }
294 }
295
296
297 /* get an ULONG value using the NDIS packet request call */
298 int
299 wpcap_packet_request_ulong(void *adapter, ULONG Oid, ULONG *value)
300 {
301     BOOLEAN     Status;
302     int         length = sizeof(ULONG);
303
304
305     Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
306     if(Status && length == sizeof(ULONG)) {
307         return 1;
308     } else {
309         return 0;
310     }
311 }
312
313
314 #else /* HAVE_LIBPCAP && _WIN32 */
315
316 void
317 wpcap_packet_load(void)
318 {
319     return;
320 }
321
322 #endif /* HAVE_LIBPCAP */
323
324 /*
325  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
326  *
327  * Local variables:
328  * c-basic-offset: 4
329  * tab-width: 8
330  * indent-tabs-mode: nil
331  * End:
332  *
333  * ex: set shiftwidth=4 tabstop=8 expandtab
334  * :indentSize=4:tabSize=8:noTabs=true:
335  */