wlantest.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * wlantest - IEEE 802.11 protocol monitoring and testing tool
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "wlantest.h"
  18. extern int wpa_debug_level;
  19. extern int wpa_debug_show_keys;
  20. static void wlantest_terminate(int sig, void *signal_ctx)
  21. {
  22. eloop_terminate();
  23. }
  24. static void usage(void)
  25. {
  26. printf("wlantest [-ddhqq] [-i<ifname>] [-r<pcap file>] "
  27. "[-p<passphrase>]\n");
  28. }
  29. static void passphrase_deinit(struct wlantest_passphrase *p)
  30. {
  31. dl_list_del(&p->list);
  32. os_free(p);
  33. }
  34. static void wlantest_init(struct wlantest *wt)
  35. {
  36. os_memset(wt, 0, sizeof(*wt));
  37. wt->monitor_sock = -1;
  38. dl_list_init(&wt->passphrase);
  39. dl_list_init(&wt->bss);
  40. }
  41. static void wlantest_deinit(struct wlantest *wt)
  42. {
  43. struct wlantest_bss *bss, *n;
  44. struct wlantest_passphrase *p, *pn;
  45. if (wt->monitor_sock >= 0)
  46. monitor_deinit(wt);
  47. dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
  48. bss_deinit(bss);
  49. dl_list_for_each_safe(p, pn, &wt->passphrase,
  50. struct wlantest_passphrase, list)
  51. passphrase_deinit(p);
  52. }
  53. static void add_passphrase(struct wlantest *wt, const char *passphrase)
  54. {
  55. struct wlantest_passphrase *p;
  56. size_t len = os_strlen(passphrase);
  57. if (len < 8 || len > 63)
  58. return;
  59. p = os_zalloc(sizeof(*p));
  60. if (p == NULL)
  61. return;
  62. os_memcpy(p->passphrase, passphrase, len);
  63. dl_list_add(&wt->passphrase, &p->list);
  64. }
  65. int main(int argc, char *argv[])
  66. {
  67. int c;
  68. const char *read_file = NULL;
  69. const char *ifname = NULL;
  70. struct wlantest wt;
  71. wpa_debug_level = MSG_INFO;
  72. wpa_debug_show_keys = 1;
  73. if (os_program_init())
  74. return -1;
  75. wlantest_init(&wt);
  76. for (;;) {
  77. c = getopt(argc, argv, "dhi:p:qr:");
  78. if (c < 0)
  79. break;
  80. switch (c) {
  81. case 'd':
  82. if (wpa_debug_level > 0)
  83. wpa_debug_level--;
  84. break;
  85. case 'h':
  86. usage();
  87. return 0;
  88. case 'i':
  89. ifname = optarg;
  90. break;
  91. case 'p':
  92. add_passphrase(&wt, optarg);
  93. break;
  94. case 'q':
  95. wpa_debug_level++;
  96. break;
  97. case 'r':
  98. read_file = optarg;
  99. break;
  100. default:
  101. usage();
  102. return -1;
  103. }
  104. }
  105. if (ifname == NULL && read_file == NULL) {
  106. usage();
  107. return 0;
  108. }
  109. if (eloop_init())
  110. return -1;
  111. if (read_file && read_cap_file(&wt, read_file) < 0)
  112. return -1;
  113. if (ifname && monitor_init(&wt, ifname) < 0)
  114. return -1;
  115. eloop_register_signal_terminate(wlantest_terminate, &wt);
  116. eloop_run();
  117. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  118. "fcs_error=%u",
  119. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  120. wlantest_deinit(&wt);
  121. eloop_destroy();
  122. os_program_deinit();
  123. return 0;
  124. }