-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud_mod.c
1899 lines (1597 loc) · 57.5 KB
/
cloud_mod.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
/* cloud_mod.c - change and optimize connections among the boxes in the cloud
*
* 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: cloud_mod.c,v 1.16 2012-02-22 19:27:22 greg Exp $
*/
static const char Version[] = "Version "
"$Id: cloud_mod.c,v 1.16 2012-02-22 19:27:22 greg Exp $";
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "cloud.h"
#include "cloud_mod.h"
#include "print.h"
#include "lock.h"
#include "random.h"
#include "timer.h"
#include "nbr.h"
#include "stp_beacon.h"
#include "cloud_box.h"
char force_local_change = 0;
char old_db5;
/* are we in the midst of a protocol activity to change the connectivity
* of the stp tree?
*/
bool_t doing_stp_update()
{
int i;
if (locks_owned_count > 0) { return true; }
if (locks_granted_count > 0) { return true; }
for (i= 0; i < pending_request_count; i++) {
lockable_resource_t *l = &pending_requests[i];
if (l->type == local_stp_add_request_msg
|| l->type == local_lock_req_new_msg
|| l->type == local_lock_req_old_msg
|| l->type == local_stp_add_changed_request_msg
|| l->type == local_stp_delete_request_msg)
{
return true;
}
}
return false;
}
/* initiate the process of possibly swapping arcs to adjacent cloud boxes,
* if an arc not being used is better than one that is being used.
* see merge_cloud.notes.
*/
void check_local_improvement()
{
int i, j, k;
char found;
mac_address_ptr_t old, new;
int max_strength, min_strength;
int max_diff, new_sig_strength, old_sig_strength;
if (db[11].d) {
ddprintf("check_local_improvement..\n");
print_cloud_list(nbr_device_list, nbr_device_list_count,
"neighbor list:");
node_list_print(eprintf, stderr, "stp_list", stp_list, stp_list_count);
}
/* don't initiate a local improvement if we are already
* in the middle of another stp update operation.
*/
if (doing_stp_update()) {
if (db[11].d) {
ddprintf("doing something else already.\n");
}
goto done;
}
/* look at each neighbor we are getting stp beacons from that
* we don't currently have an stp link to. these are the candidates for
* being added as new arcs. consider swapping with the stp
* neighbor we currently receive stp beacons for the neighbor from.
* bug: syntax of above sentence.
*/
if (db[11].d) {
ddprintf("check_local_improvement; nbr_device_list_count %d\n",
nbr_device_list_count);
}
max_diff = -1;
for (i = 0; i < nbr_device_list_count; i++) {
if (db[11].d) {
ddprintf("check_local_improvement; doing neighbor %s..\n",
mac_sprintf(mac_buf1, nbr_device_list[i].name));
}
/* if this neighbor is already an stp list connection, don't consider
* it.
*/
found = 0;
for (j = 0; j < stp_list_count; j++) {
if (mac_equal(nbr_device_list[i].name, stp_list[j].box.name)) {
found = 1;
break;
}
}
if (found) {
if (db[11].d) { ddprintf("already an stp neighbor.\n"); }
continue;
}
/* if we aren't getting stp beacons from this neighbor, don't consider
* it.
*/
found = 0;
for (j = 0; j < stp_recv_beacon_count; j++) {
if (mac_equal(nbr_device_list[i].name,
stp_recv_beacons[j].stp_beacon.originator))
{
found = 1;
break;
}
}
if (!found) {
if (db[11].d) { ddprintf("no stp beacons.\n"); }
continue;
}
/* find the stp neighbor that stp beacons from this guy are
* passing through, and see if the connection to this guy is
* stronger.
*/
found = 0;
for (k = 0; k < stp_list_count; k++) {
if (mac_equal(stp_recv_beacons[j].neighbor, stp_list[k].box.name)) {
found = 1;
break;
}
}
if (!found) {
if (db[11].d) { ddprintf("couldn't find stp neighbor.\n"); }
continue;
}
old_sig_strength = get_sig_strength(stp_list[k].box.name);
new_sig_strength = nbr_device_list[i].signal_strength;
if (db[11].d) {
ddprintf("new %d, old %d.\n", new_sig_strength, old_sig_strength);
}
/* sig_strength 1 is default weak signal. want a sorta real signal. */
if (new_sig_strength > 1
&& old_sig_strength > 1
&& new_sig_strength > old_sig_strength
&& (new_sig_strength - old_sig_strength) > max_diff)
{
new = nbr_device_list[i].name;
old = stp_list[k].box.name;
max_diff = new_sig_strength - old_sig_strength;
max_strength = new_sig_strength;
min_strength = old_sig_strength;
} else {
if (db[11].d) { ddprintf("didn't update.\n"); }
}
}
if (db[11].d) {
ddprintf("check_local_improvement got max diff %d\n", max_diff);
if (max_diff != -1) {
ddprintf("check_local_improvement got max strength %d to node %s, "
"min strength %d to node %s\n",
max_strength, mac_sprintf(mac_buf1, new),
min_strength, mac_sprintf(mac_buf2, old));
}
}
if (max_diff == -1) {
goto done;
}
if (!force_local_change && !random_eval(max_diff, stp_recv_beacon_count)) {
if (db[11].d) {
ddprintf("random_eval wasn't big enough; not making change.\n");
}
goto done;
}
if (force_local_change) {
force_local_change = 0;
db[5].d = old_db5;
}
ddprintf("check_local_improvement; SETTING UP TO MAKE THE CHANGE.\n");
print_state();
add_local_lock_request(old, local_lock_req_old_msg);
add_local_lock_request(new, local_lock_req_new_msg);
ddprintf("check_local_improvement; AFTER SETTING UP TO MAKE THE CHANGE:\n");
print_state();
done :
if (db[11].d) {
ddprintf("end check_local_improvement.\n");
}
} /* check_local_improvement */
/* during improvement, guy in the middle sends lock requests to the other
* two guys using this routine. (local_lock_req_{old,new}_msg).
*
* if there isn't already a pending request or lock granted to this guy,
* add a pending request to him of type 'type' and send him the message.
*/
void add_local_lock_request(mac_address_t node_mac, message_type_t type)
{
lockable_resource_t *p;
struct timeval tv;
struct timezone tz;
message_t message;
int i;
memset(&message, 0, sizeof(message));
if (db[3].d || db[21].d) {
ddprintf("add_local_lock_request %s to ", message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
}
for (i = 0; i < pending_request_count; i++) {
lockable_resource_t *p = &pending_requests[i];
if (mac_equal(p->node_1, node_mac) && p->type == type) {
ddprintf("add_local_lock_request: lock already pending; "
"returning.\n");
return;
}
}
for (i = 0; i < locks_granted_count; i++) {
if (mac_equal(locks_granted[i].node_1, node_mac)) {
ddprintf("add_local_lock_request: lock already granted "
"to someone; returning.\n");
return;
}
}
if (gettimeofday(&tv, &tz)) {
printf("add_local_lock_request: gettimeofday failed\n");
return;
}
if (pending_request_count > MAX_CLOUD) {
ddprintf("too many pending requests\n");
return;
}
p = &pending_requests[pending_request_count++];
p->type = type;
mac_copy(p->node_1, node_mac);
set_lock_timer(&pending_requests[pending_request_count - 1]);
if (db[21].d) {
ddprintf("add_local_lock_request: new pending request %s to node ",
message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
}
if (db[3].d) { print_state(); }
message.message_type = type;
mac_copy(message.dest, node_mac);
if (db[21].d) { print_msg("add_local_lock_request", &message); }
send_cloud_message(&message);
} /* add_local_lock_request */
/* local improvement; i'm not the guy in the middle, but am one of the
* two guys he's trying to swap around. he has issued each of us
* lock requests. decide whether to grant this lock and let him know,
* possibly adding the lock to our 'locks_granted' list.
*/
void process_local_lock_req(message_t *message, int device_index)
{
lockable_resource_t *p;
int i;
mac_address_ptr_t sender;
bool_t deny = false;
message_t response;
struct timeval tv;
struct timezone tz;
memset(&response, 0, sizeof(response));
if (gettimeofday(&tv, &tz)) {
printf("process_local_lock_req: gettimeofday failed\n");
return;
}
sender = get_name(device_index, message->eth_header.h_source);
if (sender == NULL) {
ddprintf("process_local_lock_req: could not get device name\n");
return;
}
if (db[21].d) {
ddprintf("process_local_lock_req; message type %s from ",
message_type_string(message->message_type));
mac_dprint(eprintf, stderr, sender);
}
for (i = 0; i < locks_granted_count; i++) {
if (mac_equal(sender, locks_granted[i].node_1)) {
// if (db[21].d) {
ddprintf(" lock already granted to ");
mac_dprint(eprintf, stderr, sender);
// }
deny = true;
break;
}
}
if (!deny) {
for (i = 0; i < pending_request_count; i++) {
if (mac_equal(sender, pending_requests[i].node_1)) {
// if (db[21].d) {
ddprintf(" request already pending to ");
mac_dprint(eprintf, stderr, sender);
// }
deny = true;
break;
}
}
}
if (!deny) {
for (i = 0; i < locks_owned_count; i++) {
if (mac_equal(sender, locks_owned[i].node_1)) {
// if (db[21].d) {
ddprintf(" lock already owned to ");
mac_dprint(eprintf, stderr, sender);
// }
deny = true;
break;
}
}
}
if (!deny && locks_granted_count >= MAX_CLOUD) {
ddprintf("process_local_lock_request: too many nodes.\n");
deny = true;
}
if (deny) {
ddprintf("process_local_lock_req; denying..\n");
response.message_type = local_lock_deny_msg;
} else {
ddprintf("process_local_lock_req; granting..\n");
p = &locks_granted[locks_granted_count];
p->type = local_lock_grant_msg;
// p->sec = tv.tv_sec;
// p->usec = tv.tv_usec;
mac_copy(p->node_1, sender);
locks_granted_count++;
set_lock_timer(&locks_granted[locks_granted_count - 1]);
if (db[21].d) {
ddprintf("process_local_lock_req; "
"adding granted request local_lock_grant_msg to node ");
mac_dprint(eprintf, stderr, sender);
}
response.message_type = local_lock_grant_msg;
}
mac_copy(response.dest, sender);
if (db[21].d) {
ddprintf("process_local_lock_req; sending message %s to node ",
message_type_string(response.message_type));
mac_dprint(eprintf, stderr, sender);
}
send_cloud_message(&response);
} /* process_local_lock_req */
/* we asked another node to grant us a lock. they responded in the affirmative
* with this message. look for a pending lock request to that node.
* if not found, tell the other guy we are releasing his lock and give up.
* if found, delete that pending request and add a lock on the other guy
* to our list of locks held.
*/
void process_local_lock_grant(message_t *message, int device_index)
{
int i;
mac_address_ptr_t sender;
char found;
message_t response;
message_type_t pending_type;
memset(&response, 0, sizeof(response));
if (db[3].d) { ddprintf("process_local_lock_grant\n"); }
sender = get_name(device_index, message->eth_header.h_source);
if (sender == NULL) {
ddprintf("process_local_lock_grant: get_name"
" returned null\n");
return;
}
if (db[21].d) {
ddprintf("process_local_lock_grant; message type %s from ",
message_type_string(message->message_type));
mac_dprint(eprintf, stderr, sender);
}
found = 0;
for (i = 0; i < pending_request_count; i++) {
lockable_resource_t *p = &pending_requests[i];
if (mac_equal(sender, p->node_1)
&& (p->type == local_lock_req_new_msg
|| p->type == local_lock_req_old_msg))
{
found = 1;
pending_type = pending_requests[i].type;
break;
}
}
if (!found) {
if (db[3].d || db[21].d) {
ddprintf("process_local_lock_grant: didn't find "
"pending message; release lock and forget it\n");
}
response.message_type = local_lock_release_msg;
mac_copy(response.dest, sender);
send_cloud_message(&response);
goto done;
}
if (pending_type == local_lock_req_new_msg
|| pending_type == local_lock_req_old_msg)
{
process_local_connect(message, sender);
}
done :;
} /* process_local_lock_grant */
/* as part of clearing any pending stp update state, send messages to any
* cloud boxes we had been granted locks from that we release those locks.
*/
static void send_lock_release_messages()
{
int i;
for (i = 0; i < locks_owned_count; i++) {
lockable_resource_t *l = &locks_owned[i];
message_t response;
memset(&response, 0, sizeof(response));
response.message_type = local_lock_release_msg;
mac_copy(response.dest, l->node_1);
send_cloud_message(&response);
}
}
/* totally clean out any pending stp-change state. 'doing_stp_state()'
* should return false after this. this routine assumes that we only
* do at most one stp change at a time. we clear out all locks owned,
* locks granted, and any pending requests that have to do with changing
* stp connectivity.
*/
void clear_stp_update_state()
{
bool_t did_something;
send_lock_release_messages();
locks_owned_count = 0;
locks_granted_count = 0;
do {
int i, j;
did_something = false;
for (i= 0; i < pending_request_count; i++) {
lockable_resource_t *l = &pending_requests[i];
if (l->type == local_stp_add_request_msg
|| l->type == local_lock_req_new_msg
|| l->type == local_lock_req_old_msg
|| l->type == local_stp_add_changed_request_msg
|| l->type == local_stp_delete_request_msg)
{
for (j = i; j < pending_request_count - 1; j++) {
pending_requests[j] = pending_requests[j + 1];
}
pending_request_count--;
did_something = true;
break;
}
}
} while (did_something);
reset_lock_timer();
}
/* we are the guy in the middle on a local improvement. we sent messages
* to two neighbor nodes requesting locks from both of them.
* one of them sent back a message refusing our lock request.
* so, we cancel the whole attempt and clean up stp-changing state
* to start over fresh later.
*/
void process_local_lock_deny(message_t *message, int device_index)
{
mac_address_ptr_t sender;
bool_t found_sender = true;
ddprintf("process_local_lock_deny\n");
sender = get_name(device_index, message->eth_header.h_source);
if (sender == NULL) {
ddprintf("process_local_lock_deny: get_name returned null\n");
found_sender = false;
}
clear_stp_update_state();
}
/* doing a local improvement; this routine processes affirmative messages
* the guy in the middle hears from the other two guys.
* if we already had the lock from the other guy, clean up the pending
* request we were waiting from from this guy, the lock we held on the
* other guy, and finish up by sending local_stp_add_changed_request_msg
* to new guy and local_stp_delete_request_msg to old guy.
* (if this is the first response from the two guys, set the lock on him
* and wait to hear from the second guy.)
*/
void process_local_connect(message_t *message, mac_address_ptr_t sender)
{
char have_other_lock;
int i, pend_ind, lock_ind;
message_type_t pending_type, other_type, pending_response, other_response;
char found;
mac_address_t old_mac_address, new_mac_address;
// if (db[10].d) {
ddprintf("process_local_connect..\n");
print_state();
// }
found = 0;
for (pend_ind = 0; pend_ind < pending_request_count; pend_ind++) {
if (mac_equal(sender, pending_requests[pend_ind].node_1)) {
found = 1;
pending_type = pending_requests[pend_ind].type;
break;
}
}
if (!found) {
ddprintf("process_local_connect; didn't find pending request\n");
goto done;
}
if (pending_type == local_lock_req_new_msg) {
other_type = local_lock_req_old_msg;
pending_response = local_add_release_msg;
other_response = local_delete_release_msg;
} else if (pending_type == local_lock_req_old_msg) {
other_type = local_lock_req_new_msg;
pending_response = local_delete_release_msg;
other_response = local_add_release_msg;
} else {
ddprintf("process_local_connect; found pending request of weird type "
"%s\n", message_type_string(pending_type));
goto done;
}
have_other_lock = 0;
for (lock_ind = 0; lock_ind < locks_owned_count; lock_ind++) {
if (locks_owned[lock_ind].type == other_type) {
have_other_lock = 1;
break;
}
}
if (have_other_lock) {
// message_t response;
// if (db[3].d) {
ddprintf("local_add_release_msg.. have other lock.\n");
print_state();
// }
if (pending_type == local_lock_req_new_msg) {
mac_copy(new_mac_address, pending_requests[pend_ind].node_1);
mac_copy(old_mac_address, locks_owned[lock_ind].node_1);
} else {
mac_copy(old_mac_address, pending_requests[pend_ind].node_1);
mac_copy(new_mac_address, locks_owned[lock_ind].node_1);
}
ddprintf("old_mac_address: ");
mac_dprint(eprintf, stderr, old_mac_address);
ddprintf("new_mac_address: ");
mac_dprint(eprintf, stderr, new_mac_address);
#if 0
response.message_type = pending_response;
mac_copy(response.dest, pending_requests[pend_ind].node_1);
send_cloud_message(&response);
response.message_type = other_response;
mac_copy(response.dest, locks_owned[lock_ind].node_1);
send_cloud_message(&response);
#endif
// add_stp_link(new_mac_address);
delete_me = old_mac_address;
trim_list(stp_list, &stp_list_count, delete_node, NULL);
ddprintf("process_local_connect.. have both locks now..\n");
#if 0
for (i = pend_ind; i < pending_request_count - 1; i++) {
pending_requests[i] = pending_requests[i + 1];
}
pending_request_count--;
for (i = lock_ind; i < locks_owned_count - 1; i++) {
locks_owned[i] = locks_owned[i + 1];
}
locks_owned_count--;
reset_lock_timer();
#endif
local_stp_add_request(new_mac_address,
local_stp_add_changed_request_msg);
local_stp_add_request(old_mac_address, local_stp_delete_request_msg);
} else {
ddprintf("process_local_connect.. waiting for other lock.\n");
}
if (locks_owned_count >= MAX_CLOUD) {
ddprintf("process_local_connect: too many locks owned.\n");
/* XXX clean up explicitly rather than letting everything time out?
*/
to_nominal_state();
goto done;
}
locks_owned[locks_owned_count++] = pending_requests[pend_ind];
set_lock_timer(&locks_owned[locks_owned_count - 1]);
for (i = pend_ind; i < pending_request_count - 1; i++) {
pending_requests[i] = pending_requests[i + 1];
}
pending_request_count--;
reset_lock_timer();
done :;
// if (db[10].d) {
ddprintf("process_local_connect returning; state:\n");
print_state();
// }
} /* process_local_connect */
/* get the name of the guy sending the message (give up if can't).
* trim first pending request to the guy that we can find in our
* pending_requests list. (?)
* (if no pending request found, return.)
* (maybe should check that it's the right kind of pending request)
*
* do an add_stp_link. (re-init link statistics.)
*
* to update other nodes' model of the new topology,
* send_stp_beacon (send my beacon).
* send_stp_beacons (send all beacons I have from my old sub-graph
* to the sender and thence to all nodes in the sender's old sub-graph).
*/
void process_local_stp_added(message_t *message, int device_index)
{
int i, j;
mac_address_ptr_t sender;
char found = 0;
// ddprintf("process_local_lock_add_release\n");
sender = get_name(device_index, message->eth_header.h_source);
if (sender == NULL) {
ddprintf("process_local_lock_add_release: get_name returned"
" null\n");
goto done;
}
if (db[21].d) {
ddprintf("process_local_stp_added; message type %s from ",
message_type_string(message->message_type));
mac_dprint(eprintf, stderr, sender);
print_state();
}
for (i = 0; i < pending_request_count; i++) {
lockable_resource_t *p = &pending_requests[i];
if (mac_equal(sender, p->node_1)
&& p->type == local_stp_add_request_msg)
{
found = 1;
break;
}
}
if (!found) {
message_t msg;
memset(&msg, 0, sizeof(msg));
ddprintf("process_local_stp_added; did not find a pending request.\n");
// #if 0
ddprintf(" telling other guy to delete his end of the stp arc\n");
msg.message_type = stp_arc_delete_msg;
mac_copy(msg.dest, sender);
send_cloud_message(&msg);
// #endif
goto done;
}
if (db[21].d) {
lockable_resource_t *p = &pending_requests[i];
ddprintf("process_local_stp_added; deleting pending request %s "
"to ", message_type_string(p->type));
mac_dprint(eprintf, stderr, p->node_1);
}
for (j = i; j < pending_request_count - 1; j++) {
pending_requests[j] = pending_requests[j + 1];
}
pending_request_count--;
reset_lock_timer();
add_stp_link(sender);
/* immediately send out an stp beacon to let everyone reachable know
* and try to reduce the probability that some other neighbor from
* the other side will try to connect to me.
*
* but only if we are connecting previously unconnected subgraphs.
* (don't do this if we are re-wiring a connected subgraph.)
*/
send_stp_beacon(false /* no disconnected nbr */);
send_stp_beacons(sender);
print_state();
done :;
} /* process_local_stp_added */
/* send a message of type "type" to the cloud box "node_mac".
* add the request to pending_requests, and if it is not a
* local_stp_add_request_msg set the request to time out.
*
* handle local_stp_add_request_msg
* local_stp_add_changed_request_msg
* local_stp_delete_request_msg
*
* send the message to the other guy.
* if the send attempt came back with 'unreachable', give up.
* create a pending request entry. (reuse old pending request entry of
* same type if there is one.)
* if the request type is local_stp_add_request_msg, set timeout to -1.
* after box's idea of time gets a little bigger than right after midnight,
* this means the pending request times out the first time
* timeout_lockables() gets called. (happens in post_repeated_cloud_maint,
* before check_connectivity(). i guess we hope to hear back from the
* other guy before we do another interrupt ourselves.)
* otherwise, set the timeout on the pending request.
*/
void local_stp_add_request(mac_address_t node_mac, message_type_t type)
{
lockable_resource_t *p;
message_t message;
int i, pend_ind;
int result;
bool_t have_pend_ind = false;
if (db[3].d || db[21].d) {
ddprintf("local_stp_add_request %s to ", message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
}
memset(&message, 0, sizeof(message));
for (i = 0; i < pending_request_count; i++) {
if (mac_equal(pending_requests[i].node_1, node_mac)
&& pending_requests[i].type == type)
{
DEBUG_SPARSE_PRINT(
ddprintf("local_stp_add_request(%d); request type %s to ",
print_count, message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
ddprintf("already pending; doing but with current pending.\n");
)
pend_ind = i;
have_pend_ind = true;
break;
}
}
message.message_type = type;
mac_copy(message.dest, node_mac);
if (db[21].d) {
print_msg("local_stp_add_request", &message);
}
result = send_cloud_message(&message);
if (result == 0 || errno != EHOSTUNREACH) {
if (have_pend_ind) {
p = &pending_requests[pend_ind];
} else {
if (pending_request_count > MAX_CLOUD) {
ddprintf("local_stp_add_request: too many pending requests\n");
return;
}
p = &pending_requests[pending_request_count++];
}
p->type = type;
mac_copy(p->node_1, node_mac);
if (type == local_stp_add_request_msg) {
pending_requests[pending_request_count - 1].sec = -1;
} else {
set_lock_timer(&pending_requests[pending_request_count - 1]);
}
if (db[21].d) {
ddprintf("local_stp_add_request: new pending request %s to node ",
message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
}
} else {
if (db[21].d) {
ddprintf("local_stp_add_request: no route; not adding pending "
" request %s to node ", message_type_string(type));
mac_dprint(eprintf, stderr, node_mac);
}
}
if (db[3].d) { print_state(); }
} /* local_stp_add_request */
/* if we have any pending local_stp_add_request_msg's, trim them.
* we've gotten a connect request from another box, and these
* local_stp_add_request_msg are light-weight things that may be waiting
* around for a response from a box that is sending beacons but not running
* cloud software. (i.e., a potential ad-hoc client.)
*/
static void trim_pending_subgraph_connect_invitations()
{
bool_t did_something;
do {
int i, j;
did_something = false;
for (i = 0; i < pending_request_count; i++) {
lockable_resource_t *p = &pending_requests[i];
if (p->type == local_stp_add_request_msg) {
for (j = i; j < pending_request_count - 1; j++) {
pending_requests[j] = pending_requests[j + 1];
}
pending_request_count--;
reset_lock_timer();
did_something = true;
break;
}
}
} while (did_something);
}
/* got a request from another box to build an stp arc.
*
* if the other box wants to build a new arc to connect two previously
* unconnected sub-graphs, the message will be 'local_stp_add_request_msg'.
*
* if the other box wants to improve connectivity by deleting an arc and
* replacing it with a better one, message will be
* 'local_stp_add_changed_request_msg', which we process.
* (we should find that we had granted the guy a lock in that case.
* we free the lock.)
* (process_local_stp_delete_request() processes the message to delete the
* old other stp arc to the other box.)
*/
void process_local_stp_add_request(message_t *message, int device_index)
{
int i;
mac_address_ptr_t sender;
bool_t already_linked = false;
char found = 0;
bool_t lock_granted;
bool_t deny = false;
message_t response;
struct timeval tv;
struct timezone tz;
memset(&response, 0, sizeof(response));
// ddprintf("process_local_stp_add_request\n");
sender = get_name(device_index, message->eth_header.h_source);
if (sender == NULL) {
ddprintf("process_local_stp_add_request: get_name returned"
" null\n");
goto done;
}
if (db[21].d) {
ddprintf("process_local_stp_add_request; message type %s from ",
message_type_string(message->message_type));
mac_dprint(eprintf, stderr, sender);
ddprintf("h_dest ");
mac_dprint(eprintf, stderr, message->eth_header.h_dest);
}
if (gettimeofday(&tv, &tz)) {
printf("process_local_stp_add_request: gettimeofday failed\n");
return;
}
/* if this is a connect-subgraphs invitation, get rid of any
* pending connect-subgraphs invitations we have issued, on the
* chance that they are to zombie nodes. if by chance we get a
* response from one of these guys, we won't remember the pending
* invitation, so we will just tell them to tear down their end.
*/
if (message->message_type == local_stp_add_request_msg) {
trim_pending_subgraph_connect_invitations();
}
/* if this is local-improvement, if things are normal we will have
* granted him a lock. one way or the other, we don't need it any more.
* we're done with the local-improvement attempt.
*/
lock_granted = false;
for (i = 0; i < locks_granted_count; i++) {
if (mac_equal(sender, locks_granted[i].node_1)) {
if (db[21].d) {
ddprintf(" found lock that had been granted to ");
mac_dprint(eprintf, stderr, sender);
}
lock_granted = true;
break;
}
}
if (lock_granted) {
int j;
for (j = i; j < locks_granted_count - 1; j++) {
locks_granted[j] = locks_granted[j + 1];
}
locks_granted_count--;
reset_lock_timer();
}