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