forked from g8bpq/ardop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpktSession.c
3806 lines (2731 loc) · 79.3 KB
/
pktSession.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
*/
// Level 2 Code for ARDOP-Packet
#ifdef WIN32
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <windows.h>
#include <winioctl.h>
#else
#define HANDLE int
#endif
#include "ARDOPC.h"
#ifndef WIN32
#define strtok_s strtok_r
#define _strupr strupr
#define SOCKET int
#include <sys/types.h>
#ifndef TEENSY
#include <sys/socket.h>
#endif
#endif
#define PFBIT 0x10 // POLL/FINAL BIT IN CONTROL BYTE
#define REJSENT 1 // SET WHEN FIRST REJ IS SENT IN REPLY
// TO AN I(P)
#define RNRSET 2 // RNR RECEIVED FROM OTHER END
#define DISCPENDING 8 // SEND DISC WHEN ALL DATA ACK'ED
#define RNRSENT 0x10 // WE HAVE SEND RNR
#define POLLSENT 0x20 // POLL BIT OUTSTANDING
#define L2TICK 10 // Timer called every 1/10 sec
#define TENSECS 10 * L2TICK
#define THREESECS 3 * L2TICK
#define MAXHOSTCHANNELS 10
#define UI 3
#define SABM 0x2F
#define DISC 0x43
#define DM 0x0F
#define UA 0x63
#define FRMR 0x87
#define RR 1
#define RNR 5
#define REJ 9
// V2.2 Types
#define SREJ 0x0D
#define SABME 0x6F
#define XID 0xAF
#define TEST 0xE3
#define SUPPORT2point2 1
// Reason Equates
#define NORMALCLOSE 0
#define RETRIEDOUT 1
#define SETUPFAILED 2
#define LINKLOST 3
#define LINKSTUCK 4
#define LINKIDLE 5
// XID Optional Functions
#define OPMustHave 0x02A080 // Sync TEST 16 bit FCS Extended Address
#define OPSREJ 4
#define OPSREJMult 0x200000
#define OPREJ 2
#define OPMod8 0x400
#define OPMod128 0x800
#define MAXBUFFS 50
extern int KRXPutPtr;
extern int KRXGetPtr;
extern UCHAR PacketMon[360];
extern int PacketMonMore;
extern int PacketMonLength;
typedef struct _MESSAGE
{
// BASIC LINK LEVEL MESSAGE BUFFER LAYOUT
// struct _LINKTABLE * Linkptr;
int DataLen;
UCHAR * DEST;
UCHAR * ORIGIN;
UCHAR * DIGIS;
UCHAR CTL;
UCHAR PID;
UCHAR * L2DATA;
}MESSAGE, *PMESSAGE;
typedef struct _DATABUFFER
{
// Data content of a packet to send or receive;
int DataLen;
UCHAR * Data;
} DATABUFFER;
typedef struct _LINKTABLE
{
//;
//; LEVEL 2 LINK CONTROL TABLE
//;
struct _LINKTABLE * Next;
UCHAR LINKCALL[7]; // CALLSIGN OF STATION
UCHAR OURCALL[7]; // CALLSIGN OF OUR END
UCHAR DIGIS[56]; // LEVEL 2 DIGIS IN PATH
UCHAR LINKTYPE; // 1 = UP, 2= DOWN, 3 = INTERNODE
UCHAR LINKNR;
UCHAR LINKNS; // LEV 2 SEQUENCE COUNTS
UCHAR LINKWS; // WINDOW START
UCHAR LINKOWS; // OLD (LAST ACKED) WINDOW START
UCHAR LINKWINDOW; // LEVEL 2 WINDOW SIZE
int PACLEN;
UCHAR L2FLAGS; // CONTROL BITS
UCHAR * TXBuffer; // Bytes to send (Malloced)
int TXCount; // Count of bytes to send
int TXBuffersize; // Currently allocated size
UCHAR * RXBuffer; // Bytes to send (Malloced)
int RXCount; // COunt of nytes to send
int RXBuffersize; // CUrrently allocated size
struct _DATABUFFER FRAMES[8]; // FRAMES WAITING ACK
struct _DATABUFFER RXFRAMES[8]; // Frames received out of sequence
UCHAR L2STATE; // PROCESSING STATE
UCHAR UseSREJ; // Set if running 2.2
unsigned short L2TIMER; // FRAME RETRY TIMER
UCHAR L2TIME; // RETRY TIMER INITIAL VALUE
unsigned short L2SLOTIM; // DELAY FOR LINK VALIDATION POLL
UCHAR REJTIMER; // TO TIME OUT REJ IN VERSION 1
unsigned short LAST_F_TIME; // TIME LAST R(F) SENT
UCHAR SDREJF; // FLAGS FOR FRMR
UCHAR SDRBYTE; // SAVED CONTROL BYTE FOR FRMR
UCHAR SDTSLOT ; // POINTER TO NEXT TXSLOT TO USE
UCHAR L2RETRIES; // RETRY COUNTER
UCHAR SESSACTIVE; // SET WHEN WE ARE SURE SESSION IS UP
unsigned short KILLTIMER; // TIME TO KILL IDLE LINK
VOID * L2FRAG_Q; // DEFRAGMENTATION QUEUE
int HostChannel; // DED Channel
int ReportFlags; // Inicate need to report event to host
#define ConFailed 1
#define ConBusy 2
#define ConOK 4
#define DiscRpt 8
#define Incomming 16
#define ReportQueue 32
// Stats for dynamic frame type, window and paclen
int pktMode;
int L2FRAMESFORUS;
int L2FRAMESSENT;
int L2TIMEOUTS;
int BytesSent;
int BytesReceived;
int PacketsSent;
int PacketsReceived;
int L2FRMRRX;
int L2FRMRTX;
// int RXERRORS; // RECEIVE ERRORS
int L2REJCOUNT; // REJ FRAMES RECEIVED
int L2OUTOFSEQ; // FRAMES RECEIVED OUT OF SEQUENCE
int L2RESEQ; // FRAMES RESEQUENCED
} LINKTABLE;
UCHAR MYCALL[7] = ""; // NODE CALLSIGN (BIT SHIFTED)
extern char Callsign[10];
extern char AuxCalls[10][10];
extern int AuxCallsLength;
char AuxAX[10][7]; // Aux Calls in ax.25 format
unsigned short SENDING; // LINK STATUS BITS
unsigned short ACTIVE;
UCHAR AVSENDING; // LAST MINUTE
UCHAR AVACTIVE;
UCHAR PORTWINDOW = 4; // L2 WINDOW FOR THIS PORT
int PORTTXDELAY; // TX DELAY FOR THIS PORT
UCHAR PORTPERSISTANCE; // PERSISTANCE VALUE FOR THIS PORT
int PORTSLOTTIME; // SLOT TIME
int PORTT1 = 4 * L2TICK;// L2 TIMEOUT
int PORTN2 = 6; // RETRIES
int PORTPACLEN; // DEFAULT PACLEN FOR INCOMING SESSIONS
int PORTMAXDIGIS = 3;
int T3 = 180 * L2TICK;
int L2KILLTIME = 600 * L2TICK;
VOID * PORTTX_Q;
VOID * FREE_Q;
int initMode = 1;
int SABMMode = 1;
// Modes now
// "4PSK/200",
// "4FSK/500", "4PSK/500", "8PSK/500", "16QAM/500",
// "4FSK/1000", "4PSK/1000", "8PSK/1000", "16QAM/1000",
// "4FSK/2000", "4PSK/2000", "8PSK/2000", "16QAM/2000"
int CtlMode[16] = { // Control frames are sent in faster modes but not full speed as they are short
1,
1, 2, 2, 2,
5, 6, 6, 6,
9, 9, 9 ,9};
int initMaxFrame = 7; // Will be overriden depending on Mode
int initPacLen = 0;
const int defaultPacLen[16] = {
32,
32, 64, 64, 64,
64, 128, 128, 128,
128, 256, 256, 256};
// Paclen 256 is fine with 8 car 4PSK and just about ok with 4 car 4PSK.
// Also just about ok with 2 car QAM (4 Secs packet time)
// 2 Car 4PSK best limited to 128, 1 CAR 64
struct _LINKTABLE * LINKS = NULL;
int QCOUNT = 0;
int MINBUFFCOUNT = 0;
BOOL PacketHost = 0; // Set if TCP Packet connection is in Host Mode not KISS Mode
VOID L2SENDCOMMAND();
VOID SETUPL2MESSAGE(MESSAGE * Buffer, struct _LINKTABLE * LINK, UCHAR CMD);
VOID SendSupervisCmd(struct _LINKTABLE * LINK);
void SEND_RR_RESP(struct _LINKTABLE * LINK, UCHAR PF);
VOID L2SENDRESPONSE(struct _LINKTABLE * LINK, int CMD);
VOID L2SENDCOMMAND(struct _LINKTABLE * LINK, int CMD);
VOID ACKMSG(struct _LINKTABLE * LINK);
VOID InformPartner(struct _LINKTABLE * LINK, int Reason);
unsigned int RR_OR_RNR(struct _LINKTABLE * LINK);
VOID L2TIMEOUT(struct _LINKTABLE * LINK);
VOID CLEAROUTLINK(struct _LINKTABLE * LINK);
VOID SENDFRMR(struct _LINKTABLE * LINK);
VOID SDFRMR(struct _LINKTABLE * LINK);
VOID SDNRCHK(struct _LINKTABLE * LINK, UCHAR CTL);
VOID RESETNS(struct _LINKTABLE * LINK, UCHAR NS);
VOID PROC_I_FRAME(struct _LINKTABLE * LINK, UCHAR * Data, int DataLen);
VOID RESET2X(struct _LINKTABLE * LINK);
VOID RESET2(struct _LINKTABLE * LINK);
VOID CONNECTREFUSED(struct _LINKTABLE * LINK);
VOID SDUFRM(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL);
VOID SFRAME(struct _LINKTABLE * LINK, UCHAR CTL, UCHAR MSGFLAG);
VOID SDIFRM(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
VOID SENDCONNECTREPLY(struct _LINKTABLE * LINK);
VOID SETUPNEWL2SESSION(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR MSGFLAG);
VOID L2SABM(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR MSGFLAG);
VOID L2SENDUA(struct _LINKTABLE * LINK, MESSAGE * Buffer);
VOID L2SENDDM(struct _LINKTABLE * LINK, MESSAGE * Buffer);
VOID L2SENDRESP(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL);
int COUNTLINKS(int Port);
VOID L2_PROCESS(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
VOID PUT_ON_PORT_Q(struct _LINKTABLE * LINK, MESSAGE * Buffer);
VOID L2SWAPADDRESSES(MESSAGE * Buffer);
struct _LINKTABLE * FindLink(UCHAR * LinkCall, UCHAR * OurCall);
VOID SENDSABM(struct _LINKTABLE * LINK);
VOID L2SENDXID(struct _LINKTABLE * LINK);
VOID L2LINKACTIVE(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
BOOL CompareAliases(UCHAR * c1, UCHAR * c2);
VOID L2FORUS(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
VOID MHPROC(MESSAGE * Buffer);
VOID L2SENDINVALIDCTRL(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL);
UCHAR * SETUPADDRESSES(struct _LINKTABLE * LINK, PMESSAGE Msg);
VOID ProcessXIDCommand(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
UCHAR * PacketSessionPoll(UCHAR * NextChan);
VOID PktSend(UCHAR * Msg, int Len);
VOID ProcessKISSByte(UCHAR c);
VOID PutString(UCHAR * Msg);
int PutChar(UCHAR c);
int SerialSendData(UCHAR * Message,int MsgLen);
VOID EmCRCStuffAndSend(UCHAR * Msg, int Len);
VOID DisplaySessionStats(struct _LINKTABLE * LINK, int Exitcode);
int REALTIMETICKS = 0;
// MSGFLAG contains CMD/RESPONSE BITS
#define CMDBIT 4 // CURRENT MESSAGE IS A COMMAND
#define RESP 2 // CURRENT MSG IS RESPONSE
// FRMR REJECT FLAGS
#define SDINVC 1 // INVALID COMMAND
#define SDNRER 8 // INVALID N(R)
UCHAR NO_CTEXT = 0;
UCHAR MSGFLAG = 0;
extern UCHAR * ALIASPTR;
UCHAR QSTCALL[7] = {'Q'+'Q','S'+'S','T'+'T',0x40,0x40,0x40,0xe0}; // QST IN AX25
UCHAR NODECALL[7] = {0x9C, 0x9E, 0x88, 0x8A, 0xA6, 0x40, 0xE0}; // 'NODES' IN AX25 FORMAT
VOID * GetBuff()
{
MESSAGE * Buffer = malloc(sizeof(MESSAGE));
return Buffer;
}
unsigned int ReleaseBuffer(VOID *pBUFF)
{
free(pBUFF);
return 0;
}
int C_Q_ADD(VOID *PQ, VOID *PBUFF)
{
unsigned int * Q;
unsigned int * BUFF = (unsigned int *)PBUFF;
unsigned int * next;
int n = 0;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = (unsigned int *) PQ;
BUFF[0]=0; // Clear chain in new buffer
if (Q[0] == 0) // Empty
{
Q[0]=(unsigned int)BUFF; // New one on front
return(0);
}
next = (unsigned int *)Q[0];
while (next[0]!=0)
{
next=(unsigned int *)next[0]; // Chain to end of queue
}
next[0]=(unsigned int)BUFF; // New one on end
return(0);
}
BOOL ConvToAX25(char * callsign, unsigned char * ax25call)
{
int i;
memset(ax25call,0x40,6); // in case short
ax25call[6]=0x60; // default SSID
for (i=0;i<7;i++)
{
if (callsign[i] == '-')
{
//
// process ssid and return
//
i = atoi(&callsign[i+1]);
if (i < 16)
{
ax25call[6] |= i<<1;
return (TRUE);
}
return (FALSE);
}
if (callsign[i] == 0 || callsign[i] == 13 || callsign[i] == ' ' || callsign[i] == ',')
{
//
// End of call - no ssid
//
return (TRUE);
}
ax25call[i] = callsign[i] << 1;
}
//
// Too many chars
//
return (FALSE);
}
int ConvFromAX25(unsigned char * incall, char * outcall)
{
int in,out=0;
unsigned char chr;
memset(outcall,0x20,10);
for (in=0;in<6;in++)
{
chr=incall[in];
if (chr == 0x40)
break;
chr >>= 1;
outcall[out++]=chr;
}
chr=incall[6]; // ssid
if (chr == 0x42)
{
outcall[out++]='-';
outcall[out++]='T';
return out;
}
if (chr == 0x44)
{
outcall[out++]='-';
outcall[out++]='R';
return out;
}
chr >>= 1;
chr &= 15;
if (chr > 0)
{
outcall[out++]='-';
if (chr > 9)
{
chr-=10;
outcall[out++]='1';
}
chr+=48;
outcall[out++]=chr;
}
return (out);
}
VOID ConvertCallstoAX25()
{
int n;
ConvToAX25(Callsign, MYCALL);
for (n = 0; n < AuxCallsLength; n++)
ConvToAX25(&AuxCalls[n][0], &AuxAX[n][0]);
}
BOOL CompareCalls(UCHAR * c1, UCHAR * c2)
{
// COMPARE AX25 CALLSIGNS IGNORING EXTRA BITS IN SSID
if (memcmp(c1, c2, 6))
return FALSE; // No Match
if ((c1[6] & 0x1e) == (c2[6] & 0x1e))
return TRUE;
return FALSE;
}
BOOL CompareAliases(UCHAR * c1, UCHAR * c2)
{
// COMPARE first 6 chars of AX25 CALLSIGNS
if (memcmp(c1, c2, 6))
return FALSE; // No Match
return TRUE;
}
struct _LINKTABLE * NewLink()
{
struct _LINKTABLE * LINK = malloc(sizeof(struct _LINKTABLE));
int i;
if (LINK == NULL)
return NULL;
memset(LINK, 0, sizeof(struct _LINKTABLE));
// Allocate TX and RX Buffers
LINK->TXBuffer = malloc(512); // Initial TX size. May Realloc if more needed
if (LINK->TXBuffer == NULL)
{
free(LINK);
return NULL;
}
LINK->RXBuffer = malloc(512); // Initial TX size. May Realloc if more needed
if (LINK->RXBuffer == NULL)
{
free (LINK->TXBuffer);
free(LINK);
return NULL;
}
LINK->TXBuffersize = 512; // Currently allocated size
LINK->RXBuffersize = 512; // Currently allocated size
LINK->pktMode = initMode; // Set by PAC commands
// Get Default PACLEN for Mode
// Paclen 256 is fine with 8 car 4PSK and just about ok with 4 car 4PSK.
// Also just about ok with 2 car QAM (4 Secs packet time)
// 2 Car 4PSK best limited to 128
if (initPacLen == 0) // Use configured if set
LINK->PACLEN = defaultPacLen[initMode]; // Starting Point
else
LINK->PACLEN = initPacLen; // Starting Point
initMaxFrame = 7;
LINK->LINKWINDOW = initMaxFrame; // Starting Point
LINK->UseSREJ = TRUE;
// Allocate Frame buffers
for (i = 0; i < 8; i++)
{
LINK->FRAMES[i].Data = malloc(LINK->PACLEN); // paclen
}
if (LINK->FRAMES[7].Data == NULL)
{
// No Memory - release everything
for (i = 0; i < 8; i++)
{
if (LINK->FRAMES[i].Data)
free(LINK->FRAMES[i].Data);
}
free (LINK->TXBuffer);
free (LINK->RXBuffer);
free(LINK);
return NULL;
}
// Add to chain
if (LINKS == NULL)
LINKS = LINK;
else
{
LINK->Next = LINKS;
LINKS = LINK;
}
return LINK;
}
struct _LINKTABLE * FindLink(UCHAR * LinkCall, UCHAR * OurCall)
{
struct _LINKTABLE * LINK = LINKS;
while (LINK)
{
if (CompareCalls(LINK->LINKCALL, LinkCall) && CompareCalls(LINK->OURCALL, OurCall))
return LINK;
LINK = LINK->Next;
}
return NULL;
}
int FindFreeHostChannel()
{
struct _LINKTABLE * LINK = LINKS;
int Channels[32] = {0};
int i;
while (LINK)
{
Channels[LINK->HostChannel] = 1; // Mark as in use
LINK = LINK->Next;
}
// Return first free
for(i = 1; i < MAXHOSTCHANNELS; i++)
{
if (Channels[i] == 0)
return i;
}
return 0;
}
VOID L2Routine(UCHAR * Packet, int Length, int FrameQuality, int totalRSErrors, int NumCar, int pktRXMode)
{
// LEVEL 2 PROCESSING
// Packet is passed as received from Soundcard.c. It might be quite long
// if using high speed modes (not sure of max size yet)
// Note we must have finished with the data in the buffer before returning.
// If we need to queue anything will have to make a copy
MESSAGE Buffer = {0};
struct _LINKTABLE * LINK;
UCHAR * pktptr = Packet;
UCHAR * ptr;
int n;
UCHAR CTL;
UCHAR c;
// Check for invalid length (< 22 7Header + 7Addr + 7Addr + CTL
if (Length < 15)
{
return;
}
// Trace it
if (PacketMonLength == 0) // Ingore if one queued
{
PacketMon[0] = 0; // RX Flag
memcpy(&PacketMon[1], Packet, Length);
PacketMonLength = Length + 1;
}
// Separate out Header fields.
Buffer.DEST = pktptr;
pktptr += 7;
Buffer.ORIGIN = pktptr;
pktptr += 7;
// Check for corrupt callsign
// Check for Corrupted Callsign in Origin (to keep MH list clean)
ptr = Buffer.ORIGIN;
n = 6;
while(n--)
{
// Try a bit harder to detect corruption
c = *(ptr++);
if (c & 1)
{
return;
}
c = c >> 1;
if (!isalnum(c) && !(c == '#') && !(c == ' '))
{
return;
}
}
// Check Digis if present
Buffer.DIGIS = NULL;
if ((Buffer.ORIGIN[6] & 1) == 0) // Digis
{
ptr = pktptr;
Buffer.DIGIS = ptr;
n = 6;
while(n--)
{
c = *(ptr++);
if (c & 1)
{
return;
}
c = c >> 1;
if (!isalnum(c) && !(c == '#') && !(c == ' '))
{
return;
}
}
}
// Check Digis if present
// For the moment ARDOP Packet doesn't support digipeating
// CHECK THAT ALL DIGIS HAVE BEEN ACTIONED,
n = 8; // MAX DIGIS
ptr = &Buffer.ORIGIN[6]; // End of Address bit
while ((*ptr & 1) == 0)
{
// MORE TO COME
ptr += 7;
if ((*ptr & 0x80) == 0) // Digi'd bit
{
return; // not complete and not for us
}
n--;
if (n == 0)
{
return; // Corrupt - no end of address bit
}
}
// Reached End of digis, and all actioned, so can process it
MSGFLAG = 0; // CMD/RESP UNDEFINED
ptr++; // now points to CTL
Buffer.CTL = *(ptr++);
Buffer.PID = *(ptr++);
Buffer.L2DATA = ptr;
Buffer.DataLen = Length - (ptr - Packet);
// GET CMD/RESP BITS
if (Buffer.DEST[6] & 0x80)
{
MSGFLAG |= CMDBIT;
}
else
{
if (Buffer.ORIGIN[6] & 0x80) // Only Dest Set
MSGFLAG |= RESP;
}
// SEE IF FOR AN ACTIVE LINK SESSION
CTL = Buffer.CTL;
// IF A UI, THERE IS NO SESSION
LINK = FindLink(Buffer.ORIGIN, Buffer.DEST);
if (LINK)
{
L2LINKACTIVE(LINK, &Buffer, CTL, MSGFLAG);
return;
}
if (CompareCalls(Buffer.DEST, MYCALL))
goto FORUS;
for (n = 0; n < AuxCallsLength; n++)
{
if (CompareCalls(Buffer.DEST, &AuxAX[n][0]))
goto FORUS;
}
// Not addresses to us
return;
FORUS:
// if a UI frame and UIHook Specified, call it
L2FORUS(LINK, &Buffer, CTL, MSGFLAG);
}
VOID L2FORUS(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG)
{
// MESSAGE ADDRESSED TO OUR CALL OR ALIAS, BUT NOT FOR AN ACTIVE SESSION
// LINK points to an empty link table entry
int CTLlessPF = CTL & ~PFBIT;
NO_CTEXT = 0;
// ONLY SABM or UI ALLOWED IF NO SESSION
// Plus XID/TEST/SABME if V2.2 support enabled
if (CTLlessPF == 3) // UI
{
// A UI ADDRESSED TO US - SHOULD ONLY BE FOR IP, or possibly addressed NODES
switch(Buffer->PID)
{
case 0xcf: // Netrom
break;
case 0xcc: // TCP
case 0xcd: // ARP
case 0x08: // NOS FRAGMENTED AX25 TCP/IP
return;
}
return;
}
if (CTLlessPF == SABME)
{
// Although some say V2.2 requires SABME I don't agree!
// Reject until we support Mod 128
L2SENDINVALIDCTRL(LINK, Buffer, CTL);
return;
}
if (CTLlessPF == SREJ) // Used to see if other end supports SREJ on 2.0
{
// We do, so send DM
L2SENDRESP(LINK, Buffer, DM);
return;
}
if (CTLlessPF == XID)
{
// Send FRMR if we only support V 2.0
if (SUPPORT2point2 == FALSE)
{
L2SENDINVALIDCTRL(LINK, Buffer, CTL);
return;
}
// if Support 2.2 drop through
}
if (CTLlessPF == TEST)
{
// I can't see amy harm in replying to TEST
L2SENDRESP(LINK, Buffer, TEST);
return;
}
// if (CTLlessPF != SABM && CTLlessPF != SABME)
if (CTLlessPF != SABM && CTLlessPF != XID)
{
if ((MSGFLAG & CMDBIT) && (CTL & PFBIT)) // Command with P?
L2SENDDM(LINK, Buffer);
return;
}
// Exclude and limit tests are done for XID and SABM
#ifdef EXCLUDEBITS
// CHECK ExcludeList
if (CheckExcludeList(Buffer->ORIGIN) == 0)
{
ReleaseBuffer(Buffer);
return;
}
#endif
// OK to accept SABM or XID
LINK = NewLink();
if (LINK == NULL)
{
L2SENDDM(LINK, Buffer); // No memory
return;
}
// Find Free Host Session
LINK->HostChannel = FindFreeHostChannel();
if (LINK->HostChannel == 0)
{
L2SENDDM(LINK, Buffer); // Busy
return;
}
if (CTLlessPF == XID)
{
ProcessXIDCommand(LINK, Buffer, CTL, MSGFLAG);
return;
}
// Not XID, so must be SABM
L2SABM(LINK, Buffer, MSGFLAG); // Process the SABM
}
VOID ProcessXIDCommand(struct _LINKTABLE * LINK, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG)
{
// I think it is fairly safe to accept XID as soon as we
// can process SREJ, but only accept Mod 8 and 256 Byte frames
// I think the only way to run 2.2 Mod 8 is to preceed a
// SABM with XID, but others don't seem to agree!
// Run through XID fields, changing any we don't like,
// then return an XID response
// Decode and process XID
UCHAR * ptr = &Buffer->PID;
UCHAR * ptr1, * ptr2;
UCHAR TEMPDIGI[57];
int n;
if (*ptr++ == 0x82 && *ptr++ == 0x80)
{
int Type;
int Len;
unsigned int value;
int xidlen = *(ptr++) << 8;
xidlen += *ptr++;
// XID is set of Type, Len, Value n-tuples