about summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp210
1 files changed, 0 insertions, 210 deletions
diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index d34874c..0000000 --- a/src/main.cpp +++ /dev/null
@@ -1,210 +0,0 @@
1#include <errno.h>
2#include <fcntl.h>
3#include <libwifi.h>
4#include <linux/if_packet.h>
5#include <net/if.h>
6#include <netinet/ether.h>
7#include <netinet/ip.h>
8#include <pcap/pcap.h>
9#include <stdio.h>
10#include <sys/ioctl.h>
11#include <unistd.h>
12
13#include "beacon_data.h"
14
15static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF";
16
17int get_interface_mac_address(int sock, const char *interface, unsigned char *output)
18{
19 struct ifreq ifr = {0};
20 strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
21
22 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
23 {
24 return -1;
25 }
26
27 memcpy(output, ifr.ifr_hwaddr.sa_data, 6);
28 return 0;
29}
30
31int get_interface_index(int sock, const char *interface, int *index)
32{
33 struct ifreq ifr = {0};
34 strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
35
36 if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
37 {
38 return -1;
39 }
40
41 *index = ifr.ifr_ifindex;
42 return 0;
43}
44
45int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, unsigned char **buf)
46{
47 struct libwifi_beacon beacon = {0};
48
49 if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 7))
50 {
51 printf("Could not create beacon frame.\n");
52 return -3;
53 }
54
55 beacon.frame_header.seq_control.sequence_number = seq_number;
56
57 static const unsigned char supported_rates[] = {0x82, 0x84, 0x0b, 0x16, 0x24, 0x30, 0x48, 0x6c};
58 if (libwifi_quick_add_tag(&beacon.tags, TAG_SUPP_RATES, supported_rates, 8)) {
59 printf("Could not add supported rates tag.\n");
60 return -7;
61 }
62
63 static const unsigned char extended_supported_rates[] = {0x0c, 0x12, 0x18, 0x60};
64 if (libwifi_quick_add_tag(&beacon.tags, TAG_EXTENDED_SUPPORTED_RATES, extended_supported_rates, 4)) {
65 printf("Could not add extended supported rates tag.\n");
66 return -7;
67 }
68
69 unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8];
70 payload_data[0] = 0x00;
71 payload_data[1] = 0x09;
72 payload_data[2] = 0xBF;
73 payload_data[3] = 0x00;
74 payload_data[4] = 0xFF;
75 payload_data[5] = 0xFF;
76 payload_data[6] = 0x00;
77 payload_data[7] = 0x00;
78 memcpy(payload_data + 8, kBeaconPayloads[index], BEACON_PAYLOAD_LENGTH);
79
80 if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8))
81 {
82 printf("Could not add beacon data tag.\n");
83 return -6;
84 }
85
86 size_t beacon_size = libwifi_get_beacon_length(&beacon);
87 unsigned char *beacon_output = (unsigned char *)malloc(beacon_size);
88 if (beacon_output == NULL)
89 {
90 printf("Could not allocate beacon output.\n");
91 return -4;
92 }
93
94 if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0)
95 {
96 printf("Could not dump beacon.\n");
97 return -5;
98 }
99
100 libwifi_free_beacon(&beacon);
101
102 *buf = beacon_output;
103
104 return beacon_size;
105}
106
107int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output)
108{
109 static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00";
110 static const int radiotap_size = 8;
111
112 int output_size = input_size + radiotap_size;
113
114 unsigned char *buf = (unsigned char *)malloc(output_size);
115 if (buf == NULL)
116 {
117 return -1;
118 }
119
120 memcpy(buf, radiotap, radiotap_size);
121 memcpy(buf + radiotap_size, input, input_size);
122
123 *output = buf;
124
125 return output_size;
126}
127
128int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size)
129{
130 struct sockaddr_ll socket_address;
131 socket_address.sll_ifindex = device_index;
132 socket_address.sll_halen = ETH_ALEN;
133 memcpy(socket_address.sll_addr, dst_addr, 6);
134
135 if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0)
136 {
137 printf("Could not send packet.\n");
138 return errno;
139 }
140
141 return 0;
142}
143
144int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size)
145{
146 unsigned char *buffer;
147 int buffer_size = prepend_radiotap(packet, packet_size, &buffer);
148 if (buffer_size < 0)
149 {
150 printf("Could not prepend radiotap.\n");
151 return -2;
152 }
153
154 int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size);
155 free(buffer);
156
157 return ret;
158}
159
160int main(int argc, char **argv)
161{
162 static const char *interface_name = "mon0";
163
164 int sock = socket(AF_PACKET, SOCK_RAW, 0);
165 if (sock == -1)
166 {
167 printf("Could not open socket.\n");
168 return 1;
169 }
170
171 unsigned char tx_addr[6];
172 if (get_interface_mac_address(sock, interface_name, tx_addr))
173 {
174 printf("Could not get hardware address.\n");
175 return 2;
176 }
177
178 int device_index = 0;
179 if (get_interface_index(sock, interface_name, &device_index))
180 {
181 printf("Could not get device index.\n");
182 return 2;
183 }
184
185 int beacon_index = 0;
186 int seq_number = 0;
187 for (;;)
188 {
189 unsigned char *beacon_output;
190 int beacon_size = make_beacon_frame(beacon_index, tx_addr, seq_number, &beacon_output);
191 if (beacon_size < 0)
192 {
193 return 3;
194 }
195
196 if (send_packet_with_radiotap(sock, device_index, kBroadcastAddress, beacon_output, beacon_size))
197 {
198 return 4;
199 }
200
201 free(beacon_output);
202
203 beacon_index = (beacon_index + 1) % 10;
204 seq_number++;
205
206 usleep(1024 * 100);
207 }
208
209 return 0;
210}