Debian Codegraph @

contrib/commview/commview.c

raw
1 #include <net/if.h>
2 #include <sys/ioctl.h>
3 #include <string.h>
4 #include <dlfcn.h>
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <wchar.h>
11
12 #include <windows.h>
13 #include <iphlpapi.h>
14 #include <setupapi.h>
15 #include <devguid.h>
16 #include <pthread.h>
17
18 #include "osdep.h"
19 #include "tap-win32/common.h"
20
21 #define BUFSIZE 0x3E8000
22
23 struct CV_Header {
24 unsigned int TickCount; // GetTickCount() / 1000 at time of packet
25 unsigned int Micros; // Microsecond counter at time of packet
26 /* The following might be backwards, unsure */
27 int PacketSize; // Full packet size?
28 int SliceSize; // Truncated packet size?
29 int Unknown; // Not sure what this is.
30 };
31
32 struct CV_Header2 {
33 char ErrorFlag; // ErrorFlag & 1 = CRC error
34 char Unknown2[6]; // Unknown
35 char Power; // Power
36 char Unknown3[6]; // Unknown
37 };
38
39 struct cstate {
40 char cs_param[256];
41 int cs_ioctls;
42 struct ifreq cs_ifreq;
43 char cs_guid[256];
44 HKEY cs_key;
45 int cs_chan;
46 volatile int cs_restarting;
47 void *cs_lib;
48 pthread_mutex_t cs_mtx;
49 int cs_debug;
50
51 char (__cdecl *cs_F1)(int Code);
52 char (__cdecl *cs_F2)(void);
53 char (__cdecl *cs_T1)(int Size, unsigned char *Buffer);
54 char (__cdecl *cs_CC)(int Channel);
55 char (__cdecl *cs_S1)(int Channel);
56 int (__cdecl *cs_S5)(unsigned char *Buffer, int Length);
57 int (__cdecl *cs_GN)(wchar_t *);
58 int (*cs_SC)(int band);
59 } _cs;
60
61 static struct cstate *get_cs(void)
62 {
63 return &_cs;
64 }
65
66 static int print_error(char *fmt, ...)
67 {
68 va_list ap;
69
70 va_start(ap, fmt);
71 vprintf(fmt, ap);
72 va_end(ap);
73 printf("\n");
74
75 return -1;
76 }
77
78 static void print_debug(char *fmt, ...)
79 {
80 struct cstate *cs = get_cs();
81 va_list ap;
82
83 if (!cs->cs_debug)
84 return;
85
86 va_start(ap, fmt);
87 vprintf(fmt, ap);
88 va_end(ap);
89 printf("\n");
90 }
91
92 static int do_init_lib(struct cstate *cs)
93 {
94 /* init */
95 if (!cs->cs_F1(BUFSIZE))
96 return print_error("F1");
97
98 /* start monitor */
99 if (!cs->cs_S1(cs->cs_chan))
100 return print_error("S1");
101
102 /* change chan */
103 if (!cs->cs_CC(cs->cs_chan))
104 return print_error("CC");
105
106 return 0;
107 }
108
109 static int init_lib(struct cstate *cs)
110 {
111 char *lib = "ca2k.dll";
112 void *ca2k_dll;
113
114 ca2k_dll = dlopen(lib, RTLD_LAZY);
115 if (!ca2k_dll)
116 return print_error("dlopen(%s)", lib);
117 cs->cs_lib = ca2k_dll;
118
119 // Initialise
120 cs->cs_F1 = dlsym(ca2k_dll, "F1");
121 // Transmit
122 cs->cs_T1 = dlsym(ca2k_dll, "T1");
123 // Change monitoring channel
124 cs->cs_CC = dlsym(ca2k_dll, "CC");
125 // Start monitoring
126 cs->cs_S1 = dlsym(ca2k_dll, "S1");
127 // Read packets
128 cs->cs_S5 = dlsym(ca2k_dll, "S5");
129 // Finalize
130 cs->cs_F2 = dlsym(ca2k_dll, "F2");
131 // Get Adapter Name
132 cs->cs_GN = dlsym(ca2k_dll, "GN");
133
134 if (!(cs->cs_F1 && cs->cs_T1 && cs->cs_CC && cs->cs_S1 && cs->cs_S5
135 && cs->cs_F2 && cs->cs_GN))
136 return print_error("Can't find syms");
137
138 return do_init_lib(cs);
139 }
140
141 static int get_name(struct cstate *cs, char *name)
142 {
143 wchar_t wname[1024];
144 unsigned int i;
145
146 if (!(cs->cs_GN(wname) & 1))
147 return print_error("GN()");
148
149 /* XXX */
150 for (i = 0; i < (sizeof(wname)/sizeof(wchar_t)); i++) {
151 if (wname[i] == 0)
152 break;
153
154 *name++ = (char) ((unsigned char) wname[i]);
155 }
156 *name = 0;
157
158 return 0;
159 }
160
161 static int get_guid(struct cstate *cs, char *param)
162 {
163 IP_ADAPTER_INFO ai[16];
164 DWORD len = sizeof(ai);
165 PIP_ADAPTER_INFO p;
166 char name[1024];
167 int found;
168
169 if (get_name(cs, name) == -1)
170 return print_error("get_name()");
171
172 print_debug("Name: %s", name);
173
174 if (GetAdaptersInfo(ai, &len) != ERROR_SUCCESS)
175 return print_error("GetAdaptersInfo()");
176
177 p = ai;
178 while (p) {
179 print_debug("get_guid: name: %s desc: %s",
180 p->AdapterName, p->Description);
181
182 found = (param && strcmp(p->AdapterName, param) == 0)
183 || strstr(p->Description, name);
184
185 /* XXX */
186 if (cs->cs_debug) {
187 char yea[512];
188
189 printf("Does this look like your card? [y/n]\n");
190 yea[0] = 0;
191 fgets(yea, sizeof(yea), stdin);
192 if (yea[0] == 'y')
193 found = 1;
194 else
195 found = 0;
196 }
197
198 if (found) {
199 snprintf(cs->cs_guid, sizeof(cs->cs_guid)-1, "%s",
200 p->AdapterName);
201 return 0;
202 }
203
204 p = p->Next;
205 }
206
207 return print_error("Adapter not found");
208 }
209
210 static int open_key(struct cstate *cs, char *name)
211 {
212 char key[256];
213 DWORD dt, len = sizeof(key);
214
215 /* open key */
216 snprintf(key, sizeof(key)-1, "%s\\%s", ADAPTER_KEY, name);
217 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_ALL_ACCESS,
218 &cs->cs_key) != ERROR_SUCCESS)
219 return print_error("RegOpenKeyEx()");
220
221 /* check if its our guid */
222 if ((RegQueryValueEx(cs->cs_key, "NetCfgInstanceId", NULL, &dt,
223 (unsigned char*)key, &len) == ERROR_SUCCESS)
224 && (dt == REG_SZ) && (strcmp(key, cs->cs_guid) == 0))
225 return 1; /* closekey done by cleanup */
226
227 /* nope */
228 RegCloseKey(cs->cs_key);
229 cs->cs_key = NULL;
230
231 return 0;
232 }
233
234 static int open_conf(struct cstate *cs)
235 {
236 HKEY ak47;
237 int rc = -1;
238 int i;
239 char name[256];
240 DWORD len;
241
242 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, ADAPTER_KEY, 0, KEY_READ, &ak47)
243 != ERROR_SUCCESS)
244 return print_error("RegOpenKeyEx()");
245
246 for (i = 0;; i++) {
247 len = sizeof(name);
248 if (RegEnumKeyEx(ak47, i, name, &len, NULL, NULL, NULL, NULL)
249 != ERROR_SUCCESS)
250 break;
251
252 rc = open_key(cs, name);
253 if (rc)
254 break;
255 else
256 rc = -1;
257 }
258
259 RegCloseKey(ak47);
260 return rc;
261 }
262
263 static int check_param(struct cstate *cs, char **p)
264 {
265 char *param = *p;
266
267 /* assume it's ifname */
268 if (strncmp(param, "eth", 3) == 0) {
269 snprintf(cs->cs_param, sizeof(cs->cs_param), "%s", param);
270 snprintf(cs->cs_ifreq.ifr_name,
271 sizeof(cs->cs_ifreq.ifr_name), "%s", cs->cs_param);
272
273 cs->cs_ioctls = socket(PF_INET, SOCK_DGRAM, 0);
274 if (cs->cs_ioctls == -1) {
275 cs->cs_ioctls = 0;
276 return print_error("socket()");
277 }
278 } else if(strcmp(param, "debug") == 0) {
279 cs->cs_debug = 1;
280 *p = NULL;
281 }
282
283 return 0;
284 }
285
286 int cygwin_init(char *param)
287 {
288 struct cstate *cs = get_cs();
289
290 memset(cs, 0, sizeof(*cs));
291 cs->cs_chan = 1;
292
293 if (pthread_mutex_init(&cs->cs_mtx, NULL))
294 return print_error("pthread_mutex_init()");
295
296 if (param) {
297 if (check_param(cs, &param))
298 return -1;
299 }
300
301 if (init_lib(cs) == -1)
302 return print_error("init_lib()");
303
304 if (get_guid(cs, param) == -1)
305 return print_error("get_guid()");
306
307 if (open_conf(cs) == -1)
308 return print_error("open_conf()");
309
310 return 0;
311 }
312
313 int cygwin_set_chan(int chan)
314 {
315 struct cstate *cs = get_cs();
316
317 if (!cs->cs_CC(chan))
318 return -1;
319
320 cs->cs_chan = chan;
321
322 return 0;
323 }
324
325 int cygwin_inject(void *buf, int len, struct tx_info *ti)
326 {
327 struct cstate *cs = get_cs();
328
329 if (ti) {} /* XXX unused */
330
331 if (!cs->cs_T1(len, buf))
332 return -1;
333
334 return len;
335 }
336
337 static int read_single_packet(struct cstate *cs, unsigned char *buf, int len,
338 struct rx_info *ri)
339 {
340 static unsigned char data[BUFSIZE];
341 static int totlen = 0;
342 static unsigned char *next;
343 struct CV_Header *cvh;
344 struct CV_Header2 *cvh2;
345 unsigned char *hdr;
346 int align, plen;
347
348 /* read data if necessary */
349 if (totlen == 0) {
350 /* XXX can't kill iface if we're reading */
351 if (pthread_mutex_lock(&cs->cs_mtx))
352 return -1;
353 totlen = cs->cs_S5(data, sizeof(data));
354 if (pthread_mutex_unlock(&cs->cs_mtx))
355 return -1;
356
357 if (totlen < 1)
358 return -1;
359
360 next = data;
361 }
362
363 /* copy packet */
364 cvh = (struct CV_Header*) next;
365 cvh2 = (struct CV_Header2*) (cvh+1);
366 hdr = (unsigned char*) (cvh2+1);
367 plen = cvh->SliceSize - sizeof(*cvh2);
368 assert(plen > 0);
369 if (plen < len)
370 len = plen;
371 memcpy(buf, hdr, len);
372
373 if (ri)
374 ri->ri_power = cvh2->Power;
375
376 /* go to next packet */
377 next = hdr + plen;
378 align = ((unsigned long)next - (unsigned long)cvh ) % 4;
379 if (align)
380 align = 4 - align;
381 next += align;
382 totlen -= sizeof(*cvh) + cvh->SliceSize;
383 assert(totlen >= 0);
384 if (totlen > 0)
385 totlen -= align;
386 assert(totlen >= 0);
387
388 return (cvh2->ErrorFlag & 1) ? 0 : len;
389 }
390
391 int cygwin_sniff(void *buf, int len, struct rx_info *ri)
392 {
393 struct cstate *cs = get_cs();
394 int rc;
395 int tries = 60;
396
397 while ((rc = read_single_packet(cs, buf, len, ri)) == 0);
398
399 if (rc != -1)
400 return rc;
401
402 /* check if we're restarting */
403 while (cs->cs_restarting && tries--) {
404 /* try again */
405 if (cs->cs_restarting == 2) {
406 cs->cs_restarting = 0;
407 return cygwin_sniff(buf, len, ri);
408 }
409
410 sleep(1);
411 }
412
413 return rc;
414 }
415
416 static int do_get_mac_win(struct cstate *cs, unsigned char *mac)
417 {
418 IP_ADAPTER_INFO ai[16];
419 DWORD len = sizeof(ai);
420 PIP_ADAPTER_INFO p;
421
422 if (GetAdaptersInfo(ai, &len) != ERROR_SUCCESS)
423 return -1;
424
425 p = ai;
426 while (p) {
427 if (strcmp(cs->cs_guid, p->AdapterName) == 0) {
428 memcpy(mac, p->Address, 6);
429 return 0;
430 }
431
432 p = p->Next;
433 }
434
435 return -1;
436 }
437
438 static int do_get_mac_cygwin(struct cstate *cs, unsigned char *mac)
439 {
440 if (ioctl(cs->cs_ioctls, SIOCGIFHWADDR, &cs->cs_ifreq) == -1)
441 return -1;
442
443 memcpy(mac, cs->cs_ifreq.ifr_addr.sa_data, 6);
444
445 return 0;
446 }
447
448 int cygwin_get_mac(unsigned char *mac)
449 {
450 struct cstate *cs = get_cs();
451
452 if (cs->cs_ioctls)
453 return do_get_mac_cygwin(cs, mac);
454
455 return do_get_mac_win(cs, mac);
456 }
457
458 static int is_us2(struct cstate *cs, HDEVINFO *hdi, SP_DEVINFO_DATA *did)
459 {
460 char buf[256];
461 DWORD len = sizeof(buf), dt;
462
463 if (cs) {} /* XXX unused */
464
465 if (!SetupDiGetDeviceRegistryProperty(*hdi, did, SPDRP_DEVICEDESC, &dt,
466 (unsigned char*)buf, len, &len))
467 return 0;
468
469 if (dt != REG_SZ)
470 return 0;
471
472 return strstr(buf, "CommView") != NULL;
473 }
474
475 static int reset_state(HDEVINFO *hdi, SP_DEVINFO_DATA *did, DWORD state)
476 {
477 SP_PROPCHANGE_PARAMS parm;
478
479 parm.ClassInstallHeader.cbSize = sizeof(parm.ClassInstallHeader);
480 parm.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
481 parm.Scope = DICS_FLAG_GLOBAL;
482 parm.StateChange = state;
483
484 if (!SetupDiSetClassInstallParams(*hdi, did, (SP_CLASSINSTALL_HEADER*)
485 &parm, sizeof(parm)))
486 return -1;
487
488 if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, *hdi, did))
489 return -1;
490
491 return 0;
492 }
493
494 static int do_reset(HDEVINFO *hdi, SP_DEVINFO_DATA *did)
495 {
496 int rc;
497
498 rc = reset_state(hdi, did, DICS_DISABLE);
499 if (rc)
500 return rc;
501
502 return reset_state(hdi, did, DICS_ENABLE);
503 }
504
505 static int restart(struct cstate *cs)
506 {
507 int rc;
508
509 rc = do_init_lib(cs);
510
511 return rc;
512 }
513
514 static int reset(struct cstate *cs)
515 {
516 HDEVINFO hdi;
517 SP_DEVINFO_DATA did;
518 int i;
519 int rc = -1;
520
521 hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL,
522 DIGCF_PRESENT);
523 if (hdi == INVALID_HANDLE_VALUE)
524 return -1;
525
526 /* find device */
527 for (i = 0;; i++) {
528 did.cbSize = sizeof(did);
529 if (!SetupDiEnumDeviceInfo(hdi, i, &did))
530 break;
531
532 if (!is_us2(cs, &hdi, &did))
533 continue;
534
535 /* XXX we are blocked on reader. */
536 if (pthread_mutex_lock(&cs->cs_mtx))
537 break;
538 cs->cs_restarting = 1;
539
540 /* kill lib */
541 if (!cs->cs_F2())
542 break;
543
544 /* reset NIC */
545 rc = do_reset(&hdi, &did);
546 if (rc)
547 break;
548
549 sleep(1); /* XXX seems necessary */
550
551 /* reinit lib */
552 rc = restart(cs);
553 cs->cs_restarting = 2;
554
555 /* done */
556 if (pthread_mutex_unlock(&cs->cs_mtx))
557 break;
558
559 break;
560 }
561
562 SetupDiDestroyDeviceInfoList(hdi);
563
564 return rc;
565 }
566
567 int cygwin_set_mac(unsigned char *mac)
568 {
569 struct cstate *cs = get_cs();
570 char str[2*6+1];
571 char strold[sizeof(str)];
572 int i;
573 char *key = "NetworkAddress";
574 DWORD dt, len = sizeof(strold);
575
576 /* convert */
577 str[0] = 0;
578 for (i = 0; i < 6; i++) {
579 char tmp[3];
580
581 if (sprintf(tmp, "%.2X", *mac++) != 2)
582 return -1;
583 strcat(str, tmp);
584 }
585
586 /* check old */
587 if ((RegQueryValueEx(cs->cs_key, key, NULL, &dt, (unsigned char*)
588 strold, &len) == ERROR_SUCCESS) && (dt == REG_SZ)) {
589 if (strcmp(str, strold) == 0)
590 return 0;
591 }
592
593 /* set */
594 if (RegSetValueEx(cs->cs_key, key, 0, REG_SZ,
595 (unsigned char *)str, strlen(str)+1) != ERROR_SUCCESS)
596 return -1;
597
598 if (reset(cs) == -1)
599 return -1;
600
601 return 0;
602 }
603
604 void cygwin_close(void)
605 {
606 struct cstate *cs = get_cs();
607
608 if (cs->cs_ioctls)
609 close(cs->cs_ioctls);
610
611 if (cs->cs_key)
612 RegCloseKey(cs->cs_key);
613
614 if (cs->cs_lib) {
615 cs->cs_F2();
616 dlclose(cs->cs_lib);
617 }
618 }