| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include "config.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <pthread.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <dlfcn.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <windows.h> |
| 35 |
|
| 36 |
#include "osdep.h" |
| 37 |
#include "network.h" |
| 38 |
#include "cygwin.h" |
| 39 |
|
| 40 |
#ifdef HAVE_AIRPCAP |
| 41 |
#include "aircrack_ng_airpcap.h" |
| 42 |
#endif |
| 43 |
|
| 44 |
#define xstr(s) str(s) |
| 45 |
#define str(s) #s |
| 46 |
|
| 47 |
#define DLL_EXTENSION ".dll" |
| 48 |
|
| 49 |
struct priv_cygwin |
| 50 |
{ |
| 51 |
pthread_t pc_reader; |
| 52 |
volatile int pc_running; |
| 53 |
int pc_pipe[2]; |
| 54 |
int pc_channel; |
| 55 |
int pc_frequency; |
| 56 |
struct wif * pc_wi; |
| 57 |
int pc_did_init; |
| 58 |
|
| 59 |
int isAirpcap; |
| 60 |
int useDll; |
| 61 |
|
| 62 |
int (*pc_init)(char * param); |
| 63 |
int (*pc_set_chan)(int chan); |
| 64 |
int (*pc_set_freq)(int freq); |
| 65 |
int (*pc_inject)(void * buf, int len, struct tx_info * ti); |
| 66 |
int (*pc_sniff)(void * buf, int len, struct rx_info * ri); |
| 67 |
int (*pc_get_mac)(void * mac); |
| 68 |
int (*pc_set_mac)(void * mac); |
| 69 |
void (*pc_close)(void); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
char * stristr(const char * String, const char * Pattern) |
| 80 |
{ |
| 81 |
char *pptr, *sptr, *start; |
| 82 |
unsigned slen, plen; |
| 83 |
|
| 84 |
for (start = (char *) String, |
| 85 |
pptr = (char *) Pattern, |
| 86 |
slen = strlen(String), |
| 87 |
plen = strlen(Pattern); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
slen >= plen; |
| 92 |
|
| 93 |
start++, slen--) |
| 94 |
{ |
| 95 |
|
| 96 |
while (toupper((int) *start) != toupper((int) *Pattern)) |
| 97 |
{ |
| 98 |
start++; |
| 99 |
slen--; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if (slen < plen) return (NULL); |
| 104 |
} |
| 105 |
|
| 106 |
sptr = start; |
| 107 |
pptr = (char *) Pattern; |
| 108 |
|
| 109 |
while (toupper((int) *sptr) == toupper((int) *pptr)) |
| 110 |
{ |
| 111 |
sptr++; |
| 112 |
pptr++; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if ('\0' == *pptr) return (start); |
| 117 |
} |
| 118 |
} |
| 119 |
return (NULL); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static int do_cygwin_open(struct wif * wi, char * iface) |
| 131 |
{ |
| 132 |
struct priv_cygwin * priv = wi_priv(wi); |
| 133 |
void * lib; |
| 134 |
char * file; |
| 135 |
char * parm; |
| 136 |
int rc = -1; |
| 137 |
|
| 138 |
if (!iface) return -1; |
| 139 |
if (*iface == 0) return -1; |
| 140 |
|
| 141 |
priv->useDll = 0; |
| 142 |
|
| 143 |
if (stristr(iface, DLL_EXTENSION)) priv->useDll = 1; |
| 144 |
|
| 145 |
if (priv->useDll) |
| 146 |
{ |
| 147 |
file = strdup(iface); |
| 148 |
if (!file) return -1; |
| 149 |
|
| 150 |
parm = strchr(file, '|'); |
| 151 |
if (parm) *parm++ = 0; |
| 152 |
|
| 153 |
|
| 154 |
lib = dlopen(file, RTLD_LAZY); |
| 155 |
if (!lib) goto errdll; |
| 156 |
|
| 157 |
#if defined(__GNUC__) || defined(__llvm__) || defined(__clang__) |
| 158 |
#pragma GCC diagnostic push |
| 159 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 160 |
#endif |
| 161 |
priv->pc_init = dlsym(lib, xstr(CYGWIN_DLL_INIT)); |
| 162 |
priv->pc_set_chan = dlsym(lib, xstr(CYGWIN_DLL_SET_CHAN)); |
| 163 |
priv->pc_set_freq = dlsym(lib, xstr(CYGWIN_DLL_SET_FREQ)); |
| 164 |
priv->pc_get_mac = dlsym(lib, xstr(CYGWIN_DLL_GET_MAC)); |
| 165 |
priv->pc_set_mac = dlsym(lib, xstr(CYGWIN_DLL_SET_MAC)); |
| 166 |
priv->pc_close = dlsym(lib, xstr(CYGWIN_DLL_CLOSE)); |
| 167 |
priv->pc_inject = dlsym(lib, xstr(CYGWIN_DLL_INJECT)); |
| 168 |
priv->pc_sniff = dlsym(lib, xstr(CYGWIN_DLL_SNIFF)); |
| 169 |
#if defined(__GNUC__) || defined(__llvm__) || defined(__clang__) |
| 170 |
#pragma GCC diagnostic pop |
| 171 |
#endif |
| 172 |
|
| 173 |
if (!(priv->pc_init && priv->pc_set_chan && priv->pc_get_mac |
| 174 |
&& priv->pc_inject |
| 175 |
&& priv->pc_sniff |
| 176 |
&& priv->pc_close)) |
| 177 |
goto errdll; |
| 178 |
|
| 179 |
|
| 180 |
if ((rc = priv->pc_init(parm))) goto errdll; |
| 181 |
priv->pc_did_init = 1; |
| 182 |
|
| 183 |
rc = 0; |
| 184 |
|
| 185 |
errdll: |
| 186 |
free(file); |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
#ifdef HAVE_AIRPCAP |
| 191 |
|
| 192 |
priv->isAirpcap = isAirpcapDevice(iface); |
| 193 |
|
| 194 |
if (priv->isAirpcap) |
| 195 |
{ |
| 196 |
|
| 197 |
priv->pc_init = airpcap_init; |
| 198 |
priv->pc_set_chan = airpcap_set_chan; |
| 199 |
priv->pc_get_mac = airpcap_get_mac; |
| 200 |
priv->pc_set_mac = airpcap_set_mac; |
| 201 |
priv->pc_close = airpcap_close; |
| 202 |
priv->pc_inject = airpcap_inject; |
| 203 |
priv->pc_sniff = airpcap_sniff; |
| 204 |
|
| 205 |
rc = 0; |
| 206 |
} |
| 207 |
|
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
if (rc == 0) |
| 212 |
{ |
| 213 |
|
| 214 |
if (!priv->useDll) |
| 215 |
{ |
| 216 |
rc = priv->pc_init(iface); |
| 217 |
|
| 218 |
if (rc == 0) |
| 219 |
priv->pc_did_init = 1; |
| 220 |
else |
| 221 |
fprintf(stderr, "Error initializing <%s>\n", iface); |
| 222 |
} |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
|
| 227 |
fprintf(stderr, "Adapter <%s> not supported\n", iface); |
| 228 |
} |
| 229 |
|
| 230 |
return rc; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static int cygwin_set_channel(struct wif * wi, int chan) |
| 239 |
{ |
| 240 |
struct priv_cygwin * priv = wi_priv(wi); |
| 241 |
|
| 242 |
if (priv->pc_set_chan(chan) == -1) return -1; |
| 243 |
|
| 244 |
priv->pc_channel = chan; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static int cygwin_set_freq(struct wif * wi, int freq) |
| 254 |
{ |
| 255 |
struct priv_cygwin * priv = wi_priv(wi); |
| 256 |
|
| 257 |
if (!priv->pc_set_freq || priv->pc_set_freq(freq) == -1) return -1; |
| 258 |
|
| 259 |
priv->pc_frequency = freq; |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
static int cygwin_read_packet(struct priv_cygwin * priv, |
| 271 |
void * buf, |
| 272 |
int len, |
| 273 |
struct rx_info * ri) |
| 274 |
{ |
| 275 |
int rd; |
| 276 |
|
| 277 |
memset(ri, 0, sizeof(*ri)); |
| 278 |
|
| 279 |
rd = priv->pc_sniff(buf, len, ri); |
| 280 |
if (rd == -1) return -1; |
| 281 |
|
| 282 |
if (!ri->ri_channel) ri->ri_channel = wi_get_channel(priv->pc_wi); |
| 283 |
|
| 284 |
return rd; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static int cygwin_write(struct wif * wi, |
| 295 |
struct timespec * ts, |
| 296 |
int dlt, |
| 297 |
unsigned char * h80211, |
| 298 |
int len, |
| 299 |
struct tx_info * ti) |
| 300 |
{ |
| 301 |
struct priv_cygwin * priv = wi_priv(wi); |
| 302 |
int rc; |
| 303 |
|
| 304 |
(void) ts; |
| 305 |
(void) dlt; |
| 306 |
|
| 307 |
if ((rc = priv->pc_inject(h80211, len, ti)) == -1) return -1; |
| 308 |
|
| 309 |
return rc; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
static int cygwin_get_channel(struct wif * wi) |
| 317 |
{ |
| 318 |
struct priv_cygwin * pc = wi_priv(wi); |
| 319 |
|
| 320 |
return pc->pc_channel; |
| 321 |
} |
| 322 |
|
| 323 |
static int cygwin_get_freq(struct wif * wi) |
| 324 |
{ |
| 325 |
struct priv_cygwin * pc = wi_priv(wi); |
| 326 |
|
| 327 |
return pc->pc_frequency; |
| 328 |
} |
| 329 |
|
| 330 |
int cygwin_read_reader(int fd, int plen, void * dst, int len) |
| 331 |
{ |
| 332 |
|
| 333 |
if (len > plen) len = plen; |
| 334 |
if (net_read_exact(fd, dst, len) == -1) return -1; |
| 335 |
plen -= len; |
| 336 |
|
| 337 |
|
| 338 |
while (plen) |
| 339 |
{ |
| 340 |
char lame[1024]; |
| 341 |
int rd = sizeof(lame); |
| 342 |
|
| 343 |
if (rd > plen) rd = plen; |
| 344 |
|
| 345 |
if (net_read_exact(fd, lame, rd) == -1) return -1; |
| 346 |
|
| 347 |
plen -= rd; |
| 348 |
|
| 349 |
assert(plen >= 0); |
| 350 |
} |
| 351 |
|
| 352 |
return len; |
| 353 |
} |
| 354 |
|
| 355 |
static int cygwin_read(struct wif * wi, |
| 356 |
struct timespec * ts, |
| 357 |
int * dlt, |
| 358 |
unsigned char * h80211, |
| 359 |
int len, |
| 360 |
struct rx_info * ri) |
| 361 |
{ |
| 362 |
struct priv_cygwin * pc = wi_priv(wi); |
| 363 |
struct rx_info tmp; |
| 364 |
int plen; |
| 365 |
|
| 366 |
(void) ts; |
| 367 |
(void) dlt; |
| 368 |
|
| 369 |
if (pc->pc_running == -1) return -1; |
| 370 |
|
| 371 |
if (!ri) ri = &tmp; |
| 372 |
|
| 373 |
|
| 374 |
if (net_read_exact(pc->pc_pipe[0], &plen, sizeof(plen)) == -1) return -1; |
| 375 |
|
| 376 |
|
| 377 |
if (net_read_exact(pc->pc_pipe[0], ri, sizeof(*ri)) == -1) return -1; |
| 378 |
plen -= sizeof(*ri); |
| 379 |
assert(plen > 0); |
| 380 |
|
| 381 |
return cygwin_read_reader(pc->pc_pipe[0], plen, h80211, len); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
static void do_free(struct wif * wi) |
| 388 |
{ |
| 389 |
struct priv_cygwin * pc = wi_priv(wi); |
| 390 |
int tries = 3; |
| 391 |
|
| 392 |
|
| 393 |
if (pc->pc_running == 1) |
| 394 |
{ |
| 395 |
pc->pc_running = 0; |
| 396 |
|
| 397 |
while ((pc->pc_running != -1) && tries--) sleep(1); |
| 398 |
} |
| 399 |
|
| 400 |
if (pc->pc_pipe[0]) |
| 401 |
{ |
| 402 |
close(pc->pc_pipe[0]); |
| 403 |
close(pc->pc_pipe[1]); |
| 404 |
} |
| 405 |
|
| 406 |
if (pc->pc_did_init) pc->pc_close(); |
| 407 |
|
| 408 |
assert(wi->wi_priv); |
| 409 |
free(wi->wi_priv); |
| 410 |
wi->wi_priv = 0; |
| 411 |
|
| 412 |
free(wi); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static void cygwin_close(struct wif * wi) { do_free(wi); } |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
static int cygwin_fd(struct wif * wi) |
| 424 |
{ |
| 425 |
struct priv_cygwin * pc = wi_priv(wi); |
| 426 |
|
| 427 |
if (pc->pc_running == -1) return -1; |
| 428 |
|
| 429 |
return pc->pc_pipe[0]; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
static int cygwin_get_mac(struct wif * wi, unsigned char * mac) |
| 438 |
{ |
| 439 |
struct priv_cygwin * pc = wi_priv(wi); |
| 440 |
|
| 441 |
return pc->pc_get_mac(mac); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
static int cygwin_set_mac(struct wif * wi, unsigned char * mac) |
| 449 |
{ |
| 450 |
struct priv_cygwin * pc = wi_priv(wi); |
| 451 |
|
| 452 |
return pc->pc_set_mac(mac); |
| 453 |
} |
| 454 |
|
| 455 |
static int cygwin_get_monitor(struct wif * wi) |
| 456 |
{ |
| 457 |
if (wi) |
| 458 |
{ |
| 459 |
} |
| 460 |
|
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
static int cygwin_get_rate(struct wif * wi) |
| 465 |
{ |
| 466 |
if (wi) |
| 467 |
{ |
| 468 |
} |
| 469 |
|
| 470 |
return 1000000; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
static int cygwin_set_rate(struct wif * wi, int rate) |
| 479 |
{ |
| 480 |
if (wi || rate) |
| 481 |
{ |
| 482 |
} |
| 483 |
|
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
static void * cygwin_reader(void * arg) |
| 488 |
{ |
| 489 |
struct priv_cygwin * priv = arg; |
| 490 |
unsigned char buf[2048]; |
| 491 |
int len; |
| 492 |
struct rx_info ri; |
| 493 |
|
| 494 |
while (priv->pc_running) |
| 495 |
{ |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
len = cygwin_read_packet(priv, buf, sizeof(buf), &ri); |
| 503 |
if (len == -1) break; |
| 504 |
|
| 505 |
|
| 506 |
len += sizeof(ri); |
| 507 |
if (write(priv->pc_pipe[1], &len, sizeof(len)) != sizeof(len)) break; |
| 508 |
len -= sizeof(ri); |
| 509 |
|
| 510 |
|
| 511 |
if (write(priv->pc_pipe[1], &ri, sizeof(ri)) != sizeof(ri)) break; |
| 512 |
|
| 513 |
|
| 514 |
if (write(priv->pc_pipe[1], buf, len) != len) break; |
| 515 |
} |
| 516 |
|
| 517 |
priv->pc_running = -1; |
| 518 |
return NULL; |
| 519 |
} |
| 520 |
|
| 521 |
static struct wif * cygwin_open(char * iface) |
| 522 |
{ |
| 523 |
struct wif * wi; |
| 524 |
struct priv_cygwin * priv; |
| 525 |
|
| 526 |
|
| 527 |
wi = wi_alloc(sizeof(*priv)); |
| 528 |
if (!wi) return NULL; |
| 529 |
wi->wi_read = cygwin_read; |
| 530 |
wi->wi_write = cygwin_write; |
| 531 |
wi->wi_set_channel = cygwin_set_channel; |
| 532 |
wi->wi_get_channel = cygwin_get_channel; |
| 533 |
wi->wi_set_freq = cygwin_set_freq; |
| 534 |
wi->wi_get_freq = cygwin_get_freq; |
| 535 |
wi->wi_close = cygwin_close; |
| 536 |
wi->wi_fd = cygwin_fd; |
| 537 |
wi->wi_get_mac = cygwin_get_mac; |
| 538 |
wi->wi_set_mac = cygwin_set_mac; |
| 539 |
wi->wi_get_rate = cygwin_get_rate; |
| 540 |
wi->wi_set_rate = cygwin_set_rate; |
| 541 |
wi->wi_get_monitor = cygwin_get_monitor; |
| 542 |
|
| 543 |
|
| 544 |
if (do_cygwin_open(wi, iface) == -1) goto err; |
| 545 |
|
| 546 |
|
| 547 |
priv = wi_priv(wi); |
| 548 |
priv->pc_wi = wi; |
| 549 |
|
| 550 |
|
| 551 |
if (pipe(priv->pc_pipe) == -1) goto err; |
| 552 |
priv->pc_running = 2; |
| 553 |
if (pthread_create(&priv->pc_reader, NULL, cygwin_reader, priv)) goto err; |
| 554 |
priv->pc_running = 1; |
| 555 |
|
| 556 |
return wi; |
| 557 |
|
| 558 |
err: |
| 559 |
do_free(wi); |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
|
| 563 |
struct wif * wi_open_osdep(char * iface) { return cygwin_open(iface); } |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
EXPORT int get_battery_state(void) |
| 570 |
{ |
| 571 |
SYSTEM_POWER_STATUS powerStatus; |
| 572 |
int batteryTime = 0; |
| 573 |
|
| 574 |
if (GetSystemPowerStatus(&powerStatus) == TRUE) |
| 575 |
{ |
| 576 |
|
| 577 |
if (powerStatus.ACLineStatus == 0) |
| 578 |
batteryTime = (int) powerStatus.BatteryLifeTime; |
| 579 |
} |
| 580 |
return batteryTime; |
| 581 |
} |