win32 only: get interface details from WinPcap's packet.dll (direct access to NDIS...
[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 Ethereal binary and one Tethereal binary
5  * for Windows, regardless of whether WinPcap is installed or not.
6  *
7  * $Id$
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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 /* XXX - yes, I know, I should move cppmagic.h to a generic location. */
38 #include "tools/lemon/cppmagic.h"
39
40 #include <epan/value_string.h>
41
42 #include <Packet32.h>
43 #include <windows.h>
44 #include <windowsx.h>
45 #include <Ntddndis.h>
46
47 #include "capture_wpcap_packet.h"
48
49 gboolean has_wpacket = FALSE;
50
51
52 /* This module will use the PacketRequest function in packet.dll (coming with WinPcap) to "directly" access 
53  * the Win32 NDIS network driver(s) and ask for various values (status, statistics, ...).
54  *
55  * Unfortunately, the definitions required for this are not available through the usual windows header files, 
56  * but require the Windows "Device Driver Kit" which is not available for free :-( 
57  *
58  * Fortunately, the definitions needed to access the various NDIS values are available from various OSS projects:
59  * - WinPcap in Ntddndis.h
60  * - Ndiswrapper in driver/ndis.h and driver/iw_ndis.h
61  * - cygwin (MingW?) in usr/include/w32api/ddk/ndis.h and ntddndis.h
62  * - FreeBSD (netperf)
63  */
64
65 /* The MSDN description of the NDIS driver API is available at:
66 /* MSDN Home >  MSDN Library >  Win32 and COM Development >  Driver Development Kit >  Network Devices and Protocols >  Reference */
67 /* "NDIS Objects" */
68 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/network/hh/network/21oidovw_d55042e5-0b8a-4439-8ef2-be7331e98464.xml.asp */
69
70 /* Some more interesting links:
71  * http://sourceforge.net/projects/ndiswrapper/
72  * http://www.osronline.com/lists_archive/windbg/thread521.html
73  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ndis.h?view=markup
74  * http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ntddndis.h?view=markup
75  */
76
77
78
79 /******************************************************************************************************************************/
80 /* stuff to load WinPcap's packet.dll and the functions required from it */
81
82 static LPADAPTER (*p_PacketOpenAdapter) (char *adaptername);
83 static void      (*p_PacketCloseAdapter) (LPADAPTER);
84 static int       (*p_PacketRequest) (LPADAPTER, int, void *);
85
86 typedef struct {
87         const char      *name;
88         gpointer        *ptr;
89         gboolean        optional;
90 } symbol_table_t;
91
92 #define SYM(x, y)       { STRINGIFY(x) , (gpointer) &CONCAT(p_,x), y }
93
94 void
95 wpcap_packet_load(void)
96 {
97
98         /* These are the symbols I need or want from packet.dll */
99         static const symbol_table_t     symbols[] = {
100                 SYM(PacketOpenAdapter, FALSE),
101                 SYM(PacketCloseAdapter, FALSE),
102                 SYM(PacketRequest, TRUE),
103                 { NULL, NULL, FALSE }
104         };
105
106         GModule         *wh; /* wpcap handle */
107         const symbol_table_t    *sym;
108
109         wh = g_module_open("packet", 0);
110
111         if (!wh) {
112                 return;
113         }
114
115         sym = symbols;
116         while (sym->name) {
117                 if (!g_module_symbol(wh, sym->name, sym->ptr)) {
118                         if (sym->optional) {
119                                 /*
120                                  * We don't care if it's missing; we just
121                                  * don't use it.
122                                  */
123                                 *sym->ptr = NULL;
124                         } else {
125                                 /*
126                                  * We require this symbol.
127                                  */
128                                 return;
129                         }
130                 }
131                 sym++;
132         }
133
134
135         has_wpacket = TRUE;
136 }
137
138
139
140 /******************************************************************************************************************************/
141 /* functions to access the NDIS driver values */
142
143
144 /* open the interface */
145 LPADAPTER
146 wpcap_packet_open(char *if_name)
147 {
148     LPADAPTER adapter;
149
150     adapter = p_PacketOpenAdapter(if_name);
151
152     return adapter;
153 }
154
155
156 /* close the interface */
157 void
158 wpcap_packet_close(LPADAPTER adapter)
159 {
160
161     p_PacketCloseAdapter(adapter);
162 }
163
164
165 /* do a packet request call */
166 int 
167 wpcap_packet_request(LPADAPTER a, ULONG Oid, int set, char *value, unsigned int *length)
168 {
169     BOOLEAN    Status;
170     ULONG      IoCtlBufferLength=(sizeof(PACKET_OID_DATA) + (*length) - 1);
171     PPACKET_OID_DATA  OidData;
172
173
174         g_assert(has_wpacket);
175
176     if(p_PacketRequest == NULL) {
177         g_warning("packet_request not available\n");
178         return 0;
179     }
180
181     OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,IoCtlBufferLength);
182     if (OidData == NULL) {
183         g_warning("packet_link_status failed\n");
184         return 0;
185     }
186
187     OidData->Oid = Oid;
188     OidData->Length = *length;
189
190     Status = p_PacketRequest(a, set, OidData);
191
192     if(Status) {
193         g_assert(OidData->Length <= *length);
194         memcpy(value, OidData->Data, OidData->Length);
195         *length = OidData->Length;
196     }
197
198     GlobalFreePtr (OidData);
199
200     if(Status) {
201         return 1;
202     } else {
203         return 0;
204     }
205 }
206
207
208 /* get an UINT value using the packet request call */
209 int
210 wpcap_packet_request_uint(LPADAPTER a, ULONG Oid, UINT *value)
211 {
212     BOOLEAN     Status;
213     int         length = sizeof(UINT);
214
215
216     Status = wpcap_packet_request(a, Oid, FALSE /* !set */, (char *) value, &length);
217     if(Status) {
218         g_assert(length == sizeof(UINT));
219         return 1;
220     } else {
221         return 0;
222     }
223 }
224
225
226 /* get an ULONG value using the NDIS packet request call */
227 int
228 wpcap_packet_request_ulong(LPADAPTER a, ULONG Oid, ULONG *value)
229 {
230     BOOLEAN     Status;
231     int         length = sizeof(ULONG);
232
233
234     Status = wpcap_packet_request(a, Oid, FALSE /* !set */, (char *) value, &length);
235     if(Status) {
236         g_assert(length == sizeof(ULONG));
237         return 1;
238     } else {
239         return 0;
240     }
241 }
242
243
244 #else /* HAVE_LIBPCAP && _WIN32 */
245
246 void
247 wpcap_packet_load(void)
248 {
249         return;
250 }
251
252 #endif /* HAVE_LIBPCAP */