forked from Wi-FiQuickTrack/Wi-FiQuickTrack-ControlAppC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
1758 lines (1493 loc) · 50.6 KB
/
utils.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2020 Wi-Fi Alliance */
/* Permission to use, copy, modify, and/or distribute this software for any */
/* purpose with or without fee is hereby granted, provided that the above */
/* copyright notice and this permission notice appear in all copies. */
/* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL */
/* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL */
/* THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR */
/* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING */
/* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF */
/* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT */
/* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS */
/* SOFTWARE. */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#ifdef _OPENWRT_
#include <sys/time.h>
#endif
#include <time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <stdint.h>
typedef uint8_t u_int8_t;
typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
#include "vendor_specific.h"
#include "utils.h"
#include "eloop.h"
/* Log */
int stdout_level = LOG_LEVEL_DEBUG;
int syslog_level = LOG_LEVEL_INFO;
/* multiple VAPs */
int interface_count = 0;
int configured_interface_count = 0;
struct interface_info interfaces[16];
int band_mbssid_cnt[16];
struct interface_info* default_interface;
static struct loopback_info loopback = {};
/* bridge used for wireless interfaces */
char wlans_bridge[32];
#if UPLOAD_TC_APP_LOG
/* per test case control app log */
FILE *app_log;
extern struct sockaddr_in *tool_addr;
#endif
#ifdef HOSTAPD_SUPPORT_MBSSID_WAR
int use_openwrt_wpad = 0;
#endif
void send_continuous_loopback_packet(void *eloop_ctx, void *sock_ctx);
void debug_print_timestamp(void) {
time_t rawtime;
struct tm *info;
char buffer[32];
time(&rawtime);
info = localtime(&rawtime);
if (info) {
strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S", info);
}
printf("%s ", buffer);
#if UPLOAD_TC_APP_LOG
if (app_log) {
fprintf(app_log, "%s ", buffer);
}
#endif
}
void indigo_logger(int level, const char *fmt, ...) {
char *format, *log_type;
int maxlen;
int priority;
va_list ap;
maxlen = strlen(fmt) + 100;
format = malloc(maxlen);
if (!format) {
return;
}
switch (level) {
case LOG_LEVEL_DEBUG_VERBOSE:
log_type = "debugverbose";
break;
case LOG_LEVEL_DEBUG:
log_type = "debug";
break;
case LOG_LEVEL_INFO:
log_type = "info";
break;
case LOG_LEVEL_NOTICE:
log_type = "notice";
break;
case LOG_LEVEL_WARNING:
log_type = "warning";
break;
default:
log_type = "info";
break;
}
snprintf(format, maxlen, "controlappc.%8s %s", log_type, fmt);
if (level >= stdout_level) {
debug_print_timestamp();
va_start(ap, fmt);
vprintf(format, ap);
va_end(ap);
printf("\n");
#if UPLOAD_TC_APP_LOG
if (app_log) {
va_start(ap, fmt);
vfprintf(app_log, format, ap);
fprintf(app_log, "\n");
va_end(ap);
}
#endif
}
if (level >= stdout_level) {
switch (level) {
case LOG_LEVEL_DEBUG_VERBOSE:
case LOG_LEVEL_DEBUG:
priority = LOG_DEBUG;
break;
case LOG_LEVEL_INFO:
priority = LOG_INFO;
break;
case LOG_LEVEL_NOTICE:
priority = LOG_NOTICE;
break;
case LOG_LEVEL_WARNING:
priority = LOG_WARNING;
break;
default:
priority = LOG_INFO;
break;
}
va_start(ap, fmt);
vsyslog(priority, format, ap);
va_end(ap);
}
}
void open_tc_app_log() {
#if UPLOAD_TC_APP_LOG
if (app_log) {
fclose(app_log);
app_log = NULL;
}
app_log = fopen(APP_LOG_FILE, "w");
if (app_log == NULL) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to open the file %s", APP_LOG_FILE);
}
#endif
}
/* Close file handle and upload test case control app log */
void close_tc_app_log() {
#if UPLOAD_TC_APP_LOG
if (app_log) {
fclose(app_log);
app_log = NULL;
if (tool_addr != NULL) {
http_file_post(inet_ntoa(tool_addr->sin_addr), TOOL_POST_PORT, HAPD_UPLOAD_API, APP_LOG_FILE);
}
}
#endif
}
/* System */
int pipe_command(char *buffer, int buffer_size, char *cmd, char *parameter[]) {
int pipefds[2], len;
pid_t pid;
if (pipe(pipefds) == -1){
indigo_logger(LOG_LEVEL_ERROR, "Failed to create the pipe");
return -1;
}
pid = fork();
if (pid == -1) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to fork");
return -1;
}
if (pid == 0) {
// Replace stdout with the write end of the pipe
dup2(pipefds[1], STDOUT_FILENO);
// Close read to pipe, in child
close(pipefds[0]);
execv(cmd, parameter);
exit(EXIT_SUCCESS);
} else {
close(pipefds[1]);
len = read(pipefds[0], buffer, buffer_size);
indigo_logger(LOG_LEVEL_DEBUG_VERBOSE, "Pipe system call= %s, Return length= %d, result= %s", cmd, len, buffer);
close(pipefds[0]);
wait(NULL); /* Parent waits for the child to terminate */
}
return len;
}
char* read_file(char *fn) {
struct stat st;
int fd, size;
char *buffer = NULL;
memset(&st, 0, sizeof(struct stat));
stat(fn, &st);
size = st.st_size;
fd = open(fn, O_RDONLY);
if (fd) {
buffer = (char*)malloc(sizeof(char)*(size+1));
memset(buffer, 0, size+1);
read(fd, buffer, size);
close(fd);
}
return buffer;
}
int write_file(char *fn, char *buffer, int len) {
int fd;
fd = open(fn, O_CREAT | O_WRONLY | O_TRUNC);
if (fd > 0) {
(void)write(fd, buffer, len);
close(fd);
return 0;
}
return -1;
}
int append_file(char *fn, char *buffer, int len) {
int fd;
fd = open(fn, O_CREAT | O_WRONLY | O_APPEND);
if (fd > 0) {
(void)write(fd, buffer, len);
close(fd);
return 0;
}
return -1;
}
/* strrstr(), reversed strstr(), is not available in some compilers. Here is the implementation. */
static char* indigo_strrstr(char *input, const char *token) {
char *result = NULL, *p = NULL;
if (*token == '\0') {
return (char *) input;
}
while (1) {
p = strstr(input, token);
if (p == NULL)
break;
result = p;
input = p + 1;
}
return result;
}
/* Loopback */
int loopback_socket = 0;
static void loopback_server_receive_message(int sock, void *eloop_ctx, void *sock_ctx) {
struct sockaddr_storage from;
unsigned char buffer[BUFFER_LEN];
int fromlen, len;
fromlen = sizeof(from);
len = recvfrom(sock, buffer, BUFFER_LEN, 0, (struct sockaddr *) &from, &fromlen);
if (len < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Loopback server recvfrom[server] error");
return ;
}
indigo_logger(LOG_LEVEL_INFO, "Loopback server received length = %d", len);
len = sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (struct sockaddr *)&from, sizeof(from));
indigo_logger(LOG_LEVEL_INFO, "Loopback server echo back length = %d", len);
}
static void loopback_server_timeout(void *eloop_ctx, void *timeout_ctx) {
int s = (intptr_t)eloop_ctx;
eloop_unregister_read_sock(s);
close(s);
loopback_socket = 0;
indigo_logger(LOG_LEVEL_INFO, "Loopback server stops");
}
int loopback_server_start(char *local_ip, char *local_port, int timeout) {
int s = 0;
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
/* Open UDP socket */
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to open server socket");
return -1;
}
/* Bind specific IP */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (local_ip) {
addr.sin_addr.s_addr = inet_addr(local_ip);
}
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to bind server socket");
close(s);
return -1;
}
if (getsockname(s, (struct sockaddr *)&addr, &len) == -1) {
indigo_logger(LOG_LEVEL_INFO, "Failed to get socket port number");
close(s);
return -1;
} else {
indigo_logger(LOG_LEVEL_INFO, "loopback server port number %d\n", ntohs(addr.sin_port));
sprintf(local_port, "%d", ntohs(addr.sin_port));
}
/* Register to eloop and ready for the socket event */
if (eloop_register_read_sock(s, loopback_server_receive_message, NULL, NULL)) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to initiate ControlAppC");
return -1;
}
loopback_socket = s;
eloop_register_timeout(timeout, 0, loopback_server_timeout, (void*)(intptr_t)s, NULL);
indigo_logger(LOG_LEVEL_INFO, "Loopback Client starts ip %s port %s", local_ip, local_port);
return 0;
}
int loopback_server_stop() {
if (loopback_socket) {
eloop_cancel_timeout(loopback_server_timeout, (void*)(intptr_t)loopback_socket, NULL);
eloop_unregister_read_sock(loopback_socket);
close(loopback_socket);
loopback_socket = 0;
}
return 0;
}
int loopback_server_status() {
return !!loopback_socket;
}
unsigned short icmp_checksum(unsigned short *buf, int size)
{
unsigned long sum = 0;
while (size > 1) {
sum += *buf;
buf++;
size -= 2;
}
if (size == 1)
sum += *(unsigned char *)buf;
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
void setup_icmphdr(u_int8_t type, u_int8_t code, u_int16_t id,
u_int16_t seq, struct icmphdr *icmphdr, int packet_size)
{
memset(icmphdr, 0, sizeof(struct icmphdr));
icmphdr->type = type;
icmphdr->code = code;
icmphdr->un.echo.id = id;
icmphdr->un.echo.sequence = seq;
icmphdr->checksum = icmp_checksum((unsigned short *)icmphdr, packet_size);
}
void send_one_loopback_icmp_packet(struct loopback_info *info) {
int n;
char server_reply[1600];
struct in_addr insaddr;
struct icmphdr *icmphdr, *recv_icmphdr;
struct iphdr *recv_iphdr;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(info->target_ip);
icmphdr = (struct icmphdr *)&info->message;
info->pkt_sent++;
setup_icmphdr(ICMP_ECHO, 0, 0, info->pkt_sent, icmphdr, info->pkt_size);
n = sendto(info->sock, (char *)info->message, info->pkt_size, 0, (struct sockaddr *)&addr, sizeof(addr));
if (n < 0) {
indigo_logger(LOG_LEVEL_WARNING, "Send failed on icmp packet %d", info->pkt_sent);
goto done;
}
indigo_logger(LOG_LEVEL_INFO, "Packet %d: Send icmp %d bytes data to ip %s",
info->pkt_sent, n, info->target_ip);
memset(&server_reply, 0, sizeof(server_reply));
n = recv(info->sock, server_reply, sizeof(server_reply), 0);
if (n < 0) {
indigo_logger(LOG_LEVEL_WARNING, "recv failed on icmp packet %d", info->pkt_sent);
goto done;
} else {
recv_iphdr = (struct iphdr *)server_reply;
recv_icmphdr = (struct icmphdr *)(server_reply + (recv_iphdr->ihl << 2));
insaddr.s_addr = recv_iphdr->saddr;
if (!strcmp(info->target_ip, inet_ntoa(insaddr)) && recv_icmphdr->type == ICMP_ECHOREPLY) {
indigo_logger(LOG_LEVEL_INFO, "icmp echo reply from %s, Receive echo %d bytes data", info->target_ip, n - 20);
info->pkt_rcv++;
} else {
indigo_logger(LOG_LEVEL_INFO, "Received packet is not the ICMP reply from the DUT");
}
}
done:
eloop_register_timeout(0, info->rate * 1000000, send_continuous_loopback_packet, info, NULL);
}
void send_one_loopback_udp_packet(struct loopback_info *info) {
char server_reply[1600];
ssize_t recv_len = 0, send_len = 0;
memset(&server_reply, 0, sizeof(server_reply));
info->pkt_sent++;
send_len = send(info->sock, info->message, strlen(info->message), 0);
if (send_len < 0) {
indigo_logger(LOG_LEVEL_INFO, "Send failed on packet %d", info->pkt_sent);
// In case Tool doesn't send stop or doesn't receive stop
if (info->pkt_sent < 1000)
eloop_register_timeout(0, info->rate*1000000, send_continuous_loopback_packet, info, NULL);
return;
}
indigo_logger(LOG_LEVEL_INFO, "Packet %d: Send loopback %d bytes data",
info->pkt_sent, send_len);
recv_len = recv(info->sock, server_reply, sizeof(server_reply), 0);
if (recv_len < 0) {
indigo_logger(LOG_LEVEL_INFO, "recv failed on packet %d", info->pkt_sent);
// In case Tool doesn't send stop or doesn't receive stop
if (info->pkt_sent < 1000)
eloop_register_timeout(0, info->rate*1000000, send_continuous_loopback_packet, info, NULL);
return;
}
info->pkt_rcv++;
indigo_logger(LOG_LEVEL_INFO, "Receive echo %d bytes data", recv_len);
eloop_register_timeout(0, info->rate*1000000, send_continuous_loopback_packet, info, NULL);
}
void send_continuous_loopback_packet(void *eloop_ctx, void *sock_ctx) {
struct loopback_info *info = (struct loopback_info *)eloop_ctx;
if (info->pkt_type == DATA_TYPE_ICMP) {
send_one_loopback_icmp_packet(info);
} else {
send_one_loopback_udp_packet(info);
}
}
/* Stop to send continuous loopback data */
int stop_loopback_data(int *pkt_sent)
{
if (loopback.sock <= 0)
return 0;
eloop_cancel_timeout(send_continuous_loopback_packet, &loopback, NULL);
close(loopback.sock);
loopback.sock = 0;
if (pkt_sent)
*pkt_sent = loopback.pkt_sent;
return loopback.pkt_rcv;
}
int send_udp_data(char *target_ip, int target_port, int packet_count, int packet_size, double rate) {
int s = 0, i = 0;
struct sockaddr_in addr;
int pkt_sent = 0, pkt_rcv = 0;
char message[1600], server_reply[1600], ifname[32];
ssize_t recv_len = 0, send_len = 0;
struct timeval timeout;
/* Open UDP socket */
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to open socket");
return -1;
}
if (rate < 1) {
timeout.tv_sec = 0;
timeout.tv_usec = rate * 1000000;
} else {
timeout.tv_sec = 1;
timeout.tv_usec = 0;
}
if (is_bridge_created()) {
snprintf(ifname, sizeof(ifname), "%s", get_wlans_bridge());
} else if (get_p2p_group_if(ifname, sizeof(ifname)) != 0)
snprintf(ifname, sizeof(ifname), "%s", get_wireless_interface());
const int len = strnlen(ifname, IFNAMSIZ);
if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, len) < 0) {
indigo_logger(LOG_LEVEL_ERROR, "failed to bind the interface %s", ifname);
return -1;
}
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (target_ip) {
addr.sin_addr.s_addr = inet_addr(target_ip);
}
addr.sin_port = htons(target_port);
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Connect failed. Error");
close(s);
return -1;
}
indigo_logger(LOG_LEVEL_INFO, "packet_count %d rate %lf\n",
packet_count, rate);
/* Continuous data case: reply OK and use eloop timeout to send data */
if (packet_count == -1) {
loopback.sock = s;
loopback.pkt_type = DATA_TYPE_UDP;
loopback.rate = rate;
loopback.pkt_sent = loopback.pkt_rcv = 0;
memset(loopback.message, 0, sizeof(loopback.message));
for (i = 0; (i < packet_size) && (i < sizeof(loopback.message)); i++)
loopback.message[i] = 0x0A;
eloop_register_timeout(0, 0, send_continuous_loopback_packet, &loopback, NULL);
indigo_logger(LOG_LEVEL_INFO, "Send continuous loopback data to ip %s port %u",
target_ip, target_port);
return 0;
}
memset(message, 0, sizeof(message));
for (i = 0; (i < packet_size) && (i < sizeof(message)); i++)
message[i] = 0x0A;
for (pkt_sent = 1; pkt_sent <= packet_count; pkt_sent++) {
memset(&server_reply, 0, sizeof(server_reply));
send_len = send(s, message, strlen(message), 0);
if (send_len < 0) {
indigo_logger(LOG_LEVEL_INFO, "Send failed on packet %d", pkt_sent);
usleep(rate*1000000);
continue;
}
indigo_logger(LOG_LEVEL_INFO, "Packet %d: Send loopback %d bytes data to ip %s port %u",
pkt_sent, send_len, target_ip, target_port);
recv_len = recv(s, server_reply, sizeof(server_reply), 0);
if (recv_len < 0) {
indigo_logger(LOG_LEVEL_INFO, "recv failed on packet %d", pkt_sent);
if (rate > 1)
usleep((rate-1)*1000000);
continue;
}
pkt_rcv++;
usleep(rate*1000000);
indigo_logger(LOG_LEVEL_INFO, "Receive echo %d bytes data", recv_len);
}
close(s);
return pkt_rcv;
}
int send_icmp_data(char *target_ip, int packet_count, int packet_size, double rate)
{
int n, sock, i;
char buf[1600], server_reply[1600], ifname[32];
struct sockaddr_in addr;
struct in_addr insaddr;
struct icmphdr *icmphdr, *recv_icmphdr;
struct iphdr *recv_iphdr;
struct timeval timeout;
int pkt_sent = 0, pkt_rcv = 0;
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(target_ip);
if (rate < 1) {
timeout.tv_sec = 0;
timeout.tv_usec = rate * 1000000;
} else {
timeout.tv_sec = 1;
timeout.tv_usec = 0;
}
if (is_bridge_created()) {
snprintf(ifname, sizeof(ifname), "%s", get_wlans_bridge());
} else if (get_p2p_group_if(ifname, sizeof(ifname)) != 0)
snprintf(ifname, sizeof(ifname), "%s", get_wireless_interface());
const int len = strnlen(ifname, IFNAMSIZ);
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, len) < 0) {
indigo_logger(LOG_LEVEL_ERROR, "failed to bind the interface %s", ifname);
return -1;
}
indigo_logger(LOG_LEVEL_DEBUG, "Bind the interface %s", ifname);
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
/* Continuous data case: reply OK and use eloop timeout to send data */
if (packet_count == -1) {
memset(&loopback, 0, sizeof(loopback));
loopback.sock = sock;
loopback.pkt_type = DATA_TYPE_ICMP;
loopback.rate = rate;
loopback.pkt_size = packet_size;
snprintf(loopback.target_ip, sizeof(loopback.target_ip), "%s", target_ip);
for (i = sizeof(struct icmphdr); (i < packet_size) && (i < sizeof(loopback.message)); i++)
loopback.message[i] = 0x0A;
eloop_register_timeout(0, 0, send_continuous_loopback_packet, &loopback, NULL);
indigo_logger(LOG_LEVEL_INFO, "Send continuous loopback data to ip %s", loopback.target_ip);
return 0;
}
icmphdr = (struct icmphdr *)&buf;
memset(&buf, 0, sizeof(buf));
for (i = sizeof(struct icmphdr); (i < packet_size) && (i < sizeof(buf)); i++)
buf[i] = 0x0A;
for (pkt_sent = 1; pkt_sent <= packet_count; pkt_sent++) {
memset(&server_reply, 0, sizeof(server_reply));
setup_icmphdr(ICMP_ECHO, 0, 0, pkt_sent, icmphdr, packet_size);
n = sendto(sock, (char *)buf, packet_size, 0, (struct sockaddr *)&addr, sizeof(addr));
if (n < 0) {
indigo_logger(LOG_LEVEL_WARNING, "Send failed on icmp packet %d", pkt_sent);
usleep(rate * 1000000);
continue;
}
indigo_logger(LOG_LEVEL_INFO, "Packet %d: Send icmp %d bytes data to ip %s",
pkt_sent, n, target_ip);
n = recv(sock, server_reply, sizeof(server_reply), 0);
if (n < 0) {
indigo_logger(LOG_LEVEL_WARNING, "recv failed on icmp packet %d", pkt_sent);
if (rate > 1)
usleep((rate - 1) * 1000000);
continue;
} else {
recv_iphdr = (struct iphdr *)server_reply;
recv_icmphdr = (struct icmphdr *)(server_reply + (recv_iphdr->ihl << 2));
insaddr.s_addr = recv_iphdr->saddr;
if (!strcmp(target_ip, inet_ntoa(insaddr)) && recv_icmphdr->type == ICMP_ECHOREPLY) {
/* IP header 20 bytes */
indigo_logger(LOG_LEVEL_INFO, "icmp echo reply from %s, Receive echo %d bytes data", target_ip, n - 20);
pkt_rcv++;
} else {
indigo_logger(LOG_LEVEL_INFO, "Received packet is not the ICMP reply from the Destination");
}
}
usleep(rate * 1000000);
}
close(sock);
return pkt_rcv;
}
int send_broadcast_arp(char *target_ip, int *send_count, int rate) {
char buffer[S_BUFFER_LEN];
FILE *fp;
int recv = 0;
#ifdef _OPENWRT_
snprintf(buffer, sizeof(buffer), "arping -I %s %s -c %d -b | grep broadcast", get_wireless_interface(), target_ip, *send_count);
#else
snprintf(buffer, sizeof(buffer), "arping -i %s %s -c %d -W %d | grep packet", get_wireless_interface(), target_ip, *send_count, rate);
#endif
fp = popen(buffer, "r");
if (fp == NULL)
return 0;
#ifdef _OPENWRT_
//Format: Sent 3 probe(s) (3 broadcast(s))
fgets(buffer, sizeof(buffer), fp);
sscanf(buffer, "%*s %d", send_count);
//Format: Received 0 reply (0 request(s), 0 broadcast(s))
fgets(buffer, sizeof(buffer), fp);
sscanf(buffer, "%*s %d", &recv);
#else
//arping output format: 1 packets transmitted, 1 packets received, 0% unanswered (0 extra)
fscanf(fp, "%d %*s %*s %d", send_count, &recv);
#endif
indigo_logger(LOG_LEVEL_INFO, "ARP TEST - send: %d recv: %d", *send_count, recv );
pclose(fp);
return recv;
}
int find_interface_ip(char *ipaddr, int ipaddr_len, char *name) {
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char *addr = NULL;
getifaddrs(&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET && strcmp(ifa->ifa_name, name) == 0) {
sa = (struct sockaddr_in *) ifa->ifa_addr;
addr = inet_ntoa(sa->sin_addr);
if (ipaddr) {
strcpy(ipaddr, addr);
}
return 1;
}
}
freeifaddrs(ifap);
return 0;
}
int get_mac_address(char *buffer, int size, char *interface) {
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd <= 0) {
goto done;
}
strcpy(s.ifr_name, interface);
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
(char)s.ifr_addr.sa_data[0]&0x00ff, (char)s.ifr_addr.sa_data[1]&0x00ff, (char)s.ifr_addr.sa_data[2]&0x00ff,
(char)s.ifr_addr.sa_data[3]&0x00ff, (char)s.ifr_addr.sa_data[4]&0x00ff, (char)s.ifr_addr.sa_data[5]&0x00ff);
close(fd);
return 0;
}
close(fd);
done:
return 1;
}
int set_mac_address(char *ifname, char *mac) {
char cmd[S_BUFFER_LEN];
/* If the system doesn't support ip command, please use ifconfig. E.g., */
/* sprintf(cmd, "ifconfig %s hw ether %s", ifname, mac_addr) */
sprintf(cmd, "ip link set dev %s address %s", ifname, mac);
return system(cmd);
}
int bridge_created = 0;
char* get_wlans_bridge() {
return wlans_bridge;
}
int set_wlans_bridge(char* br) {
memset(wlans_bridge, 0, sizeof(wlans_bridge));
snprintf(wlans_bridge, sizeof(wlans_bridge), "%s", br);
printf("\nwlans_bridge = %s.\n", wlans_bridge);
return 0;
}
int is_bridge_created() {
return bridge_created;
}
void bridge_init(char *br) {
/* Create bridge for multiple VAPs */
if (configured_interface_count >= 2) {
create_bridge(br);
add_all_wireless_interface_to_bridge(br);
}
}
int create_bridge(char *br) {
char cmd[S_BUFFER_LEN];
/* Create new bridge */
sprintf(cmd, "brctl addbr %s", br);
system(cmd);
/* Bring up bridge */
control_interface(br, "up");
bridge_created = 1;
return 0;
}
int add_interface_to_bridge(char *br, char *ifname) {
char cmd[S_BUFFER_LEN];
/* Reset IP address */
reset_interface_ip(ifname);
/* Add interface to bridge */
sprintf(cmd, "brctl addif %s %s", br, ifname);
system(cmd);
printf("%s\n", cmd);
return 0;
}
int reset_bridge(char *br) {
char cmd[S_BUFFER_LEN];
/* Bring down bridge */
control_interface(br, "down");
sprintf(cmd, "brctl delbr %s", br);
system(cmd);
bridge_created = 0;
return 0;
}
int add_wireless_interface(char *ifname) {
char cmd[S_BUFFER_LEN];
sprintf(cmd, "iw dev %s interface add %s type managed", get_wireless_interface(), ifname);
system(cmd);
return 0;
}
int delete_wireless_interface(char *ifname) {
char cmd[S_BUFFER_LEN];
sprintf(cmd, "iw dev %s del", ifname);
system(cmd);
return 0;
}
int control_interface(char *ifname, char *op) {
char cmd[S_BUFFER_LEN];
/* If the system doesn't support ip command, please use ifconfig. E.g., */
/* sprintf(cmd, "ifconfig %s %s", ifname, op); */
sprintf(cmd, "ip link set %s %s", ifname, op);
system(cmd);
return 0;
}
int set_interface_ip(char *ifname, char *ip) {
char cmd[S_BUFFER_LEN];
/* If the system doesn't support ip command, please use ifconfig. */
/* Please also update the caller to use netmask instead of CIDR. E.g., */
/* sprintf(cmd, "ifconfig %s %s", ifname, ip); */
sprintf(cmd, "ip addr add %s dev %s", ip, ifname);
system(cmd);
return 0;
}
int reset_interface_ip(char *ifname) {
char cmd[S_BUFFER_LEN];
/* If the system doesn't support ip command, please use ifconfig. E.g., */
/* sprintf(cmd, "ifconfig %s 0.0.0.0", ifname); */
sprintf(cmd, "ip addr flush dev %s", ifname);
return system(cmd);
}
void detect_del_arp_entry(char *ip) {
char buffer[S_BUFFER_LEN];
char res_ip[32], res_dev[16], res_inf[16];
FILE *fp;
snprintf(buffer, sizeof(buffer), "ip neigh show %s", ip);
fp = popen(buffer, "r");
if (fp == NULL)
return;
if (NULL == fgets(buffer, sizeof(buffer), fp)) {
} else if (3 == sscanf(buffer, "%s %s %s", res_ip, res_dev, res_inf)) {
if (!strcmp(res_ip, ip) && !strcmp(res_dev, "dev")) {
indigo_logger(LOG_LEVEL_INFO, "Delete existing ARP entry: %s", ip);
snprintf(buffer, sizeof(buffer), "ip neigh del %s %s %s", res_ip, res_dev, res_inf);
system(buffer);
} else {
indigo_logger(LOG_LEVEL_INFO, "Format mismatch?: %s %s %s\n", res_ip, res_dev, res_inf);
}
}
pclose(fp);
return;
}
int add_all_wireless_interface_to_bridge(char *br) {
int i;
for (i = 0; i < interface_count; i++) {
if (interfaces[i].identifier != UNUSED_IDENTIFIER) {
control_interface(interfaces[i].ifname, "up");
add_interface_to_bridge(br, interfaces[i].ifname);
}
}
return 0;
}
/* Environment */
int service_port = SERVICE_PORT_DEFAULT;
char hapd_exec_file[64];
char hapd_full_exec_path[64] = HAPD_EXEC_FILE_DEFAULT;
char hapd_ctrl_path[64] = HAPD_CTRL_PATH_DEFAULT;
char hapd_full_ctrl_path[128];
char hapd_global_ctrl_path[64] = HAPD_GLOBAL_CTRL_PATH_DEFAULT;
char hapd_conf_file[64] = HAPD_CONF_FILE_DEFAULT;
int hostapd_debug_level = DEBUG_LEVEL_DISABLE;
char wpas_exec_file[64];
char wpas_full_exec_path[64] = WPAS_EXEC_FILE_DEFAULT;
char wpas_ctrl_path[64] = WPAS_CTRL_PATH_DEFAULT;
char wpas_full_ctrl_path[128];
char wpas_global_ctrl_path[64] = WPAS_GLOBAL_CTRL_PATH_DEFAULT;
char wpas_conf_file[64] = WPAS_CONF_FILE_DEFAULT;
int wpas_debug_level = DEBUG_LEVEL_DISABLE;
struct interface_info* assign_wireless_interface_info(struct bss_identifier_info *bss) {
int i;
for (i = 0; i < interface_count; i++) {
if ((interfaces[i].band == bss->band) &&
(interfaces[i].identifier == UNUSED_IDENTIFIER)) {
configured_interface_count++;
interfaces[i].identifier = bss->identifier;
interfaces[i].mbssid_enable = bss->mbssid_enable;
interfaces[i].transmitter = bss->transmitter;
interfaces[i].hapd_bss_id = band_mbssid_cnt[bss->band];
band_mbssid_cnt[bss->band]++;
memset(interfaces[i].hapd_conf_file, 0, sizeof(interfaces[i].hapd_conf_file));
snprintf(interfaces[i].hapd_conf_file, sizeof(interfaces[i].hapd_conf_file),
"%s/hostapd_%s.conf", HAPD_CONF_FILE_DEFAULT_PATH, interfaces[i].ifname);
return &interfaces[i];
}
}
return NULL;
}
struct interface_info* get_wireless_interface_info(int band, int identifier) {
int i;
for (i = 0; i < interface_count; i++) {
if ((interfaces[i].band == band) &&
((interfaces[i].identifier != UNUSED_IDENTIFIER) &&
(interfaces[i].identifier == identifier))) {
return &interfaces[i];
}
}
return NULL;
}
struct interface_info* get_first_configured_wireless_interface_info() {
int i;
for (i = 0; i < interface_count; i++) {
if (interfaces[i].identifier != UNUSED_IDENTIFIER) {
return &interfaces[i];
}
}