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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
/*
* Copyright (c) 2007, 2008, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
*
* OS dependent API for cygwin. It relies on an external
* DLL to do the actual wifi stuff
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <windows.h>
#include "osdep.h"
#include "network.h"
#include "cygwin.h"
#ifdef HAVE_AIRPCAP
#include "aircrack_ng_airpcap.h"
#endif
#define xstr(s) str(s)
#define str(s) #s
#define DLL_EXTENSION ".dll"
struct priv_cygwin
{
pthread_t pc_reader;
volatile int pc_running;
int pc_pipe[2]; /* reader -> parent */
int pc_channel;
int pc_frequency;
struct wif * pc_wi;
int pc_did_init;
int isAirpcap;
int useDll;
int (*pc_init)(char * param);
int (*pc_set_chan)(int chan);
int (*pc_set_freq)(int freq);
int (*pc_inject)(void * buf, int len, struct tx_info * ti);
int (*pc_sniff)(void * buf, int len, struct rx_info * ri);
int (*pc_get_mac)(void * mac);
int (*pc_set_mac)(void * mac);
void (*pc_close)(void);
};
/**
* strstr() function case insensitive
* @param String C string to be scanned
* @param Pattern C string containing the sequence of characters to match
* @return Pointer to the first occurrence of Pattern in String, or a null
* pointer if there Pattern is not part of String.
*/
char * stristr(const char * String, const char * Pattern)
{
char *pptr, *sptr, *start;
unsigned slen, plen;
for (start = (char *) String,
pptr = (char *) Pattern,
slen = strlen(String),
plen = strlen(Pattern);
/* while string length not shorter than pattern length */
slen >= plen;
start++, slen--)
{
/* find start of pattern in string */
while (toupper((int) *start) != toupper((int) *Pattern))
{
start++;
slen--;
/* if pattern longer than string */
if (slen < plen) return (NULL);
}
sptr = start;
pptr = (char *) Pattern;
while (toupper((int) *sptr) == toupper((int) *pptr))
{
sptr++;
pptr++;
/* if end of pattern then pattern was found */
if ('\0' == *pptr) return (start);
}
}
return (NULL);
}
/**
* Get the different functions for to interact with the device:
* - setting monitor mode
* - changing channel
* - capturing data
* - injecting packets
* @param iface The interface name
*/
static int do_cygwin_open(struct wif * wi, char * iface)
{
struct priv_cygwin * priv = wi_priv(wi);
void * lib;
char * file;
char * parm;
int rc = -1;
if (!iface) return -1;
if (strlen(iface) == 0) return -1;
priv->useDll = 0;
if (stristr(iface, DLL_EXTENSION)) priv->useDll = 1;
if (priv->useDll)
{
file = strdup(iface);
if (!file) return -1;
parm = strchr(file, '|');
if (parm) *parm++ = 0;
/* load lib */
lib = dlopen(file, RTLD_LAZY);
if (!lib) goto errdll;
#if defined(__GNUC__) || defined(__llvm__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
priv->pc_init = dlsym(lib, xstr(CYGWIN_DLL_INIT));
priv->pc_set_chan = dlsym(lib, xstr(CYGWIN_DLL_SET_CHAN));
priv->pc_set_freq = dlsym(lib, xstr(CYGWIN_DLL_SET_FREQ));
priv->pc_get_mac = dlsym(lib, xstr(CYGWIN_DLL_GET_MAC));
priv->pc_set_mac = dlsym(lib, xstr(CYGWIN_DLL_SET_MAC));
priv->pc_close = dlsym(lib, xstr(CYGWIN_DLL_CLOSE));
priv->pc_inject = dlsym(lib, xstr(CYGWIN_DLL_INJECT));
priv->pc_sniff = dlsym(lib, xstr(CYGWIN_DLL_SNIFF));
#if defined(__GNUC__) || defined(__llvm__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
if (!(priv->pc_init && priv->pc_set_chan && priv->pc_get_mac
&& priv->pc_inject
&& priv->pc_sniff
&& priv->pc_close))
goto errdll;
/* init lib */
if ((rc = priv->pc_init(parm))) goto errdll;
priv->pc_did_init = 1;
rc = 0;
errdll:
free(file);
}
else
{
#ifdef HAVE_AIRPCAP
// Check if it's an Airpcap device
priv->isAirpcap = isAirpcapDevice(iface);
if (priv->isAirpcap)
{
// Get functions
priv->pc_init = airpcap_init;
priv->pc_set_chan = airpcap_set_chan;
priv->pc_get_mac = airpcap_get_mac;
priv->pc_set_mac = airpcap_set_mac;
priv->pc_close = airpcap_close;
priv->pc_inject = airpcap_inject;
priv->pc_sniff = airpcap_sniff;
rc = 0;
}
#endif
}
if (rc == 0)
{
// Don't forget to initialize
if (!priv->useDll)
{
rc = priv->pc_init(iface);
if (rc == 0)
priv->pc_did_init = 1;
else
fprintf(stderr, "Error initializing <%s>\n", iface);
}
}
else
{
// Show an error message if the adapter is not supported
fprintf(stderr, "Adapter <%s> not supported\n", iface);
}
return rc;
}
/**
* Change channel
* @param chan Channel
* @return 0 if successful, -1 if it failed
*/
static int cygwin_set_channel(struct wif * wi, int chan)
{
struct priv_cygwin * priv = wi_priv(wi);
if (priv->pc_set_chan(chan) == -1) return -1;
priv->pc_channel = chan;
return 0;
}
/**
* Change frequency
* @param freq Frequency
* @return 0 if successful, -1 if it failed
*/
static int cygwin_set_freq(struct wif * wi, int freq)
{
struct priv_cygwin * priv = wi_priv(wi);
if (!priv->pc_set_freq || priv->pc_set_freq(freq) == -1) return -1;
priv->pc_frequency = freq;
return 0;
}
/**
* Capture a packet
* @param buf Buffer for the packet (has to be already allocated)
* @param len Length of the buffer
* @param ri Receive information structure
* @return -1 in case of failure or the number of bytes received
*/
static int cygwin_read_packet(struct priv_cygwin * priv,
void * buf,
int len,
struct rx_info * ri)
{
int rd;
memset(ri, 0, sizeof(*ri));
rd = priv->pc_sniff(buf, len, ri);
if (rd == -1) return -1;
if (!ri->ri_channel) ri->ri_channel = wi_get_channel(priv->pc_wi);
return rd;
}
/**
* Send a packet
* @param h80211 The packet itself
* @param len Length of the packet
* @param ti Transmit information
* @return -1 if failure or the number of bytes sent
*/
static int cygwin_write(struct wif * wi,
struct timespec * ts,
int dlt,
unsigned char * h80211,
int len,
struct tx_info * ti)
{
struct priv_cygwin * priv = wi_priv(wi);
int rc;
(void) ts;
(void) dlt;
if ((rc = priv->pc_inject(h80211, len, ti)) == -1) return -1;
return rc;
}
/**
* Get device channel
* @return channel
*/
static int cygwin_get_channel(struct wif * wi)
{
struct priv_cygwin * pc = wi_priv(wi);
return pc->pc_channel;
}
static int cygwin_get_freq(struct wif * wi)
{
struct priv_cygwin * pc = wi_priv(wi);
return pc->pc_frequency;
}
int cygwin_read_reader(int fd, int plen, void * dst, int len)
{
/* packet */
if (len > plen) len = plen;
if (net_read_exact(fd, dst, len) == -1) return -1;
plen -= len;
/* consume packet */
while (plen)
{
char lame[1024];
int rd = sizeof(lame);
if (rd > plen) rd = plen;
if (net_read_exact(fd, lame, rd) == -1) return -1;
plen -= rd;
assert(plen >= 0);
}
return len;
}
static int cygwin_read(struct wif * wi,
struct timespec * ts,
int * dlt,
unsigned char * h80211,
int len,
struct rx_info * ri)
{
struct priv_cygwin * pc = wi_priv(wi);
struct rx_info tmp;
int plen;
(void) ts;
(void) dlt;
if (pc->pc_running == -1) return -1;
if (!ri) ri = &tmp;
/* length */
if (net_read_exact(pc->pc_pipe[0], &plen, sizeof(plen)) == -1) return -1;
/* ri */
if (net_read_exact(pc->pc_pipe[0], ri, sizeof(*ri)) == -1) return -1;
plen -= sizeof(*ri);
assert(plen > 0);
return cygwin_read_reader(pc->pc_pipe[0], plen, h80211, len);
}
/**
* Free allocated data
*/
static void do_free(struct wif * wi)
{
struct priv_cygwin * pc = wi_priv(wi);
int tries = 3;
/* wait for reader */
if (pc->pc_running == 1)
{
pc->pc_running = 0;
while ((pc->pc_running != -1) && tries--) sleep(1);
}
if (pc->pc_pipe[0])
{
close(pc->pc_pipe[0]);
close(pc->pc_pipe[1]);
}
if (pc->pc_did_init) pc->pc_close();
assert(wi->wi_priv);
free(wi->wi_priv);
wi->wi_priv = 0;
free(wi);
}
/**
* Close the device and free data
*/
static void cygwin_close(struct wif * wi) { do_free(wi); }
/**
* Get the file descriptor for the device
*/
static int cygwin_fd(struct wif * wi)
{
struct priv_cygwin * pc = wi_priv(wi);
if (pc->pc_running == -1) return -1;
return pc->pc_pipe[0];
}
/**
* Get MAC Address of the device
* @param mac It will contain the mac address
* @return 0 if successful
*/
static int cygwin_get_mac(struct wif * wi, unsigned char * mac)
{
struct priv_cygwin * pc = wi_priv(wi);
return pc->pc_get_mac(mac);
}
/**
* Set MAC Address of the device
* @param mac MAC Address
* @return 0 if successful
*/
static int cygwin_set_mac(struct wif * wi, unsigned char * mac)
{
struct priv_cygwin * pc = wi_priv(wi);
return pc->pc_set_mac(mac);
}
static int cygwin_get_monitor(struct wif * wi)
{
if (wi)
{
} /* XXX unused */
return 0;
}
static int cygwin_get_rate(struct wif * wi)
{
if (wi)
{
} /* XXX unused */
return 1000000;
}
/**
* Set (injection) rate of the device
* @param rate Rate to be used
* @return 0 (successful)
*/
static int cygwin_set_rate(struct wif * wi, int rate)
{
if (wi || rate)
{
} /* XXX unused */
return 0;
}
static void * cygwin_reader(void * arg)
{
struct priv_cygwin * priv = arg;
unsigned char buf[2048];
int len;
struct rx_info ri;
while (priv->pc_running)
{
/* read one packet */
/* a potential problem: the cygwin_read_packet will never return
* if there no packet sniffered, so the thread cannot be closed
* correctly.
*/
len = cygwin_read_packet(priv, buf, sizeof(buf), &ri);
if (len == -1) break;
/* len */
len += sizeof(ri);
if (write(priv->pc_pipe[1], &len, sizeof(len)) != sizeof(len)) break;
len -= sizeof(ri);
/* ri */
if (write(priv->pc_pipe[1], &ri, sizeof(ri)) != sizeof(ri)) break;
/* packet */
if (write(priv->pc_pipe[1], buf, len) != len) break;
}
priv->pc_running = -1;
return NULL;
}
static struct wif * cygwin_open(char * iface)
{
struct wif * wi;
struct priv_cygwin * priv;
/* setup wi struct */
wi = wi_alloc(sizeof(*priv));
if (!wi) return NULL;
wi->wi_read = cygwin_read;
wi->wi_write = cygwin_write;
wi->wi_set_channel = cygwin_set_channel;
wi->wi_get_channel = cygwin_get_channel;
wi->wi_set_freq = cygwin_set_freq;
wi->wi_get_freq = cygwin_get_freq;
wi->wi_close = cygwin_close;
wi->wi_fd = cygwin_fd;
wi->wi_get_mac = cygwin_get_mac;
wi->wi_set_mac = cygwin_set_mac;
wi->wi_get_rate = cygwin_get_rate;
wi->wi_set_rate = cygwin_set_rate;
wi->wi_get_monitor = cygwin_get_monitor;
/* setup iface */
if (do_cygwin_open(wi, iface) == -1) goto err;
/* setup private state */
priv = wi_priv(wi);
priv->pc_wi = wi;
/* setup reader */
if (pipe(priv->pc_pipe) == -1) goto err;
priv->pc_running = 2;
if (pthread_create(&priv->pc_reader, NULL, cygwin_reader, priv)) goto err;
priv->pc_running = 1;
return wi;
err:
do_free(wi);
return NULL;
}
struct wif * wi_open_osdep(char * iface) { return cygwin_open(iface); }
/**
* Return remaining battery time in seconds.
* @return Battery time in seconds or 0 if no battery (or connected to power)
*/
EXPORT int get_battery_state(void)
{
SYSTEM_POWER_STATUS powerStatus;
int batteryTime = 0;
if (GetSystemPowerStatus(&powerStatus) == TRUE)
{
if (powerStatus.ACLineStatus == 0)
batteryTime = (int) powerStatus.BatteryLifeTime;
}
return batteryTime;
}
|