wlantest: Add preliminary version of IEEE 802.11 protocol testing tool
[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 int main(int argc, char *argv[])
38 {
39         int c;
40         const char *read_file = NULL;
41         const char *ifname = NULL;
42         struct wlantest wt;
43
44         wpa_debug_level = MSG_INFO;
45
46         if (os_program_init())
47                 return -1;
48
49         os_memset(&wt, 0, sizeof(wt));
50         wt.monitor_sock = -1;
51
52         for (;;) {
53                 c = getopt(argc, argv, "dhi:r:q");
54                 if (c < 0)
55                         break;
56                 switch (c) {
57                 case 'd':
58                         if (wpa_debug_level > 0)
59                                 wpa_debug_level--;
60                         break;
61                 case 'h':
62                         usage();
63                         return 0;
64                 case 'i':
65                         ifname = optarg;
66                         break;
67                 case 'q':
68                         wpa_debug_level++;
69                         break;
70                 case 'r':
71                         read_file = optarg;
72                         break;
73                 default:
74                         usage();
75                         return -1;
76                 }
77         }
78
79         if (ifname == NULL && read_file == NULL) {
80                 usage();
81                 return 0;
82         }
83
84         if (eloop_init())
85                 return -1;
86
87         if (read_file && read_cap_file(&wt, read_file) < 0)
88                 return -1;
89
90         if (ifname && monitor_init(&wt, ifname) < 0)
91                 return -1;
92
93         eloop_register_signal_terminate(wlantest_terminate, &wt);
94
95         eloop_run();
96
97         wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
98                    "fcs_error=%u",
99                    wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
100
101         if (ifname)
102                 monitor_deinit(&wt);
103
104         eloop_destroy();
105         os_program_deinit();
106
107         return 0;
108 }