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