-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcapture_wpcap_packet.c
301 lines (252 loc) · 8.2 KB
/
capture_wpcap_packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* capture_wpcap_packet.c
* WinPcap-specific interfaces for low-level information (packet.dll).
* We load WinPcap at run time, so that we only need one binary
* for Windows, regardless of whether WinPcap is installed or not.
*
* By Gerald Combs <[email protected]>
* Copyright 2001 Gerald Combs
*
* Modified by Tim Dorssers
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if defined _WIN32
#include <pcap.h>
#include <winsock2.h> /* Needed here to force a definition of WINVER */
/* for some (all ?) Microsoft compilers newer than vc6. */
/* (If windows.h were used instead, there might be */
/* issues re winsock.h included before winsock2.h ) */
#include <windowsx.h>
#include <Ntddndis.h>
#include "capture_wpcap_packet.h"
/* packet32.h requires sockaddr_storage
* whether sockaddr_storage is defined or not depends on the Platform SDK
* version installed. The only one not defining it is the SDK that comes
* with MSVC 6.0 (WINVER 0x0400).
*
* copied from RFC2553
* XXX - defined more than once, move this to a header file */
#ifndef WINVER
#error WINVER not defined ....
#endif
#if (WINVER <= 0x0400) && defined(_MSC_VER)
typedef unsigned short eth_sa_family_t;
/*
* Desired design of maximum size and alignment
*/
#define ETH_SS_MAXSIZE 128 /* Implementation specific max size */
#define ETH_SS_ALIGNSIZE (sizeof (int64_t))
/* Implementation specific desired alignment */
/*
* Definitions used for sockaddr_storage structure paddings design.
*/
#define ETH_SS_PAD1SIZE (ETH_SS_ALIGNSIZE - sizeof (eth_sa_family_t))
#define ETH_SS_PAD2SIZE (ETH_SS_MAXSIZE - (sizeof (eth_sa_family_t) + \
ETH_SS_PAD1SIZE + ETH_SS_ALIGNSIZE))
struct sockaddr_storage {
eth_sa_family_t __ss_family; /* address family */
/* Following fields are implementation specific */
char __ss_pad1[ETH_SS_PAD1SIZE];
/* 6 byte pad, this is to make implementation */
/* specific pad up to alignment field that */
/* follows explicit in the data structure */
int64_t __ss_align; /* field to force desired structure */
/* storage alignment */
char __ss_pad2[ETH_SS_PAD2SIZE];
/* 112 byte pad to achieve desired size, */
/* _SS_MAXSIZE value minus size of ss_family */
/* __ss_pad1, __ss_align fields is 112 */
};
/* ... copied from RFC2553 */
#endif /* WINVER */
#include <Packet32.h>
boolean has_wpacket = FALSE;
/******************************************************************************************************************************/
/* stuff to load WinPcap's packet.dll and the functions required from it */
typedef PCHAR(*PPACKETGETVERSION)(void);
typedef LPADAPTER(*PPACKETOPENADAPTER) (LPTSTR);
typedef void(*PPACKETCLOSEADAPTER) (LPADAPTER);
typedef BOOLEAN(*PPACKETREQUEST) (LPADAPTER, int, void *);
typedef BOOLEAN(*PPACKETGETNETINFOEX) (LPTSTR, npf_if_addr *, PLONG);
static PPACKETGETVERSION p_PacketGetVersion;
static PPACKETOPENADAPTER p_PacketOpenAdapter;
static PPACKETCLOSEADAPTER p_PacketCloseAdapter;
static PPACKETREQUEST p_PacketRequest;
static PPACKETGETNETINFOEX p_PacketGetNetInfoEx;
void
wpcap_packet_load(void)
{
HMODULE wh;
//char *ptr;
/* Load packet.dll */
wh = LoadLibrary(TEXT("packet.dll"));
if (wh == NULL) {
/* Load failed */
return;
}
/* These are the symbols I need or want from packet.dll */
p_PacketGetVersion = (PPACKETGETVERSION)GetProcAddress(wh, "PacketGetVersion");
if (p_PacketGetVersion == NULL) {
/*
* We require this symbol.
*/
return;
}
p_PacketOpenAdapter = (PPACKETOPENADAPTER)GetProcAddress(wh, "PacketOpenAdapter");
if (p_PacketOpenAdapter == NULL) {
/*
* We require this symbol.
*/
return;
}
p_PacketCloseAdapter = (PPACKETCLOSEADAPTER)GetProcAddress(wh, "PacketCloseAdapter");
if (p_PacketCloseAdapter == NULL) {
/*
* We require this symbol.
*/
return;
}
p_PacketRequest = (PPACKETREQUEST)GetProcAddress(wh, "PacketRequest");
if (p_PacketRequest == NULL) {
/*
* We require this symbol.
*/
return;
}
p_PacketGetNetInfoEx = (PPACKETGETNETINFOEX)GetProcAddress(wh, "PacketGetNetInfoEx");
if (p_PacketGetNetInfoEx == NULL) {
/*
* We require this symbol.
*/
return;
}
has_wpacket = TRUE;
}
/******************************************************************************************************************************/
/* functions to access the NDIS driver values */
int
wpcap_packet_get_net_info(char *if_name, PULONG netp, PULONG maskp)
{
npf_if_addr buffer;
LONG NEntries = 1;
if (!has_wpacket) {
return 0;
}
if (p_PacketGetNetInfoEx(if_name, &buffer, &NEntries)) {
memcpy(netp, &((struct sockaddr_in *)&buffer.IPAddress)->sin_addr.s_addr, sizeof(netp));
memcpy(maskp, &((struct sockaddr_in *)&buffer.IPAddress)->sin_addr.s_addr, sizeof(maskp));
return 1;
}
else
return 0;
}
/* get dll version */
char *
wpcap_packet_get_version(void)
{
if(!has_wpacket) {
return NULL;
}
return p_PacketGetVersion();
}
/* open the interface */
void *
wpcap_packet_open(char *if_name)
{
LPADAPTER adapter;
if (!has_wpacket) {
return NULL;
}
adapter = p_PacketOpenAdapter(if_name);
return adapter;
}
/* close the interface */
void
wpcap_packet_close(void *adapter)
{
if (!has_wpacket) {
return;
}
p_PacketCloseAdapter(adapter);
}
/* do a packet request call */
int
wpcap_packet_request(void *adapter, ULONG Oid, int set, char *value, unsigned int *length)
{
BOOLEAN Status;
ULONG IoCtlBufferLength=(sizeof(PACKET_OID_DATA) + (*length) - 1);
PPACKET_OID_DATA OidData;
if (!has_wpacket) {
return 0;
}
if(p_PacketRequest == NULL) {
fprintf(stderr, "packet_request not available\n");
return 0;
}
/* get a buffer suitable for PacketRequest() */
OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,IoCtlBufferLength);
if (OidData == NULL) {
fprintf(stderr, "GlobalAllocPtr failed for %u\n", IoCtlBufferLength);
return 0;
}
OidData->Oid = Oid;
OidData->Length = *length;
memcpy(OidData->Data, value, *length);
Status = p_PacketRequest(adapter, set, OidData);
if(Status) {
if(OidData->Length <= *length) {
/* copy value from driver */
memcpy(value, OidData->Data, OidData->Length);
*length = OidData->Length;
} else {
/* the driver returned a value that is longer than expected (and longer than the given buffer) */
fprintf(stderr, "returned oid too long, Oid: 0x%x OidLen:%u MaxLen:%u", Oid, OidData->Length, *length);
Status = FALSE;
}
}
GlobalFreePtr (OidData);
if(Status) {
return 1;
} else {
return 0;
}
}
/* get an UINT value using the packet request call */
int
wpcap_packet_request_uint(void *adapter, ULONG Oid, UINT *value)
{
BOOLEAN Status;
unsigned int length = sizeof(UINT);
Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
if(Status && length == sizeof(UINT)) {
return 1;
} else {
return 0;
}
}
/* get an ULONG value using the NDIS packet request call */
int
wpcap_packet_request_ulong(void *adapter, ULONG Oid, ULONG *value)
{
BOOLEAN Status;
unsigned int length = sizeof(ULONG);
Status = wpcap_packet_request(adapter, Oid, FALSE /* !set */, (char *) value, &length);
if(Status && length == sizeof(ULONG)) {
return 1;
} else {
return 0;
}
}
#endif _WIN32 */