9856368a747aa07f835c55491eb83ebf5e176a55
[mech_eap.git] / wlantest / wlantest.c
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
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "wlantest.h"
20
21
22 extern int wpa_debug_level;
23
24
25 static void wlantest_terminate(int sig, void *signal_ctx)
26 {
27         eloop_terminate();
28 }
29
30
31 static void usage(void)
32 {
33         printf("wlantest [-ddhqq] [-i<ifname>] [-r<pcap file>]\n");
34 }
35
36
37 static void wlantest_init(struct wlantest *wt)
38 {
39         os_memset(wt, 0, sizeof(*wt));
40         wt->monitor_sock = -1;
41         dl_list_init(&wt->bss);
42 }
43
44
45 static void wlantest_deinit(struct wlantest *wt)
46 {
47         struct wlantest_bss *bss, *n;
48         if (wt->monitor_sock >= 0)
49                 monitor_deinit(wt);
50         dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
51                 bss_deinit(bss);
52 }
53
54
55 int main(int argc, char *argv[])
56 {
57         int c;
58         const char *read_file = NULL;
59         const char *ifname = NULL;
60         struct wlantest wt;
61
62         wpa_debug_level = MSG_INFO;
63
64         if (os_program_init())
65                 return -1;
66
67         wlantest_init(&wt);
68
69         for (;;) {
70                 c = getopt(argc, argv, "dhi:r:q");
71                 if (c < 0)
72                         break;
73                 switch (c) {
74                 case 'd':
75                         if (wpa_debug_level > 0)
76                                 wpa_debug_level--;
77                         break;
78                 case 'h':
79                         usage();
80                         return 0;
81                 case 'i':
82                         ifname = optarg;
83                         break;
84                 case 'q':
85                         wpa_debug_level++;
86                         break;
87                 case 'r':
88                         read_file = optarg;
89                         break;
90                 default:
91                         usage();
92                         return -1;
93                 }
94         }
95
96         if (ifname == NULL && read_file == NULL) {
97                 usage();
98                 return 0;
99         }
100
101         if (eloop_init())
102                 return -1;
103
104         if (read_file && read_cap_file(&wt, read_file) < 0)
105                 return -1;
106
107         if (ifname && monitor_init(&wt, ifname) < 0)
108                 return -1;
109
110         eloop_register_signal_terminate(wlantest_terminate, &wt);
111
112         eloop_run();
113
114         wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
115                    "fcs_error=%u",
116                    wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
117
118         wlantest_deinit(&wt);
119
120         eloop_destroy();
121         os_program_deinit();
122
123         return 0;
124 }