/
driver.c
269 lines (234 loc) · 8.51 KB
/
driver.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
/* -*- mode: C; c-file-style: "k&r"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
#include <stdio.h>
#include <pcap.h>
#include <unistd.h>
#include "airpcap.h"
void libpcap_test(PAirpcapHandle, PCHAR);
void test(PCHAR name)
{
PAirpcapHandle handle;
AirpcapMacAddress mac;
PAirpcapDeviceCapabilities cap;
CHAR ebuf[AIRPCAP_ERRBUF_SIZE];
printf("Attempting to open device %s.\n", name);
handle = AirpcapOpen(name, ebuf);
if (NULL == handle) {
fprintf(stderr, "AirpcapOpen error: %s\n", ebuf);
return;
}
AirpcapGetMacAddress(handle, &mac);
printf("Adapter MAC address: ");
for (int i = 0; i < 6; i++) {
printf("%02x", mac.Address[i]);
if (i < 5)
printf(":");
}
printf("\n");
if (FALSE == AirpcapGetDeviceCapabilities(handle, &cap)) {
fprintf(stderr, "AirpcapGetDeviceCapabilities failed!\n");
} else {
printf("Device capabilities:\n");
printf("AdapterModelName: %s\n", cap->AdapterModelName);
printf("Supported bands: ");
if (cap->SupportedBands & AIRPCAP_BAND_2GHZ)
printf("2.4GHz ");
if (cap->SupportedBands & AIRPCAP_BAND_5GHZ)
printf("5GHz");
printf("\n");
printf("Supported media: ");
if (cap->SupportedMedia & AIRPCAP_MEDIUM_802_11_A)
printf("802.11a ");
if (cap->SupportedMedia & AIRPCAP_MEDIUM_802_11_B)
printf("802.11b ");
if (cap->SupportedMedia & AIRPCAP_MEDIUM_802_11_G)
printf("802.11g ");
if (cap->SupportedMedia & AIRPCAP_MEDIUM_802_11_N)
printf("802.11n ");
printf("\n");
printf("Can inject packets: ");
if (TRUE == cap->CanTransmit)
printf("Yes");
else
printf("No");
printf("\n");
printf("Can set transmit power: ");
if (TRUE == cap->CanSetTransmitPower)
printf("Yes");
else
printf("No");
printf("\n");
}
PAirpcapChannelInfo channel_info;
UINT channel_info_count;
BOOL ret;
ret = AirpcapGetDeviceSupportedChannels(handle,
&channel_info,
&channel_info_count);
if (TRUE == ret) {
printf("Channels supported by this device:\n");
for (UINT c = 0; c < channel_info_count; c++) {
UINT frequency, channel;
BOOL ret;
frequency = channel_info[c].Frequency;
/* Ignore the band */
ret = AirpcapConvertFrequencyToChannel(frequency,
&channel,
NULL);
if (FALSE == ret) {
fprintf(stderr, "BUG: invalid frequency %d!\n", frequency);
continue;
}
printf(" Channel %d (%d MHz)\n", channel, frequency);
}
} else {
printf("Error getting supported channels:\n");
printf("%s\n", AirpcapGetLastError(handle));
}
UINT chan;
AirpcapChannelInfo chaninfo;
if (FALSE == AirpcapGetDeviceChannel(handle, &chan)) {
printf("Error getting current channel: %s\n", AirpcapGetLastError(handle));
} else {
printf("AirpcapGetDeviceChannel = %u\n", chan);
if (6 != chan) {
printf("AirpcapGetDeviceChannel did not return 6 on Open - bug?\n");
}
}
if (FALSE == AirpcapGetDeviceChannelEx(handle, &chaninfo)) {
printf("Error getting current channel: %s\n", AirpcapGetLastError(handle));
} else {
printf("AirpcapGetDeviceChannelEx = {Freq = %u, Ext = %hhu, Flags = %hhu}\n",
chaninfo.Frequency,
chaninfo.ExtChannel,
chaninfo.Flags);
if (2437 != chaninfo.Frequency) {
printf("AirpcapGetDeviceChannelEx did not return channel 6 on Open - bug?\n");
}
}
UINT power;
if (FALSE == AirpcapGetTxPower(handle, &power)) {
printf("Error getting current TX power: %s\n", AirpcapGetLastError(handle));
} else {
printf("AirpcapGetTxPower() => %u mBm\n", power);
}
chaninfo.Frequency = 2422;
chaninfo.ExtChannel = 0;
chaninfo.Flags = 0;
if (FALSE == AirpcapSetDeviceChannelEx(handle, chaninfo)) {
printf("AirpcapSetDeviceChannelEx failed: %s", AirpcapGetLastError(handle));
} else {
printf("AirpcapSetDeviceChannelEx() OK - new freq = %u MHz\n", chaninfo.Frequency);
}
if (FALSE == AirpcapGetTxPower(handle, &power)) {
printf("Error getting current TX power: %s\n", AirpcapGetLastError(handle));
} else {
printf("AirpcapGetTxPower() => %u mBm\n", power);
}
UINT newPower = 1300;
if (FALSE == AirpcapSetTxPower(handle, newPower)) {
printf("Error setting TX power to %u mBm: %s\n",
newPower, AirpcapGetLastError(handle));
} else {
printf("AirpcapSetTxPower(%u) OK!\n", newPower);
if (FALSE == AirpcapGetTxPower(handle, &power)) {
printf("Error getting current TX power: %s\n", AirpcapGetLastError(handle));
} else {
printf("AirpcapGetTxPower() => %u mBm\n", power);
if (newPower != power) {
printf("** Error: AirpcapGetTxPower() returned %u, "
"but AirpcapSetTxPower(%u) succeeded??", power, newPower);
}
}
}
AirpcapValidationType validation;
if (FALSE == AirpcapGetFcsValidation(handle, &validation)) {
printf("AirpcapGetFcsValidation failed: %s", AirpcapGetLastError(handle));
} else {
if (AIRPCAP_VT_ACCEPT_EVERYTHING != validation) {
printf("FAILED: AirpcapGetFcsValidation initially returns %d, "
"not VT_ACCEPT_EVERYTHING!\n", validation);
} else {
printf("PASSED: AirpcapGetFcsValidation initially returned "
"VT_ACCEPT_EVERYTHING.\n");
}
}
printf("Attempting libpcap test.\n");
libpcap_test(handle, name);
printf("OK! Closing handle.\n");
AirpcapClose(handle);
}
void libpcap_test(PAirpcapHandle handle, PCHAR devname)
{
pcap_t *dev, *dev_writer;
pcap_dumper_t *writer;
char errbuf[PCAP_ERRBUF_SIZE];
int packets;
printf("libpcap test: %s\n", devname);
dev = pcap_open_live(devname, 65535, 1, 2, errbuf);
if (NULL == dev) {
printf("pcap_open_live failed: %s\n", errbuf);
}
dev_writer = pcap_open_dead(pcap_datalink(dev), 65535);
writer = pcap_dump_open(dev_writer, "test.pcap");
if (NULL == writer) {
printf("pcap_dump_open failed: %s\n", pcap_geterr(dev_writer));
pcap_close(dev_writer);
pcap_close(dev);
}
UINT i;
UINT channel = 0;
UINT channel_list[] = {1, 6, 11, 153, 157};
for (i = 0; i < (sizeof(channel_list) / sizeof(UINT)); i++) {
AirpcapChannelInfo info;
info.ExtChannel = 0;
info.Flags = 0;
channel = channel_list[i];
AirpcapConvertChannelToFrequency(channel, &info.Frequency);
printf("AirpcapSetDeviceChannelEx(%u, %u) ", channel, info.Frequency);
if (FALSE == AirpcapSetDeviceChannelEx(handle, info)) {
printf("failed: %s\n", AirpcapGetLastError(handle));
goto done;
} else {
printf("OK, sniffing 20 packets\n");
}
sleep(1);
for (packets = 0; packets < 20; packets++) {
struct pcap_pkthdr *header;
const u_char *data;
if (pcap_next_ex(dev, &header, &data) < 0) {
printf("Packet error: %s\n", pcap_geterr(dev));
break;
}
/* Y U USE WRONG POINTER TYPE LIBPCAP */
pcap_dump((u_char *)writer, header, data);
}
}
done:
pcap_dump_close(writer);
pcap_close(dev_writer);
pcap_close(dev);
}
int main(int argc,
char **argv)
{
UINT major, minor, rev, build;
CHAR ebuf[AIRPCAP_ERRBUF_SIZE];
AirpcapGetVersion(&major, &minor, &rev, &build);
printf("Airpcap version %d.%d.%d.%d\n",
major, minor, rev, build);
printf("Airpcap device enumeration:\n");
PAirpcapDeviceDescription desc;
if (FALSE == AirpcapGetDeviceList(&desc, ebuf)) {
fprintf(stderr, "AirpcapGetDeviceList error: %s\n", ebuf);
} else {
for(PAirpcapDeviceDescription d = desc; d; d = d->next) {
printf("Device %s: %s\n", d->Name, d->Description);
}
}
AirpcapFreeDeviceList(desc);
printf("\n");
if (argc > 1) {
test(argv[1]);
}
return 0;
}