-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_cloud.c
2064 lines (1761 loc) · 70.1 KB
/
merge_cloud.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
/* merge_cloud.c - cloud hub main program; link-level mesh networking
*
* Copyright (C) 2012, Greg Johnson
* Released under the terms of the GNU GPL v2.0.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* $Id: merge_cloud.c,v 1.42 2012-02-22 19:27:23 greg Exp $
*/
static const char Version[] = "Version "
"$Id: merge_cloud.c,v 1.42 2012-02-22 19:27:23 greg Exp $";
/* This is the main program in the cloud_hub project. It creates and
* maintains a link-level mesh network, and manages client traffic over
* the mesh.
*
* command-line arguments:
* -a /tmp/sig_strength: file to save mac addresses and signal
* strengths we have detected from other boxes
*
* -A: run in ad-hoc mode versus WDS mode. (found that works a LOT
* better.)
*
* -b /tmp/eth_beacons: file containing the mac addresses of other cloud
* boxes in our mesh that we notice via cat-5 LAN interface
*
* -c N: for debugging; process N messages and then exit
*
* -d /tmp/beacons: file containing the mac addresses of other cloud
* boxes in our mesh that we notice wirelessly
*
* -D N: toggle debug variable N
*
* -e eth0: the name of the cat-5 lan interface
*
* -E nn:nn:nn:nn:nn:nn
* specify the mac address of the cat-5 lan interface. if this is
* not given, use ifconfig to figure it out.
*
* -f: send ethernet beacon messages out on the cat-5 LAN interface,
* so that other boxes that can see me via cat-5 can join in my
* mesh, but communicating via "infinitely high signal strength"
* cat-5 connection
*
* -i /tmp: run in simulation mode, using pipes for communication to
* simulate wireless traffic. this is old, probably broken, and
* would be worth getting working again at some point.
*
* -l: run an ll_shell_ftp service out of merge_cloud on the
* LAN cat-5 interface
*
* -L /tmp/perm_log_file:
* name of permanent log file to write to under debugging control
* (if we want a long file rather than the short truncated file
* generated by "label" from our stdout/stderr)
*
* -m prism0: name of device to read for monitoring traffic to look
* for wireless beacons
*
* -n: don't read from stdin for interactive commands; run in batch mode
*
* -N nn:nn:nn:nn:nn:nn
* specify mac address of client for link-level interactive debugging
*
* -p /tmp/other_box_info:
* specify the file containing info about other boxes - their
* mac addresses, signal strength from them to us, etc.
* this is an input to merge_cloud. the kernel or another
* program might generate it, or perhaps merge_cloud itself
* might generate it if the same file name is used here and with
* the "-a" argument above.
*
* -P: use a second interface (i.e., eth0:1) for mesh protocol messages,
* separate from the interface (i.e., eth0) used for client payload
* messages. this is the default, and is a very good idea to always
* leave on.
*
* -s /tmp/cloud_status:
* specify the file that mesh status is written to, so that
* the status_lights utility can read it and blink lights on the
* front of the box giving status information about the mesh
*
* -T /tmp/temp_log:
* name of temporary log file (we use this to send debug messages
* to the internet interface)
*
* -w eth0: the wireless interface (i.e., "wlan0" etc.)
*
* -W nn:nn:nn:nn:nn:nn
* specify the mac address of the wireless interface
* (we figure it out on our own using ifconfig if this isn't given)
*
* -y: detect signal strength by looking at wireless beacons
* (alternative is debugging mode where we look in signal
* strength file and ignore wireless beacons)
*/
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <features.h> /* for the glibc version number */
#include <asm/types.h>
#include <sys/types.h>
#include <linux/wireless.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <netinet/in.h>
#include <ctype.h>
#include "cloud.h"
#include "mac.h"
#include "util.h"
#include "pio.h"
#include "wrt_util.h"
#include "eth_util.h"
#include "com_util.h"
#include "status.h"
#include "device_type.h"
#include "graphit.h"
#include "sequence.h"
#include "html_status.h"
#include "ping.h"
#include "cloud_mod.h"
#include "print.h"
#include "timer.h"
#include "random.h"
#include "io_stat.h"
#include "ad_hoc_client.h"
#include "lock.h"
#include "stp_beacon.h"
#include "device.h"
#include "nbr.h"
#include "scan_msg.h"
#include "parm_change.h"
#ifdef WRT54G
#include "pcritical_section.h"
#else
#include "critical_section.h"
#endif
/* debugging and configuration variables that can be changed at runtime
* both locally and via messages across the entire cloud
*/
char_str_t db[] = {
/* 0 */ {0, "add and delete devices"},
/* 1 */ {0, "communication of non-cloud client messages"},
/* 2 */ {0, "wait times"},
/* 3 */ {0, "internal state (lock lists etc.)"},
/* 4 */ {0, "terse, one-line message counts"},
/* 5 */ {1, "permit local improvement"},
/* 6 */ {0, "stp beacons (sending and receiving)"},
/* 7 */ {0, "send_cloud_message pings"},
/* 8 */ {0, "send_cloud_message print almost all messages"},
/* 9 */ {0, "check_connectivity"},
/* 10 */ {0, "local improvement debugging"},
/* 11 */ {0, "check_local_improvement debugging"},
/* 12 */ {0, "send_cloud_message print stp_beacon_msg's"},
/* 13 */ {0, "bcast_forward_message non-cloud messages"},
/* 14 */ {0, "recvfrom print"},
/* 15 */ {0, "sendto print"},
/* 16 */ {0, "pre_repeated_cloud_maint"},
/* 17 */ {0, "check_nbr_devices"},
/* 18 */ {0, "debug output to eth0"},
/* 19 */ {0, "post_repeated_cloud_maint control flow"},
/* 20 */ {0, "do_eth_beacon_no_send"},
/* 21 */ {0, "connectivity when something happens"},
/* 22 */ {0, "turn on flow control (always disabled by default; broken)"},
/* 23 */ {0, "flow control debug"},
/* 24 */ {0, "introduce dropped packet to test flow control"},
/* 25 */ {0, "short print noncloud io stats"},
/* 26 */ {0, "short print cloud io stats"},
/* 27 */ {0, "suppress fprintf(stderr, ..)"},
/* 28 */ {0, "alarm timeouts"},
/* 29 */ {0, "debug drop an stp_beacon_recv_msg"},
/* 30 */ {0, "send cloud status with stp beacons"},
/* 31 */ {0, "build cloud stp list"},
/* 32 */ {0, "debug cloud_stp_list"},
/* 33 */ {0, "debug output to eth0 for interactive commands"},
/* 34 */ {0, "main select"},
/* 35 */ {0, "write to permanent log file"},
/* 36 */ {1, "disable messages on the wlan device"},
/* 37 */ {0, "overwrite log file"},
/* 38 */ {0, "generate mesh topology display"},
/* 39 */ {1, "slow down stp beacons based on number of boxes in cloud"},
/* 40 */ {0, "multiplicative factor to slow down stp beacons to debug"},
/* 41 */ {0, "print only 1 out of 20 error messages"},
/* 42 */ {0, "dump all messages from cloud device"},
/* 43 */ {0, "dump all messages from non-cloud devices"},
/* 44 */ {0, "debug k-of-n message delivery"},
/* 45 */ {0, "debug print beacon messages"},
/* 46 */ {1, "enable ping neighbors"},
/* 47 */ {1, "use secondary :1 devices for cloud messages"},
/* 48 */ {0, "debug :1 accounting problem"},
/* 49 */ {0, "wait_till_wont_block"},
/* 50 */ {1, "ad-hoc passive client mode"},
/* 51 */ {0, "debug ad_hoc_clients"},
/* 52 */ {0, "debug ad-hoc passive client mode"},
/* 53 */ {0, "debug input_available after select"},
/* 54 */ {0, "make send_stp interrupts manual"},
/* 55 */ {0, "clear permanent log file"},
/* 56 */ {0, "debug ad-hoc client broadcast messages"},
/* 57 */ {0, "emergency dump every message"},
/* 58 */ {0, "wrt_util - print histogram of beacon inter-arrival times"},
/* 59 */ {0, "wrt_util - debug wrt_util_process_message"},
/* 60 */ {1, "non-local shortcuts for wrapped client messages"},
/* 61 */ {0, "debug non-local shortcuts for wrapped client messages"},
/* 62 */ {1, "enable wifi scanning"},
/* 63 */ {0, "debug wifi scanning"},
/* 64 */ {1, "debug global wifi paramater changing"},
/* 65 */ {0, "don't really do global wifi paramater change for debugging"},
{-1, NULL},
};
bool_t update_cloud_db = false;
int cloud_db_ind;
#define MAX_CLOUD 32
char *wds_file = "/proc/net/hostap/wlan0/wds";
char *eth_fname = "/usr/local/etc/wifi_cloud/eth_beacons";
char *wlan_fname = "/usr/local/etc/wifi_cloud/beacons";
char *sig_strength_fname = "/proc/net/hostap/wlan0/ap";
char *perm_log_fname = "/tmp/merge_cloud.perm_log";
char *temp_log_fname = "/tmp/merge_cloud.temp_log";
char *eth_device_name = 0;
char *wlan_device_name = "wlan0";
bool_t have_mon_device = false;
char *wlan_mon_device_name = NULL;
static int prism_fd = -1;
char use_pipes = 0;
char *pipe_directory = NULL;
static char stdin_input = 1;
static int msg_count = -1;
bool_t ad_hoc_mode = false;
/* use a separate interface such as eth0:1 etc. for cloud messages? */
static bool_t cloud_interface = true;
char do_wrt_beacon = 0;
static char do_eth_beacon = 0;
char do_ll_shell = 0;
char did_com_util_init = 0;
static mac_address_t com_util_mac_address = {0,0,0,0,0,0};
int wrapper_len = (int) (&((message_t *) 0)->v.msg.msg_body);
static byte *wrapper;
short my_weakest_stp_link = -1;
static short cloud_weak_link_boxes = -1;
static char *cloud_status_file = "/tmp/cloud_status";
static char cloud_status_tmp_file[PATH_MAX] = "/tmp/cloud_status.tmp";
int max_packet_len = 0;
static void cleanup(void);
/* this is my official name as a cloud box. */
mac_address_t my_wlan_mac_address;
/* did we get our wireless mac address from a command-line argument,
* or do we need to figure it out?
*/
static char got_wlan_mac_addr = 0;
/* this is the optional mac address of my ethernet port. it is good
* iff eth_device_name is not null.
*/
mac_address_t my_eth_mac_address;
static char got_eth_mac_addr = 0;
#if 0
/* convenient debugging breakpoint function */
static void bp1(void)
{
ddprintf("hi from bp1.\n");
}
#endif
/* I think this means we got called because of the arrival of a beacon,
* and it is in the beacon variable below.
*/
bool_t have_beacon = false;
message_t beacon;
bool_t have_my_beacon = false;
message_t my_beacon;
/* these are our stp neighbors */
node_t stp_list[MAX_CLOUD];
int stp_list_count = 0;
/* do a complete print of the state of this cloud box.
* print the locks we own, the locks we have granted to other boxes,
* and the locks we have pending requests for to other boxes.
*
* print timed out locks.
*
* print the list of all nodes in our cloud.
*
* print the list of all of our neighbors, i.e., nodes we see 802.11 beacons
* from or eth_beacons from (i.e., nodes we are attached to via cat-5 cable).
*/
void print_state()
{
print_lockable_list(pending_requests, pending_request_count,
"pending_requests");
print_lockable_list(locks_granted, locks_granted_count,
"locks_granted");
print_lockable_list(locks_owned, locks_owned_count,
"locks_owned");
print_lockable_list(timed_out_lockables, timed_out_lockable_count,
"timed out lockables");
node_list_print(eprintf, stderr, "stp_list", stp_list, stp_list_count);
print_cloud_list(nbr_device_list, nbr_device_list_count, "neighbor list:");
ddprintf("my originator sequence number: %d\n",
(int) originator_sequence_num);
print_stp_recv_beacons();
}
// #define DEBUG_48
#ifdef DEBUG_48
static message_t db_messages[MAX_CLOUD];
static int db_i[MAX_CLOUD];
static int db_dev_index[MAX_CLOUD];
#endif
/* this is an oh-sh*t type situation. we abort all cloud protocol we were
* in the middle of, and delete all stp beacons not from direct neighbors.
*/
void to_nominal_state()
{
locks_owned_count = 0;
locks_granted_count = 0;
pending_request_count = 0;
/* get rid of received beacons that came from stp neighbors we
* no longer have.
*/
trim_stp_recv_beacons(from_stp_neighbor);
}
/* this is for status_lights, the external indicator on the box of our
* model of the whole cloud.
*/
static void update_cloud_status()
{
int i, j;
bool_t found;
FILE *status;
if (cloud_status_file == NULL) { return; }
my_weakest_stp_link = 10000;
for (i = 0; i < stp_list_count; i++) {
found = false;
for (j = 0; j < nbr_device_list_count; j++) {
if (mac_equal(nbr_device_list[j].name, stp_list[i].box.name)) {
found = true;
break;
}
}
if (found) {
if (my_weakest_stp_link > nbr_device_list[j].signal_strength) {
my_weakest_stp_link = nbr_device_list[j].signal_strength;
}
}
}
if (my_weakest_stp_link == 10000) {
my_weakest_stp_link = -1;
}
cloud_weak_link_boxes = 0;
if (my_weakest_stp_link != -1 && my_weakest_stp_link < weak_threshold) {
cloud_weak_link_boxes++;
}
for (i = 0; i < stp_recv_beacon_count; i++) {
if (stp_recv_beacons[i].stp_beacon.weakest_stp_link < weak_threshold
&& stp_recv_beacons[i].stp_beacon.weakest_stp_link != -1)
{
cloud_weak_link_boxes++;
}
}
status = fopen(cloud_status_tmp_file, "w");
if (status == NULL) {
ddprintf("update_cloud_status; unable to open %s: %s\n",
cloud_status_tmp_file, strerror(errno));
return;
}
fprintf(status, "%4d # cloud box count\n", stp_recv_beacon_count + 1);
fprintf(status, "%4d # my weakest stp link\n", my_weakest_stp_link);
fprintf(status, "%4d # cloud weak link count\n", cloud_weak_link_boxes);
fclose(status);
if (rename(cloud_status_tmp_file, cloud_status_file) != 0) {
ddprintf("update_cloud_status; unable to rename %s to %s: %s\n",
cloud_status_tmp_file, cloud_status_file, strerror(errno));
}
} /* update_cloud_status */
/* do initial periodic processing after an internally generated timing
* interrupt if the interrupt was a send_stp or a lockable_timeout.
* we check to see if there are any new cloud boxes or clients we didn't
* already know about, and update our idea of our own cloud box neighbors.
* we update our idea of signal strength to our neighbors, i.e., the cloud
* boxes we can see directly.
*
* this routine is called before processing any incoming messages that
* may be available to be read and processed.
*/
static bool_t pre_repeated_cloud_maint()
{
bool_t result;
bool_t nbr_connectivity_changed;
if (db[16].d) { ddprintf("pre_repeated_cloud_maint..\n"); }
result = check_devices();
if (db[16].d) { ddprintf(" changed %d..\n", result); }
nbr_connectivity_changed = check_nbr_devices();
result |= nbr_connectivity_changed;
if (nbr_connectivity_changed) { update_stp_nbr_connectivity(); }
update_nbr_signal_strength();
if (db[16].d) { ddprintf(" changed %d..\n", result); }
return result;
}
/* do final periodic processing after an internally generated timing
* interrupt if the interrupt was a send_stp or a lockable_timeout.
* this routine is called after pending incoming messages have been
* processed. we check the cloud topology and decide based on measured
* signal strengths to neighbors whether we want to initiate a cloud
* topology change. we send out an stp beacon.
*/
static void post_repeated_cloud_maint()
{
if (db[3].d) { ddprintf("post_repeated_cloud_maint..\n"); }
timeout_lockables();
check_connectivity();
send_stp_beacon(false /* no disconnected nbr */);
if (db[5].d) {
check_local_improvement();
}
if (db[50].d) {
check_ad_hoc_client_improvement();
}
} /* post_repeated_cloud_maint */
static void catch_hup(int arg)
{
ddprintf("caught a hup..\n");
}
static void catch_int(int arg)
{
ddprintf("caught an int..\n");
}
/* for debugging; after "msg_count" messages, exit. */
void check_msg_count()
{
if (msg_count != -1) {
if (--msg_count <= 0) {
cleanup();
exit(0);
}
ddprintf("%d: ", msg_count);
}
}
static void reset_state(void)
{
pending_request_count = 0;
locks_granted_count = 0;
locks_owned_count = 0;
timed_out_lockable_count = 0;
stp_recv_beacon_count = 0;
stp_list_count = 0;
io_stat_count = 0;
}
/* delete pending requests, locks granted, and locks owned to this node,
* and delete them from our stp_list. circularity has been detected.
* after clearing everything, we will probably try to rebuild, but that's
* not the job of this routine.
*/
void clear_state(mac_address_ptr_t name)
{
delete_lockable(pending_requests, &pending_request_count, name);
delete_lockable(locks_granted, &locks_granted_count, name);
delete_lockable(locks_owned, &locks_owned_count, name);
delete_me = name;
trim_list(stp_list, &stp_list_count, delete_node, NULL);
delete_stp_beacon(name);
}
static void usage()
{
ddprintf("usage:\ncloud [-e ethN] \\\n"
" [-E eth-mac-address] \\\n"
" [-W wlan-mac-address]\n"
" [-w wlanN] \\\n"
" [-p wds_file] \\\n"
" [-a sig_strength_file] \\\n"
" [-b eth_beacon_file] \\\n"
" [-d wireless_beacon_file] \\\n"
" [-c message_count] \\\n"
" [-D debug_index] \\\n"
" [-n] \\\n"
" [-i pipe_directory]\n");
exit(1);
}
static void process_args(int argc, char **argv)
{
int c;
int i;
ddprintf("args: ");
for (i = 0; i < argc; i++) {
ddprintf("%s ", argv[i]);
}
ddprintf("\n");
while ((c = getopt(argc, argv, "Alfm:yc:np:a:w:W:b:d:e:E:D:i:N:s:L:P"))
!= -1)
{
switch (c) {
case 'a' :
sig_strength_fname = strdup(optarg);
break;
case 'A' :
ad_hoc_mode = true;
break;
case 'b' :
eth_fname = strdup(optarg);
break;
case 'c' : {
int result = sscanf(optarg, "%d", &msg_count);
if (result != 1) {
ddprintf("invalid message count '%s'\n", optarg);
exit(1);
}
break;
}
case 'd' :
wlan_fname = strdup(optarg);
break;
case 'D' :
tweak_db(optarg, db);
break;
case 'e' :
eth_device_name = strdup(optarg);
break;
case 'E' :
if (1 != mac_sscanf(my_eth_mac_address, optarg)) {
ddprintf("invalid eth mac address '%s'\n", optarg);
exit(1);
}
got_eth_mac_addr = 1;
break;
case 'f' :
do_eth_beacon = 1;
break;
case 'i' :
use_pipes = 1;
pipe_directory = strdup(optarg);
break;
case 'l' :
do_ll_shell = 1;
break;
case 'L' :
perm_log_fname = strdup(optarg);
break;
case 'm' :
have_mon_device = true;
wlan_mon_device_name = strdup(optarg);
break;
case 'n' :
stdin_input = 0;
break;
case 'N' :
if (1 != mac_sscanf(com_util_mac_address, optarg)) {
ddprintf("invalid com_util mac address '%s'\n", optarg);
exit(1);
}
break;
case 'p' :
wds_file = strdup(optarg);
break;
case 'P' :
cloud_interface = true;
db[47].d = true;
break;
case 's' :
cloud_status_file = strdup(optarg);
strncpy(cloud_status_tmp_file, cloud_status_file, PATH_MAX);
break;
case 'T' :
temp_log_fname = strdup(optarg);
break;
case 'w' :
wlan_device_name = strdup(optarg);
break;
case 'W' :
if (1 != mac_sscanf(my_wlan_mac_address, optarg)) {
ddprintf("invalid wlan mac address '%s'\n", optarg);
exit(1);
}
got_wlan_mac_addr = 1;
break;
case 'y' :
do_wrt_beacon = 1;
break;
default :
usage();
}
}
}
#if 0
/* debug-print bits of a select() result */
static void db_print_read_set(char *title, int result, fd_set *read_set,
int max_fd)
{
int i;
ddprintf("%s; select returned %d: ", title, result);
for (i = 0; i < max_fd + 1; i++) {
if (FD_ISSET(i, read_set)) {
ddprintf("%d ", i);
}
}
ddprintf("\n");
}
#endif
/* do a non-blocking test to see if fd can be read without blocking */
static bool_t read_wont_block(int fd)
{
static struct timeval timer = { 0, 0 };
fd_set read_set;
int result;
FD_ZERO(&read_set);
FD_SET(fd, &read_set);
result = select(fd + 1, &read_set, 0, 0, &timer);
return (FD_ISSET(fd, &read_set));
}
static void cleanup()
{
#ifdef WRT54G
pcritical_section_delete();
#else
critical_section_delete();
#endif
if (do_ll_shell) {
com_util_exit();
}
}
int main(int argc, char **argv)
{
struct sockaddr_ll recv_arg;
socklen_t recv_arg_len;
int result;
int input_available;
int got_input;
char buf[256];
int i;
bool_t fake_interrupt = false;
/* avoid compiler warnings */
(void) mac_buf1; (void) mac_buf2; (void) mac_buf3;
while (!checked_gettimeofday(&now));
start = now;
/* pretend that each timed function last occurred at program start */
times[0] = now;
times[1] = now;
times[2] = now;
process_args(argc, argv);
#ifdef WRT54G
pcritical_section_init();
#else
critical_section_init();
#endif
/* if we are in ad-hoc mode, disable messsages to wireless ap clients */
if (ad_hoc_mode) { db[36].d = true; }
/* for now, just always disable flow control. */
db[22].d = false;
while (true) {
wrapper = malloc(wrapper_len);
if (wrapper == NULL) {
ddprintf("give me a break. the malloc for wrapper didn't work.\n");
} else {
break;
}
}
for (i = 0; i < MAX_CLOUD; i++) {
send_sequence[i] = 0;
send_data_sequence[i] = 0;
send_ping_sequence[i] = 0;
have_recv_sequence[i] = false;
have_recv_data_sequence[i] = false;
have_recv_ping_sequence[i] = false;
}
improve_prob_init(stp_recv_beacon_count);
if (!got_wlan_mac_addr) {
mac_get(my_wlan_mac_address, wlan_device_name);
}
init_cloud_stp_tree();
/* adding wlan0:1 first hopefully means that if there are available
* messages both on it and wlan0, wlan0:1 will get processed first.
* (would like to process 'ignore ad-hoc client bcast messages' messages
* before ad-hoc client bcast messages.)
*/
if (db[47].d && ad_hoc_mode) {
sprintf(buf, "%s:1", wlan_device_name);
add_device(buf, my_wlan_mac_address, device_type_cloud_wlan);
}
add_device(wlan_device_name, my_wlan_mac_address, device_type_wlan);
status_add_by_mac(perm_io_stat, &perm_io_stat_count, my_wlan_mac_address,
device_type_wlan);
if (db[50].d) {
promiscuous(wlan_device_name, true);
}
if (eth_device_name != 0) {
if (!got_eth_mac_addr) {
mac_get(my_eth_mac_address, eth_device_name);
}
/* for consistency with above; process ethN:1 messages before ethN
* messages.
*/
if (db[47].d) {
sprintf(buf, "%s:1", eth_device_name);
add_device(buf, my_eth_mac_address, device_type_cloud_eth);
}
add_device(eth_device_name, my_eth_mac_address, device_type_eth);
status_add_by_mac(perm_io_stat, &perm_io_stat_count, my_eth_mac_address,
device_type_eth);
promiscuous(eth_device_name, true);
}
if (have_mon_device) {
add_device(wlan_mon_device_name, my_wlan_mac_address,
device_type_wlan_mon);
prism_fd = device_list[device_list_count - 1].fd;
ddprintf("prism fd: %d\n", prism_fd);
}
init_random(my_wlan_mac_address);
timer_init();
if (do_eth_beacon || do_ll_shell || do_wrt_beacon) {
int i;
char did_eth_beacon = 0, did_wrt_beacon = 0;
for (i = 0; i < device_list_count; i++) {
if (do_eth_beacon
&& device_list[i].device_type == device_type_eth
&& !did_eth_beacon)
{
did_eth_beacon = 1;
eth_util_init(eth_fname, device_list[i].fd,
device_list[i].if_index,
use_pipes ? &device_list[i].out_pio : 0,
my_eth_mac_address,
my_wlan_mac_address);
}
if (do_ll_shell
&& device_list[i].device_type == device_type_wlan
&& !did_com_util_init)
{
did_com_util_init = 1;
com_util_init(device_list[i].fd, device_list[i].if_index,
use_pipes ? &device_list[i].out_pio : 0,
my_wlan_mac_address, com_util_mac_address);
}
if (do_wrt_beacon
&& device_list[i].device_type == device_type_wlan
&& !did_wrt_beacon)
{
did_wrt_beacon = 1;
wrt_util_init(wlan_fname, sig_strength_fname);
}
}
if (do_eth_beacon && !did_eth_beacon) {
ddprintf("did not find eth device for eth_util_init\n");
exit(1);
}
if (do_ll_shell && !did_com_util_init) {
ddprintf("did not find eth device for com_util_init\n");
exit(1);
}
if (do_wrt_beacon && !did_wrt_beacon) {
ddprintf("did not find wrt device for wrt_util_init\n");
exit(1);
}
}
if (do_ll_shell) {
if (com_util_start_shell() != 0) {
ddprintf("com_util_start_shell failed\n");
exit(1);
}
}
check_devices();
check_nbr_devices();
update_nbr_signal_strength();
#if USE_TIMER
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = repeated;
sigaction(SIGALRM, &action, NULL);
set_next_alarm();
memset(&action, 0, sizeof(action));
action.sa_handler = catch_hup;
sigaction(SIGHUP, &action, NULL);
memset(&action, 0, sizeof(action));
action.sa_handler = catch_int;
sigaction(SIGINT, &action, NULL);
}
#else
/* for gdb-based debugging */
// signal(SIGINT, &repeated);
#endif
while (1) {
int max_fd = -1;
int dev, dev_index;
fd_set read_set;
int select_result;
/* set up the read_set for the select */
FD_ZERO(&read_set);
/* get input from a pio? rejuvenate this code eventually. */
input_available = 0;
if (use_pipes) {
int i;
for (i = 0; i < device_list_count; i++) {
if (ack_read_ok(i) && pio_read_ok(&device_list[i].in_pio)) {
input_available = 1;
break;
}
}
}
/* do a select to see which interface has input available.
* we do the wds interfaces, the wireless interfaces, and the ethernet
* interface. if we are doing flow control, and are waiting for
* an ack from one or more downstream devices, only read input from
* the devices that may give us the ack's we are waiting for.
*/
if (!input_available) {
int i;
for (i = 0; i < device_list_count; i++) {
if (ack_read_ok(i)) {
if (db[47].d
|| (device_list[i].device_type != device_type_cloud_wlan
&& device_list[i].device_type != device_type_cloud_eth))
{
FD_SET(device_list[i].fd, &read_set);
if (device_list[i].fd > max_fd) {
max_fd = device_list[i].fd;
}
}
}
}
FD_SET(interrupt_pipe[0], &read_set);
if (interrupt_pipe[0] > max_fd) { max_fd = interrupt_pipe[0]; }
if (stdin_input) { FD_SET(0, &read_set); }
if (do_ll_shell) {
com_util_set_select(&max_fd, &read_set);
}
select_result = select(max_fd + 1, &read_set, 0, 0, 0);
if (select_result == -1) {
if (errno != EINTR) {
ddprintf("main; select failed: %s\n", strerror(errno));
}
continue;
}
if (db[53].d) {
bool_t prt = false;
for (i = 0; i < device_list_count; i++) {
if (FD_ISSET(device_list[i].fd, &read_set)
&& device_list[i].device_type != device_type_wlan_mon)
{
if (!prt) {