-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.c
266 lines (228 loc) · 6.23 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include "capture/common.h"
#include "capture/osdep.h"
#include "utils/utils.h"
#include "utils/wifi_scan.h"
#include "airkiss.h"
#define MAX_CHANNELS 14
#define DEBUG 0
static airkiss_context_t *akcontex = NULL;
const airkiss_config_t akconf = {
(airkiss_memset_fn)&memset,
(airkiss_memcpy_fn)&memcpy,
(airkiss_memcmp_fn)&memcmp,
(airkiss_printf_fn)&printf };
airkiss_result_t ak_result;
struct itimerval my_timer;
int startTimer(struct itimerval *timer, int ms);
int udp_broadcast(unsigned char random, int port);
char *wifi_if = NULL;
struct wif *wi = NULL;
int g_channels[MAX_CHANNELS] = {0};
int g_channel_index = 0;
int g_channel_nums = 0;
pthread_mutex_t lock;
void switch_channel_callback(void)
{
pthread_mutex_lock(&lock);
g_channel_index++;
if(g_channel_index > g_channel_nums - 1)
{
g_channel_index = 0;
LOG_TRACE("scan all channels");
}
int ret = wi->wi_set_channel(wi, g_channels[g_channel_index]);
if (ret) {
LOG_TRACE("cannot set channel to %d", g_channels[g_channel_index]);
}
airkiss_change_channel(akcontex);
pthread_mutex_unlock(&lock);
}
int process_airkiss(const unsigned char *packet, int size)
{
pthread_mutex_lock(&lock);
int ret;
ret = airkiss_recv(akcontex, (void *)packet, size);
if(ret == AIRKISS_STATUS_CONTINUE)
{
;//
}
else if(ret == AIRKISS_STATUS_CHANNEL_LOCKED)
{
startTimer(&my_timer, 0);
LOG_TRACE("Lock channel in %d", g_channels[g_channel_index]);
}
else if(ret == AIRKISS_STATUS_COMPLETE)
{
LOG_TRACE("Airkiss completed.");
airkiss_get_result(akcontex, &ak_result);
LOG_TRACE("Result:\nssid_crc:[%x]\nkey_len:[%d]\nkey:[%s]\nrandom:[0x%02x]",
ak_result.reserved,
ak_result.pwd_length,
ak_result.pwd,
ak_result.random);
//TODO: scan and connect to wifi
udp_broadcast(ak_result.random, 10000);
}
pthread_mutex_unlock(&lock);
if(DEBUG) {
/* print header */
printf("len:%4d, airkiss ret:%d [ ", size, ret);
int i;
unsigned char ch;
for(i=0; i<24; i++)
{
ch = (unsigned char)*(packet + i);
printf("%02x ", ch);
}
printf("]\n");
}
return ret;
}
void add_channel(int chan) {
int i;
for(i=0; i<g_channel_nums; i++) {
if(g_channels[i]==chan)
break;
}
if(i==g_channel_nums) {
g_channel_nums += 1;
g_channels[i] = chan;
}
}
void init_channels()
{
int i;
for(i=1; i<MAX_CHANNELS; i++)
{
add_channel(i);
}
}
int startTimer(struct itimerval *timer, int ms)
{
time_t secs, usecs;
secs = ms/1000;
usecs = ms%1000 * 1000;
timer->it_interval.tv_sec = secs;
timer->it_interval.tv_usec = usecs;
timer->it_value.tv_sec = secs;
timer->it_value.tv_usec = usecs;
setitimer(ITIMER_REAL, timer, NULL);
return 0;
}
int udp_broadcast(unsigned char random, int port)
{
int fd;
int enabled = 1;
int err;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_BROADCAST;
addr.sin_port = htons(port);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
LOG_TRACE("Error to create socket, reason: %s", strerror(errno));
return 1;
}
err = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &enabled, sizeof(enabled));
if(err == -1)
{
close(fd);
return 1;
}
LOG_TRACE("Sending random to broadcast..");
int i;
useconds_t usecs = 1000*20;
for(i=0;i<50;i++)
{
sendto(fd, (unsigned char *)&random, 1, 0, (struct sockaddr*)&addr, sizeof(struct sockaddr));
usleep(usecs);
}
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
if(argc!=2)
{
LOG_ERROR("Usage: %s <device-name>", argv[0]);
return 1;
}
wifi_if = argv[1];
wireless_scan_head head;
wireless_scan *presult = NULL;
LOG_TRACE("Scanning accesss point...");
if(wifi_scan(wifi_if, &head) == 0)
{
LOG_TRACE("Scan success.");
presult = head.result;
while(presult != NULL) {
char essid[MAX_ESSID_SIZE];
char bssid[MAX_BSSID_SIZE];
unsigned int freq;
int channel,power;
unsigned char essid_crc;
get_essid(presult, essid, MAX_ESSID_SIZE);
get_bssid(presult, bssid, MAX_BSSID_SIZE);
freq = get_freq_mhz(presult);
power = get_strength_dbm(presult);
channel = getChannelFromFrequency(freq);
essid_crc = calcrc_bytes((unsigned char*)essid, strlen(essid));
LOG_TRACE("bssid:[%s], channel:[%2d], pow:[%d dBm], essid_crc:[%02x], essid:[%s]",
bssid, channel, power, essid_crc, essid);
add_channel(channel);
presult = presult->next;
}
}
else
{
LOG_ERROR("ERROR to scan AP, init with all %d channels", MAX_CHANNELS);
init_channels();
}
/* Open the interface and set mode monitor */
wi = wi_open(wifi_if);
if (!wi) {
LOG_ERROR("cannot init interface %s", wifi_if);
return 1;
}
/* airkiss setup */
int result;
akcontex = (airkiss_context_t *)malloc(sizeof(airkiss_context_t));
result = airkiss_init(akcontex, &akconf);
if(result != 0)
{
LOG_ERROR("Airkiss init failed!!");
return 1;
}
LOG_TRACE("Airkiss version: %s", airkiss_version());
if(pthread_mutex_init(&lock, NULL) != 0)
{
LOG_ERROR("mutex init failed");
return 1;
}
/* Setup channel switch timer */
startTimer(&my_timer, 400);
signal(SIGALRM,(__sighandler_t)&switch_channel_callback);
int read_size;
unsigned char buf[RECV_BUFSIZE] = {0};
for(;;)
{
read_size = wi->wi_read(wi, buf, RECV_BUFSIZE, NULL);
if (read_size < 0) {
LOG_ERROR("recv failed, ret %d", read_size);
break;
}
if(AIRKISS_STATUS_COMPLETE==process_airkiss(buf, read_size))
break;
}
free(akcontex);
pthread_mutex_destroy(&lock);
return 0;
}