Debian Codegraph @

contrib/peek/omnipeek.c

raw
1 /* ripped from devine's windows airodump */
2
3 #include <string.h>
4 #include <dlfcn.h>
5 #include <windows.h>
6 #include <pthread.h>
7 #include <assert.h>
8 #include <err.h>
9
10 #include "osdep.h"
11 #include "cygwin.h"
12
13 #ifdef UNUSED
14 #elif defined(__GNUC__)
15 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
16 #elif defined(__LCLINT__)
17 # define UNUSED(x) /*@unused@*/ x
18 #else
19 # define UNUSED(x) x
20 #endif
21
22 #define MAGIC1 0x3E8000
23 #define MAGIC2 0x21
24 #define MAGICCHAN 0xFF636713
25
26 struct pstate
27 {
28 void *ps_lib;
29 HANDLE ps_adapter;
30 HANDLE ps_ctx;
31 pthread_cond_t ps_sem;
32 pthread_mutex_t ps_mtx;
33 unsigned char ps_data[4096];
34 int ps_len;
35
36 int (*ps_peek_initialize_library)(void);
37 HANDLE (*ps_peek_open_adapter)(LPSTR);
38 int (*ps_peek_start_capture)(HANDLE);
39 int (*ps_peek_request)(HANDLE, void*, void*);
40 int (*ps_peek_stop_capture)(HANDLE);
41 int (*ps_peek_close_adapter)(HANDLE);
42 int (*ps_peek_packet_send)(HANDLE, void*, int, int*, LPOVERLAPPED,
43 int);
44 HANDLE (*ps_peek_create_capture_context)(HANDLE, void*, int,
45 int, void*);
46 } _pstate;
47
48 static struct pstate *get_ps(void)
49 {
50 return &_pstate;
51 }
52
53 static int init_lib(struct pstate *ps)
54 {
55 char *libname = "Peek.dll";
56 void *lib;
57
58 if (!(lib = dlopen(libname, RTLD_LAZY)))
59 return -1;
60
61 ps->ps_lib = lib;
62
63 ps->ps_peek_open_adapter = dlsym(lib, "PeekOpenAdapter");
64 ps->ps_peek_start_capture = dlsym(lib, "PeekStartCapture");
65 ps->ps_peek_request = dlsym(lib, "PeekRequest");
66 ps->ps_peek_stop_capture = dlsym(lib, "PeekStopCapture");
67 ps->ps_peek_close_adapter = dlsym(lib, "PeekCloseAdapter");
68 ps->ps_peek_packet_send = dlsym(lib, "PeekPacketSend");
69 ps->ps_peek_create_capture_context =
70 dlsym(lib, "PeekCreateCaptureContext");
71 ps->ps_peek_initialize_library =
72 dlsym(lib, "PeekInitializeLibrary");
73
74 if (!(ps->ps_peek_open_adapter
75 && ps->ps_peek_start_capture
76 && ps->ps_peek_request
77 && ps->ps_peek_stop_capture
78 && ps->ps_peek_close_adapter
79 && ps->ps_peek_packet_send
80 && ps->ps_peek_create_capture_context
81 && ps->ps_peek_initialize_library
82 ))
83 return -1;
84
85 return 0;
86 }
87
88 static void do_cleanup(struct pstate *ps)
89 {
90 if (!ps->ps_lib)
91 return;
92
93 if (ps->ps_ctx != INVALID_HANDLE_VALUE)
94 ps->ps_peek_stop_capture(ps->ps_ctx);
95
96 if (ps->ps_adapter != INVALID_HANDLE_VALUE)
97 ps->ps_peek_close_adapter(ps->ps_adapter);
98
99 dlclose(ps->ps_lib);
100 }
101
102 static int set_chan(struct pstate *ps, int channel)
103 {
104 unsigned long reqdata[139];
105 OVERLAPPED iodata;
106
107 memset(reqdata, 0, sizeof(reqdata));
108 memset(&iodata, 0, sizeof(iodata));
109
110 iodata.hEvent = CreateEvent(0, 0, 0, 0);
111
112 reqdata[5] = 1;
113 reqdata[6] = MAGICCHAN;
114 reqdata[7] = (unsigned long) &channel;
115 reqdata[8] = 4;
116
117 return ps->ps_peek_request(ps->ps_adapter, reqdata, &iodata);
118 }
119
120 static void do_lock(struct pstate *ps)
121 {
122 if (pthread_mutex_lock(&ps->ps_mtx))
123 err(1, "pthread_mutex_lock()");
124 }
125
126 static void do_signal(struct pstate *ps)
127 {
128 do_lock(ps);
129
130 if (pthread_cond_signal(&ps->ps_sem))
131 err(1, "pthread_cond_signal()");
132 }
133
134 static void do_wait(struct pstate *ps)
135 {
136 do_lock(ps);
137
138 if (pthread_cond_wait(&ps->ps_sem, &ps->ps_mtx))
139 err(1, "pthread_cond_wait()");
140 }
141
142 static int WINAPI callback(unsigned char *data, int len, int UNUSED(caplen),
143 __int64 UNUSED(timestamp), int flags,
144 int UNUSED(arg7))
145 {
146 struct pstate *ps = get_ps();
147
148 if ((flags & 1) != 0)
149 return 1;
150
151 assert(len <= (int) sizeof(ps->ps_data));
152 ps->ps_len = len;
153 memcpy(ps->ps_data, data, ps->ps_len);
154
155 /* tell him we wrote */
156 do_signal(ps);
157
158 /* wait for him to copy */
159 do_wait(ps);
160
161 return 1;
162 }
163
164 static int init_card(struct pstate *ps, char *dev)
165 {
166 int rc, len;
167 char *unicode, *p;
168
169 if (ps->ps_peek_initialize_library() == 0)
170 return -1;
171
172 /* convert dev to unicode - i'm sure there's a standard function, but
173 * aingottime.
174 * Format: \Device\{GUID}
175 */
176 if (!dev)
177 return -1;
178
179 len = strlen(dev);
180 unicode = p = malloc((len+1)*2);
181 if (!unicode)
182 return -1;
183
184 for (rc = 0; rc < len; rc++) {
185 *p++ = dev[rc];
186 *p++ = 0;
187 }
188 *p++ = 0;
189 *p++ = 0;
190
191 ps->ps_adapter = ps->ps_peek_open_adapter(unicode);
192 free(unicode);
193 if (ps->ps_adapter == INVALID_HANDLE_VALUE)
194 return -1;
195
196 ps->ps_ctx = ps->ps_peek_create_capture_context(ps->ps_adapter,
197 callback, MAGIC1, MAGIC2, NULL);
198
199 if ((rc = ps->ps_peek_start_capture(ps->ps_ctx)))
200 return rc;
201
202 return set_chan(ps, 1);
203 }
204
205 int CYGWIN_DLL_INIT (char *param)
206 {
207 struct pstate *ps = get_ps();
208 int rc;
209
210 memset(ps, 0, sizeof(*ps));
211 ps->ps_adapter = INVALID_HANDLE_VALUE;
212 ps->ps_ctx = INVALID_HANDLE_VALUE;
213
214 if ((rc = pthread_cond_init(&ps->ps_sem, NULL)))
215 goto out;
216
217 if ((rc = pthread_mutex_init(&ps->ps_mtx, NULL)))
218 goto out;
219
220 if ((rc = init_lib(ps)))
221 goto out;
222
223 if ((rc = init_card(ps, param)))
224 goto out;
225
226 return 0;
227 out:
228 do_cleanup(ps);
229 return rc;
230 }
231
232 int CYGWIN_DLL_SET_CHAN (int chan)
233 {
234 struct pstate *ps = get_ps();
235
236 return set_chan(ps, chan) ? -1 : 0;
237 }
238
239 int CYGWIN_DLL_INJECT (void* buf, int len,
240 struct tx_info* UNUSED(ti))
241 {
242 struct pstate *ps = get_ps();
243 int rc;
244 int wrote = 0;
245 OVERLAPPED iodata;
246
247 memset(&iodata, 0, sizeof(iodata));
248 iodata.hEvent = CreateEvent(0, 0, 0, 0);
249
250 rc = ps->ps_peek_packet_send(ps->ps_adapter, buf, len, &wrote,
251 &iodata, 0);
252 if (rc)
253 return rc;
254
255 return len;
256 }
257
258 int CYGWIN_DLL_SNIFF (void *buf, int len, struct rx_info* UNUSED(ri))
259 {
260 struct pstate *ps = get_ps();
261
262 /* wait for shit */
263 do_wait(ps);
264
265 /* copy it */
266 if (ps->ps_len < len)
267 len = ps->ps_len;
268
269 memcpy(buf, ps->ps_data, len);
270
271 /* tell him we're done */
272 do_signal(ps);
273
274 return len;
275 }
276
277 int CYGWIN_DLL_GET_MAC (unsigned char* UNUSED(mac))
278 {
279 return -1;
280 }
281
282 int CYGWIN_DLL_SET_MAC (unsigned char* UNUSED(mac))
283 {
284 return -1;
285 }
286
287 void CYGWIN_DLL_CLOSE (void)
288 {
289 struct pstate *ps = get_ps();
290
291 do_cleanup(ps);
292 }