forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonCode.c
5565 lines (4027 loc) · 113 KB
/
CommonCode.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-2022 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
*/
// General C Routines common to bpq32 and linbpq. Mainly moved from BPQ32.c
#pragma data_seg("_BPQDATA")
#define _CRT_SECURE_NO_DEPRECATE
#include <stdlib.h>
#include <string.h>
#include <time.h>
#pragma data_seg("_BPQDATA")
#include "CHeaders.h"
#include "tncinfo.h"
#include "configstructs.h"
extern struct CONFIGTABLE xxcfg;
#define LIBCONFIG_STATIC
#include "libconfig.h"
#ifndef LINBPQ
//#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#include "commctrl.h"
#include "Commdlg.h"
#endif
struct TNCINFO * TNCInfo[71]; // Records are Malloc'd
extern int ReportTimer;
Dll VOID APIENTRY Send_AX(UCHAR * Block, DWORD Len, UCHAR Port);
TRANSPORTENTRY * SetupSessionFromHost(PBPQVECSTRUC HOST, UINT ApplMask);
int Check_Timer();
VOID SENDUIMESSAGE(struct DATAMESSAGE * Msg);
DllExport struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot);
VOID APIENTRY md5 (char *arg, unsigned char * checksum);
VOID COMSetDTR(HANDLE fd);
VOID COMClearDTR(HANDLE fd);
VOID COMSetRTS(HANDLE fd);
VOID COMClearRTS(HANDLE fd);
VOID WriteMiniDump();
void printStack(void);
char * FormatMH(PMHSTRUC MH, char Format);
void WriteConnectLog(char * fromCall, char * toCall, UCHAR * Mode);
void SendDataToPktMap(char *Msg);
extern BOOL LogAllConnects;
extern BOOL M0LTEMap;
extern VOID * ENDBUFFERPOOL;
// Read/Write length field in a buffer header
// Needed for Big/LittleEndian and ARM5 (unaligned operation problem) portability
VOID PutLengthinBuffer(PDATAMESSAGE buff, USHORT datalen)
{
if (datalen <= sizeof(void *) + 4)
datalen = sizeof(void *) + 4; // Protect
memcpy(&buff->LENGTH, &datalen, 2);
}
int GetLengthfromBuffer(PDATAMESSAGE buff)
{
USHORT Length;
memcpy(&Length, &buff->LENGTH, 2);
return Length;
}
BOOL CheckQHeadder(UINT * Q)
{
#ifdef WIN32
UINT Test;
__try
{
Test = *Q;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Debugprintf("Invalid Q Header %p", Q);
printStack();
return FALSE;
}
#endif
return TRUE;
}
// Get buffer from Queue
VOID * _Q_REM(VOID **PQ, char * File, int Line)
{
void ** Q;
void ** first;
VOID * next;
PMESSAGE Test;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = PQ;
if (Semaphore.Flag == 0)
Debugprintf("Q_REM called without semaphore from %s Line %d", File, Line);
if (CheckQHeadder((UINT *) Q) == 0)
return(0);
first = Q[0];
if (first == 0)
return (0); // Empty
next = first[0]; // Address of next buffer
Q[0] = next;
// Make sure guard zone is zeros
Test = (PMESSAGE)first;
if (Test->GuardZone != 0)
{
Debugprintf("Q_REM %p GUARD ZONE CORRUPT %x Called from %s Line %d", first, Test->GuardZone, File, Line);
printStack();
}
return first;
}
// Non=pool version (for IPGateway)
VOID * _Q_REM_NP(VOID *PQ, char * File, int Line)
{
void ** Q;
void ** first;
void * next;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = PQ;
if (CheckQHeadder((UINT *)Q) == 0)
return(0);
first = Q[0];
if (first == 0) return (0); // Empty
next = first[0]; // Address of next buffer
Q[0] = next;
return first;
}
// Return Buffer to Free Queue
extern VOID * BUFFERPOOL;
extern void ** Bufferlist[1000];
void printStack(void);
void _CheckGuardZone(char * File, int Line)
{
int n = 0, i, offset = 0;
PMESSAGE Test;
UINT CodeDump[8];
unsigned char * ptr;
n = NUMBEROFBUFFERS;
while (n--)
{
Test = (PMESSAGE)Bufferlist[n];
if (Test && Test->GuardZone)
{
Debugprintf("CheckGuardZone %p GUARD ZONE CORRUPT %d Called from %s Line %d", Test, Test->Process, File, Line);
offset = 0;
ptr = (unsigned char *)Test;
while (offset < 400)
{
memcpy(CodeDump, &ptr[offset], 32);
for (i = 0; i < 8; i++)
CodeDump[i] = htonl(CodeDump[i]);
Debugprintf("%08x %08x %08x %08x %08x %08x %08x %08x %08x ",
&ptr[offset], CodeDump[0], CodeDump[1], CodeDump[2], CodeDump[3], CodeDump[4], CodeDump[5], CodeDump[6], CodeDump[7]);
offset += 32;
}
WriteMiniDump();
#ifdef MDIKERNEL
CloseAllNeeded = 1;
#endif
}
}
}
UINT _ReleaseBuffer(VOID *pBUFF, char * File, int Line)
{
void ** pointer, ** BUFF = pBUFF;
int n = 0;
void ** debug;
PMESSAGE Test;
UINT CodeDump[16];
int i;
unsigned int rev;
if (Semaphore.Flag == 0)
Debugprintf("ReleaseBuffer called without semaphore from %s Line %d", File, Line);
// Make sure address is within pool
if ((uintptr_t)BUFF < (uintptr_t)BUFFERPOOL || (uintptr_t)BUFF > (uintptr_t)ENDBUFFERPOOL)
{
// Not pointing to a buffer . debug points to the buffer that this is chained from
// Dump first chunk and source tag
memcpy(CodeDump, BUFF, 64);
Debugprintf("Releasebuffer Buffer not in pool from %s Line %d, ptr %p prev %d", File, Line, BUFF, 0);
for (i = 0; i < 16; i++)
{
rev = (CodeDump[i] & 0xff) << 24;
rev |= (CodeDump[i] & 0xff00) << 8;
rev |= (CodeDump[i] & 0xff0000) >> 8;
rev |= (CodeDump[i] & 0xff000000) >> 24;
CodeDump[i] = rev;
}
Debugprintf("%08x %08x %08x %08x %08x %08x %08x %08x %08x ",
Bufferlist[n], CodeDump[0], CodeDump[1], CodeDump[2], CodeDump[3], CodeDump[4], CodeDump[5], CodeDump[6], CodeDump[7]);
Debugprintf(" %08x %08x %08x %08x %08x %08x %08x %08x",
CodeDump[8], CodeDump[9], CodeDump[10], CodeDump[11], CodeDump[12], CodeDump[13], CodeDump[14], CodeDump[15]);
return 0;
}
Test = (PMESSAGE)pBUFF;
if (Test->GuardZone != 0)
{
Debugprintf("_ReleaseBuffer %p GUARD ZONE CORRUPT %x Called from %s Line %d", pBUFF, Test->GuardZone, File, Line);
}
while (n <= NUMBEROFBUFFERS)
{
if (BUFF == Bufferlist[n++])
goto BOK1;
}
Debugprintf("ReleaseBuffer %X not in Pool called from %s Line %d", BUFF, File, Line);
printStack();
return 0;
BOK1:
n = 0;
// validate free Queue
pointer = FREE_Q;
debug = &FREE_Q;
while (pointer)
{
// Validate pointer to make sure it is in pool - it may be a duff address if Q is corrupt
Test = (PMESSAGE)pointer;
if (Test->GuardZone || (uintptr_t)pointer < (uintptr_t)BUFFERPOOL || (uintptr_t)pointer > (uintptr_t)ENDBUFFERPOOL)
{
// Not pointing to a buffer . debug points to the buffer that this is chained from
// Dump first chunk and source tag
memcpy(CodeDump, debug, 64);
Debugprintf("Releasebuffer Pool Corruption n = %d, ptr %p prev %p", n, pointer, debug);
for (i = 0; i < 16; i++)
{
rev = (CodeDump[i] & 0xff) << 24;
rev |= (CodeDump[i] & 0xff00) << 8;
rev |= (CodeDump[i] & 0xff0000) >> 8;
rev |= (CodeDump[i] & 0xff000000) >> 24;
CodeDump[i] = rev;
}
Debugprintf("%08x %08x %08x %08x %08x %08x %08x %08x %08x ",
Bufferlist[n], CodeDump[0], CodeDump[1], CodeDump[2], CodeDump[3], CodeDump[4], CodeDump[5], CodeDump[6], CodeDump[7]);
Debugprintf(" %08x %08x %08x %08x %08x %08x %08x %08x",
CodeDump[8], CodeDump[9], CodeDump[10], CodeDump[11], CodeDump[12], CodeDump[13], CodeDump[14], CodeDump[15]);
if (debug[400])
Debugprintf(" %s", &debug[400]);
}
// See if already on free Queue
if (pointer == BUFF)
{
Debugprintf("Trying to free buffer %p when already on FREE_Q called from %s Line %d", BUFF, File, Line);
// WriteMiniDump();
return 0;
}
// if (pointer[0] && pointer == pointer[0])
// {
// Debugprintf("Buffer chained to itself");
// return 0;
// }
debug = pointer;
pointer = pointer[0];
n++;
if (n > 1000)
{
Debugprintf("Loop searching free chain - pointer = %p %p", debug, pointer);
return 0;
}
}
pointer = FREE_Q;
*BUFF = pointer;
FREE_Q = BUFF;
QCOUNT++;
return 0;
}
int _C_Q_ADD(VOID *PQ, VOID *PBUFF, char * File, int Line)
{
void ** Q;
void ** BUFF = PBUFF;
void ** next;
PMESSAGE Test;
int n = 0;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = PQ;
if (Semaphore.Flag == 0)
Debugprintf("C_Q_ADD called without semaphore from %s Line %d", File, Line);
if (CheckQHeadder((UINT *)Q) == 0) // Make sure Q header is readable
return(0);
// Make sure guard zone is zeros
Test = (PMESSAGE)PBUFF;
if (Test->GuardZone != 0)
{
Debugprintf("C_Q_ADD %p GUARD ZONE CORRUPT %x Called from %s Line %d", PBUFF, Test->GuardZone, File, Line);
}
Test = (PMESSAGE)Q;
// Make sure address is within pool
while (n <= NUMBEROFBUFFERS)
{
if (BUFF == Bufferlist[n++])
goto BOK2;
}
Debugprintf("C_Q_ADD %X not in Pool called from %s Line %d", BUFF, File, Line);
printStack();
return 0;
BOK2:
BUFF[0] = 0; // Clear chain in new buffer
if (Q[0] == 0) // Empty
{
Q[0]=BUFF; // New one on front
return(0);
}
next = Q[0];
while (next[0] != 0)
{
next = next[0]; // Chain to end of queue
}
next[0] = BUFF; // New one on end
return(0);
}
// Non-pool version
int C_Q_ADD_NP(VOID *PQ, VOID *PBUFF)
{
void ** Q;
void ** BUFF = PBUFF;
void ** next;
int n = 0;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = PQ;
if (CheckQHeadder((UINT *)Q) == 0) // Make sure Q header is readable
return(0);
BUFF[0]=0; // Clear chain in new buffer
if (Q[0] == 0) // Empty
{
Q[0]=BUFF; // New one on front
// memcpy(PQ, &BUFF, 4);
return 0;
}
next = Q[0];
while (next[0] != 0)
next=next[0]; // Chain to end of queue
next[0] = BUFF; // New one on end
return(0);
}
int C_Q_COUNT(VOID *PQ)
{
void ** Q;
int count = 0;
// PQ may not be word aligned, so copy as bytes (for ARM5)
Q = PQ;
if (CheckQHeadder((UINT *)Q) == 0) // Make sure Q header is readable
return(0);
// SEE HOW MANY BUFFERS ATTACHED TO Q HEADER
while (*Q)
{
count++;
if ((count + QCOUNT) > MAXBUFFS)
{
Debugprintf("C_Q_COUNT Detected corrupt Q %p len %d", PQ, count);
return count;
}
Q = *Q;
}
return count;
}
VOID * _GetBuff(char * File, int Line)
{
UINT * Temp;
MESSAGE * Msg;
char * fptr = 0;
unsigned char * byteaddr;
Temp = Q_REM(&FREE_Q);
// FindLostBuffers();
if (Semaphore.Flag == 0)
Debugprintf("GetBuff called without semaphore from %s Line %d", File, Line);
if (Temp)
{
QCOUNT--;
if (QCOUNT < MINBUFFCOUNT)
MINBUFFCOUNT = QCOUNT;
Msg = (MESSAGE *)Temp;
fptr = File + (int)strlen(File);
while (*fptr != '\\' && *fptr != '/')
fptr--;
fptr++;
// Buffer Length is BUFFLEN, but buffers are allocated 512
// So add file info in gap between
byteaddr = (unsigned char *)Msg;
memset(&byteaddr[0], 0, 64); // simplify debugging lost buffers
memset(&byteaddr[400], 0, 64); // simplify debugging lost buffers
sprintf(&byteaddr[400], "%s %d", fptr, Line);
Msg->Process = (short)GetCurrentProcessId();
Msg->Linkptr = NULL;
}
else
Debugprintf("Warning - Getbuff returned NULL");
return Temp;
}
void * zalloc(int len)
{
// malloc and clear
void * ptr;
ptr=malloc(len);
if (ptr)
memset(ptr, 0, len);
return ptr;
}
char * strlop(char * buf, char delim)
{
// Terminate buf at delim, and return rest of string
char * ptr;
if (buf == NULL) return NULL; // Protect
ptr = strchr(buf, delim);
if (ptr == NULL) return NULL;
*(ptr)++=0;
return ptr;
}
VOID DISPLAYCIRCUIT(TRANSPORTENTRY * L4, char * Buffer)
{
UCHAR Type = L4->L4CIRCUITTYPE;
struct PORTCONTROL * PORT;
struct _LINKTABLE * LINK;
BPQVECSTRUC * VEC;
struct DEST_LIST * DEST;
char Normcall[20] = ""; // Could be alias:call
char Normcall2[11] = "";
char Alias[11] = "";
Buffer[0] = 0;
switch (Type)
{
case PACTOR+UPLINK:
PORT = L4->L4TARGET.PORT;
ConvFromAX25(L4->L4USER, Normcall);
strlop(Normcall, ' ');
if (PORT)
sprintf(Buffer, "%s %d/%d(%s)", "TNC Uplink Port", PORT->PORTNUMBER, L4->KAMSESSION, Normcall);
return;
case PACTOR+DOWNLINK:
PORT = L4->L4TARGET.PORT;
if (PORT)
sprintf(Buffer, "%s %d/%d", "Attached to Port", PORT->PORTNUMBER, L4->KAMSESSION);
return;
case L2LINK+UPLINK:
LINK = L4->L4TARGET.LINK;
ConvFromAX25(L4->L4USER, Normcall);
strlop(Normcall, ' ');
if (LINK &&LINK->LINKPORT)
sprintf(Buffer, "%s %d(%s)", "Uplink", LINK->LINKPORT->PORTNUMBER, Normcall);
return;
case L2LINK+DOWNLINK:
LINK = L4->L4TARGET.LINK;
if (LINK == NULL)
return;
ConvFromAX25(LINK->OURCALL, Normcall);
strlop(Normcall, ' ');
ConvFromAX25(LINK->LINKCALL, Normcall2);
strlop(Normcall2, ' ');
sprintf(Buffer, "%s %d(%s %s)", "Downlink", LINK->LINKPORT->PORTNUMBER, Normcall, Normcall2);
return;
case BPQHOST + UPLINK:
case BPQHOST + DOWNLINK:
// if the call has a Level 4 address display ALIAS:CALL, else just Call
if (FindDestination(L4->L4USER, &DEST))
Normcall[DecodeNodeName(DEST->DEST_CALL, Normcall)] = 0; // null terminate
else
Normcall[ConvFromAX25(L4->L4USER, Normcall)] = 0;
VEC = L4->L4TARGET.HOST;
sprintf(Buffer, "%s%02d(%s)", "Host", (int)(VEC - BPQHOSTVECTOR) + 1, Normcall);
return;
case SESSION + DOWNLINK:
case SESSION + UPLINK:
ConvFromAX25(L4->L4USER, Normcall);
strlop(Normcall, ' ');
DEST = L4->L4TARGET.DEST;
if (DEST == NULL)
return;
ConvFromAX25(DEST->DEST_CALL, Normcall2);
strlop(Normcall2, ' ');
memcpy(Alias, DEST->DEST_ALIAS, 6);
strlop(Alias, ' ');
sprintf(Buffer, "Circuit(%s:%s %s)", Alias, Normcall2, Normcall);
return;
}
}
VOID CheckForDetach(struct TNCINFO * TNC, int Stream, struct STREAMINFO * STREAM,
VOID TidyCloseProc(), VOID ForcedCloseProc(), VOID CloseComplete())
{
void ** buffptr;
if (TNC->PortRecord->ATTACHEDSESSIONS[Stream] == 0)
{
// Node has disconnected - clear any connection
if (STREAM->Disconnecting)
{
// Already detected the detach, and have started to close
STREAM->DisconnectingTimeout--;
if (STREAM->DisconnectingTimeout)
return; // Give it a bit longer
// Close has timed out - force a disc, and clear
ForcedCloseProc(TNC, Stream); // Send Tidy Disconnect
goto NotConnected;
}
// New Disconnect
Debugprintf("New Disconnect Port %d Q %x", TNC->Port, STREAM->BPQtoPACTOR_Q);
if (STREAM->Connected || STREAM->Connecting)
{
char logmsg[120];
time_t Duration;
// Need to do a tidy close
STREAM->Connecting = FALSE;
STREAM->Disconnecting = TRUE;
STREAM->DisconnectingTimeout = 300; // 30 Secs
if (Stream == 0)
SetWindowText(TNC->xIDC_TNCSTATE, "Disconnecting");
// Create a traffic record
if (STREAM->Connected && STREAM->ConnectTime)
{
Duration = time(NULL) - STREAM->ConnectTime;
if (Duration == 0)
Duration = 1; // Or will get divide by zero error
sprintf(logmsg,"Port %2d %9s Bytes Sent %d BPS %d Bytes Received %d BPS %d Time %d Seconds",
TNC->Port, STREAM->RemoteCall,
STREAM->BytesTXed, (int)(STREAM->BytesTXed/Duration),
STREAM->BytesRXed, (int)(STREAM->BytesRXed/Duration), (int)Duration);
Debugprintf(logmsg);
STREAM->ConnectTime = 0;
}
if (STREAM->BPQtoPACTOR_Q) // Still data to send?
return; // Will close when all acked
// if (STREAM->FramesOutstanding && TNC->Hardware == H_UZ7HO)
// return; // Will close when all acked
TidyCloseProc(TNC, Stream); // Send Tidy Disconnect
return;
}
// Not connected
NotConnected:
STREAM->Disconnecting = FALSE;
STREAM->Attached = FALSE;
STREAM->Connecting = FALSE;
STREAM->Connected = FALSE;
if (Stream == 0)
SetWindowText(TNC->xIDC_TNCSTATE, "Free");
STREAM->FramesQueued = 0;
STREAM->FramesOutstanding = 0;
CloseComplete(TNC, Stream);
if (TNC->DefaultRXFreq && TNC->RXRadio)
{
char Msg[128];
sprintf(Msg, "R%d %f", TNC->RXRadio, TNC->DefaultRXFreq);
Rig_Command( (TRANSPORTENTRY *) -1, Msg);
}
if (TNC->DefaultTXFreq && TNC->TXRadio && TNC->TXRadio != TNC->RXRadio)
{
char Msg[128];
sprintf(Msg, "R%d %f", TNC->TXRadio, TNC->DefaultTXFreq);
Rig_Command( (TRANSPORTENTRY *) -1, Msg);
}
while(STREAM->BPQtoPACTOR_Q)
{
buffptr=Q_REM(&STREAM->BPQtoPACTOR_Q);
ReleaseBuffer(buffptr);
}
while(STREAM->PACTORtoBPQ_Q)
{
buffptr=Q_REM(&STREAM->PACTORtoBPQ_Q);
ReleaseBuffer(buffptr);
}
}
}
char * CheckAppl(struct TNCINFO * TNC, char * Appl)
{
APPLCALLS * APPL;
BPQVECSTRUC * PORTVEC;
int Allocated = 0, Available = 0;
int App, Stream;
struct TNCINFO * APPLTNC;
// Debugprintf("Checking if %s is running", Appl);
for (App = 0; App < 32; App++)
{
APPL=&APPLCALLTABLE[App];
if (_memicmp(APPL->APPLCMD, Appl, 12) == 0)
{
int _APPLMASK = 1 << App;
// If App has an alias, assume it is running , unless a CMS alias - then check CMS
if (APPL->APPLHASALIAS)
{
if (_memicmp(APPL->APPLCMD, "RELAY ", 6) == 0)
return APPL->APPLCALL_TEXT; // Assume people using RELAY know what they are doing
if (APPL->APPLPORT && (_memicmp(APPL->APPLCMD, "RMS ", 4) == 0))
{
APPLTNC = TNCInfo[APPL->APPLPORT];
{
if (APPLTNC)
{
if (APPLTNC->TCPInfo && !APPLTNC->TCPInfo->CMSOK && !APPLTNC->TCPInfo->FallbacktoRelay)
return NULL;
}
}
}
return APPL->APPLCALL_TEXT;
}
// See if App is running
PORTVEC = &BPQHOSTVECTOR[0];
for (Stream = 0; Stream < 64; Stream++)
{
if (PORTVEC->HOSTAPPLMASK & _APPLMASK)
{
Allocated++;
if (PORTVEC->HOSTSESSION == 0 && (PORTVEC->HOSTFLAGS & 3) == 0)
{
// Free and no outstanding report
return APPL->APPLCALL_TEXT; // Running
}
}
PORTVEC++;
}
}
}
return NULL; // Not Running
}
VOID SetApplPorts()
{
// If any appl has an alias, get port number
struct APPLCONFIG * App;
APPLCALLS * APPL;
char C[80];
char Port[80];
char Call[80];
int i, n;
App = &xxcfg.C_APPL[0];
for (i=0; i < NumberofAppls; i++)
{
APPL=&APPLCALLTABLE[i];
if (APPL->APPLHASALIAS)
{
n = sscanf(App->CommandAlias, "%s %s %s", &C[0], &Port[0], &Call[0]);
if (n == 3)
APPL->APPLPORT = atoi(Port);
}
App++;
}
}
char Modenames[19][10] = {"WINMOR", "SCS", "KAM", "AEA", "HAL", "TELNET", "TRK",
"V4", "UZ7HO", "MPSK", "FLDIGI", "UIARQ", "ARDOP", "VARA",
"SERIAL", "KISSHF", "WINRPR", "HSMODEM", "FREEDATA"};
BOOL ProcessIncommingConnect(struct TNCINFO * TNC, char * Call, int Stream, BOOL SENDCTEXT)
{
return ProcessIncommingConnectEx(TNC, Call, Stream, SENDCTEXT, FALSE);
}
BOOL ProcessIncommingConnectEx(struct TNCINFO * TNC, char * Call, int Stream, BOOL SENDCTEXT, BOOL AllowTR)
{
TRANSPORTENTRY * Session;
int Index = 0;
PMSGWITHLEN buffptr;
int Totallen = 0;
UCHAR * ptr;
struct PORTCONTROL * PORT = (struct PORTCONTROL *)TNC->PortRecord;
// Stop Scanner
if (Stream == 0 || TNC->Hardware == H_UZ7HO)
{
char Msg[80];
sprintf(Msg, "%d SCANSTOP", TNC->Port);
Rig_Command( (TRANSPORTENTRY *) -1, Msg);
UpdateMH(TNC, Call, '+', 'I');
}
Session = L4TABLE;
// Find a free Circuit Entry
while (Index < MAXCIRCUITS)
{
if (Session->L4USER[0] == 0)
break;
Session++;
Index++;
}
if (Index == MAXCIRCUITS)
return FALSE; // Tables Full
memset(Session, 0, sizeof(TRANSPORTENTRY));
memcpy(TNC->Streams[Stream].RemoteCall, Call, 9); // Save Text Callsign
if (AllowTR)
ConvToAX25Ex(Call, Session->L4USER); // Allow -T and -R SSID's for MPS
else
ConvToAX25(Call, Session->L4USER);
ConvToAX25(MYNODECALL, Session->L4MYCALL);
Session->CIRCUITINDEX = Index;
Session->CIRCUITID = NEXTID;
NEXTID++;
if (NEXTID == 0) NEXTID++; // Keep non-zero
TNC->PortRecord->ATTACHEDSESSIONS[Stream] = Session;
TNC->Streams[Stream].Attached = TRUE;
Session->L4TARGET.EXTPORT = TNC->PortRecord;
Session->L4CIRCUITTYPE = UPLINK+PACTOR;
Session->L4WINDOW = L4DEFAULTWINDOW;
Session->L4STATE = 5;
Session->SESSIONT1 = L4T1;
Session->SESSPACLEN = TNC->PortRecord->PORTCONTROL.PORTPACLEN;
Session->KAMSESSION = Stream;
TNC->Streams[Stream].Connected = TRUE; // Subsequent data to data channel
if (LogAllConnects)
{
if (TNC->TargetCall[0])
WriteConnectLog(Call, TNC->TargetCall, Modenames[TNC->Hardware - 1]);
else
WriteConnectLog(Call, MYNODECALL, Modenames[TNC->Hardware - 1]);
}
if (SENDCTEXT == 0)
return TRUE;
// if Port CTEXT defined, use it
if (PORT->CTEXT)
{
Totallen = strlen(PORT->CTEXT);
ptr = PORT->CTEXT;
}
else if (HFCTEXTLEN > 0)
{
Totallen = HFCTEXTLEN;
ptr = HFCTEXT;