about summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt72
-rw-r--r--test/README.md69
-rw-r--r--test/pcaps/assoc_req.pcapbin4374 -> 0 bytes
-rw-r--r--test/pcaps/assoc_resp.pcapbin3924 -> 0 bytes
-rw-r--r--test/pcaps/auth.pcapbin6524 -> 0 bytes
-rw-r--r--test/pcaps/beacons_with_221.pcapbin7674 -> 0 bytes
-rw-r--r--test/pcaps/deauth.pcapbin3074 -> 0 bytes
-rw-r--r--test/pcaps/deauth_with_221.pcapbin5024 -> 0 bytes
-rw-r--r--test/pcaps/disassoc.pcapbin3074 -> 0 bytes
-rw-r--r--test/pcaps/probe_req.pcapbin4074 -> 0 bytes
-rw-r--r--test/pcaps/probe_resp_with_221.pcapbin7874 -> 0 bytes
-rw-r--r--test/pcaps/reassoc_req.pcapbin4674 -> 0 bytes
-rw-r--r--test/pcaps/reassoc_resp.pcapbin3924 -> 0 bytes
-rw-r--r--test/src/.clang-format8
-rw-r--r--test/src/action_tests.c87
-rw-r--r--test/src/assoc_req_tests.c91
-rw-r--r--test/src/assoc_resp_tests.c91
-rw-r--r--test/src/atim_tests.c35
-rw-r--r--test/src/auth_tests.c91
-rw-r--r--test/src/beacon_tests.c140
-rw-r--r--test/src/deauth_tests.c91
-rw-r--r--test/src/disassoc_tests.c91
-rw-r--r--test/src/helpers.c31
-rw-r--r--test/src/helpers.h60
-rw-r--r--test/src/probe_req_tests.c91
-rw-r--r--test/src/probe_resp_tests.c91
-rw-r--r--test/src/reassoc_req_tests.c91
-rw-r--r--test/src/reassoc_resp_tests.c91
-rw-r--r--test/src/test_generation.c924
-rw-r--r--test/src/test_misc.c30
-rw-r--r--test/src/test_parsing.c613
-rw-r--r--test/src/timing_ad_tests.c57
32 files changed, 1273 insertions, 1672 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b418f93..64f8f36 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt
@@ -7,10 +7,70 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
7 7
8set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") 8set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0")
9 9
10add_executable(test_misc src/helpers.c src/test_misc.c) 10link_directories("../build/")
11add_executable(test_generation src/helpers.c src/test_generation.c)
12add_executable(test_parsing src/helpers.c src/test_parsing.c)
13 11
14target_link_libraries(test_misc wifi) 12add_executable(action_tests src/action_tests.c)
15target_link_libraries(test_generation wifi pcap) 13target_link_libraries(action_tests wifi)
16target_link_libraries(test_parsing wifi pcap) 14add_executable(assoc_req_tests src/assoc_req_tests.c)
15target_link_libraries(assoc_req_tests wifi)
16add_executable(assoc_resp_tests src/assoc_resp_tests.c)
17target_link_libraries(assoc_resp_tests wifi)
18add_executable(atim_tests src/atim_tests.c)
19target_link_libraries(atim_tests wifi)
20add_executable(auth_tests src/auth_tests.c)
21target_link_libraries(auth_tests wifi)
22add_executable(beacon_tests src/beacon_tests.c)
23target_link_libraries(beacon_tests wifi)
24add_executable(deauth_tests src/deauth_tests.c)
25target_link_libraries(deauth_tests wifi)
26add_executable(disassoc_tests src/disassoc_tests.c)
27target_link_libraries(disassoc_tests wifi)
28add_executable(probe_req_tests src/probe_req_tests.c)
29target_link_libraries(probe_req_tests wifi)
30add_executable(probe_resp_tests src/probe_resp_tests.c)
31target_link_libraries(probe_resp_tests wifi)
32add_executable(reassoc_req_tests src/reassoc_req_tests.c)
33target_link_libraries(reassoc_req_tests wifi)
34add_executable(reassoc_resp_tests src/reassoc_resp_tests.c)
35target_link_libraries(reassoc_resp_tests wifi)
36add_executable(timing_ad_tests src/timing_ad_tests.c)
37target_link_libraries(timing_ad_tests wifi)
38
39enable_testing()
40add_test(NAME test_action_gen_full COMMAND action_tests --action-gen-full)
41add_test(NAME test_action_gen_details COMMAND action_tests --action-gen-details)
42
43add_test(NAME test_assoc_req_gen_full COMMAND assoc_req_tests --assoc_req-gen-full)
44add_test(NAME test_assoc_req_gen_tags COMMAND assoc_req_tests --assoc_req-gen-tags)
45
46add_test(NAME test_assoc_resp_gen_full COMMAND assoc_resp_tests --assoc_resp-gen-full)
47add_test(NAME test_assoc_resp_gen_tags COMMAND assoc_resp_tests --assoc_resp-gen-tags)
48
49add_test(NAME test_atim_gen_full COMMAND atim_tests --atim-gen-full)
50
51add_test(NAME test_auth_gen_full COMMAND auth_tests --auth-gen-full)
52add_test(NAME test_auth_gen_tags COMMAND auth_tests --auth-gen-tags)
53
54add_test(NAME test_beacon_gen_full COMMAND beacon_tests --beacon-gen-full)
55add_test(NAME test_beacon_gen_tags COMMAND beacon_tests --beacon-gen-tags)
56add_test(NAME test_beacon_parse COMMAND beacon_tests --beacon-parse)
57
58add_test(NAME test_deauth_gen_full COMMAND deauth_tests --deauth-gen-full)
59add_test(NAME test_deauth_gen_tags COMMAND deauth_tests --deauth-gen-tags)
60
61add_test(NAME test_disassoc_gen_full COMMAND disassoc_tests --disassoc-gen-full)
62add_test(NAME test_disassoc_gen_tags COMMAND disassoc_tests --disassoc-gen-tags)
63
64add_test(NAME test_probe_req_gen_full COMMAND probe_req_tests --probe_req-gen-full)
65add_test(NAME test_probe_req_gen_tags COMMAND probe_req_tests --probe_req-gen-tags)
66
67add_test(NAME test_probe_resp_gen_full COMMAND probe_resp_tests --probe_resp-gen-full)
68add_test(NAME test_probe_resp_gen_tags COMMAND probe_resp_tests --probe_resp-gen-tags)
69
70add_test(NAME test_reassoc_req_gen_full COMMAND reassoc_req_tests --reassoc_req-gen-full)
71add_test(NAME test_reassoc_req_gen_tags COMMAND reassoc_req_tests --reassoc_req-gen-tags)
72
73add_test(NAME test_reassoc_resp_gen_full COMMAND reassoc_resp_tests --reassoc_resp-gen-full)
74add_test(NAME test_reassoc_resp_gen_tags COMMAND reassoc_resp_tests --reassoc_resp-gen-tags)
75
76add_test(NAME test_timing_ad_gen_tags COMMAND timing_ad_tests --timing_ad-gen-full)
diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..402fa87 --- /dev/null +++ b/test/README.md
@@ -0,0 +1,69 @@
1# libwifi Tests
2libwifi uses CMakes testing functionalities. The tests are in the `src/` directory, and can be used with `make test`.
3
4## Running Tests
5```
6>> mkdir build
7>> cd build
8>> cmake ..
9>> make && make test
10```
11
12## Expected Output
13```
14>> make test
15Running tests...
16Test project libwifi/test/build
17 Start 1: test_action_gen_full
18 1/24 Test #1: test_action_gen_full ............. Passed 0.00 sec
19 Start 2: test_action_gen_details
20 2/24 Test #2: test_action_gen_details .......... Passed 0.00 sec
21 Start 3: test_assoc_req_gen_full
22 3/24 Test #3: test_assoc_req_gen_full .......... Passed 0.00 sec
23 Start 4: test_assoc_req_gen_tags
24 4/24 Test #4: test_assoc_req_gen_tags .......... Passed 0.00 sec
25 Start 5: test_assoc_resp_gen_full
26 5/24 Test #5: test_assoc_resp_gen_full ......... Passed 0.00 sec
27 Start 6: test_assoc_resp_gen_tags
28 6/24 Test #6: test_assoc_resp_gen_tags ......... Passed 0.00 sec
29 Start 7: test_atim_gen_full
30 7/24 Test #7: test_atim_gen_full ............... Passed 0.00 sec
31 Start 8: test_auth_gen_full
32 8/24 Test #8: test_auth_gen_full ............... Passed 0.00 sec
33 Start 9: test_auth_gen_tags
34 9/24 Test #9: test_auth_gen_tags ............... Passed 0.00 sec
35 Start 10: test_beacon_gen_full
3610/24 Test #10: test_beacon_gen_full ............. Passed 0.00 sec
37 Start 11: test_beacon_gen_tags
3811/24 Test #11: test_beacon_gen_tags ............. Passed 0.00 sec
39 Start 12: test_deauth_gen_full
4012/24 Test #12: test_deauth_gen_full ............. Passed 0.00 sec
41 Start 13: test_deauth_gen_tags
4213/24 Test #13: test_deauth_gen_tags ............. Passed 0.00 sec
43 Start 14: test_disassoc_gen_full
4414/24 Test #14: test_disassoc_gen_full ........... Passed 0.00 sec
45 Start 15: test_disassoc_gen_tags
4615/24 Test #15: test_disassoc_gen_tags ........... Passed 0.00 sec
47 Start 16: test_probe_req_gen_full
4816/24 Test #16: test_probe_req_gen_full .......... Passed 0.00 sec
49 Start 17: test_probe_req_gen_tags
5017/24 Test #17: test_probe_req_gen_tags .......... Passed 0.00 sec
51 Start 18: test_probe_resp_gen_full
5218/24 Test #18: test_probe_resp_gen_full ......... Passed 0.00 sec
53 Start 19: test_probe_resp_gen_tags
5419/24 Test #19: test_probe_resp_gen_tags ......... Passed 0.00 sec
55 Start 20: test_reassoc_req_gen_full
5620/24 Test #20: test_reassoc_req_gen_full ........ Passed 0.00 sec
57 Start 21: test_reassoc_req_gen_tags
5821/24 Test #21: test_reassoc_req_gen_tags ........ Passed 0.00 sec
59 Start 22: test_reassoc_resp_gen_full
6022/24 Test #22: test_reassoc_resp_gen_full ....... Passed 0.00 sec
61 Start 23: test_reassoc_resp_gen_tags
6223/24 Test #23: test_reassoc_resp_gen_tags ....... Passed 0.00 sec
63 Start 24: test_timing_ad_gen_tags
6424/24 Test #24: test_timing_ad_gen_tags .......... Passed 0.00 sec
65
66100% tests passed, 0 tests failed out of 24
67
68Total Test time (real) = 0.06 sec
69```
diff --git a/test/pcaps/assoc_req.pcap b/test/pcaps/assoc_req.pcap deleted file mode 100644 index 87ad19b..0000000 --- a/test/pcaps/assoc_req.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/assoc_resp.pcap b/test/pcaps/assoc_resp.pcap deleted file mode 100644 index 16f6fad..0000000 --- a/test/pcaps/assoc_resp.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/auth.pcap b/test/pcaps/auth.pcap deleted file mode 100644 index e667432..0000000 --- a/test/pcaps/auth.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/beacons_with_221.pcap b/test/pcaps/beacons_with_221.pcap deleted file mode 100644 index 5cc20ff..0000000 --- a/test/pcaps/beacons_with_221.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/deauth.pcap b/test/pcaps/deauth.pcap deleted file mode 100644 index c8fb77c..0000000 --- a/test/pcaps/deauth.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/deauth_with_221.pcap b/test/pcaps/deauth_with_221.pcap deleted file mode 100644 index ba53dd1..0000000 --- a/test/pcaps/deauth_with_221.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/disassoc.pcap b/test/pcaps/disassoc.pcap deleted file mode 100644 index f217150..0000000 --- a/test/pcaps/disassoc.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/probe_req.pcap b/test/pcaps/probe_req.pcap deleted file mode 100644 index b039e4b..0000000 --- a/test/pcaps/probe_req.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/probe_resp_with_221.pcap b/test/pcaps/probe_resp_with_221.pcap deleted file mode 100644 index 3d664b0..0000000 --- a/test/pcaps/probe_resp_with_221.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/reassoc_req.pcap b/test/pcaps/reassoc_req.pcap deleted file mode 100644 index f5bd99e..0000000 --- a/test/pcaps/reassoc_req.pcap +++ /dev/null
Binary files differ
diff --git a/test/pcaps/reassoc_resp.pcap b/test/pcaps/reassoc_resp.pcap deleted file mode 100644 index c3e657f..0000000 --- a/test/pcaps/reassoc_resp.pcap +++ /dev/null
Binary files differ
diff --git a/test/src/.clang-format b/test/src/.clang-format deleted file mode 100644 index 111249f..0000000 --- a/test/src/.clang-format +++ /dev/null
@@ -1,8 +0,0 @@
1---
2BasedOnStyle: LLVM
3IndentWidth: '4'
4SpaceAfterCStyleCast: 'true'
5ColumnLimit: 400
6AllowShortFunctionsOnASingleLine: None
7IndentCaseLabels: 'true'
8...
diff --git a/test/src/action_tests.c b/test/src/action_tests.c new file mode 100644 index 0000000..997095d --- /dev/null +++ b/test/src/action_tests.c
@@ -0,0 +1,87 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_action_gen_full() {
13 struct libwifi_action action = {0};
14
15 int ret = libwifi_create_action(&action, bcast, to, ACTION_HT);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create action: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int action_len = libwifi_get_action_length(&action);
22 if (action_len <= 0) {
23 fprintf(stderr, "Invalid action length: %d\n", action_len);
24 return action_len;
25 }
26
27 unsigned char *buf = malloc(action_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_action(&action, buf, action_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump action\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_action_add_detail() {
43 struct libwifi_action action = {0};
44
45 int ret = libwifi_create_action(&action, bcast, to, ACTION_HT);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create action: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_add_action_detail(&action.fixed_parameters.details, (const unsigned char *) "HELLO WORLD", strlen("HELLO WORLD"));
52
53 int action_len = libwifi_get_action_length(&action);
54 if (action_len <= 0) {
55 fprintf(stderr, "Invalid action length: %d\n", action_len);
56 return action_len;
57 }
58
59 unsigned char *buf = malloc(action_len);
60 if (buf == NULL) {
61 fprintf(stderr, "Failed to allocate buffer\n");
62 return -1;
63 }
64
65 int dump_len = libwifi_dump_action(&action, buf, action_len);
66 if (dump_len <= 0) {
67 fprintf(stderr, "Failed to dump action\n");
68 return ret;
69 }
70
71 return 0;
72}
73
74int main(int argc, char **argv) {
75 if (argc < 2) {
76 printf("Specify test\n");
77 return -1;
78 }
79
80 if (strcmp(argv[1], "--action-gen-full") == 0) {
81 return test_action_gen_full();
82 } else if (strcmp(argv[1], "--action-gen-details") == 0) {
83 return test_action_add_detail();
84 }
85
86 return -1;
87}
diff --git a/test/src/assoc_req_tests.c b/test/src/assoc_req_tests.c new file mode 100644 index 0000000..fc6379f --- /dev/null +++ b/test/src/assoc_req_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_assoc_req_gen_full() {
13 struct libwifi_assoc_req assoc_req = {0};
14
15 int ret = libwifi_create_assoc_req(&assoc_req, bcast, to, "Some SSID", 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create assoc_req: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int assoc_req_len = libwifi_get_assoc_req_length(&assoc_req);
22 if (assoc_req_len <= 0) {
23 fprintf(stderr, "Invalid assoc_req length: %d\n", assoc_req_len);
24 return assoc_req_len;
25 }
26
27 unsigned char *buf = malloc(assoc_req_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_assoc_req(&assoc_req, buf, assoc_req_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump assoc_req\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_assoc_req_add_tag() {
43 struct libwifi_assoc_req assoc_req = {0};
44
45 int ret = libwifi_create_assoc_req(&assoc_req, bcast, to, "Some SSID", 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create assoc_req: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&assoc_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add assoc_req tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int assoc_req_len = libwifi_get_assoc_req_length(&assoc_req);
58 if (assoc_req_len <= 0) {
59 fprintf(stderr, "Invalid assoc_req length: %d\n", assoc_req_len);
60 return assoc_req_len;
61 }
62
63 unsigned char *buf = malloc(assoc_req_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_assoc_req(&assoc_req, buf, assoc_req_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump assoc_req\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--assoc_req-gen-full") == 0) {
85 return test_assoc_req_gen_full();
86 } else if (strcmp(argv[1], "--assoc_req-gen-tags") == 0) {
87 return test_assoc_req_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/assoc_resp_tests.c b/test/src/assoc_resp_tests.c new file mode 100644 index 0000000..3a261ed --- /dev/null +++ b/test/src/assoc_resp_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_assoc_resp_gen_full() {
13 struct libwifi_assoc_resp assoc_resp = {0};
14
15 int ret = libwifi_create_assoc_resp(&assoc_resp, bcast, to, 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create assoc_resp: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int assoc_resp_len = libwifi_get_assoc_resp_length(&assoc_resp);
22 if (assoc_resp_len <= 0) {
23 fprintf(stderr, "Invalid assoc_resp length: %d\n", assoc_resp_len);
24 return assoc_resp_len;
25 }
26
27 unsigned char *buf = malloc(assoc_resp_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_assoc_resp(&assoc_resp, buf, assoc_resp_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump assoc_resp\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_assoc_resp_add_tag() {
43 struct libwifi_assoc_resp assoc_resp = {0};
44
45 int ret = libwifi_create_assoc_resp(&assoc_resp, bcast, to, 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create assoc_resp: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&assoc_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add assoc_resp tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int assoc_resp_len = libwifi_get_assoc_resp_length(&assoc_resp);
58 if (assoc_resp_len <= 0) {
59 fprintf(stderr, "Invalid assoc_resp length: %d\n", assoc_resp_len);
60 return assoc_resp_len;
61 }
62
63 unsigned char *buf = malloc(assoc_resp_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_assoc_resp(&assoc_resp, buf, assoc_resp_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump assoc_resp\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--assoc_resp-gen-full") == 0) {
85 return test_assoc_resp_gen_full();
86 } else if (strcmp(argv[1], "--assoc_resp-gen-tags") == 0) {
87 return test_assoc_resp_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/atim_tests.c b/test/src/atim_tests.c new file mode 100644 index 0000000..43f2265 --- /dev/null +++ b/test/src/atim_tests.c
@@ -0,0 +1,35 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_atim_gen_full() {
13 struct libwifi_atim atim = {0};
14
15 int ret = libwifi_create_atim(&atim, bcast, to, to);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create atim: %s\n", strerror(ret));
18 return ret;
19 }
20
21 return 0;
22}
23
24int main(int argc, char **argv) {
25 if (argc < 2) {
26 printf("Specify test\n");
27 return -1;
28 }
29
30 if (strcmp(argv[1], "--atim-gen-full") == 0) {
31 return test_atim_gen_full();
32 }
33
34 return -1;
35}
diff --git a/test/src/auth_tests.c b/test/src/auth_tests.c new file mode 100644 index 0000000..f78aeed --- /dev/null +++ b/test/src/auth_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_auth_gen_full() {
13 struct libwifi_auth auth = {0};
14
15 int ret = libwifi_create_auth(&auth, bcast, to, 0, 100, STATUS_SUCCESS);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create auth: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int auth_len = libwifi_get_auth_length(&auth);
22 if (auth_len <= 0) {
23 fprintf(stderr, "Invalid auth length: %d\n", auth_len);
24 return auth_len;
25 }
26
27 unsigned char *buf = malloc(auth_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_auth(&auth, buf, auth_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump auth\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_auth_add_tag() {
43 struct libwifi_auth auth = {0};
44
45 int ret = libwifi_create_auth(&auth, bcast, to, 0, 100, STATUS_SUCCESS);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create auth: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&auth.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add auth tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int auth_len = libwifi_get_auth_length(&auth);
58 if (auth_len <= 0) {
59 fprintf(stderr, "Invalid auth length: %d\n", auth_len);
60 return auth_len;
61 }
62
63 unsigned char *buf = malloc(auth_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_auth(&auth, buf, auth_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump auth\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--auth-gen-full") == 0) {
85 return test_auth_gen_full();
86 } else if (strcmp(argv[1], "--auth-gen-tags") == 0) {
87 return test_auth_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/beacon_tests.c b/test/src/beacon_tests.c new file mode 100644 index 0000000..d152d5e --- /dev/null +++ b/test/src/beacon_tests.c
@@ -0,0 +1,140 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12const unsigned char beacon[] = "\x00\x00\x18\x00\x8e\x58\x00\x00\x10\x02\x6c\x09\xa0\x00\x54\x00" \
13 "\x00\x2b\x00\x00\x9f\x61\xc9\x5c\x80\x00\x00\x00\xff\xff\xff\xff" \
14 "\xff\xff\x00\x0c\x41\x82\xb2\x55\x00\x0c\x41\x82\xb2\x55\x50\xf8" \
15 "\x89\xf1\xd4\x1b\x01\x00\x00\x00\x64\x00\x11\x04\x00\x07\x43\x6f" \
16 "\x68\x65\x72\x65\x72\x01\x08\x82\x84\x8b\x96\x24\x30\x48\x6c\x03" \
17 "\x01\x01\x05\x04\x00\x01\x00\x00\x2a\x01\x02\x2f\x01\x02\x30\x18" \
18 "\x01\x00\x00\x0f\xac\x02\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x02" \
19 "\x01\x00\x00\x0f\xac\x02\x00\x00\x32\x04\x0c\x12\x18\x60\xdd\x06" \
20 "\x00\x10\x18\x02\x00\x04\xdd\x1c\x00\x50\xf2\x01\x01\x00\x00\x50" \
21 "\xf2\x02\x02\x00\x00\x50\xf2\x04\x00\x50\xf2\x02\x01\x00\x00\x50" \
22 "\xf2\x02\x00\x00\x9f\x61\xc9\x5c";
23
24int test_beacon_gen_full() {
25 struct libwifi_beacon beacon = {0};
26
27 int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11);
28 if (ret != 0) {
29 fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret));
30 return ret;
31 }
32
33 int beacon_len = libwifi_get_beacon_length(&beacon);
34 if (beacon_len <= 0) {
35 fprintf(stderr, "Invalid beacon length: %d\n", beacon_len);
36 return beacon_len;
37 }
38
39 unsigned char *buf = malloc(beacon_len);
40 if (buf == NULL) {
41 fprintf(stderr, "Failed to allocate buffer\n");
42 return -1;
43 }
44
45 int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len);
46 if (dump_len <= 0) {
47 fprintf(stderr, "Failed to dump beacon\n");
48 return ret;
49 }
50
51 return 0;
52}
53
54int test_beacon_add_tag() {
55 struct libwifi_beacon beacon = {0};
56
57 int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11);
58 if (ret != 0) {
59 fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret));
60 return ret;
61 }
62
63 ret = libwifi_quick_add_tag(&beacon.tags,
64 TAG_VENDOR_SPECIFIC,
65 (const unsigned char *) "\x00\x11\x22\xAAHello World",
66 15);
67 if (ret != 0) {
68 fprintf(stderr, "Failed to add beacon tagged parameter: %s\n", strerror(ret));
69 return ret;
70 }
71
72 int beacon_len = libwifi_get_beacon_length(&beacon);
73 if (beacon_len <= 0) {
74 fprintf(stderr, "Invalid beacon length: %d\n", beacon_len);
75 return beacon_len;
76 }
77
78 unsigned char *buf = malloc(beacon_len);
79 if (buf == NULL) {
80 fprintf(stderr, "Failed to allocate buffer\n");
81 return -1;
82 }
83
84 int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len);
85 if (dump_len <= 0) {
86 fprintf(stderr, "Failed to dump beacon\n");
87 return ret;
88 }
89
90 return 0;
91}
92
93int test_beacon_parse() {
94 struct libwifi_frame frame = {0};
95 struct libwifi_bss bss = {0};
96
97 int ret = libwifi_get_wifi_frame(&frame, beacon, sizeof(beacon), 1);
98 if (ret != 0) {
99 return ret;
100 }
101
102 if (frame.frame_control.type != TYPE_MANAGEMENT || frame.frame_control.subtype != SUBTYPE_BEACON) {
103 return -1;
104 }
105
106 ret = libwifi_parse_beacon(&bss, &frame);
107 if (ret != 0) {
108 return ret;
109 }
110
111 if (strcmp(bss.ssid, "Coherer") != 0) {
112 return -2;
113 }
114
115 if (bss.channel != 1) {
116 return -3;
117 }
118
119 libwifi_free_bss(&bss);
120 libwifi_free_wifi_frame(&frame);
121
122 return 0;
123}
124
125int main(int argc, char **argv) {
126 if (argc < 2) {
127 printf("Specify test\n");
128 return -1;
129 }
130
131 if (strcmp(argv[1], "--beacon-gen-full") == 0) {
132 return test_beacon_gen_full();
133 } else if (strcmp(argv[1], "--beacon-gen-tags") == 0) {
134 return test_beacon_add_tag();
135 } else if (strcmp(argv[1], "--beacon-parse") == 0) {
136 return test_beacon_parse();
137 }
138
139 return -1;
140}
diff --git a/test/src/deauth_tests.c b/test/src/deauth_tests.c new file mode 100644 index 0000000..9033574 --- /dev/null +++ b/test/src/deauth_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_deauth_gen_full() {
13 struct libwifi_deauth deauth = {0};
14
15 int ret = libwifi_create_deauth(&deauth, bcast, to, REASON_STA_LEAVING);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create deauth: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int deauth_len = libwifi_get_deauth_length(&deauth);
22 if (deauth_len <= 0) {
23 fprintf(stderr, "Invalid deauth length: %d\n", deauth_len);
24 return deauth_len;
25 }
26
27 unsigned char *buf = malloc(deauth_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_deauth(&deauth, buf, deauth_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump deauth\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_deauth_add_tag() {
43 struct libwifi_deauth deauth = {0};
44
45 int ret = libwifi_create_deauth(&deauth, bcast, to, REASON_STA_LEAVING);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create deauth: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&deauth.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add deauth tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int deauth_len = libwifi_get_deauth_length(&deauth);
58 if (deauth_len <= 0) {
59 fprintf(stderr, "Invalid deauth length: %d\n", deauth_len);
60 return deauth_len;
61 }
62
63 unsigned char *buf = malloc(deauth_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_deauth(&deauth, buf, deauth_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump deauth\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--deauth-gen-full") == 0) {
85 return test_deauth_gen_full();
86 } else if (strcmp(argv[1], "--deauth-gen-tags") == 0) {
87 return test_deauth_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/disassoc_tests.c b/test/src/disassoc_tests.c new file mode 100644 index 0000000..c5e27de --- /dev/null +++ b/test/src/disassoc_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_disassoc_gen_full() {
13 struct libwifi_disassoc disassoc = {0};
14
15 int ret = libwifi_create_disassoc(&disassoc, bcast, to, REASON_STA_LEAVING);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create disassoc: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int disassoc_len = libwifi_get_disassoc_length(&disassoc);
22 if (disassoc_len <= 0) {
23 fprintf(stderr, "Invalid disassoc length: %d\n", disassoc_len);
24 return disassoc_len;
25 }
26
27 unsigned char *buf = malloc(disassoc_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_disassoc(&disassoc, buf, disassoc_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump disassoc\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_disassoc_add_tag() {
43 struct libwifi_disassoc disassoc = {0};
44
45 int ret = libwifi_create_disassoc(&disassoc, bcast, to, REASON_STA_LEAVING);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create disassoc: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&disassoc.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add disassoc tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int disassoc_len = libwifi_get_disassoc_length(&disassoc);
58 if (disassoc_len <= 0) {
59 fprintf(stderr, "Invalid disassoc length: %d\n", disassoc_len);
60 return disassoc_len;
61 }
62
63 unsigned char *buf = malloc(disassoc_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_disassoc(&disassoc, buf, disassoc_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump disassoc\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--disassoc-gen-full") == 0) {
85 return test_disassoc_gen_full();
86 } else if (strcmp(argv[1], "--disassoc-gen-tags") == 0) {
87 return test_disassoc_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/helpers.c b/test/src/helpers.c deleted file mode 100644 index 9fc9d0b..0000000 --- a/test/src/helpers.c +++ /dev/null
@@ -1,31 +0,0 @@
1#include "helpers.h"
2#include <stdio.h>
3
4void hexdump(void *data, size_t size) {
5 char ascii[17];
6 size_t i, j;
7 ascii[16] = '\0';
8 for (i = 0; i < size; ++i) {
9 printf("%02X ", ((unsigned char *) data)[i]);
10 if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
11 ascii[i % 16] = ((unsigned char *) data)[i];
12 } else {
13 ascii[i % 16] = '.';
14 }
15 if ((i + 1) % 8 == 0 || i + 1 == size) {
16 printf(" ");
17 if ((i + 1) % 16 == 0) {
18 printf("| %s \n", ascii);
19 } else if (i + 1 == size) {
20 ascii[(i + 1) % 16] = '\0';
21 if ((i + 1) % 16 <= 8) {
22 printf(" ");
23 }
24 for (j = (i + 1) % 16; j < 16; ++j) {
25 printf(" ");
26 }
27 printf("| %s \n", ascii);
28 }
29 }
30 }
31}
diff --git a/test/src/helpers.h b/test/src/helpers.h deleted file mode 100644 index 99a5329..0000000 --- a/test/src/helpers.h +++ /dev/null
@@ -1,60 +0,0 @@
1#include <stdint.h>
2#include <sys/types.h>
3
4#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
5#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
6
7static const uint8_t radiotap_data[] = {
8 0x00,
9 0x00, // <-- radiotap version (ignore this)
10 0x18,
11 0x00, // <-- number of bytes in our header (count the number of "0x"s)
12
13 /**
14 * The next field is a bitmap of which options we are including.
15 * The full list of which field is which option is in ieee80211_radiotap.h,
16 * but I've chosen to include:
17 * 0x00 0x01: timestamp
18 * 0x00 0x02: flags
19 * 0x00 0x03: rate
20 * 0x00 0x04: channel
21 * 0x80 0x00: tx flags (seems silly to have this AND flags, but oh well)
22 */
23 0x0f,
24 0x80,
25 0x00,
26 0x00,
27
28 0x00,
29 0x00,
30 0x00,
31 0x00,
32 0x00,
33 0x00,
34 0x00,
35 0x00, // <-- timestamp
36
37 /**
38 * This is the first set of flags, and we've set the bit corresponding to
39 * IEEE80211_RADIOTAP_F_FCS, meaning we want the card to add a FCS at the
40 * end of our buffer for us.
41 */
42 0x10,
43
44 0x00, // <-- rate
45 0x00,
46 0x00,
47 0x00,
48 0x00, // <-- channel
49
50 /**
51 * This is the second set of flags, specifically related to transmissions.
52 * The bit we've set is IEEE80211_RADIOTAP_F_TX_NOACK, which means the card
53 * won't wait for an ACK for this frame, and that it won't retry if it
54 * doesn't get one.
55 */
56 0x08,
57 0x00,
58};
59
60void hexdump(void *data, size_t size);
diff --git a/test/src/probe_req_tests.c b/test/src/probe_req_tests.c new file mode 100644 index 0000000..553c9d9 --- /dev/null +++ b/test/src/probe_req_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_probe_req_gen_full() {
13 struct libwifi_probe_req probe_req = {0};
14
15 int ret = libwifi_create_probe_req(&probe_req, bcast, to, to, "Some SSID", 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create probe_req: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int probe_req_len = libwifi_get_probe_req_length(&probe_req);
22 if (probe_req_len <= 0) {
23 fprintf(stderr, "Invalid probe_req length: %d\n", probe_req_len);
24 return probe_req_len;
25 }
26
27 unsigned char *buf = malloc(probe_req_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_probe_req(&probe_req, buf, probe_req_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump probe_req\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_probe_req_add_tag() {
43 struct libwifi_probe_req probe_req = {0};
44
45 int ret = libwifi_create_probe_req(&probe_req, bcast, to, to, "Some SSID", 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create probe_req: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&probe_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add probe_req tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int probe_req_len = libwifi_get_probe_req_length(&probe_req);
58 if (probe_req_len <= 0) {
59 fprintf(stderr, "Invalid probe_req length: %d\n", probe_req_len);
60 return probe_req_len;
61 }
62
63 unsigned char *buf = malloc(probe_req_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_probe_req(&probe_req, buf, probe_req_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump probe_req\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--probe_req-gen-full") == 0) {
85 return test_probe_req_gen_full();
86 } else if (strcmp(argv[1], "--probe_req-gen-tags") == 0) {
87 return test_probe_req_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/probe_resp_tests.c b/test/src/probe_resp_tests.c new file mode 100644 index 0000000..463a90a --- /dev/null +++ b/test/src/probe_resp_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_probe_resp_gen_full() {
13 struct libwifi_probe_resp probe_resp = {0};
14
15 int ret = libwifi_create_probe_resp(&probe_resp, bcast, to, "Some SSID", 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create probe_resp: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int probe_resp_len = libwifi_get_probe_resp_length(&probe_resp);
22 if (probe_resp_len <= 0) {
23 fprintf(stderr, "Invalid probe_resp length: %d\n", probe_resp_len);
24 return probe_resp_len;
25 }
26
27 unsigned char *buf = malloc(probe_resp_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_probe_resp(&probe_resp, buf, probe_resp_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump probe_resp\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_probe_resp_add_tag() {
43 struct libwifi_probe_resp probe_resp = {0};
44
45 int ret = libwifi_create_probe_resp(&probe_resp, bcast, to, "Some SSID", 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create probe_resp: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add probe_resp tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int probe_resp_len = libwifi_get_probe_resp_length(&probe_resp);
58 if (probe_resp_len <= 0) {
59 fprintf(stderr, "Invalid probe_resp length: %d\n", probe_resp_len);
60 return probe_resp_len;
61 }
62
63 unsigned char *buf = malloc(probe_resp_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_probe_resp(&probe_resp, buf, probe_resp_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump probe_resp\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--probe_resp-gen-full") == 0) {
85 return test_probe_resp_gen_full();
86 } else if (strcmp(argv[1], "--probe_resp-gen-tags") == 0) {
87 return test_probe_resp_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/reassoc_req_tests.c b/test/src/reassoc_req_tests.c new file mode 100644 index 0000000..00e2b53 --- /dev/null +++ b/test/src/reassoc_req_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_reassoc_req_gen_full() {
13 struct libwifi_reassoc_req reassoc_req = {0};
14
15 int ret = libwifi_create_reassoc_req(&reassoc_req, bcast, to, to, "Some SSID", 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create reassoc_req: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int reassoc_req_len = libwifi_get_reassoc_req_length(&reassoc_req);
22 if (reassoc_req_len <= 0) {
23 fprintf(stderr, "Invalid reassoc_req length: %d\n", reassoc_req_len);
24 return reassoc_req_len;
25 }
26
27 unsigned char *buf = malloc(reassoc_req_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_reassoc_req(&reassoc_req, buf, reassoc_req_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump reassoc_req\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_reassoc_req_add_tag() {
43 struct libwifi_reassoc_req reassoc_req = {0};
44
45 int ret = libwifi_create_reassoc_req(&reassoc_req, bcast, to, to, "Some SSID", 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create reassoc_req: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&reassoc_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add reassoc_req tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int reassoc_req_len = libwifi_get_reassoc_req_length(&reassoc_req);
58 if (reassoc_req_len <= 0) {
59 fprintf(stderr, "Invalid reassoc_req length: %d\n", reassoc_req_len);
60 return reassoc_req_len;
61 }
62
63 unsigned char *buf = malloc(reassoc_req_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_reassoc_req(&reassoc_req, buf, reassoc_req_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump reassoc_req\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--reassoc_req-gen-full") == 0) {
85 return test_reassoc_req_gen_full();
86 } else if (strcmp(argv[1], "--reassoc_req-gen-tags") == 0) {
87 return test_reassoc_req_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/reassoc_resp_tests.c b/test/src/reassoc_resp_tests.c new file mode 100644 index 0000000..8167916 --- /dev/null +++ b/test/src/reassoc_resp_tests.c
@@ -0,0 +1,91 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_reassoc_resp_gen_full() {
13 struct libwifi_reassoc_resp reassoc_resp = {0};
14
15 int ret = libwifi_create_reassoc_resp(&reassoc_resp, bcast, to, 11);
16 if (ret != 0) {
17 fprintf(stderr, "Failed to create reassoc_resp: %s\n", strerror(ret));
18 return ret;
19 }
20
21 int reassoc_resp_len = libwifi_get_reassoc_resp_length(&reassoc_resp);
22 if (reassoc_resp_len <= 0) {
23 fprintf(stderr, "Invalid reassoc_resp length: %d\n", reassoc_resp_len);
24 return reassoc_resp_len;
25 }
26
27 unsigned char *buf = malloc(reassoc_resp_len);
28 if (buf == NULL) {
29 fprintf(stderr, "Failed to allocate buffer\n");
30 return -1;
31 }
32
33 int dump_len = libwifi_dump_reassoc_resp(&reassoc_resp, buf, reassoc_resp_len);
34 if (dump_len <= 0) {
35 fprintf(stderr, "Failed to dump reassoc_resp\n");
36 return ret;
37 }
38
39 return 0;
40}
41
42int test_reassoc_resp_add_tag() {
43 struct libwifi_reassoc_resp reassoc_resp = {0};
44
45 int ret = libwifi_create_reassoc_resp(&reassoc_resp, bcast, to, 11);
46 if (ret != 0) {
47 fprintf(stderr, "Failed to create reassoc_resp: %s\n", strerror(ret));
48 return ret;
49 }
50
51 ret = libwifi_quick_add_tag(&reassoc_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to add reassoc_resp tagged parameter: %s\n", strerror(ret));
54 return ret;
55 }
56
57 int reassoc_resp_len = libwifi_get_reassoc_resp_length(&reassoc_resp);
58 if (reassoc_resp_len <= 0) {
59 fprintf(stderr, "Invalid reassoc_resp length: %d\n", reassoc_resp_len);
60 return reassoc_resp_len;
61 }
62
63 unsigned char *buf = malloc(reassoc_resp_len);
64 if (buf == NULL) {
65 fprintf(stderr, "Failed to allocate buffer\n");
66 return -1;
67 }
68
69 int dump_len = libwifi_dump_reassoc_resp(&reassoc_resp, buf, reassoc_resp_len);
70 if (dump_len <= 0) {
71 fprintf(stderr, "Failed to dump reassoc_resp\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78int main(int argc, char **argv) {
79 if (argc < 2) {
80 printf("Specify test\n");
81 return -1;
82 }
83
84 if (strcmp(argv[1], "--reassoc_resp-gen-full") == 0) {
85 return test_reassoc_resp_gen_full();
86 } else if (strcmp(argv[1], "--reassoc_resp-gen-tags") == 0) {
87 return test_reassoc_resp_add_tag();
88 }
89
90 return -1;
91}
diff --git a/test/src/test_generation.c b/test/src/test_generation.c deleted file mode 100644 index 407e87f..0000000 --- a/test/src/test_generation.c +++ /dev/null
@@ -1,924 +0,0 @@
1#include <errno.h>
2#include <libwifi.h>
3#include <libwifi/core/frame/management/action.h>
4#include <libwifi/core/frame/management/timing_ad.h>
5#include <libwifi/core/frame/tag.h>
6#include <libwifi/core/misc/types.h>
7#include <libwifi/gen/management/timing_ad.h>
8#include <pcap.h>
9#include <pcap/dlt.h>
10#include <pcap/pcap.h>
11#include <signal.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/time.h>
18#include <unistd.h>
19
20#include "helpers.h"
21
22#define LIVE_INJECT 0
23#define OFFLINE_DUMP 1
24
25#define MODE_BEACON 0
26#define MODE_PROBE_RESPONSE 1
27#define MODE_PROBE_REQUEST 2
28#define MODE_DEAUTH 3
29#define MODE_DISASSOC 4
30#define MODE_ASSOC_RESPONSE 5
31#define MODE_ASSOC_REQUEST 6
32#define MODE_REASSOC_RESPONSE 7
33#define MODE_REASSOC_REQUEST 8
34#define MODE_AUTH 9
35#define MODE_RTS 10
36#define MODE_CTS 11
37#define MODE_RANDOM_BEACON 12
38#define MODE_ACTION 13
39#define MODE_ACTION_NOACK 14
40#define MODE_TIMING_AD 15
41#define MODE_ATIM 16
42
43#define SNAPLEN 96
44#define CHANNEL 11
45#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
46#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
47#define FROM_MAC "\x00\x20\x91\x11\x22\x33"
48#define REASSOC_MAC "\xAA\xBB\xCC\xDD\xEE\xFF"
49#define BEACON_SSID "libwifi-beacon"
50#define PROBE_RESP_SSID "libwifi-probe-resp"
51#define PROBE_REQ_SSID "libwifi-probe-req"
52#define ASSOC_REQ_SSID "libwifi-assoc-req"
53#define REASSOC_REQ_SSID "libwifi-reassoc-req"
54
55pcap_t *handle = NULL;
56pcap_dumper_t *outputHandle = NULL;
57FILE *filename = NULL;
58
59static unsigned char to[] = TO_MAC;
60static unsigned char from[] = FROM_MAC;
61static unsigned char bcast[] = BCAST_MAC;
62static unsigned char reassoc_mac[] = REASSOC_MAC;
63static unsigned char tag_data[] = "\x00\x00\00\x01This is a 221 tag from libwifi.\n";
64
65static int mode = 0;
66static int inject_mode = 0;
67
68void handle_interupt(int signal) {
69 if (signal == SIGINT) {
70 int oldmode = inject_mode;
71 mode = -1;
72 inject_mode = -1;
73
74 if (oldmode == LIVE_INJECT) {
75 pcap_close(handle);
76 printf("\n\nClosed Capture Handle!\n");
77 } else if (oldmode == OFFLINE_DUMP) {
78 pcap_dump_flush(outputHandle);
79 pcap_dump_close(outputHandle);
80 printf("\n\nDumped and Closed Output File!\n");
81 }
82
83 exit(EXIT_SUCCESS);
84 }
85}
86
87void inject_frame(void *buf, size_t buf_sz) {
88 struct libwifi_radiotap_info info = {0};
89 info.present = 0x0000002e; // 0x002e: Flags, Rate, Channel, dBm Ant Signal
90 info.channel.flags = 0x0140; // OFDM, 5GHz
91 info.channel.freq = 5180; // Channel 46
92 info.flags = 0x0000; // No Flags
93 info.rate = 1; // 1 Mbit
94 info.rate_raw = info.rate * 2; // Radiotap uses 500kb/s increments
95 info.signal = -20; // Signal in dBm
96
97 char *rtap = NULL;
98 rtap = malloc(LIBWIFI_MAX_RADIOTAP_LEN);
99 if (rtap == NULL) {
100 printf("malloc failure: %s\n", strerror(errno));
101 return;
102 }
103 memset(rtap, 0, LIBWIFI_MAX_RADIOTAP_LEN);
104
105 int rtap_len = libwifi_create_radiotap(&info, rtap);
106 if (rtap_len == -1) {
107 printf("error generating radiotap header\n");
108 return;
109 }
110
111 void *frame = NULL;
112 size_t frame_sz = rtap_len + buf_sz;
113 frame = malloc(frame_sz);
114 if (frame == NULL) {
115 printf("malloc failure: %s\n", strerror(errno));
116 exit(EXIT_FAILURE);
117 }
118
119 memcpy(frame, rtap, rtap_len);
120 memcpy(frame + rtap_len, buf, buf_sz);
121
122 hexdump(rtap, rtap_len);
123 printf("-----\n");
124 hexdump(frame, frame_sz);
125
126 if (inject_mode == LIVE_INJECT) {
127 pcap_inject(handle, frame, frame_sz);
128 } else if (inject_mode == OFFLINE_DUMP) {
129 struct pcap_pkthdr hdr = {0};
130 hdr.caplen = frame_sz;
131 hdr.len = frame_sz;
132 struct timeval tv;
133 gettimeofday(&tv, NULL);
134 hdr.ts = tv;
135 pcap_dump((unsigned char *) outputHandle, &hdr, frame);
136 }
137
138 free(rtap);
139 free(frame);
140}
141
142void inject_beacons(int random_mac) {
143 while (1) {
144 printf("Sending 50 beacons...\n");
145 for (int i = 0; i < 50; ++i) {
146 struct libwifi_beacon beacon;
147 unsigned char txmac[6] = {0};
148 memset(&beacon, 0, sizeof(struct libwifi_beacon));
149
150 if (random_mac) {
151 libwifi_random_mac(txmac, NULL);
152 } else {
153 memcpy(txmac, FROM_MAC, 6);
154 }
155 libwifi_create_beacon(&beacon, bcast, txmac, BEACON_SSID, CHANNEL);
156 libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
157
158 unsigned char *buf = NULL;
159 size_t buf_sz = libwifi_get_beacon_length(&beacon);
160
161 buf = malloc(buf_sz);
162 if (buf == NULL) {
163 printf("malloc failure: %s", strerror(errno));
164 exit(EXIT_FAILURE);
165 }
166
167 printf("Injecting beacon with:\n");
168 printf("\tSSID: %s\n", BEACON_SSID);
169 printf("\tChannel: %d\n", CHANNEL);
170 printf("\tSource: " MACSTR "\n", MAC2STR(txmac));
171 printf("\tDestination: " MACSTR "\n", MAC2STR(bcast));
172
173 libwifi_dump_beacon(&beacon, buf, buf_sz);
174 inject_frame(buf, buf_sz);
175
176 libwifi_free_beacon(&beacon);
177 free(buf);
178 usleep(1e4); // 10ms
179 }
180 sleep(1);
181 }
182}
183
184void inject_probe_responses() {
185 while (1) {
186 printf("Sending 50 probe responses, then sleeping for 1 second\n");
187 for (int i = 0; i < 50; ++i) {
188 struct libwifi_probe_resp probe_resp;
189 memset(&probe_resp, 0, sizeof(struct libwifi_probe_resp));
190
191 libwifi_create_probe_resp(&probe_resp, to, from, PROBE_RESP_SSID, CHANNEL);
192 libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
193
194 unsigned char *buf = NULL;
195 size_t buf_sz = libwifi_get_probe_resp_length(&probe_resp);
196
197 buf = malloc(buf_sz);
198 if (buf == NULL) {
199 printf("malloc failure: %s", strerror(errno));
200 exit(EXIT_FAILURE);
201 }
202
203 printf("Injecting probe responses with:\n");
204 printf("\tSSID: %s\n", PROBE_RESP_SSID);
205 printf("\tChannel: %d\n", CHANNEL);
206 printf("\tSource: " MACSTR "\n", MAC2STR(from));
207 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
208
209 libwifi_dump_probe_resp(&probe_resp, buf, buf_sz);
210 inject_frame(buf, buf_sz);
211
212 libwifi_free_probe_resp(&probe_resp);
213 free(buf);
214 usleep(1e4); // 10ms
215 }
216 sleep(1);
217 }
218}
219
220void inject_probe_requests() {
221 while (1) {
222 printf("Sending 50 probe responses, then sleeping for 1 second\n");
223 for (int i = 0; i < 50; ++i) {
224 struct libwifi_probe_req probe;
225 memset(&probe, 0, sizeof(struct libwifi_probe_req));
226
227 libwifi_create_probe_req(&probe, to, from, to, PROBE_REQ_SSID, CHANNEL);
228
229 unsigned char *buf = NULL;
230 size_t buf_sz = libwifi_get_probe_req_length(&probe);
231
232 buf = malloc(buf_sz);
233 if (buf == NULL) {
234 printf("malloc failure: %s", strerror(errno));
235 exit(EXIT_FAILURE);
236 }
237
238 printf("Injecting probe requests with:\n");
239 printf("\tSSID: %s\n", PROBE_REQ_SSID);
240 printf("\tChannel: %d\n", CHANNEL);
241 printf("\tSource: " MACSTR "\n", MAC2STR(from));
242 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
243
244 libwifi_dump_probe_req(&probe, buf, buf_sz);
245 inject_frame(buf, buf_sz);
246
247 libwifi_free_probe_req(&probe);
248 free(buf);
249
250 usleep(1e4); // 10ms
251 }
252 sleep(1);
253 }
254}
255
256void inject_deauths() {
257 while (1) {
258 printf("Sending 50 probe responses, then sleeping for 1 second\n");
259 for (int i = 0; i < 50; ++i) {
260 struct libwifi_deauth deauth;
261 memset(&deauth, 0, sizeof(struct libwifi_deauth));
262
263 libwifi_create_deauth(&deauth, to, from, REASON_STA_LEAVING);
264
265 unsigned char *buf = NULL;
266 size_t buf_sz = libwifi_get_deauth_length(&deauth);
267
268 buf = malloc(buf_sz);
269 if (buf == NULL) {
270 printf("malloc failure: %s", strerror(errno));
271 exit(EXIT_FAILURE);
272 }
273
274 printf("Injecting deauths with:\n");
275 printf("\tChannel: %d\n", CHANNEL);
276 printf("\tReason: %d\n", REASON_STA_LEAVING);
277 printf("\tSource: " MACSTR "\n", MAC2STR(from));
278 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
279
280 libwifi_dump_deauth(&deauth, buf, buf_sz);
281 inject_frame(buf, buf_sz);
282
283 free(buf);
284
285 usleep(1e4); // 10ms
286 }
287 sleep(1);
288 }
289}
290
291void inject_disassocs() {
292 while (1) {
293 printf("Sending 50 probe responses, then sleeping for 1 second\n");
294 for (int i = 0; i < 50; ++i) {
295 struct libwifi_disassoc disassoc;
296 memset(&disassoc, 0, sizeof(struct libwifi_disassoc));
297
298 libwifi_create_disassoc(&disassoc, to, from, REASON_STA_LEAVING);
299
300 unsigned char *buf = NULL;
301 size_t buf_sz = libwifi_get_disassoc_length(&disassoc);
302
303 buf = malloc(buf_sz);
304 if (buf == NULL) {
305 printf("malloc failure: %s", strerror(errno));
306 exit(EXIT_FAILURE);
307 }
308
309 printf("Injecting disassocs with:\n");
310 printf("\tChannel: %d\n", CHANNEL);
311 printf("\tReason: %d\n", REASON_STA_LEAVING);
312 printf("\tSource: " MACSTR "\n", MAC2STR(from));
313 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
314
315 libwifi_dump_disassoc(&disassoc, buf, buf_sz);
316 inject_frame(buf, buf_sz);
317
318 free(buf);
319
320 usleep(1e4); // 10ms
321 }
322 sleep(1);
323 }
324}
325
326void inject_assoc_requests() {
327 while (1) {
328 printf("Sending 50 association requests, then sleeping for 1 second\n");
329 for (int i = 0; i < 50; ++i) {
330 struct libwifi_assoc_req assoc_req;
331 memset(&assoc_req, 0, sizeof(struct libwifi_assoc_req));
332
333 libwifi_create_assoc_req(&assoc_req, to, from, ASSOC_REQ_SSID, CHANNEL);
334
335 unsigned char *buf = NULL;
336 size_t buf_sz = libwifi_get_assoc_req_length(&assoc_req);
337
338 buf = malloc(buf_sz);
339 if (buf == NULL) {
340 printf("malloc failure: %s", strerror(errno));
341 exit(EXIT_FAILURE);
342 }
343
344 printf("Injecting association requests with:\n");
345 printf("\tChannel: %d\n", CHANNEL);
346 printf("\tSource: " MACSTR "\n", MAC2STR(from));
347 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
348
349 libwifi_dump_assoc_req(&assoc_req, buf, buf_sz);
350 inject_frame(buf, buf_sz);
351
352 free(buf);
353 libwifi_free_assoc_req(&assoc_req);
354
355 usleep(1e4); // 10ms
356 }
357 sleep(1);
358 }
359}
360
361void inject_assoc_responses() {
362 while (1) {
363 printf("Sending 50 association responses, then sleeping for 1 second\n");
364 for (int i = 0; i < 50; ++i) {
365 struct libwifi_assoc_resp assoc_resp;
366 memset(&assoc_resp, 0, sizeof(struct libwifi_assoc_req));
367
368 libwifi_create_assoc_resp(&assoc_resp, to, from, CHANNEL);
369
370 unsigned char *buf = NULL;
371 size_t buf_sz = libwifi_get_assoc_resp_length(&assoc_resp);
372
373 buf = malloc(buf_sz);
374 if (buf == NULL) {
375 printf("malloc failure: %s", strerror(errno));
376 exit(EXIT_FAILURE);
377 }
378
379 printf("Injecting association responses with:\n");
380 printf("\tChannel: %d\n", CHANNEL);
381 printf("\tSource: " MACSTR "\n", MAC2STR(from));
382 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
383
384 libwifi_dump_assoc_resp(&assoc_resp, buf, buf_sz);
385 inject_frame(buf, buf_sz);
386
387 free(buf);
388 libwifi_free_assoc_resp(&assoc_resp);
389
390 usleep(1e4); // 10ms
391 }
392 sleep(1);
393 }
394}
395
396void inject_reassoc_requests() {
397 while (1) {
398 printf("Sending 50 reassociation requests, then sleeping for 1 second\n");
399 for (int i = 0; i < 50; ++i) {
400 struct libwifi_reassoc_req reassoc_req;
401 memset(&reassoc_req, 0, sizeof(struct libwifi_assoc_req));
402
403 libwifi_create_reassoc_req(&reassoc_req, to, from, reassoc_mac, REASSOC_REQ_SSID, CHANNEL);
404
405 unsigned char *buf = NULL;
406 size_t buf_sz = libwifi_get_reassoc_req_length(&reassoc_req);
407
408 buf = malloc(buf_sz);
409 if (buf == NULL) {
410 printf("malloc failure: %s", strerror(errno));
411 exit(EXIT_FAILURE);
412 }
413
414 printf("Injecting reassociation requests with:\n");
415 printf("\tChannel: %d\n", CHANNEL);
416 printf("\tSource: " MACSTR "\n", MAC2STR(from));
417 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
418 printf("\tPrevious BSSID: " MACSTR "\n", MAC2STR(reassoc_mac));
419
420 libwifi_dump_reassoc_req(&reassoc_req, buf, buf_sz);
421 inject_frame(buf, buf_sz);
422
423 free(buf);
424 libwifi_free_reassoc_req(&reassoc_req);
425
426 usleep(1e4); // 10ms
427 }
428 sleep(1);
429 }
430}
431
432void inject_reassoc_responses() {
433 while (1) {
434 printf("Sending 50 reassociation responses, then sleeping for 1 second\n");
435 for (int i = 0; i < 50; ++i) {
436 struct libwifi_reassoc_resp reassoc_resp;
437 memset(&reassoc_resp, 0, sizeof(struct libwifi_assoc_req));
438
439 libwifi_create_reassoc_resp(&reassoc_resp, to, from, CHANNEL);
440
441 unsigned char *buf = NULL;
442 size_t buf_sz = libwifi_get_reassoc_resp_length(&reassoc_resp);
443
444 buf = malloc(buf_sz);
445 if (buf == NULL) {
446 printf("malloc failure: %s", strerror(errno));
447 exit(EXIT_FAILURE);
448 }
449
450 printf("Injecting reassociation responses with:\n");
451 printf("\tChannel: %d\n", CHANNEL);
452 printf("\tSource: " MACSTR "\n", MAC2STR(from));
453 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
454
455 libwifi_dump_reassoc_resp(&reassoc_resp, buf, buf_sz);
456 inject_frame(buf, buf_sz);
457
458 free(buf);
459 libwifi_free_reassoc_resp(&reassoc_resp);
460
461 usleep(1e4); // 10ms
462 }
463 sleep(1);
464 }
465}
466
467void inject_auths() {
468 while (1) {
469 printf("Sending 50 auth frames, then sleeping for 1 second\n");
470 for (int i = 0; i < 50; ++i) {
471 struct libwifi_auth auth;
472 memset(&auth, 0, sizeof(struct libwifi_deauth));
473
474 libwifi_create_auth(&auth, to, from, AUTH_OPEN, 0, STATUS_SUCCESS);
475
476 unsigned char *buf = NULL;
477 size_t buf_sz = libwifi_get_auth_length(&auth);
478
479 buf = malloc(buf_sz);
480 if (buf == NULL) {
481 printf("malloc failure: %s", strerror(errno));
482 exit(EXIT_FAILURE);
483 }
484
485 libwifi_dump_auth(&auth, buf, buf_sz);
486 inject_frame(buf, buf_sz);
487
488 free(buf);
489
490 memset(&auth, 0, sizeof(struct libwifi_deauth));
491
492 libwifi_create_auth(&auth, from, to, AUTH_OPEN, 1, STATUS_SUCCESS);
493
494 buf = NULL;
495 buf_sz = libwifi_get_auth_length(&auth);
496
497 buf = malloc(buf_sz);
498 if (buf == NULL) {
499 printf("malloc failure: %s", strerror(errno));
500 exit(EXIT_FAILURE);
501 }
502
503 printf("Injecting auths with:\n");
504 printf("\tChannel: %d\n", CHANNEL);
505 printf("\tAlgorithm: %d\n", AUTH_OPEN);
506 printf("\tSource: " MACSTR "\n", MAC2STR(from));
507 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
508
509 libwifi_dump_auth(&auth, buf, buf_sz);
510 inject_frame(buf, buf_sz);
511
512 free(buf);
513 usleep(1e4); // 10ms
514 }
515 sleep(1);
516 }
517}
518
519void inject_timing_ads() {
520 while (1) {
521 printf("Sending 50 timing advertisement frames, then sleeping for 1 second\n");
522 for (int i = 0; i < 50; ++i) {
523 struct libwifi_timing_advert time_ad = {0};
524 struct libwifi_timing_advert_fields ad_fields = {0};
525
526 ad_fields.timing_capabilities = 2;
527 memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5);
528 memcpy(ad_fields.time_update, "\xBB", 1);
529 memcpy(ad_fields.time_value,
530 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10);
531
532 libwifi_create_timing_advert(&time_ad, to, from, &ad_fields, "GB", -56, -56, -30, -20);
533
534 unsigned char *buf = NULL;
535 size_t buf_len = libwifi_get_timing_advert_length(&time_ad);
536 buf = malloc(buf_len);
537 if (buf == NULL) {
538 printf("malloc failure: %s", strerror(errno));
539 exit(EXIT_FAILURE);
540 }
541 printf("buf_len: %zu\n", buf_len);
542
543 size_t ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len);
544 if (ret < 0) {
545 printf("error dump: %zu\n", ret);
546 exit(EXIT_FAILURE);
547 }
548 hexdump(buf, buf_len);
549 inject_frame(buf, buf_len);
550
551 free(buf);
552 libwifi_free_timing_advert(&time_ad);
553
554 usleep(1e4); // 10ms
555 }
556 sleep(1);
557 }
558}
559
560void inject_action_noacks() {
561 while (1) {
562 printf("Sending 50 action no ack frames, then sleeping for 1 second\n");
563 for (int i = 0; i < 50; ++i) {
564 struct libwifi_action action;
565 memset(&action, 0, sizeof(struct libwifi_action));
566
567 libwifi_create_action_no_ack(&action, to, from, ACTION_FAST_BSS_TRANSITION);
568
569 unsigned char *action_buf = malloc(256);
570 memset(action_buf, 0, 256);
571
572 size_t offset = 0;
573 size_t w = 0;
574
575 memcpy(action_buf, "\x01", 1); // Fast BSS Request
576 offset += 1;
577 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
578 offset += 6;
579 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
580 offset += 6;
581
582 unsigned char *tag_tmp = malloc(256);
583 memset(tag_tmp, 0, 256);
584
585 struct libwifi_tagged_parameter rsne = {0};
586 size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20);
587 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
588 memcpy(action_buf + offset, tag_tmp, w);
589 offset += w;
590
591
592 struct libwifi_tagged_parameter mobdom = {0};
593 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
594 memset(tag_tmp, 0, tsz);
595 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
596 memcpy(action_buf + offset, tag_tmp, w);
597 offset += w;
598 libwifi_free_tag(&mobdom);
599
600 struct libwifi_tagged_parameter fbss = {0};
601 tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88);
602 memset(tag_tmp, 0, tsz);
603 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
604 memcpy(action_buf + offset, tag_tmp, w);
605 offset += w;
606 libwifi_free_tag(&fbss);
607
608 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
609
610 unsigned char *buf = NULL;
611 size_t buf_sz = libwifi_get_action_length(&action);
612
613 buf = malloc(buf_sz);
614 if (buf == NULL) {
615 printf("malloc failure: %s", strerror(errno));
616 exit(EXIT_FAILURE);
617 }
618
619 printf("Injecting actions with:\n");
620 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
621 printf("\tSource: " MACSTR "\n", MAC2STR(from));
622 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
623
624 libwifi_dump_action(&action, buf, buf_sz);
625 inject_frame(buf, buf_sz);
626
627 free(buf);
628
629 usleep(1e4); // 10ms
630 }
631 sleep(1);
632 }
633}
634
635void inject_actions() {
636 while (1) {
637 printf("Sending 50 action frames, then sleeping for 1 second\n");
638 for (int i = 0; i < 50; ++i) {
639 struct libwifi_action action;
640 memset(&action, 0, sizeof(struct libwifi_action));
641
642 libwifi_create_action(&action, to, from, ACTION_FAST_BSS_TRANSITION);
643
644 unsigned char *action_buf = malloc(256);
645 memset(action_buf, 0, 256);
646
647 size_t offset = 0;
648 size_t w = 0;
649
650 memcpy(action_buf, "\x01", 1); // Fast BSS Request
651 offset += 1;
652 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
653 offset += 6;
654 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
655 offset += 6;
656
657 unsigned char *tag_tmp = malloc(256);
658 memset(tag_tmp, 0, 256);
659
660 struct libwifi_tagged_parameter rsne = {0};
661 size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20);
662 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
663 memcpy(action_buf + offset, tag_tmp, w);
664 offset += w;
665
666
667 struct libwifi_tagged_parameter mobdom = {0};
668 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
669 memset(tag_tmp, 0, tsz);
670 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
671 memcpy(action_buf + offset, tag_tmp, w);
672 offset += w;
673 libwifi_free_tag(&mobdom);
674
675 struct libwifi_tagged_parameter fbss = {0};
676 tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88);
677 memset(tag_tmp, 0, tsz);
678 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
679 memcpy(action_buf + offset, tag_tmp, w);
680 offset += w;
681 libwifi_free_tag(&fbss);
682
683 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
684
685 unsigned char *buf = NULL;
686 size_t buf_sz = libwifi_get_action_length(&action);
687
688 buf = malloc(buf_sz);
689 if (buf == NULL) {
690 printf("malloc failure: %s", strerror(errno));
691 exit(EXIT_FAILURE);
692 }
693
694 printf("Injecting actions with:\n");
695 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
696 printf("\tSource: " MACSTR "\n", MAC2STR(from));
697 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
698
699 libwifi_dump_action(&action, buf, buf_sz);
700 inject_frame(buf, buf_sz);
701
702 free(buf);
703
704 usleep(1e4); // 10ms
705 }
706 sleep(1);
707 }
708}
709
710void inject_atim() {
711 while (1) {
712 printf("Sending 50 ATIM frames, then sleeping for 1 second\n");
713 for (int i = 0; i < 50; ++i) {
714 struct libwifi_atim atim = {0};
715
716 libwifi_create_atim(&atim, to, from, from);
717
718 inject_frame(&atim, sizeof(struct libwifi_atim));
719
720 usleep(1e4); // 10ms
721 }
722 sleep(1);
723 }
724}
725
726void inject_rts() {
727 while (1) {
728 printf("Sending 50 RTS frames, then sleeping for 1 second\n");
729 for (int i = 0; i < 50; ++i) {
730 struct libwifi_rts rts = {0};
731
732 libwifi_create_rts(&rts, to, from, 32);
733
734 inject_frame(&rts, sizeof(struct libwifi_rts));
735
736 usleep(1e4); // 10ms
737 }
738 sleep(1);
739 }
740}
741
742void inject_cts() {
743 while (1) {
744 printf("Sending 50 CTS frames, then sleeping for 1 second\n");
745 for (int i = 0; i < 50; ++i) {
746 struct libwifi_cts cts = {0};
747
748 libwifi_create_cts(&cts, to, 32);
749
750 inject_frame(&cts, sizeof(struct libwifi_cts));
751
752 usleep(1e4); // 10ms
753 }
754 sleep(1);
755 }
756}
757
758void help(const char *name) {
759 fprintf(stderr, "Usage:\n");
760 fprintf(stderr, "\t%s --interface [interface] [--mode]\n", name);
761 fprintf(stderr, "\t\tor\n");
762 fprintf(stderr, "\t%s --file [output file] [--mode]\n", name);
763 fprintf(stderr, "\n");
764 fprintf(stderr, "Modes:\n");
765 fprintf(stderr, "\t--beacon\n");
766 fprintf(stderr, "\t--random-beacon\n");
767 fprintf(stderr, "\t--probe-req\n");
768 fprintf(stderr, "\t--probe-resp\n");
769 fprintf(stderr, "\t--deauth\n");
770 fprintf(stderr, "\t--disassoc\n");
771 fprintf(stderr, "\t--assoc-req\n");
772 fprintf(stderr, "\t--assoc-resp\n");
773 fprintf(stderr, "\t--reassoc-req\n");
774 fprintf(stderr, "\t--reassoc-resp\n");
775 fprintf(stderr, "\t--auth\n");
776 fprintf(stderr, "\t--timing-ad\n");
777 fprintf(stderr, "\t--atim\n");
778 fprintf(stderr, "\t--rts\n");
779 fprintf(stderr, "\t--cts\n");
780}
781
782void handle_args(int argc, const char *argv[]) {
783 char errbuf[PCAP_ERRBUF_SIZE];
784 memset(errbuf, 0, PCAP_ERRBUF_SIZE);
785
786 if (argc < 4) {
787 help(argv[0]);
788 exit(EXIT_SUCCESS);
789 }
790
791 if (strcmp(argv[1], "--file") == 0) {
792 inject_mode = OFFLINE_DUMP;
793
794 filename = fopen(argv[2], "w+");
795 if ((handle = pcap_open_dead(DLT_IEEE802_11_RADIO, BUFSIZ)) == NULL) {
796 fprintf(stderr, "1 %s: %s\n", argv[2], errbuf);
797 exit(EXIT_FAILURE);
798 }
799 if ((outputHandle = pcap_dump_fopen(handle, filename)) == NULL) {
800 fprintf(stderr, "2 %s: %s\n", argv[2], errbuf);
801 exit(EXIT_FAILURE);
802 }
803 } else if (strcmp(argv[1], "--interface") == 0) {
804 inject_mode = LIVE_INJECT;
805
806 if ((handle = pcap_create(argv[2], errbuf)) == NULL) {
807 fprintf(stderr, "Couldn't open interface %s: %s\n", argv[2], errbuf);
808 exit(EXIT_FAILURE);
809 }
810 if (pcap_activate(handle) == 0) {
811 printf("Sniffing on %s\n", argv[2]);
812 } else {
813 fprintf(stderr, "Couldn't activate %s: %s\n", argv[2], pcap_geterr(handle));
814 exit(EXIT_FAILURE);
815 }
816 } else {
817 help(argv[0]);
818 exit(EXIT_SUCCESS);
819 }
820
821 if (strcmp(argv[3], "--beacon") == 0) {
822 mode = MODE_BEACON;
823 } else if (strcmp(argv[3], "--random-beacon") == 0) {
824 mode = MODE_RANDOM_BEACON;
825 } else if (strcmp(argv[3], "--probe-resp") == 0) {
826 mode = MODE_PROBE_RESPONSE;
827 } else if (strcmp(argv[3], "--probe-req") == 0) {
828 mode = MODE_PROBE_REQUEST;
829 } else if (strcmp(argv[3], "--deauth") == 0) {
830 mode = MODE_DEAUTH;
831 } else if (strcmp(argv[3], "--disassoc") == 0) {
832 mode = MODE_DISASSOC;
833 } else if (strcmp(argv[3], "--assoc-resp") == 0) {
834 mode = MODE_ASSOC_RESPONSE;
835 } else if (strcmp(argv[3], "--assoc-req") == 0) {
836 mode = MODE_ASSOC_REQUEST;
837 } else if (strcmp(argv[3], "--reassoc-resp") == 0) {
838 mode = MODE_REASSOC_RESPONSE;
839 } else if (strcmp(argv[3], "--reassoc-req") == 0) {
840 mode = MODE_REASSOC_REQUEST;
841 } else if (strcmp(argv[3], "--auth") == 0) {
842 mode = MODE_AUTH;
843 } else if (strcmp(argv[3], "--timing-ad") == 0) {
844 mode = MODE_TIMING_AD;
845 } else if (strcmp(argv[3], "--action") == 0) {
846 mode = MODE_ACTION;
847 } else if (strcmp(argv[3], "--action-noack") == 0) {
848 mode = MODE_ACTION_NOACK;
849 } else if (strcmp(argv[3], "--atim") == 0) {
850 mode = MODE_ATIM;
851 } else if (strcmp(argv[3], "--rts") == 0) {
852 mode = MODE_RTS;
853 } else if (strcmp(argv[3], "--cts") == 0) {
854 mode = MODE_CTS;
855 } else {
856 help(argv[0]);
857 exit(EXIT_SUCCESS);
858 }
859}
860
861int main(int argc, const char *argv[]) {
862 signal(SIGINT, handle_interupt);
863 handle_args(argc, argv);
864
865 printf("Starting in 5 seconds...\n");
866
867 sleep(5);
868
869 switch (mode) {
870 case MODE_BEACON:
871 inject_beacons(0);
872 break;
873 case MODE_RANDOM_BEACON:
874 inject_beacons(1);
875 break;
876 case MODE_PROBE_RESPONSE:
877 inject_probe_responses();
878 break;
879 case MODE_PROBE_REQUEST:
880 inject_probe_requests();
881 break;
882 case MODE_DEAUTH:
883 inject_deauths();
884 break;
885 case MODE_DISASSOC:
886 inject_disassocs();
887 break;
888 case MODE_ASSOC_REQUEST:
889 inject_assoc_requests();
890 break;
891 case MODE_ASSOC_RESPONSE:
892 inject_assoc_responses();
893 break;
894 case MODE_REASSOC_REQUEST:
895 inject_reassoc_requests();
896 break;
897 case MODE_REASSOC_RESPONSE:
898 inject_reassoc_responses();
899 break;
900 case MODE_AUTH:
901 inject_auths();
902 break;
903 case MODE_ACTION:
904 inject_actions();
905 break;
906 case MODE_ACTION_NOACK:
907 inject_action_noacks();
908 break;
909 case MODE_TIMING_AD:
910 inject_timing_ads();
911 break;
912 case MODE_ATIM:
913 inject_atim();
914 break;
915 case MODE_RTS:
916 inject_rts();
917 break;
918 case MODE_CTS:
919 inject_cts();
920 break;
921 }
922
923 return 0;
924}
diff --git a/test/src/test_misc.c b/test/src/test_misc.c deleted file mode 100644 index f103455..0000000 --- a/test/src/test_misc.c +++ /dev/null
@@ -1,30 +0,0 @@
1#include <libwifi.h>
2#include <libwifi/core/core.h>
3#include <stdio.h>
4
5void gen_macs() {
6 printf("Getting 10 random MAC addresses:\n");
7 for(int i = 0; i < 10; i++) {
8 unsigned char mac[6] = {0};
9 libwifi_random_mac(mac, NULL);
10 printf(MACSTR "\n", MAC2STR(mac));
11 }
12
13 printf("Generating 10 random MAC addresses with 00:20:91 OUI:\n");
14 for(int i = 0; i < 10; i++) {
15 unsigned char mac[6] = {0};
16 libwifi_random_mac(mac, (unsigned char *) "\x00\x20\x91");
17 printf(MACSTR "\n", MAC2STR(mac));
18 }
19 printf("\n");
20}
21
22int main() {
23 libwifi_dummy();
24
25 printf("libwifi version: %s\n\n", libwifi_get_version());
26
27 gen_macs();
28
29 return 0;
30}
diff --git a/test/src/test_parsing.c b/test/src/test_parsing.c deleted file mode 100644 index c345346..0000000 --- a/test/src/test_parsing.c +++ /dev/null
@@ -1,613 +0,0 @@
1#include "helpers.h"
2#include <errno.h>
3#include <libwifi.h>
4#include <pcap.h>
5#include <pcap/pcap.h>
6#include <stddef.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/types.h>
12
13#define PCAP_SAVEFILE "/tmp/debug.pcap"
14#define FILTER ""
15#define MODE_BEACON 1
16#define MODE_PROBE_RESPONSE 2
17#define MODE_PROBE_REQUEST 3
18#define MODE_EAPOL 4
19#define MODE_DEAUTH 5
20#define MODE_DISASSOC 6
21#define MODE_ASSOC_RESPONSE 7
22#define MODE_ASSOC_REQUEST 8
23#define MODE_REASSOC_REQUEST 9
24#define MODE_REASSOC_RESPONSE 10
25#define MODE_DATA 11
26#define MODE_ALL 99
27
28static pcap_t *handle;
29pcap_dumper_t *pd;
30static struct bpf_program *filter;
31static int got_radiotap;
32static unsigned long packet_num = 0;
33static int mode = 0;
34static int parse_radiotap_header = 0;
35
36struct libwifi_bss bss = {0};
37struct libwifi_sta sta = {0};
38
39void help(const char *);
40void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet);
41void print_bss_info(struct libwifi_bss *bss);
42void print_sta_info(struct libwifi_sta *sta);
43void print_tag_info(unsigned char *data, size_t data_len);
44
45void interrupted(int signum) {
46 pcap_dump_close(pd);
47 pcap_close(handle);
48}
49
50void print_bss_info(struct libwifi_bss *bss) {
51 if (bss == NULL) {
52 return;
53 }
54
55 printf("=== BSS Parsing ===\n");
56 printf("ESSID: %s\n", bss->hidden ? "(hidden)" : bss->ssid);
57 printf("BSSID: " MACSTR "\n", MAC2STR(bss->bssid));
58 printf("Receiver: " MACSTR "\n", MAC2STR(bss->receiver));
59 printf("Transmitter: " MACSTR "\n", MAC2STR(bss->transmitter));
60 printf("Channel: %d\n", bss->channel);
61 printf("WPS: %s\n", bss->wps ? "yes" : "no");
62
63 char sec_buf[LIBWIFI_SECURITY_BUF_LEN];
64 libwifi_get_security_type(bss, sec_buf);
65 printf("Encryption: %s\n", sec_buf);
66
67 libwifi_get_group_ciphers(bss, sec_buf);
68 printf("\tGroup Ciphers: %s\n", sec_buf);
69
70 libwifi_get_pairwise_ciphers(bss, sec_buf);
71 printf("\tPairwise Ciphers: %s\n", sec_buf);
72
73 libwifi_get_auth_key_suites(bss, sec_buf);
74 printf("\tAuth Key Suites: %s\n", sec_buf);
75
76 if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) {
77 printf("\tMFP Capable: Yes\n");
78 }
79 if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) {
80 printf("\tMFP Required: Yes\n");
81 }
82
83 if (bss->tags.length) {
84 printf("Tagged Parameters:\n");
85 print_tag_info(bss->tags.parameters, bss->tags.length);
86 } else {
87 printf("Tagged Parameters: None\n");
88 }
89
90 printf("=== BSS End ===\n");
91 printf("\n\n");
92}
93
94void print_sta_info(struct libwifi_sta *sta) {
95 if (sta == NULL) {
96 return;
97 }
98
99 printf("=== STA Parsing ===\n");
100
101 if (sta->broadcast_ssid) {
102 printf("ESSID: <broadcast>\n");
103 } else {
104 printf("ESSID: %s\n", sta->ssid);
105 }
106 printf("Channel: %u\n", sta->channel);
107 printf("BSSID: " MACSTR "\n", MAC2STR(sta->bssid));
108 printf("MAC: " MACSTR "\n", MAC2STR(sta->transmitter));
109
110 printf("=== STA End ===\n");
111 printf("\n\n");
112}
113
114void print_tag_info(unsigned char *data, size_t data_len) {
115 struct libwifi_tag_iterator it;
116 if (libwifi_tag_iterator_init(&it, data, data_len) != 0) {
117 printf("Couldn't initialise tag iterator\n");
118 return;
119 }
120 do {
121 printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len);
122
123 int max_size = 16;
124 if (it.tag_header->tag_len < 16) {
125 max_size = it.tag_header->tag_len;
126 }
127 printf("\t%d bytes of Tag Data: ", max_size);
128 for (size_t i = 0; i < max_size; i++) {
129 printf("%02x ", it.tag_data[i]);
130 }
131 printf("\n");
132 } while (libwifi_tag_iterator_next(&it) != -1);
133}
134
135void parse_radiotap(const unsigned char *packet) {
136 struct libwifi_radiotap_info rtap_info;
137 libwifi_parse_radiotap_info(&rtap_info, packet);
138
139 printf("=== Radiotap Parsing ===\n");
140 printf("Radiotap Channel: %d\n", rtap_info.channel.freq);
141 printf("Radiotap Channel Flags: 0x%04x\n", rtap_info.channel.flags);
142 printf("Radiotap Rate: %.2f Mb/s\n", rtap_info.rate);
143 printf("Radiotap Rate Raw: 0x%02x\n", rtap_info.rate_raw);
144 printf("Radiotap Signal: %d dBm\n", rtap_info.signal);
145 for (int i = 0; i < rtap_info.antenna_count; i++) {
146 printf("Radiotap Antenna %d: %d dBm\n", rtap_info.antennas[i].antenna_number, rtap_info.antennas[i].signal);
147 }
148 printf("Radiotap Flags: 0x%04x\n", rtap_info.flags);
149 printf("Radiotap Extended Flags: 0x%08x\n", rtap_info.extended_flags);
150 printf("Radiotap RX Flags: 0x%04x\n", rtap_info.rx_flags);
151 printf("Radiotap TX Flags: 0x%04x\n", rtap_info.tx_flags);
152 printf("Radiotap TX Power: %d\n", rtap_info.tx_power);
153 printf("Radiotap RTS Retries: %d\n", rtap_info.rts_retries);
154 printf("Radiotap Data Retries: %d\n", rtap_info.data_retries);
155 printf("=== Radiotap End ===\n");
156}
157
158void parse_beacon(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
159 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) {
160 printf("Packet : %lu\n", packet_num);
161 int ret = libwifi_parse_beacon(&bss, &frame);
162 if (ret != 0) {
163 printf("Failed to parse beacon: %d\n", ret);
164 pcap_dump(args, header, packet);
165 return;
166 }
167
168 if (got_radiotap && parse_radiotap_header) {
169 parse_radiotap(packet);
170 }
171
172 print_bss_info(&bss);
173 }
174}
175
176void parse_probe_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
177 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_REQ) {
178 printf("Packet : %lu\n", packet_num);
179 int ret = libwifi_parse_probe_req(&sta, &frame);
180 if (ret != 0) {
181 printf("Failed to parse probe request: %d\n", ret);
182 pcap_dump(args, header, packet);
183 return;
184 }
185
186 if (got_radiotap && parse_radiotap_header) {
187 parse_radiotap(packet);
188 }
189
190 print_sta_info(&sta);
191 }
192}
193void parse_probe_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
194 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_RESP) {
195 printf("Packet : %lu\n", packet_num);
196 int ret = libwifi_parse_probe_resp(&bss, &frame);
197 if (ret != 0) {
198 printf("Failed to parse probe response: %d\n", ret);
199 pcap_dump(args, header, packet);
200 return;
201 }
202
203 if (got_radiotap && parse_radiotap_header) {
204 parse_radiotap(packet);
205 }
206
207 print_bss_info(&bss);
208 }
209}
210void parse_deauth(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
211 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DEAUTH) {
212 printf("Packet : %lu\n", packet_num);
213 struct libwifi_parsed_deauth deauth;
214 int ret = libwifi_parse_deauth(&deauth, &frame);
215 if (ret != 0) {
216 printf("Failed to parse deauthentication: %d\n", ret);
217 pcap_dump(args, header, packet);
218 return;
219 }
220
221 if (got_radiotap && parse_radiotap_header) {
222 parse_radiotap(packet);
223 }
224
225 printf("=== Deauthentication Frame ===\n");
226 if (deauth.ordered) {
227 printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr1));
228 printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr2));
229 printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr3));
230 } else {
231 printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr1));
232 printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr2));
233 printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr3));
234 }
235
236 printf("Reason: %d (0x%04x)\n", deauth.fixed_parameters.reason_code, deauth.fixed_parameters.reason_code);
237
238 if (deauth.tags.length) {
239 printf("Tagged Parameters:\n");
240 print_tag_info(deauth.tags.parameters, deauth.tags.length);
241 } else {
242 printf("Tagged Parameters: None\n");
243 }
244
245 printf("=== End Deauthentication Frame ===\n");
246 printf("\n\n");
247 }
248}
249void parse_disassoc(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
250 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DISASSOC) {
251 printf("Packet : %lu\n", packet_num);
252 struct libwifi_parsed_disassoc disassoc;
253 int ret = libwifi_parse_disassoc(&disassoc, &frame);
254 if (ret != 0) {
255 printf("Failed to parse diassociation: %d\n", ret);
256 pcap_dump(args, header, packet);
257 return;
258 }
259
260 if (got_radiotap && parse_radiotap_header) {
261 parse_radiotap(packet);
262 }
263
264 printf("=== Disassociation Frame ===\n");
265 if (disassoc.ordered) {
266 printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr1));
267 printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr2));
268 printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr3));
269 } else {
270 printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr1));
271 printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr2));
272 printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr3));
273 }
274
275 printf("Reason: %d (0x%04x)\n", disassoc.fixed_parameters.reason_code, disassoc.fixed_parameters.reason_code);
276
277 printf("Tagged Parameters:\n");
278 if (disassoc.tags.length == 0) {
279 printf("\tNo Tags\n");
280 } else {
281 printf("\tTags Found\n");
282 }
283
284 printf("=== End Disassociation Frame ===\n");
285 printf("\n\n");
286 }
287}
288void parse_assoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
289 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_REQ) {
290 printf("Packet : %lu\n", packet_num);
291 int ret = libwifi_parse_assoc_req(&sta, &frame);
292 if (ret != 0) {
293 printf("Failed to parse association request: %d\n", ret);
294 pcap_dump(args, header, packet);
295 return;
296 }
297
298 if (got_radiotap && parse_radiotap_header) {
299 parse_radiotap(packet);
300 }
301
302 print_sta_info(&sta);
303 }
304}
305void parse_assoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
306 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_RESP) {
307 printf("Packet : %lu\n", packet_num);
308 int ret = libwifi_parse_assoc_resp(&bss, &frame);
309 if (ret != 0) {
310 printf("Failed to parse association response: %d\n", ret);
311 pcap_dump(args, header, packet);
312 return;
313 }
314
315 if (got_radiotap && parse_radiotap_header) {
316 parse_radiotap(packet);
317 }
318
319 print_bss_info(&bss);
320 }
321}
322void parse_reassoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
323 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_REQ) {
324 printf("Packet : %lu\n", packet_num);
325 int ret = libwifi_parse_reassoc_req(&sta, &frame);
326 if (ret != 0) {
327 printf("Failed to parse reassociation request: %d\n", ret);
328 pcap_dump(args, header, packet);
329 return;
330 }
331
332 if (got_radiotap && parse_radiotap_header) {
333 parse_radiotap(packet);
334 }
335
336 print_sta_info(&sta);
337 }
338}
339void parse_reassoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
340 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_RESP) {
341 printf("Packet : %lu\n", packet_num);
342 int ret = libwifi_parse_reassoc_resp(&bss, &frame);
343 if (ret != 0) {
344 printf("Failed to parse reassociation response: %d\n", ret);
345 pcap_dump(args, header, packet);
346 return;
347 }
348
349 if (got_radiotap && parse_radiotap_header) {
350 parse_radiotap(packet);
351 }
352
353 print_bss_info(&bss);
354 }
355}
356void parse_data_eapol(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
357 if (frame.frame_control.type == TYPE_DATA) {
358 if (libwifi_check_wpa_handshake(&frame) > 0) {
359 printf("=== EAPOL ===\n");
360 printf("WPA Handshake\n");
361 int part = libwifi_check_wpa_message(&frame);
362 printf("WPA Handshake Message: %s\n", libwifi_get_wpa_message_string(&frame));
363
364 struct libwifi_wpa_auth_data data = {0};
365 libwifi_get_wpa_data(&frame, &data);
366
367 printf("EAPOL: Version: %d\n", data.version);
368 printf("EAPOL: Type: %d\n", data.type);
369 printf("EAPOL: Length: %d\n", data.length);
370 printf("EAPOL: Descriptor: %d\n", data.descriptor);
371 printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information);
372 printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length);
373 printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter);
374 printf("EAPOL: Key Info: Nonce: ");
375 for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) printf("%02x ", data.key_info.nonce[i]);
376 printf("\n");
377 printf("EAPOL: Key Info: IV: ");
378 for (size_t i = 0; i < sizeof(data.key_info.iv); ++i) printf("%02x ", data.key_info.iv[i]);
379 printf("\n");
380 printf("EAPOL: Key Info: RSC: ");
381 for (size_t i = 0; i < sizeof(data.key_info.rsc); ++i) printf("%02x ", data.key_info.rsc[i]);
382 printf("\n");
383 printf("EAPOL: Key Info: ID: ");
384 for (size_t i = 0; i < sizeof(data.key_info.id); ++i) printf("%02x ", data.key_info.id[i]);
385 printf("\n");
386 printf("EAPOL: Key Info: MIC: ");
387 for (size_t i = 0; i < sizeof(data.key_info.mic); ++i) printf("%02x ", data.key_info.mic[i]);
388 printf("\n");
389 printf("EAPOL: Key Info: Key Data Length: %d\n", data.key_info.key_data_length);
390 if (data.key_info.key_data_length) {
391 printf("EAPOL: Key Info: Key Data: ");
392 for (size_t i = 0; i < data.key_info.key_data_length; ++i) printf("%02x ", data.key_info.key_data[i]);
393 printf("\n");
394 }
395
396 libwifi_free_wpa_data(&data);
397
398 printf("\n\n");
399 }
400 }
401}
402
403void parse_data(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
404 if (frame.frame_control.type == TYPE_DATA) {
405 if (frame.flags & LIBWIFI_FLAGS_IS_QOS) {
406 printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr1));
407 printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr2));
408 } else {
409 printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data.addr1));
410 printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data.addr2));
411 }
412 printf("Body Length: %zu\n", frame.len - frame.header_len);
413 printf("Body:\n");
414 hexdump(frame.body, frame.len - frame.header_len);
415 }
416}
417
418void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
419 ++packet_num;
420 unsigned long data_len = header->caplen;
421 unsigned char *data = (unsigned char *) packet;
422
423 struct libwifi_frame frame = {0};
424 int ret = libwifi_get_wifi_frame(&frame, data, data_len, 1);
425 if (ret != 0) {
426 printf("[!] Error getting libwifi_frame: %d\n", ret);
427 return;
428 }
429
430 memset(&bss, 0, sizeof(struct libwifi_bss));
431 memset(&sta, 0, sizeof(struct libwifi_sta));
432
433 switch (mode) {
434 case MODE_BEACON:
435 parse_beacon(frame, args, header, packet);
436 break;
437 case MODE_PROBE_REQUEST:
438 parse_probe_request(frame, args, header, packet);
439 break;
440 case MODE_PROBE_RESPONSE:
441 parse_probe_response(frame, args, header, packet);
442 break;
443 case MODE_DEAUTH:
444 parse_deauth(frame, args, header, packet);
445 break;
446 case MODE_DISASSOC:
447 parse_disassoc(frame, args, header, packet);
448 break;
449 case MODE_ASSOC_REQUEST:
450 parse_assoc_request(frame, args, header, packet);
451 break;
452 case MODE_ASSOC_RESPONSE:
453 parse_assoc_response(frame, args, header, packet);
454 break;
455 case MODE_REASSOC_REQUEST:
456 parse_reassoc_request(frame, args, header, packet);
457 break;
458 case MODE_REASSOC_RESPONSE:
459 parse_reassoc_response(frame, args, header, packet);
460 break;
461 case MODE_EAPOL:
462 parse_data_eapol(frame, args, header, packet);
463 break;
464 case MODE_DATA:
465 parse_data(frame, args, header, packet);
466 break;
467 case MODE_ALL:
468 parse_beacon(frame, args, header, packet);
469 parse_probe_request(frame, args, header, packet);
470 parse_probe_response(frame, args, header, packet);
471 parse_deauth(frame, args, header, packet);
472 parse_disassoc(frame, args, header, packet);
473 parse_assoc_request(frame, args, header, packet);
474 parse_assoc_response(frame, args, header, packet);
475 parse_reassoc_request(frame, args, header, packet);
476 parse_reassoc_response(frame, args, header, packet);
477 parse_data_eapol(frame, args, header, packet);
478 parse_data(frame, args, header, packet);
479 default:
480 break;
481 }
482
483 libwifi_free_bss(&bss);
484 libwifi_free_wifi_frame(&frame);
485}
486
487void help(const char *name) {
488 fprintf(stderr, "Usage:\n");
489 fprintf(stderr, "\t%s --interface [interface] [--mode] [--radiotap]\n", name);
490 fprintf(stderr, "\t\tor\n");
491 fprintf(stderr, "\t%s --file [capture file] [--mode] [--radiotap]\n", name);
492 fprintf(stderr, "\n");
493 fprintf(stderr, "Modes:\n");
494 fprintf(stderr, "\t--beacon\n");
495 fprintf(stderr, "\t--probe-req\n");
496 fprintf(stderr, "\t--probe-resp\n");
497 fprintf(stderr, "\t--deauth\n");
498 fprintf(stderr, "\t--disassoc\n");
499 fprintf(stderr, "\t--assoc-req\n");
500 fprintf(stderr, "\t--assoc-resp\n");
501 fprintf(stderr, "\t--reassoc-req\n");
502 fprintf(stderr, "\t--reassoc-resp\n");
503 fprintf(stderr, "\t--eapol\n");
504}
505
506void handle_args(int argc, const char *argv[]) {
507 char errbuf[PCAP_ERRBUF_SIZE];
508
509 if (argc < 4) {
510 help(argv[0]);
511 exit(EXIT_SUCCESS);
512 }
513
514 if (strcmp(argv[1], "--file") == 0) {
515 if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) {
516 fprintf(stderr, "Couldn't read file %s: %s\n", argv[2], errbuf);
517 exit(EXIT_FAILURE);
518 }
519 } else if (strcmp(argv[1], "--interface") == 0) {
520 if ((handle = pcap_create(argv[2], errbuf)) == NULL) {
521 fprintf(stderr, "Failed to open interface \"%s\" for sniffing: %s\n", argv[2], errbuf);
522 exit(EXIT_FAILURE);
523 }
524 if (pcap_activate(handle) == 0) {
525 printf("[+] Started sniffing on %s\n", argv[2]);
526 } else {
527 fprintf(stderr, "[!] Couldn't activate capture: %s.\n", pcap_geterr(handle));
528 pcap_close(handle);
529 exit(EXIT_FAILURE);
530 }
531 } else {
532 help(argv[0]);
533 exit(EXIT_SUCCESS);
534 }
535
536 if (strcmp(argv[3], "--beacon") == 0) {
537 mode = MODE_BEACON;
538 } else if (strcmp(argv[3], "--probe-req") == 0) {
539 mode = MODE_PROBE_REQUEST;
540 } else if (strcmp(argv[3], "--probe-resp") == 0) {
541 mode = MODE_PROBE_RESPONSE;
542 } else if (strcmp(argv[3], "--deauth") == 0) {
543 mode = MODE_DEAUTH;
544 } else if (strcmp(argv[3], "--disassoc") == 0) {
545 mode = MODE_DISASSOC;
546 } else if (strcmp(argv[3], "--assoc-req") == 0) {
547 mode = MODE_ASSOC_REQUEST;
548 } else if (strcmp(argv[3], "--assoc-resp") == 0) {
549 mode = MODE_ASSOC_RESPONSE;
550 } else if (strcmp(argv[3], "--reassoc-req") == 0) {
551 mode = MODE_REASSOC_REQUEST;
552 } else if (strcmp(argv[3], "--reassoc-resp") == 0) {
553 mode = MODE_REASSOC_RESPONSE;
554 } else if (strcmp(argv[3], "--eapol") == 0) {
555 mode = MODE_EAPOL;
556 } else if (strcmp(argv[3], "--data") == 0) {
557 mode = MODE_DATA;
558 } else if (strcmp(argv[3], "--all") == 0) {
559 mode = MODE_ALL;
560 } else {
561 help(argv[0]);
562 exit(EXIT_SUCCESS);
563 }
564
565 if (argc > 4) {
566 if (strcmp(argv[4], "--radiotap") == 0) {
567 parse_radiotap_header = 1;
568 }
569 }
570}
571
572int main(int argc, const char *argv[]) {
573 packet_num = 0;
574 char errbuf[PCAP_ERRBUF_SIZE];
575
576 handle_args(argc, argv);
577
578 int linktype = pcap_datalink(handle);
579 if (linktype == DLT_IEEE802_11_RADIO) {
580 got_radiotap = 1;
581 } else if (linktype == DLT_IEEE802_11) {
582 got_radiotap = 0;
583 } else {
584 fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle));
585 pcap_close(handle);
586 exit(EXIT_FAILURE);
587 }
588
589 if ((filter = malloc(sizeof(struct bpf_program))) == NULL) {
590 perror("Malloc failure");
591 pcap_close(handle);
592 exit(EXIT_FAILURE);
593 }
594 printf("[*] Compiling and optimizing frame filter, this can take a second\n");
595 if (pcap_compile(handle, filter, FILTER, 0, 0) != 0) {
596 fprintf(stderr, "[!] Couldn't compile filter: %s\n", pcap_geterr(handle));
597 pcap_close(handle);
598 free(filter);
599 exit(EXIT_FAILURE);
600 }
601 if (pcap_setfilter(handle, filter) != 0) {
602 fprintf(stderr, "[!] Couldn't set filter: %s\n", pcap_geterr(handle));
603 pcap_close(handle);
604 free(filter);
605 exit(EXIT_FAILURE);
606 }
607 printf("[+] Complete\n");
608
609 pd = pcap_dump_open(handle, PCAP_SAVEFILE);
610 pcap_loop(handle, -1 /*INFINITY*/, &parse_packet, (unsigned char *) pd);
611
612 return 0;
613}
diff --git a/test/src/timing_ad_tests.c b/test/src/timing_ad_tests.c new file mode 100644 index 0000000..59d20eb --- /dev/null +++ b/test/src/timing_ad_tests.c
@@ -0,0 +1,57 @@
1#include "../../src/libwifi.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6
7#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
8#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
9const unsigned char to[] = TO_MAC;
10const unsigned char bcast[] = BCAST_MAC;
11
12int test_timing_ad_gen_full() {
13 struct libwifi_timing_advert time_ad = {0};
14 struct libwifi_timing_advert_fields ad_fields = {0};
15
16 ad_fields.timing_capabilities = 2;
17 memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5);
18 memcpy(ad_fields.time_update, "\xBB", 1);
19 memcpy(ad_fields.time_value,
20 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10);
21
22 int ret = libwifi_create_timing_advert(&time_ad, to, to, &ad_fields, "GB", -56, -56, -30, -20);
23 if (ret != 0) {
24 fprintf(stderr, "Failed to create timing advert\n");
25 return ret;
26 }
27
28 unsigned char *buf = NULL;
29 size_t buf_len = libwifi_get_timing_advert_length(&time_ad);
30 buf = malloc(buf_len);
31 if (buf == NULL) {
32 fprintf(stderr, "Failed to create buffer\n");
33 return -1;
34 }
35 printf("buf_len: %zu\n", buf_len);
36
37 ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len);
38 if (ret < 0) {
39 fprintf(stderr, "Failed to dump advert");
40 return ret;
41 }
42
43 return 0;
44}
45
46int main(int argc, char **argv) {
47 if (argc < 2) {
48 printf("Specify test\n");
49 return -1;
50 }
51
52 if (strcmp(argv[1], "--timing_ad-gen-full") == 0) {
53 return test_timing_ad_gen_full();
54 }
55
56 return -1;
57}