-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPQINP3.c
1374 lines (1026 loc) · 27.8 KB
/
BPQINP3.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 2001-2018 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 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.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
//
// INP3 Suport Code for BPQ32 Switch
//
// All code runs from the BPQ32 Received or Timer Routines under Semaphore.
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#pragma data_seg("_BPQDATA")
#include "CHeaders.h"
#include "time.h"
#include "stdio.h"
#include <fcntl.h>
//#include "vmm.h"
static VOID SendNetFrame(struct ROUTE * Route, struct _L3MESSAGEBUFFER * Frame)
{
// INP3 should only ever send over an active link, so just queue the message
if (Route->NEIGHBOUR_LINK)
C_Q_ADD(&Route->NEIGHBOUR_LINK->TX_Q, Frame);
else
ReleaseBuffer(Frame);
}
typedef struct _RTTMSG
{
UCHAR ID[7];
UCHAR TXTIME[11];
UCHAR SMOOTHEDRTT[11];
UCHAR LASTRTT[11];
UCHAR POINTER[11];
UCHAR ALIAS[7];
UCHAR VERSION[12];
UCHAR SWVERSION[9];
UCHAR FLAGS[10];
UCHAR PADDING[147];
} RTTMSG;
extern int COUNTNODES();
VOID __cdecl Debugprintf(const char * format, ...);
VOID SendINP3RIF(struct ROUTE * Route, UCHAR * Call, UCHAR * Alias, int Hops, int RTT);
VOID SendOurRIF(struct ROUTE * Route);
VOID UpdateNode(struct ROUTE * Route, UCHAR * axcall, UCHAR * alias, int hops, int rtt);
VOID UpdateRoute(struct DEST_LIST * Dest, struct DEST_ROUTE_ENTRY * ROUTEPTR, int hops, int rtt);
VOID KillRoute(struct DEST_ROUTE_ENTRY * ROUTEPTR);
VOID AddHere(struct DEST_ROUTE_ENTRY * ROUTEPTR,struct ROUTE * Route , int hops, int rtt);
VOID SendRIPToNeighbour(struct ROUTE * Route);
VOID DecayNETROMRoutes(struct ROUTE * Route);
VOID DeleteINP3Routes(struct ROUTE * Route);
BOOL L2SETUPCROSSLINKEX(PROUTE ROUTE, int Retries);
//#define NOINP3
struct _RTTMSG RTTMsg = {""};
//struct ROUTE DummyRoute = {"","",""};
int RIPTimerCount = 0; // 1 sec to 10 sec counter
int PosTimerCount = 0; // 1 sec to 5 Mins counter
// Timer Runs every 10 Secs
extern int MAXRTT; // 90 secs
extern int MaxHops;
extern int RTTInterval; // 4 Minutes
int RTTRetries = 2;
int RTTTimeout = 6; // 1 Min (Horizon is 1 min)
VOID InitialiseRTT()
{
UCHAR temp[sizeof(RTTMsg.FLAGS) + 4];
memset(&RTTMsg, ' ', sizeof(struct _RTTMSG));
memcpy(RTTMsg.ID, "L3RTT: ", 7);
memcpy(RTTMsg.VERSION, "LEVEL3_V2.1 ", 12);
memcpy(RTTMsg.SWVERSION, "BPQ32001 ", 9);
_snprintf(temp, sizeof(temp), "$M%d $N ", MAXRTT); // trailing spaces extend to ensure padding if the length of characters for MAXRTT changes.
memcpy(RTTMsg.FLAGS, temp, 10); // But still limit the actual characters copied.
memcpy(RTTMsg.ALIAS, &MYALIASTEXT, 6);
RTTMsg.ALIAS[6] = ' ';
}
VOID TellINP3LinkGone(struct ROUTE * Route)
{
struct DEST_LIST * Dest = DESTS;
char call[11]="";
ConvFromAX25(Route->NEIGHBOUR_CALL, call);
Debugprintf("BPQ32 L2 Link to Neighbour %s lost", call);
if (Route->NEIGHBOUR_LINK)
Debugprintf("BPQ32 Neighbour_Link not cleared");
if (Route->INP3Node == 0)
DecayNETROMRoutes(Route);
else
DeleteINP3Routes(Route);
}
VOID DeleteINP3Routes(struct ROUTE * Route)
{
int i;
struct DEST_LIST * Dest = DESTS;
// Delete any NETROM Dest entries via this Route
Route->SRTT = 0;
Route->RTT = 0;
Route->BCTimer = 0;
Route->Status = 0;
Route->Timeout = 0;
Dest--;
// Delete any Dest entries via this Route
for (i=0; i < MAXDESTS; i++)
{
Dest++;
if (Dest->DEST_CALL[0] == 0)
continue; // Spare Entry
if (Dest->ROUTE[0].ROUT_NEIGHBOUR == Route)
{
// We are deleting the best route, so need to tell other nodes
// If this is the only one, we need to keep the entry with at 60000 rtt so
// we can send it. Remove when all gone
// How do we indicate is is dead - Maybe the 60000 is enough!
if (Dest->ROUTE[1].ROUT_NEIGHBOUR == 0)
{
// Only entry
Dest->ROUTE[0].SRTT = 60000;
Dest->ROUTE[0].Hops = 255;
continue;
}
Dest->ROUTE[1].LastRTT = Dest->ROUTE[0].SRTT; // So next scan will check if rtt has increaced
// enough to need a RIF
memcpy(&Dest->ROUTE[0], &Dest->ROUTE[1], sizeof(struct DEST_ROUTE_ENTRY));
memcpy(&Dest->ROUTE[1], &Dest->ROUTE[2], sizeof(struct DEST_ROUTE_ENTRY));
memset(&Dest->ROUTE[2], 0, sizeof(struct DEST_ROUTE_ENTRY));
continue;
}
// If we aren't removing the best, we don't need to tell anyone.
if (Dest->ROUTE[1].ROUT_NEIGHBOUR == Route)
{
memcpy(&Dest->ROUTE[1], &Dest->ROUTE[2], sizeof(struct DEST_ROUTE_ENTRY));
memset(&Dest->ROUTE[2], 0, sizeof(struct DEST_ROUTE_ENTRY));
continue;
}
if (Dest->ROUTE[2].ROUT_NEIGHBOUR == Route)
{
memset(&Dest->ROUTE[2], 0, sizeof(struct DEST_ROUTE_ENTRY));
continue;
}
}
}
VOID DecayNETROMRoutes(struct ROUTE * Route)
{
int i;
struct DEST_LIST * Dest = DESTS;
Dest--;
// Decay any NETROM Dest entries via this Route. If OBS reaches zero, remove
// OBSINIT is probably too many retries. Try decrementing by 2.
for (i=0; i < MAXDESTS; i++)
{
Dest++;
if (Dest->DEST_CALL[0] == 0)
continue; // Spare Entry
if (Dest->NRROUTE[0].ROUT_NEIGHBOUR == Route)
{
if (Dest->NRROUTE[0].ROUT_OBSCOUNT && Dest->NRROUTE[0].ROUT_OBSCOUNT < 128) // Not if locked
{
Dest->NRROUTE[0].ROUT_OBSCOUNT--;
if (Dest->NRROUTE[0].ROUT_OBSCOUNT)
Dest->NRROUTE[0].ROUT_OBSCOUNT--;
}
if (Dest->NRROUTE[0].ROUT_OBSCOUNT == 0)
{
// Route expired
if (Dest->NRROUTE[1].ROUT_NEIGHBOUR == 0) // No more Netrom Routes
{
if (Dest->ROUTE[0].ROUT_NEIGHBOUR == 0) // Any INP3 ROutes?
{
// No More Routes - ZAP Dest
REMOVENODE(Dest); // Clear buffers, Remove from Sorted Nodes chain, and zap entry
continue;
}
else
{
// Still have an INP3 Route - just zap this entry
memset(&Dest->NRROUTE[0], 0, sizeof(struct NR_DEST_ROUTE_ENTRY));
continue;
}
}
memcpy(&Dest->NRROUTE[0], &Dest->NRROUTE[1], sizeof(struct NR_DEST_ROUTE_ENTRY));
memcpy(&Dest->NRROUTE[1], &Dest->NRROUTE[2], sizeof(struct NR_DEST_ROUTE_ENTRY));
memset(&Dest->NRROUTE[2], 0, sizeof(struct NR_DEST_ROUTE_ENTRY));
continue;
}
}
if (Dest->NRROUTE[1].ROUT_NEIGHBOUR == Route)
{
Dest->NRROUTE[1].ROUT_OBSCOUNT--;
if (Dest->NRROUTE[1].ROUT_OBSCOUNT == 0)
{
memcpy(&Dest->NRROUTE[1], &Dest->NRROUTE[2], sizeof(struct NR_DEST_ROUTE_ENTRY));
memset(&Dest->NRROUTE[2], 0, sizeof(struct NR_DEST_ROUTE_ENTRY));
continue;
}
}
if (Dest->NRROUTE[2].ROUT_NEIGHBOUR == Route)
{
Dest->NRROUTE[2].ROUT_OBSCOUNT--;
if (Dest->NRROUTE[2].ROUT_OBSCOUNT == 0)
{
memset(&Dest->NRROUTE[2], 0, sizeof(struct NR_DEST_ROUTE_ENTRY));
continue;
}
}
}
}
VOID TellINP3LinkSetupFailed(struct ROUTE * Route)
{
// Attempt to activate Neighbour failed
// char call[11]="";
// ConvFromAX25(Route->NEIGHBOUR_CALL, call);
// Debugprintf("BPQ32 L2 Link to Neighbour %s setup failed", call);
if (Route->INP3Node == 0)
DecayNETROMRoutes(Route);
else
DeleteINP3Routes(Route);
}
VOID ProcessRTTReply(struct ROUTE * Route, struct _L3MESSAGEBUFFER * Buff)
{
int RTT;
unsigned int OrigTime;
if ((Route->Status & GotRTTResponse) == 0)
{
// Link is just starting
Route->Status |= GotRTTResponse;
if (Route->Status & GotRTTRequest)
{
Route->Status |= SentOurRIF;
SendOurRIF(Route);
SendRIPToNeighbour(Route);
}
}
Route->Timeout = 0; // Got Response
sscanf(&Buff->L4DATA[6], "%d", &OrigTime);
RTT = GetTickCount() - OrigTime;
if (RTT > 60000)
return; // Ignore if more than 60 secs
Route->RTT = RTT;
if (Route->SRTT == 0)
Route->SRTT = RTT;
else
Route->SRTT = ((Route->SRTT * 80)/100) + ((RTT * 20)/100);
}
VOID ProcessINP3RIF(struct ROUTE * Route, UCHAR * ptr1, int msglen, int Port)
{
unsigned char axcall[7];
int hops;
unsigned short rtt;
int len;
int opcode;
char alias[6];
UINT Stamp, HH, MM;
#ifdef NOINP3
return;
#endif
if (Route->INP3Node == 0)
return; // We don't want to use INP3
// Update Timestamp on Route
time((time_t *)&Stamp);
Stamp = Stamp % 86400; // Secs into day
HH = Stamp / 3600;
Stamp -= HH * 3600;
MM = Stamp / 60;
Route->NEIGHBOUR_TIME = 256 * HH + MM;
while (msglen > 0)
{
memset(alias, ' ', 6);
memcpy(axcall, ptr1, 7);
if (axcall[0] < 0x60 || (axcall[0] & 1)) // Not valid ax25 callsign
return; // Corrupt RIF
ptr1+=7;
hops = *ptr1++;
rtt = (*ptr1++ << 8);
rtt += *ptr1++;
msglen -= 10;
while (*ptr1 && msglen > 0)
{
len = *ptr1;
opcode = *(ptr1+1);
if (len < 2 || len > msglen)
return; // Duff RIF
if (opcode == 0)
{
if (len > 1 && len < 9)
memcpy(alias, ptr1+2, len-2);
else
{
Debugprintf("Corrupt INP3 Message");
return;
}
}
ptr1+=len;
msglen -=len;
}
ptr1++;
msglen--; // EOP
UpdateNode(Route, axcall, alias, hops, rtt);
}
return;
}
VOID KillRoute(struct DEST_ROUTE_ENTRY * ROUTEPTR)
{
}
VOID UpdateNode(struct ROUTE * Route, UCHAR * axcall, UCHAR * alias, int hops, int rtt)
{
struct DEST_LIST * Dest;
struct DEST_ROUTE_ENTRY * ROUTEPTR;
int i;
char call[11]="";
if (hops > MaxHops && hops < 255)
{
// ConvFromAX25(axcall, call);
// Debugprintf("Node %s Hops %d RTT %d Ignored - Hop Count too high", call, hops, rtt);
return;
}
if (rtt > MAXRTT && rtt < 60000)
{
// ConvFromAX25(axcall, call);
// Debugprintf("Node %s Hops %d RTT %d Ignored - rtt too high", call, hops, rtt);
return;
}
if (FindDestination(axcall, &Dest))
goto Found;
if (Dest == NULL)
return; // Tsble Full
if (rtt >= 60000)
return; // No Point addind a new dead route
memset(Dest, 0, sizeof(struct DEST_LIST));
memcpy(Dest->DEST_CALL, axcall, 7);
memcpy(Dest->DEST_ALIAS, alias, 6);
// Set up First Route
Dest->ROUTE[0].Hops = hops;
Dest->ROUTE[0].SRTT = rtt;
Dest->ROUTE[0].LastRTT = 0;
Dest->INP3FLAGS = NewNode;
Dest->ROUTE[0].ROUT_NEIGHBOUR = Route;
NUMBEROFNODES++;
ConvFromAX25(Dest->DEST_CALL, call);
Debugprintf("Adding Node %s Hops %d RTT %d", call, hops, rtt);
return;
Found:
if (Dest->DEST_STATE & 0x80) // Application Entry
return;
// Update ALIAS
if (alias[0] > ' ')
memcpy(Dest->DEST_ALIAS, alias, 6);
// See if we are known to it, it not add
ROUTEPTR = &Dest->ROUTE[0];
if (rtt >= 60000)
{
i=rtt+1;
}
if (ROUTEPTR->ROUT_NEIGHBOUR == Route)
{
UpdateRoute(Dest, ROUTEPTR, hops, rtt);
return;
}
ROUTEPTR = &Dest->ROUTE[1];
if (ROUTEPTR->ROUT_NEIGHBOUR == Route)
{
UpdateRoute(Dest, ROUTEPTR, hops, rtt);
return;
}
ROUTEPTR = &Dest->ROUTE[2];
if (ROUTEPTR->ROUT_NEIGHBOUR == Route)
{
UpdateRoute(Dest, ROUTEPTR, hops, rtt);
return;
}
// Not in list. If any spare, add.
// If full, see if this is better
if (rtt >= 60000)
return; // No Point addind a new dead route
ROUTEPTR = &Dest->ROUTE[0];
for (i = 1; i < 4; i++)
{
if (ROUTEPTR->ROUT_NEIGHBOUR == NULL)
{
// Add here
Dest->ROUTE[0].Hops = hops;
Dest->ROUTE[0].SRTT = rtt;
Dest->ROUTE[0].ROUT_NEIGHBOUR = Route;
return;
}
ROUTEPTR++;
}
// Full, see if this is better
// Note that wont replace any netrom routes with INP3 ones unless we add pseudo rtt values to netrom entries
if (Dest->ROUTE[0].SRTT > rtt)
{
// We are better. Move others down and add on front
memcpy(&Dest->ROUTE[2], &Dest->ROUTE[1], sizeof(struct DEST_ROUTE_ENTRY));
memcpy(&Dest->ROUTE[1], &Dest->ROUTE[0], sizeof(struct DEST_ROUTE_ENTRY));
AddHere(&Dest->ROUTE[0], Route, hops, rtt);
return;
}
if (Dest->ROUTE[1].SRTT > rtt)
{
// We are better. Move 2nd down and add
memcpy(&Dest->ROUTE[2], &Dest->ROUTE[1], sizeof(struct DEST_ROUTE_ENTRY));
AddHere(&Dest->ROUTE[1], Route, hops, rtt);
return;
}
if (Dest->ROUTE[2].SRTT > rtt)
{
// We are better. Add here
AddHere(&Dest->ROUTE[2], Route, hops, rtt);
return;
}
// Worse than any - ignoee
}
VOID AddHere(struct DEST_ROUTE_ENTRY * ROUTEPTR,struct ROUTE * Route , int hops, int rtt)
{
ROUTEPTR->Hops = hops;
ROUTEPTR->SRTT = rtt;
ROUTEPTR->LastRTT = 0;
ROUTEPTR->RTT = 0;
ROUTEPTR->ROUT_NEIGHBOUR = Route;
return;
}
/* LEA EDI,DEST_CALL[EBX]
MOV ECX,7
REP MOVSB
MOV ECX,6 ; ADD ALIAS
MOV ESI,OFFSET32 TEMPFIELD
REP MOVSB
POP ESI
;
; GET _NEIGHBOURS FOR THIS DESTINATION
;
CALL CONVTOAX25
JNZ SHORT BADROUTE
;
CALL GETVALUE
MOV _SAVEPORT,AL ; SET PORT FOR _FINDNEIGHBOUR
CALL GETVALUE
MOV _ROUTEQUAL,AL
;
MOV ESI,OFFSET32 AX25CALL
PUSH EBX ; SAVE DEST
CALL _FINDNEIGHBOUR
MOV EAX,EBX ; ROUTE TO AX
POP EBX
JZ SHORT NOTBADROUTE
JMP SHORT BADROUTE
NOTBADROUTE:
;
; UPDATE ROUTE LIST FOR THIS DEST
;
MOV ROUT1_NEIGHBOUR[EBX],EAX
MOV AL,_ROUTEQUAL
MOV ROUT1_QUALITY[EBX],AL
MOV ROUT1_OBSCOUNT[EBX],255 ; LOCKED
;
POP EDI
POP EBX
INC _NUMBEROFNODES
JMP SENDOK
*/
VOID SortRoutes(struct DEST_LIST * Dest)
{
struct DEST_ROUTE_ENTRY Temp;
// May now be out of order
if (Dest->ROUTE[1].ROUT_NEIGHBOUR == 0)
return; // Only One, so cant be out of order
if (Dest->ROUTE[2].ROUT_NEIGHBOUR == 0)
{
// Only 2
if (Dest->ROUTE[0].SRTT <= Dest->ROUTE[1].SRTT)
return;
// Swap one and two
memcpy(&Temp, &Dest->ROUTE[0], sizeof(struct DEST_ROUTE_ENTRY));
memcpy(&Dest->ROUTE[0], &Dest->ROUTE[1], sizeof(struct DEST_ROUTE_ENTRY));
memcpy(&Dest->ROUTE[1], &Temp, sizeof(struct DEST_ROUTE_ENTRY));
return;
}
// Have 3 Entries
}
VOID UpdateRoute(struct DEST_LIST * Dest, struct DEST_ROUTE_ENTRY * ROUTEPTR, int hops, int rtt)
{
if (ROUTEPTR->Hops == 0)
{
// This is not a INP3 Route - Convert it
ROUTEPTR->Hops = hops;
ROUTEPTR->SRTT = rtt;
SortRoutes(Dest);
return;
}
if (rtt == 60000)
{
ROUTEPTR->SRTT = rtt;
ROUTEPTR->Hops = hops;
SortRoutes(Dest);
return;
}
ROUTEPTR->SRTT = rtt;
ROUTEPTR->Hops = hops;
SortRoutes(Dest);
return;
}
VOID ProcessRTTMsg(struct ROUTE * Route, struct _L3MESSAGEBUFFER * Buff, int Len, int Port)
{
// See if a reply to our message, or a new request
if (memcmp(Buff->L3SRCE, MYCALL,7) == 0)
{
ProcessRTTReply(Route, Buff);
ReleaseBuffer(Buff);
}
else
{
int OtherRTT;
int Dummy;
if (Route->INP3Node == 0)
{
ReleaseBuffer(Buff);
return; // We don't want to use INP3
}
// Extract other end's SRTT
sscanf(&Buff->L4DATA[6], "%d %d", &Dummy, &OtherRTT);
Route->NeighbourSRTT = OtherRTT * 10; // We store in mS
// Echo Back to sender
SendNetFrame(Route, Buff);
if ((Route->Status & GotRTTRequest) == 0)
{
// Link is just starting
Route->Status |= GotRTTRequest;
if (Route->Status & GotRTTResponse)
{
Route->Status |= SentOurRIF;
SendOurRIF(Route);
SendRIPToNeighbour(Route);
}
else
{
// We have not yet seen a response (and maybe haven't sent one
Route->BCTimer = 0; // So send one
}
}
}
}
VOID SendRTTMsg(struct ROUTE * Route)
{
struct _L3MESSAGEBUFFER * Msg;
char Stamp[50];
Msg = GetBuff();
if (Msg == 0)
return;
Msg->Port = Route->NEIGHBOUR_PORT;
Msg->L3PID = NRPID;
memcpy(Msg->L3DEST, L3RTT, 7);
memcpy(Msg->L3SRCE, MYCALL, 7);
Msg->L3TTL = 2;
Msg->L4ID = 0;
Msg->L4INDEX = 0;
Msg->L4RXNO = 0;
Msg->L4TXNO = 0;
Msg->L4FLAGS = L4INFO;
sprintf(Stamp, "%10d %10d %10d %10d ", GetTickCount(), Route->SRTT/10, Route->RTT/10, 0);
memcpy(RTTMsg.TXTIME, Stamp, 44);
memcpy(Msg->L4DATA, &RTTMsg, 236);
Msg->LENGTH = 256 + 1 + 7;
Route->Timeout = RTTTimeout;
SendNetFrame(Route, Msg);
}
VOID SendKeepAlive(struct ROUTE * Route)
{
struct _L3MESSAGEBUFFER * Msg = GetBuff();
if (Msg == 0)
return;
Msg->L3PID = NRPID;
memcpy(Msg->L3DEST, L3KEEP, 7);
memcpy(Msg->L3SRCE, MYCALL, 7);
Msg->L3TTL = 1;
Msg->L4ID = 0;
Msg->L4INDEX = 0;
Msg->L4RXNO = 0;
Msg->L4TXNO = 0;
Msg->L4FLAGS = L4INFO;
// Msg->L3MSG.L4DATA[0] = 'K';
Msg->LENGTH = 20 + MSGHDDRLEN + 1;
SendNetFrame(Route, Msg);
}
int BuildRIF(UCHAR * RIF, UCHAR * Call, UCHAR * Alias, int Hops, int RTT)
{
int AliasLen;
int RIFLen;
UCHAR AliasCopy[10] = "";
UCHAR * ptr;
if (RTT > 60000) RTT = 60000; // Dont send more than 60000
memcpy(&RIF[0], Call, 7);
RIF[7] = Hops;
RIF[8] = RTT >> 8;
RIF[9] = RTT & 0xff;
if (Alias)
{
// Need to null-terminate Alias
memcpy(AliasCopy, Alias, 6);
ptr = strchr(AliasCopy, ' ');
if (ptr)
*ptr = 0;
AliasLen = strlen(AliasCopy);
RIF[10] = AliasLen+2;
RIF[11] = 0;
memcpy(&RIF[12], Alias, AliasLen);
RIF[12+AliasLen] = 0;
RIFLen = 13 + AliasLen;
return RIFLen;
}
RIF[10] = 0;
return (11);
}
VOID SendOurRIF(struct ROUTE * Route)
{
struct _L3MESSAGEBUFFER * Msg;
int RIFLen;
int totLen = 1;
int App;
APPLCALLS * APPL;
Msg = GetBuff();
if (Msg == 0)
return;
Msg->L3SRCE[0] = 0xff;
// send a RIF for our Node and all our APPLCalls
RIFLen = BuildRIF(&Msg->L3SRCE[totLen], MYCALL, MYALIASTEXT, 1, 0);
totLen += RIFLen;
for (App = 0; App < NumberofAppls; App++)
{
APPL=&APPLCALLTABLE[App];
if (APPL->APPLQUAL > 0)
{
RIFLen = BuildRIF(&Msg->L3SRCE[totLen], APPL->APPLCALL, APPL->APPLALIAS_TEXT, 1, 0);
totLen += RIFLen;
}
}
Msg->L3PID = NRPID;
Msg->LENGTH = totLen + 1 + MSGHDDRLEN;
SendNetFrame(Route, Msg);
}
int SendRIPTimer()
{
int count, nodes;
struct ROUTE * Route = NEIGHBOURS;
int MaxRoutes = MAXNEIGHBOURS;
int INP3Delay;
for (count=0; count<MaxRoutes; count++)
{
if (Route->NEIGHBOUR_CALL[0] != 0)
{
if (Route->NoKeepAlive) // Keepalive Disabled
{
Route++;
continue;
}
if (Route->NEIGHBOUR_LINK == 0 || Route->NEIGHBOUR_LINK->LINKPORT == 0)
{
if (Route->NEIGHBOUR_QUAL == 0)
{
Route++;
continue; // Qual zero is a locked out route
}
// Dont Activate if link has no nodes unless INP3
if (Route->INP3Node == 0)
{
nodes = COUNTNODES(Route);
if (nodes == 0)
{
Route++;
continue;
}
}
// Delay more if Locked - they could be retrying for a long time
if ((Route->NEIGHBOUR_FLAG & 1)) // LOCKED ROUTE
INP3Delay = 1200;
else
INP3Delay = 600;
if (Route->LastConnectAttempt &&
(REALTIMETICKS - Route->LastConnectAttempt) < INP3Delay)
{
Route++;
continue; // No room for link
}
// Try to activate link
L2SETUPCROSSLINKEX(Route, 2); // Only try SABM twice
Route->LastConnectAttempt = REALTIMETICKS;
if (Route->NEIGHBOUR_LINK == 0)
{
Route++;
continue; // No room for link
}
}
if (Route->NEIGHBOUR_LINK->L2STATE != 5) // Not up yet
{
Route++;
continue;
}
if (Route->NEIGHBOUR_LINK->KILLTIMER > ((L4LIMIT - 60) * 3)) // IDLETIME - 1 Minute
{
SendKeepAlive(Route);
Route->NEIGHBOUR_LINK->KILLTIMER = 0; // Keep Open
}
#ifdef NOINP3
Route++;
continue;
#endif
if (Route->INP3Node)
{
if (Route->Timeout)
{
// Waiting for response
Route->Timeout--;
if (Route->Timeout)
{
Route++;
continue; // Wait
}
// No response Try again
Route->Retries--;
if (Route->Retries)
{
// More Left
SendRTTMsg(Route);
}
else
{
// No Response - Kill all Nodes via this link
if (Route->Status)
{
char Call [11] = "";
ConvFromAX25(Route->NEIGHBOUR_CALL, Call);
Debugprintf("BPQ32 IMP Neighbour %s Lost", Call);
Route->Status = 0; // Down
}