fix sockaddr_storage problem for MSVC 7.1 (hmmm, maybe it's a platform SDK issue...
[metze/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 <wtap.h>
38 #include <pcap.h>
39
40 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
41 #include "tools/lemon/cppmagic.h"
42
43 #include <epan/value_string.h>
44
45
46
47 #ifndef HAVE_SOCKADDR_STORAGE
48 /* packet32.h requires sockaddr_storage (usually defined in Platform SDK)
49  * copied from RFC2553 (and slightly modified because of datatypes) ...
50  * XXX - defined more than once, move this to a header file */
51 typedef unsigned short eth_sa_family_t;
52
53 /*
54  * Desired design of maximum size and alignment
55  */
56 #define ETH_SS_MAXSIZE    128  /* Implementation specific max size */
57 #define ETH_SS_ALIGNSIZE  (sizeof (gint64 /*int64_t*/))
58                          /* Implementation specific desired alignment */
59 /*
60  * Definitions used for sockaddr_storage structure paddings design.
61  */
62 #define ETH_SS_PAD1SIZE   (ETH_SS_ALIGNSIZE - sizeof (eth_sa_family_t))
63 #define ETH_SS_PAD2SIZE   (ETH_SS_MAXSIZE - (sizeof (eth_sa_family_t) + \
64                               ETH_SS_PAD1SIZE + ETH_SS_ALIGNSIZE))
65
66 /* sockaddr_storage problem with different MSVC versions
67  * - MSVC 6   (1200) doesn't define this
68  * - MSVC 7   (1300) unknown
69  * - MSVC 7.1 (1310) does
70  * - MSVC 8   (1400) does */
71 /* we might need to tweak this #if, see version_info for _MSC_VER values */
72 /* XXX - is this more of a Platform SDK issue? */
73 #if _MSC_VER < 1310
74 struct sockaddr_storage {
75     eth_sa_family_t  __ss_family;     /* address family */
76     /* Following fields are implementation specific */
77     char      __ss_pad1[ETH_SS_PAD1SIZE];
78               /* 6 byte pad, this is to make implementation */
79               /* specific pad up to alignment field that */
80               /* follows explicit in the data structure */
81     gint64 /*int64_t*/   __ss_align;     /* field to force desired structure */
82                /* storage alignment */
83     char      __ss_pad2[ETH_SS_PAD2SIZE];
84               /* 112 byte pad to achieve desired size, */
85               /* _SS_MAXSIZE value minus size of ss_family */
86               /* __ss_pad1, __ss_align fields is 112 */
87 };
88 /* ... copied from RFC2553 */
89 #endif /* _MSC_VER */
90 #endif
91
92
93 #include <Packet32.h>
94 #include <windows.h>
95 #include <windowsx.h>
96 #include <Ntddndis.h>
97
98 #include "capture_wpcap_packet.h"
99
100 gboolean has_wpacket = FALSE;
101
102
103 /* This module will use the PacketRequest function in packet.dll (coming with WinPcap) to "directly" access 
104  * the Win32 NDIS network driver(s) and ask for various values (status, statistics, ...).
105  *
106  * Unfortunately, the definitions required for this are not available through the usual windows header files, 
107  * but require the Windows "Device Driver Kit" which is not available for free :-( 
108  *
109  * Fortunately, the definitions needed to access the various NDIS values are available from various OSS projects:
110  * - WinPcap in Ntddndis.h
111  * - Ndiswrapper in driver/ndis.h and driver/iw_ndis.h
112  * - cygwin (MingW?) in usr/include/w32api/ddk/ndis.h and ntddndis.h
113  * - FreeBSD (netperf)
114  */
115
116 /* The MSDN description of the NDIS driver API is available at:
117 /* MSDN Home >  MSDN Library >  Win32 and COM Development >  Driver Development Kit >  Network Devices and Protocols >  Reference */
118 /* "NDIS Objects" */
119 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/network/hh/network/21oidovw_d55042e5-0b8a-4439-8ef2-be7331e98464.xml.asp */
120
121 /* Some more interesting links:
122  * http://sourceforge.net/projects/ndiswrapper/
123  * http://www.osronline.com/lists_archive/windbg/thread521.html
124  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ndis.h?view=markup
125  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ntddndis.h?view=markup
126  */
127
128
129
130 /******************************************************************************************************************************/
131 /* stuff to load WinPcap's packet.dll and the functions required from it */
132
133 static PCHAR     (*p_PacketGetVersion) (void);
134 static LPADAPTER (*p_PacketOpenAdapter) (char *adaptername);
135 static void      (*p_PacketCloseAdapter) (LPADAPTER);
136 static int       (*p_PacketRequest) (LPADAPTER, int, void *);
137
138 typedef struct {
139         const char      *name;
140         gpointer        *ptr;
141         gboolean        optional;
142 } symbol_table_t;
143
144 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
145
146 void
147 wpcap_packet_load(void)
148 {
149
150         /* These are the symbols I need or want from packet.dll */
151         static const symbol_table_t     symbols[] = {
152                 SYM(PacketGetVersion, FALSE),
153                 SYM(PacketOpenAdapter, FALSE),
154                 SYM(PacketCloseAdapter, FALSE),
155                 SYM(PacketRequest, FALSE),
156                 { NULL, NULL, FALSE }
157         };
158
159         GModule         *wh; /* wpcap handle */
160         const symbol_table_t    *sym;
161
162         wh = g_module_open("packet", 0);
163
164         if (!wh) {
165                 return;
166         }
167
168         sym = symbols;
169         while (sym->name) {
170                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
171                         if (sym->optional) {
172                                 /*
173                                  * We don't care if it's missing; we just
174                                  * don't use it.
175                                  */
176                                 *sym->ptr = NULL;
177                         } else {
178                                 /*
179                                  * We require this symbol.
180                                  */
181                                 return;
182                         }
183                 }
184                 sym++;
185         }
186
187         has_wpacket = TRUE;
188 }
189
190
191
192 /******************************************************************************************************************************/
193 /* functions to access the NDIS driver values */
194
195
196 /* get dll version */
197 char *
198 wpcap_packet_get_version(void)
199 {
200     if(!has_wpacket) {
201         return NULL;
202     }
203     return p_PacketGetVersion();
204 }
205
206
207 /* open the interface */
208 void *
209 wpcap_packet_open(char *if_name)
210 {
211     LPADAPTER   adapter;
212
213         g_assert(has_wpacket);
214     adapter = p_PacketOpenAdapter(if_name);
215
216     return adapter;
217 }
218
219
220 /* close the interface */
221 void
222 wpcap_packet_close(void *adapter)
223 {
224
225         g_assert(has_wpacket);
226     p_PacketCloseAdapter(adapter);
227 }
228
229
230 /* do a packet request call */
231 int 
232 wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned int *length)
233 {
234     BOOLEAN    Status;
235     ULONG      IoCtlBufferLength=(sizeof(PACKET_OID_DATA) + (*length) - 1);
236     PPACKET_OID_DATA  OidData;
237
238
239         g_assert(has_wpacket);
240
241     if(p_PacketRequest == NULL) {
242         g_warning("packet_request not available\n");
243         return 0;
244     }
245
246     OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,IoCtlBufferLength);
247     if (OidData == NULL) {
248         g_warning("packet_link_status failed\n");
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         g_assert(OidData->Length <= *length);
260         memcpy(value, OidData->Data, OidData->Length);
261         *length = OidData->Length;
262     }
263
264     GlobalFreePtr (OidData);
265
266     if(Status) {
267         return 1;
268     } else {
269         return 0;
270     }
271 }
272
273
274 /* get an UINT value using the packet request call */
275 int
276 wpcap_packet_request_uint(void *adapter, ULONG Oid, UINT *value)
277 {
278     BOOLEAN     Status;
279     int         length = sizeof(UINT);
280
281
282     Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
283     if(Status && length == sizeof(UINT)) {
284         return 1;
285     } else {
286         return 0;
287     }
288 }
289
290
291 /* get an ULONG value using the NDIS packet request call */
292 int
293 wpcap_packet_request_ulong(void *adapter, ULONG Oid, ULONG *value)
294 {
295     BOOLEAN     Status;
296     int         length = sizeof(ULONG);
297
298
299     Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
300     if(Status && length == sizeof(ULONG)) {
301         return 1;
302     } else {
303         return 0;
304     }
305 }
306
307
308 #else /* HAVE_LIBPCAP && _WIN32 */
309
310 void
311 wpcap_packet_load(void)
312 {
313         return;
314 }
315
316 #endif /* HAVE_LIBPCAP */