forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L4Code.c
2273 lines (1602 loc) · 41.8 KB
/
L4Code.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-2015 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
*/
//
// C replacement for L4Code.asm
//
#define Kernel
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#pragma data_seg("_BPQDATA")
#include "time.h"
#include "stdio.h"
#include <fcntl.h>
#include "CHeaders.h"
extern BPQVECSTRUC BPQHOSTVECTOR[];
#define BPQHOSTSTREAMS 64
#define IPHOSTVECTOR BPQHOSTVECTOR[BPQHOSTSTREAMS + 3]
VOID CLOSECURRENTSESSION(TRANSPORTENTRY * Session);
VOID SENDL4DISC(TRANSPORTENTRY * Session);
int C_Q_COUNT(VOID * Q);
TRANSPORTENTRY * SetupSessionForL2(struct _LINKTABLE * LINK);
VOID InformPartner(struct _LINKTABLE * LINK);
VOID IFRM150(TRANSPORTENTRY * Session, PDATAMESSAGE Buffer);
VOID SendConNAK(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * L3MSG);
BOOL FINDCIRCUIT(L3MESSAGEBUFFER * L3MSG, TRANSPORTENTRY ** REQL4, int * NewIndex);
int GETBUSYBIT(TRANSPORTENTRY * L4);
BOOL cATTACHTOBBS(TRANSPORTENTRY * Session, UINT Mask, int Paclen, int * AnySessions);
VOID SETUPNEWCIRCUIT(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * L3MSG,
TRANSPORTENTRY * L4, char * BPQPARAMS, int ApplMask, int * BPQNODE);
extern char * ALIASPTR;
VOID SendConACK(struct _LINKTABLE * LINK, TRANSPORTENTRY * L4, L3MESSAGEBUFFER * L3MSG, BOOL BPQNODE, UINT Applmask);
VOID L3SWAPADDRESSES(L3MESSAGEBUFFER * L3MSG);
VOID L4TIMEOUT(TRANSPORTENTRY * L4);
struct DEST_LIST * CHECKL3TABLES(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * Msg);
int CHECKIFBUSYL4(TRANSPORTENTRY * L4);
VOID AUTOTIMER();
VOID NRRecordRoute(char * Buff, int Len);
VOID REFRESHROUTE(TRANSPORTENTRY * Session);
VOID ACKFRAMES(L3MESSAGEBUFFER * L3MSG, TRANSPORTENTRY * L4, int NR);
VOID SENDL4IACK(TRANSPORTENTRY * Session);
VOID CHECKNEIGHBOUR(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * Msg);
VOID ProcessINP3RIF(struct ROUTE * Route, UCHAR * ptr1, int msglen, int Port);
VOID ProcessRTTMsg(struct ROUTE * Route, struct _L3MESSAGEBUFFER * Buff, int Len, int Port);
VOID FRAMEFORUS(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * L3MSG, int ApplMask);
extern UINT APPLMASK;
// L4 Flags Values
#define DISCPENDING 8 // SEND DISC WHEN ALL DATA ACK'ED
VOID NETROMMSG(struct _LINKTABLE * LINK, L3MESSAGEBUFFER * L3MSG)
{
// MAKE SURE PID IS 0CF - IN CASE SOMEONE IS SENDING L2 STUFF ON WHAT
// WE THINK IS A _NODE-_NODE LINK
struct DEST_LIST * DEST;
APPLCALLS * APPL;
int n;
if (L3MSG->L3PID != 0xCF)
{
ReleaseBuffer(L3MSG);
return;
}
if (LINK->NEIGHBOUR == 0)
{
// NO ROUTE ASSOCIATED WITH THIS CIRCUIT - SET ONE UP
CHECKNEIGHBOUR(LINK, L3MSG);
if (LINK->NEIGHBOUR == 0)
{
// COULDNT SET UP NEIGHBOUR - CAN ONLY THROW IT AWAY
ReleaseBuffer(L3MSG);
return;
}
}
// See if a INP3 RIF (first Byte 0xFF)
if (L3MSG->L3SRCE[0] == 0xff)
{
// INP3
ProcessINP3RIF(LINK->NEIGHBOUR, &L3MSG->L3SRCE[1], L3MSG->LENGTH - 9, L3MSG->Port);
ReleaseBuffer(L3MSG);
return;
}
APPLMASK = 0; // NOT APPLICATION
if (NODE) // _NODE SUPPORT INCLUDED?
{
if (CompareCalls(L3MSG->L3DEST, MYCALL))
{
FORUS:
FRAMEFORUS(LINK, L3MSG, APPLMASK);
return;
}
}
// CHECK ALL L4 CALLS
APPLMASK = 1;
ALIASPTR = &CMDALIAS[0][0];
n = NumberofAppls;
APPL = APPLCALLTABLE;
while (n--)
{
if (APPL->APPLCALL[0] > 0x40) // Valid ax.25 addr
{
if (CompareCalls(L3MSG->L3DEST, APPL->APPLCALL))
goto FORUS;
}
APPLMASK <<= 1;
ALIASPTR += ALIASLEN;
APPL++;
}
// IS IT INP3 (L3RTT)
if (CompareCalls(L3MSG->L3DEST, L3RTT))
{
ProcessRTTMsg(LINK->NEIGHBOUR, L3MSG, L3MSG->LENGTH, L3MSG->Port);
return;
}
L3MSG->L3TTL--;
if (L3MSG->L3TTL == 0)
{
ReleaseBuffer(L3MSG);
return;
}
// If it is a record route frame we should add our call to the list before sending on
if (L3MSG->L4FLAGS == 0 && L3MSG->L4ID == 1 && L3MSG->L4INDEX == 0)
{
// Add our call on end, and increase count
int Len = L3MSG->LENGTH;
int Count;
UCHAR * ptr = (UCHAR *)L3MSG;
if (Len < 248)
{
ptr += (Len - 1);
Count = *(ptr++) = 0x7F; // Mask End of Route
memcpy(ptr, MYCALL, 7);
ptr += 7;
Count--;
*(ptr) = Count;
if (Count)
L3MSG->LENGTH += 8;
}
}
if (L3MSG->L3TTL > L3LIVES)
L3MSG->L3TTL = L3LIVES; // ENFORCE LIMIT ON ALL FRAMES SENT
if (FindDestination(L3MSG->L3DEST, &DEST) == 0)
{
ReleaseBuffer(L3MSG); // CANT FIND DESTINATION
return;
}
// IF MESSAGE ORIGINTED HERE, THERE MUST BE A ROUTING LOOP -
// THERE IS LITTLE POINT SENDING IT OVER THE SAME ROUTE AGAIN,
// SO SET ANOTHER ROUTE ACTIVE IF POSSIBLE
if (CompareCalls(L3MSG->L3SRCE, MYCALL) || CompareCalls(L3MSG->L3SRCE, APPLCALLTABLE->APPLCALL))
{
// MESSAGE HAS COME BACK TO ITS STARTING POINT - ACTIVATE ANOTHER ROUTE,
// UNLESS THERE IS ONLY ONE, IN WHICH CASE DISCARD IT
if (DEST->NRROUTE[1].ROUT_NEIGHBOUR == 0) // No more routes
{
ReleaseBuffer(L3MSG);
return;
}
DEST->DEST_ROUTE++;
if (DEST->DEST_ROUTE == 4) // TO NEXT
DEST->DEST_ROUTE = 1; // TRY TO ACTIVATE FIRST
}
// IF CURRENT ROUTE IS BACK THE WAY WE CAME, THEN ACTIVATE
//ANOTHER (IF POSSIBLE).
if (DEST->DEST_ROUTE)
{
if (DEST->NRROUTE[DEST->DEST_ROUTE -1].ROUT_NEIGHBOUR == LINK->NEIGHBOUR)
{
// Current ROUTE IS BACK THE WAY WE CAME - ACTIVATE ANOTHER IF POSSIBLE
DEST->DEST_ROUTE++;
if (DEST->DEST_ROUTE == 4)
DEST->DEST_ROUTE =1;
}
goto NO_PROBLEM;
}
else
{
// DONT HAVE AN ACTIVE ROUTE
if (DEST->NRROUTE[0].ROUT_NEIGHBOUR == LINK->NEIGHBOUR)
{
// FIRST ROUTE IS BACK THE WAY WE CAME - ACTIVATE ANOTHER IF POSSIBLE
DEST->DEST_ROUTE = 2; // WILL BE RESET BY L3 CODE IF THERE IS NOT OTHER ROUTE
}
}
NO_PROBLEM:
CHECKL3TABLES(LINK, L3MSG);
// EVEN IF WE CANT PUT ORIGINATING NODE INTO OUR TABLES, PASS MSG ON
// ANYWAY - THE FINAL TARGET MAY HAVE ANOTHER WAY BACK
C_Q_ADD(&DEST->DEST_Q, L3MSG);
L3FRAMES++;
}
VOID SENDL4MESSAGE(TRANSPORTENTRY * L4, struct DATAMESSAGE * Msg)
{
L3MESSAGEBUFFER * L3MSG;
struct DEST_LIST * DEST;
struct DATAMESSAGE * Copy;
int FRAGFLAG = 0;
int Len;
if (Msg->LENGTH == 8)
{
// NO DATA - DISCARD IT
ReleaseBuffer(Msg);
return;
}
L3MSG = GetBuff();
if (L3MSG == 0)
{
// DONT THINK WE SHOULD GET HERE, UNLESS _QCOUNT IS CORRUPT,
// BUT IF WE DO, SHOULD RETURN MSG TO FREE Q - START TIMER, AND
// DROP THROUGH TO RELBUFF
L4->L4TIMER = L4->SESSIONT1;
ReleaseBuffer(Msg);
return;
}
L3MSG->L3PID = 0xCF; // NET MESSAGE
memcpy(L3MSG->L3SRCE, L4->L4MYCALL, 7);
DEST = L4->L4TARGET.DEST;
memcpy(L3MSG->L3DEST, DEST->DEST_CALL, 7);
L3MSG->L3TTL = L3LIVES;
L3MSG->L4ID = L4->FARID;
L3MSG->L4INDEX = L4->FARINDEX;
L3MSG->L4TXNO = L4->TXSEQNO;
// SET UP RTT TIMER
if (L4->RTT_TIMER == 0)
{
L4->RTT_SEQ = L4->TXSEQNO;
L4->RTT_TIMER = GetTickCount();
}
L4->TXSEQNO++;
L4->L4LASTACKED = L3MSG->L4RXNO = L4->RXSEQNO; // SAVE LAST NUMBER ACKED
// SEE IF CROSSSESSION IS BUSY
GETBUSYBIT(L4); // Sets BUSY in NAKBITS if Busy
L3MSG->L4FLAGS = L4INFO | L4->NAKBITS;
L4->L4TIMER = L4->SESSIONT1; // SET TIMER
L4->L4ACKREQ = 0; // CANCEL ACK NEEDED
Len = Msg->LENGTH;
if (Len > 244) // 236 DATA + 8 HEADER
{
// MUST FRAGMENT MESSAGE
L3MSG->L4FLAGS |= L4MORE;
FRAGFLAG = 1;
Len = 244;
}
Len += 20; // L3/4 Header
L3MSG->LENGTH = Len;
Len -= 28;
memcpy(L3MSG->L4DATA, Msg->L2DATA, Len);
// CREATE COPY FOR POSSIBLE RETRY
Copy = GetBuff();
if (Copy == 0)
{
// SHOULD NEVER HAPPEN
ReleaseBuffer(Msg);
return;
}
memcpy(Copy, L3MSG, L3MSG->LENGTH);
// If we have fragmented, we should adjust length, or retry will send too much
// (bug in .asm code)
if (FRAGFLAG)
Copy->LENGTH = 244;
C_Q_ADD(&L4->L4HOLD_Q, Copy);
C_Q_ADD(&DEST->DEST_Q, L3MSG);
DEST->DEST_COUNT++; // COUNT THEM
L4FRAMESTX++;
if (FRAGFLAG)
{
// MESSAGE WAS TOO BIG - ADJUST IT AND LOOP BACK
Msg->LENGTH -= 236;
memmove(Msg->L2DATA, &Msg->L2DATA[236], Msg->LENGTH - 8);
SENDL4MESSAGE(L4, Msg);
}
}
int GETBUSYBIT(TRANSPORTENTRY * L4)
{
// SEE IF CROSSSESSION IS BUSY
L4->NAKBITS &= ~L4BUSY; // Clear busy
L4->NAKBITS |= CHECKIFBUSYL4(L4); // RETURNS AL WITH BUSY BIT SET IF CROSSSESSION IS BUSY
return L4->NAKBITS;
}
VOID Q_IP_MSG(PDATAMESSAGE Buffer)
{
if (IPHOSTVECTOR.HOSTAPPLFLAGS & 0x80)
{
// CHECK WE ARENT USING TOO MANY BUFFERS
if (C_Q_COUNT(&IPHOSTVECTOR.HOSTTRACEQ) > 20)
ReleaseBuffer(Q_REM(&IPHOSTVECTOR.HOSTTRACEQ));
C_Q_ADD(&IPHOSTVECTOR.HOSTTRACEQ, Buffer);
return;
}
ReleaseBuffer(Buffer);
}
VOID SENDL4CONNECT(TRANSPORTENTRY * Session)
{
PL3MESSAGEBUFFER MSG = (PL3MESSAGEBUFFER)GetBuff();
struct DEST_LIST * DEST = Session->L4TARGET.DEST;
if (MSG == NULL)
return;
if (DEST->DEST_CALL[0] == 0)
{
Debugprintf("Trying to send L4CREEQ to NULL Destination");
ReleaseBuffer(MSG);
return;
}
MSG->L3PID = 0xCF; // NET MESSAGE
memcpy(MSG->L3SRCE, Session->L4MYCALL, 7);
memcpy(MSG->L3DEST, DEST->DEST_CALL, 7);
MSG->L3TTL = L3LIVES;
MSG->L4INDEX = Session->CIRCUITINDEX;
MSG->L4ID = Session->CIRCUITID;
MSG->L4TXNO = 0;
MSG->L4RXNO = 0;
MSG->L4FLAGS = L4CREQ;
MSG->L4DATA[0] = L4DEFAULTWINDOW; // PROPOSED WINDOW
memcpy(&MSG->L4DATA[1], Session->L4USER, 7); // ORIG CALL
memcpy(&MSG->L4DATA[8], Session->L4MYCALL, 7);
Session->L4TIMER = Session->SESSIONT1; // START TIMER
memcpy(&MSG->L4DATA[15], &Session->SESSIONT1, 2); // AND PUT IN MSG
MSG->LENGTH = &MSG->L4DATA[17] - (UCHAR *)MSG;
if (Session->SPYFLAG)
{
MSG->L4DATA[17] = 'Z'; // ADD SPY ON BBS FLAG
MSG->LENGTH++;
}
C_Q_ADD(&DEST->DEST_Q, (UINT *)MSG);
}
void RETURNEDTONODE(TRANSPORTENTRY * Session)
{
// SEND RETURNED TO ALIAS:CALL
struct DATAMESSAGE * Msg = (struct DATAMESSAGE *)GetBuff();
char Nodename[20];
if (Msg)
{
Msg->PID = 0xf0;
Nodename[DecodeNodeName(MYCALLWITHALIAS, Nodename)] = 0; // null terminate
Msg->LENGTH = sprintf(&Msg->L2DATA[0], "Returned to Node %s\r", Nodename) + 8 ;
C_Q_ADD(&Session->L4TX_Q, (UINT *)Msg);
PostDataAvailable(Session);
}
}
extern void * BUFFER;
VOID L4BG()
{
// PROCESS DATA QUEUED ON SESSIONS
int n = MAXCIRCUITS;
TRANSPORTENTRY * L4 = L4TABLE;
int MaxLinks = MAXLINKS;
UCHAR Outstanding;
struct DATAMESSAGE * Msg;
struct PORTCONTROL * PORT;
struct _LINKTABLE * LINK;
while (n--)
{
if (L4->L4USER[0] == 0)
{
L4++;
continue;
}
while (L4->L4TX_Q)
{
if (L4->L4CIRCUITTYPE & BPQHOST)
break; // Leave on TXQ
// SEE IF BUSY - NEED DIFFERENT TESTS FOR EACH SESSION TYPE
if (L4->L4CIRCUITTYPE & SESSION)
{
// L4 SESSION - WILL NEED BUFFERS FOR SAVING COPY,
// AND POSSIBLY FRAGMENTATION
if (QCOUNT < 15)
break;
if (L4->FLAGS & L4BUSY)
{
// CHOKED - MAKE SURE TIMER IS RUNNING
if (L4->L4TIMER == 0)
L4->L4TIMER = L4->SESSIONT1;
break;
}
// CHECK WINDOW
Outstanding = L4->TXSEQNO - L4->L4WS; // LAST FRAME ACKED - GIVES NUMBER OUTSTANING
// MOD 256, SO SHOULD HANDLE WRAP??
if (Outstanding > L4->L4WINDOW)
break;
}
else if (L4->L4CIRCUITTYPE & L2LINK)
{
// L2 LINK
LINK = L4->L4TARGET.LINK;
if (COUNT_AT_L2(LINK) > 5)
break;
}
// Not busy, so continue
L4->L4KILLTIMER = 0; //RESET SESSION TIMEOUTS
if(L4->L4CROSSLINK)
L4->L4CROSSLINK->L4KILLTIMER = 0;
Msg = Q_REM(&L4->L4TX_Q);
if (L4->L4CIRCUITTYPE & PACTOR)
{
// PACTOR-like - queue to Port
// Stream Number is in KAMSESSION
Msg->PORT = L4->KAMSESSION;
PORT = L4->L4TARGET.PORT;
C_Q_ADD(&PORT->PORTTX_Q, Msg);
continue;
}
// non-pactor
// IF CROSSLINK, QUEUE TO NEIGHBOUR, ELSE QUEUE ON LINK ENTRY
if (L4->L4CIRCUITTYPE & SESSION)
{
SENDL4MESSAGE(L4, Msg);
ReleaseBuffer(Msg);
break;
}
LINK = L4->L4TARGET.LINK;
C_Q_ADD(&LINK->TX_Q, Msg);
}
// if nothing on TX Queue If there is stuff on hold queue, timer must be running
// if (L4->L4TX_Q == 0 && L4->L4HOLD_Q)
if (L4->L4HOLD_Q)
{
if (L4->L4TIMER == 0)
{
L4->L4TIMER = L4->SESSIONT1;
}
}
// now check for rxed frames
while(L4->L4RX_Q)
{
Msg = Q_REM(&L4->L4RX_Q);
IFRM150(L4, Msg);
if (L4->L4USER[0] == 0) // HAVE JUST CLOSED SESSION!
goto NextSess;
}
// IF ACK IS PENDING, AND WE ARE AT RX WINDOW, SEND ACK NOW
Outstanding = L4->RXSEQNO - L4->L4LASTACKED;
if (Outstanding >= L4->L4WINDOW)
SENDL4IACK(L4);
NextSess:
L4++;
}
}
VOID CLEARSESSIONENTRY(TRANSPORTENTRY * Session)
{
// RETURN ANY QUEUED BUFFERS TO FREE QUEUE
while (Session->L4TX_Q)
ReleaseBuffer(Q_REM(&Session->L4TX_Q));
while (Session->L4RX_Q)
ReleaseBuffer(Q_REM(&Session->L4RX_Q));
while (Session->L4HOLD_Q)
ReleaseBuffer(Q_REM(&Session->L4HOLD_Q));
if (C_Q_COUNT(&Session->L4RESEQ_Q) > Session->L4WINDOW)
{
Debugprintf("Corrupt RESEQ_Q Q Len %d Free Buffs %d", C_Q_COUNT(&Session->L4RESEQ_Q), QCOUNT);
Session->L4RESEQ_Q = 0;
}
while (Session->L4RESEQ_Q)
ReleaseBuffer(Q_REM(&Session->L4RESEQ_Q));
memset(Session, 0, sizeof(TRANSPORTENTRY));
}
VOID CloseSessionPartner(TRANSPORTENTRY * Session)
{
// SEND CLOSE TO CROSSLINKED SESSION AND CLEAR LOCAL SESSION
if (Session == NULL)
return;
if (Session->L4CROSSLINK)
CLOSECURRENTSESSION(Session->L4CROSSLINK);
CLEARSESSIONENTRY(Session);
}
VOID CLOSECURRENTSESSION(TRANSPORTENTRY * Session)
{
MESSAGE * Buffer;
struct _LINKTABLE * LINK;
// SHUT DOWN SESSION AND UNLINK IF CROSSLINKED
if (Session == NULL)
return;
Session->L4CROSSLINK = NULL;
// IF STAY FLAG SET, KEEP SESSION, AND SEND MESSAGE
if (Session->STAYFLAG)
{
RETURNEDTONODE(Session);
Session->STAYFLAG = 0; // Only do it once
return;
}
if (Session->L4CIRCUITTYPE & BPQHOST)
{
// BPQ HOST MODE SESSION - INDICATE STATUS CHANGE
PBPQVECSTRUC HOST = Session->L4TARGET.HOST;
HOST->HOSTSESSION = 0;
HOST->HOSTFLAGS |= 3; /// State Change
PostStateChange(Session);
CLEARSESSIONENTRY(Session);
return;
}
if (Session->L4CIRCUITTYPE & PACTOR)
{
// PACTOR-type (Session linked to Port)
struct _EXTPORTDATA * EXTPORT = Session->L4TARGET.EXTPORT;
// If any data is queued, move it to the port entry, so it can be sent before the disconnect
while (Session->L4TX_Q)
{
Buffer = Q_REM(&Session->L4TX_Q);
EXTPORT->PORTCONTROL.PORTTXROUTINE(EXTPORT, Buffer);
}
EXTPORT->ATTACHEDSESSIONS[Session->KAMSESSION] = NULL;
CLEARSESSIONENTRY(Session);
return;
}
if (Session->L4CIRCUITTYPE & SESSION)
{
// L4 SESSION TO CLOSE
if (Session->L4HOLD_Q || Session->L4TX_Q) // WAITING FOR ACK or MORE TO SEND - SEND DISC LATER
{
Session->FLAGS |= DISCPENDING; // SEND DISC WHEN ALL DATA ACKED
return;
}
SENDL4DISC(Session);
return;
}
// Must be LEVEL 2 SESSION TO CLOSE
// COPY ANY PENDING DATA TO L2 TX Q, THEN GET RID OF SESSION
LINK = Session->L4TARGET.LINK;
if (LINK == NULL) // just in case!
{
CLEARSESSIONENTRY(Session);
return;
}
while (Session->L4TX_Q)
{
Buffer = Q_REM(&Session->L4TX_Q);
C_Q_ADD(&LINK->TX_Q, Buffer);
}
// NOTHING LEFT AT SESSION LEVEL
LINK->CIRCUITPOINTER = NULL; // CLEAR REVERSE LINK
if ((LINK->LINKWS != LINK->LINKNS) || LINK->TX_Q)
{
// STILL MORE TO SEND - SEND DISC LATER
LINK->L2FLAGS |= DISCPENDING; // SEND DISC WHEN ALL DATA ACKED
}
else
{
// NOTHING QUEUED - CAN SEND DISC NOW
LINK->L2STATE = 4; // DISCONNECTING
LINK->L2TIMER = 1; // USE TIMER TO KICK OFF DISC
}
CLEARSESSIONENTRY(Session);
}
VOID L4TimerProc()
{
// CHECK FOR TIMER EXPIRY
int n = MAXCIRCUITS;
TRANSPORTENTRY * L4 = L4TABLE;
TRANSPORTENTRY * Partner;
int MaxLinks = MAXLINKS;
while (n--)
{
if (L4->L4USER[0] == 0)
{
L4++;
continue;
}
// CHECK FOR L4BUSY SET AND NO LONGER BUSY
if (L4->NAKBITS & L4BUSY)
{
if ((CHECKIFBUSYL4(L4) & L4BUSY) == 0)
{
// no longer busy
L4->NAKBITS &= ~L4BUSY; // Clear busy
L4->L4ACKREQ = 1; // SEND ACK
}
}
if (L4->L4TIMER) // Timer Running?
{
L4->L4TIMER--;
if (L4->L4TIMER == 0) // Expired
L4TIMEOUT(L4);
}
if (L4->L4ACKREQ) // DELAYED ACK Timer Running?
{
L4->L4ACKREQ--;
if (L4->L4ACKREQ == 0) // Expired
SENDL4IACK(L4);
}
L4->L4KILLTIMER++;
// IF BIT 6 OF APPLFLAGS SET, SEND MSG EVERY 11 MINS TO KEEP SESSION OPEN
if (L4->L4CROSSLINK) // CONNECTED?
if (L4->SESS_APPLFLAGS & 0x40)
if (L4->L4KILLTIMER > 11 * 60)
AUTOTIMER(L4);
if (L4->L4LIMIT == 0)
L4->L4LIMIT = L4LIMIT;
else
{
if (L4->L4KILLTIMER > L4->L4LIMIT)
{
L4->L4KILLTIMER = 0;
// CLOSE THIS SESSION, AND ITS PARTNER (IF ANY)
L4->STAYFLAG = 0;
Partner = L4->L4CROSSLINK;
CLOSECURRENTSESSION(L4);
if (Partner)
{
Partner->L4KILLTIMER = 0; //ITS TIMES IS ALSO ABOUT TO EXPIRE
CLOSECURRENTSESSION(Partner); // CLOSE THIS ONE
}
}
}
L4++;
}
}
VOID L4TIMEOUT(TRANSPORTENTRY * L4)
{
// TIMER EXPIRED
// IF LINK UP REPEAT TEXT
// IF S2, REPEAT CONNECT REQUEST
// IF S4, REPEAT DISCONNECT REQUEST
struct DATAMESSAGE * Msg;
struct DATAMESSAGE * Copy;
struct DEST_LIST * DEST;
if (L4->L4STATE < 2)
return;
if (L4->L4STATE == 2)
{
// RETRY CONNECT
L4->L4RETRIES++;
if (L4->L4RETRIES > L4N2)
{
// RETRIED N2 TIMES - FAIL LINK
L4CONNECTFAILED(L4); // TELL OTHER PARTNER IT FAILED
CLEARSESSIONENTRY(L4);
return;
}
Debugprintf("Retrying L4 Connect Request");
SENDL4CONNECT(L4); // Resend connect
return;
}
if (L4->L4STATE == 4)
{
// RETRY DISCONNECT
L4->L4RETRIES++;
if (L4->L4RETRIES > L4N2)
{
// RETRIED N2 TIMES - FAIL LINK
CLEARSESSIONENTRY(L4);
return;
}
SENDL4DISC(L4); // Resend connect
return;
}
// STATE 5 OR ABOVE - RETRY INFO
L4->FLAGS &= ~L4BUSY; // CANCEL CHOKE
L4->L4RETRIES++;
if (L4->L4RETRIES > L4N2)
{
// RETRIED N2 TIMES - FAIL LINK
CloseSessionPartner(L4); // SEND CLOSE TO PARTNER (IF PRESENT)
return;
}
// RESEND ALL OUTSTANDING FRAMES
L4->FLAGS &= 0x7F; // CLEAR CHOKED
Msg = L4->L4HOLD_Q;
while (Msg)
{
Copy = GetBuff();
if (Copy == 0)
return;
memcpy(Copy, Msg, Msg->LENGTH);
DEST = L4->L4TARGET.DEST;
C_Q_ADD(&DEST->DEST_Q, Copy);
L4FRAMESRETRIED++;
Msg = Msg->CHAIN;
}
}
VOID AUTOTIMER(TRANSPORTENTRY * L4)
{
// SEND MESSAGE TO USER TO KEEP CIRCUIT OPEN
struct DATAMESSAGE * Msg = GetBuff();
if (Msg == 0)
return;
Msg->PID = 0xf0;
Msg->L2DATA[0] = 0;
Msg->L2DATA[1] = 0;
Msg->LENGTH = MSGHDDRLEN + 3;
C_Q_ADD(&L4->L4TX_Q, Msg);
PostDataAvailable(L4);
L4->L4KILLTIMER = 0;
if (L4->L4CROSSLINK)
L4->L4CROSSLINK->L4KILLTIMER = 0;
}
VOID L4CONNECTFAILED(TRANSPORTENTRY * L4)
{
// CONNECT HAS TIMED OUT - SEND MESSAGE TO OTHER END
struct DATAMESSAGE * Msg;
TRANSPORTENTRY * Partner;
UCHAR * ptr1;
char Nodename[20];
struct DEST_LIST * DEST;
Partner = L4->L4CROSSLINK;
if (Partner == 0)
return;
Msg = GetBuff();
if (Msg == 0)
return;
Msg->PID = 0xf0;
ptr1 = SetupNodeHeader(Msg);
DEST = L4->L4TARGET.DEST;
Nodename[DecodeNodeName(DEST->DEST_CALL, Nodename)] = 0; // null terminate
ptr1 += sprintf(ptr1, "Failure with %s\r", Nodename);
Msg->LENGTH = ptr1 - (UCHAR *)Msg;
C_Q_ADD(&Partner->L4TX_Q, Msg);
PostDataAvailable(Partner);