-
Notifications
You must be signed in to change notification settings - Fork 0
/
WINMOR.c
3051 lines (2256 loc) · 68.4 KB
/
WINMOR.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
*/
//
// DLL to provide interface to allow G8BPQ switch to use WINMOR as a Port Driver
//
// Uses BPQ EXTERNAL interface
//
// Version 1.0 January 2009 - Initial Version
//
// March 22 2010
// Send FAULTS to Monitor Window
// Force PROTOCOL = WINMOR/PACTOR (to simplifiy Config)
// July 2010
// Support up to 32 BPQ Ports
// Support up to 32 Applications
// Version 1.2.1.2 August 2010
// Save Minimized State
// Handle new "BLOCKED by Busy channel" message from TNC
// Version 1.2.1.4 August 2010
// Add Scan control of BW setting
// Reset TNC if stuck in Disconnecting
// Add option to send reports to WL2K
// Disconnect if appl not available
// Version 1.2.1.5 August 2010
// Updates to WL2K Reporting
// Send Watchdog polls every minute and restart if no response.
// Don't connect if channel is busy (unless specifically overridden)
// Version 1.2.1.6 September 2010
// Add option to kill and restart TNC after each transfer
// Fix PTT operation after Node reconfig
// Version 1.2.2.1 September 2010
// Add option to get config from bpq32.cfg
// Merge with BPQ32.dll
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <stdio.h>
#include <time.h>
#include "CHeaders.h"
#ifdef WIN32
#include <Psapi.h>
#endif
int (WINAPI FAR *GetModuleFileNameExPtr)();
int (WINAPI FAR *EnumProcessesPtr)();
#define SD_RECEIVE 0x00
#define SD_SEND 0x01
#define SD_BOTH 0x02
#include "bpq32.h"
#include "tncinfo.h"
#define WSA_ACCEPT WM_USER + 1
#define WSA_DATA WM_USER + 2
#define WSA_CONNECT WM_USER + 3
static int Socket_Data(int sock, int error, int eventcode);
int KillTNC(struct TNCINFO * TNC);
int RestartTNC(struct TNCINFO * TNC);
int KillPopups(struct TNCINFO * TNC);
VOID MoveWindows(struct TNCINFO * TNC);
int SendReporttoWL2K(struct TNCINFO * TNC);
char * CheckAppl(struct TNCINFO * TNC, char * Appl);
int DoScanLine(struct TNCINFO * TNC, char * Buff, int Len);
BOOL KillOldTNC(char * Path);
static char ClassName[]="WINMORSTATUS";
static char WindowTitle[] = "WINMOR";
static int RigControlRow = 165;
#define WINMOR
#define NARROWMODE 21
#define WIDEMODE 22
#ifndef LINBPQ
#include <commctrl.h>
#endif
extern char * PortConfig[33];
extern int SemHeldByAPI;
static RECT Rect;
struct TNCINFO * TNCInfo[34]; // Records are Malloc'd
static int ProcessLine(char * buf, int Port);
unsigned long _beginthread( void( *start_address )(), unsigned stack_size, int arglist);
// RIGCONTROL COM60 19200 ICOM IC706 5e 4 14.103/U1w 14.112/u1 18.1/U1n 10.12/l1
// There seem to be timing issues when calling SendMessage from multiple threads.
// Queue and process in main thread
UINT * WINMORTraceQ;
UINT * SetWindowTextQ;
VOID WritetoTraceSupport(struct TNCINFO * TNC, char * Msg, int Len)
{
int index = 0;
UCHAR * ptr1 = Msg, * ptr2;
UCHAR Line[1000];
int LineLen, i;
Msg[Len] = 0;
#ifndef LINBPQ
index=SendMessage(TNC->hMonitor, LB_SETCURSEL, -1, 0);
#endif
lineloop:
if (Len > 0)
{
// copy text to control a line at a time
ptr2 = memchr(ptr1, 13, Len);
if (ptr2)
{
ptr2++;
LineLen = ptr2 - ptr1;
Len -= LineLen;
memcpy(Line, ptr1, LineLen);
memcpy(&Line[LineLen - 1], "<cr>", 4);
LineLen += 3;
if ((*ptr2) == 10)
{
memcpy(&Line[LineLen], "<lf>", 4);
LineLen += 4;
ptr2++;
Len --;
}
Line[LineLen] = 0;
// If line contains any data above 7f, assume binary and dont display
for (i = 0; i < LineLen; i++)
{
if (Line[i] > 126 || Line[i] < 32)
goto Skip;
}
#ifdef LINBPQ
#else
index=SendMessage(TNC->hMonitor, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR) Line);
#endif
// Write to Web Buffer
strcat(TNC->WebBuffer, Line);
strcat(TNC->WebBuffer, "\r\n");
if (strlen(TNC->WebBuffer) > 4500)
memmove(TNC->WebBuffer, &TNC->WebBuffer[500], 4490); // Make sure null is moved
Skip:
ptr1 = ptr2;
goto lineloop;
}
// Process incomplete line
for (i = 0; i < Len; i++)
{
if (ptr1[i] > 126 || ptr1[i] < 32)
break;
}
if (i == Len)
{
#ifdef LINBPQ
#else
index=SendMessage(TNC->hMonitor, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR) ptr1 );
#endif
strcat(TNC->WebBuffer, ptr1);
strcat(TNC->WebBuffer, "\r\n");
if (strlen(TNC->WebBuffer) > 4500)
memmove(TNC->WebBuffer, &TNC->WebBuffer[500], 4490); // Make sure null is moved
}
}
#ifdef LINBPQ
#else
if (index > 1200)
do
index=index=SendMessage(TNC->hMonitor, LB_DELETESTRING, 0, 0);
while (index > 1000);
if (index > -1)
index=SendMessage(TNC->hMonitor, LB_SETCARETINDEX,(WPARAM) index, MAKELPARAM(FALSE, 0));
#endif
}
VOID MySetWindowText(HWND hWnd, char * Msg)
{
#ifdef WIN32
UINT * buffptr;
BOOL Sem = FALSE;
if (Semaphore.Flag == 0)
{
GetSemaphore(&Semaphore, 88);
Sem = TRUE;
}
buffptr = GetBuff();
if (buffptr)
{
buffptr[1] = (UINT)hWnd;
memcpy(&buffptr[2], Msg, strlen(Msg) + 1);
C_Q_ADD(&SetWindowTextQ, buffptr);
}
if (Sem)
FreeSemaphore(&Semaphore);
#endif
}
VOID SetWindowTextSupport(UINT * Buffer)
{
HWND hWnd = (HWND)Buffer[1];
char * Msg = (char *)&Buffer[2];
SetWindowText(hWnd, Msg);
ReleaseBuffer(Buffer);
}
VOID WritetoTrace(struct TNCINFO * TNC, char * Msg, int Len)
{
// It seems writing from multiple threads can cause problems in Windows
// Queue and process in main thread
#ifndef WIN32
WritetoTraceSupport(TNC, Msg, Len);
}
#else
UINT * buffptr;
BOOL Sem = FALSE;
if (Semaphore.Flag == 0)
{
GetSemaphore(&Semaphore, 88);
Sem = TRUE;
}
buffptr = GetBuff();
if (buffptr)
{
if (Len > 340)
Len = 340;
buffptr[1] = (UINT)TNC;
buffptr[2] = (UINT)Len;
memcpy(&buffptr[3], Msg, Len + 1);
C_Q_ADD(&WINMORTraceQ, buffptr);
}
if (Sem)
FreeSemaphore(&Semaphore);
}
#endif
static int ProcessLine(char * buf, int Port)
{
UCHAR * ptr,* p_cmd;
char * p_ipad = 0;
char * p_port = 0;
unsigned short WINMORport = 0;
int BPQport;
int len=510;
struct TNCINFO * TNC;
char errbuf[256];
strcpy(errbuf, buf);
ptr = strtok(buf, " \t\n\r");
if(ptr == NULL) return (TRUE);
if(*ptr =='#') return (TRUE); // comment
if(*ptr ==';') return (TRUE); // comment
if (_stricmp(buf, "ADDR"))
return FALSE; // Must start with ADDR
ptr = strtok(NULL, " \t\n\r");
BPQport = Port;
p_ipad = ptr;
TNC = TNCInfo[BPQport] = malloc(sizeof(struct TNCINFO));
memset(TNC, 0, sizeof(struct TNCINFO));
TNC->InitScript = malloc(1000);
TNC->InitScript[0] = 0;
if (p_ipad == NULL)
p_ipad = strtok(NULL, " \t\n\r");
if (p_ipad == NULL) return (FALSE);
p_port = strtok(NULL, " \t\n\r");
if (p_port == NULL) return (FALSE);
WINMORport = atoi(p_port);
TNC->destaddr.sin_family = AF_INET;
TNC->destaddr.sin_port = htons(WINMORport);
TNC->Datadestaddr.sin_family = AF_INET;
TNC->Datadestaddr.sin_port = htons(WINMORport+1);
TNC->HostName = malloc(strlen(p_ipad)+1);
if (TNC->HostName == NULL) return TRUE;
strcpy(TNC->HostName,p_ipad);
ptr = strtok(NULL, " \t\n\r");
if (ptr)
{
if (_stricmp(ptr, "PTT") == 0)
{
ptr = strtok(NULL, " \t\n\r");
if (ptr)
{
if (_stricmp(ptr, "CI-V") == 0)
TNC->PTTMode = PTTCI_V;
else if (_stricmp(ptr, "CAT") == 0)
TNC->PTTMode = PTTCI_V;
else if (_stricmp(ptr, "RTS") == 0)
TNC->PTTMode = PTTRTS;
else if (_stricmp(ptr, "DTR") == 0)
TNC->PTTMode = PTTDTR;
else if (_stricmp(ptr, "DTRRTS") == 0)
TNC->PTTMode = PTTDTR | PTTRTS;
else if (_stricmp(ptr, "CM108") == 0)
TNC->PTTMode = PTTCM108;
ptr = strtok(NULL, " \t\n\r");
}
}
}
if (ptr)
{
if (_memicmp(ptr, "PATH", 4) == 0)
{
p_cmd = strtok(NULL, "\n\r");
if (p_cmd) TNC->ProgramPath = _strdup(p_cmd);
}
}
// Read Initialisation lines
while(TRUE)
{
if (GetLine(buf) == 0)
return TRUE;
strcpy(errbuf, buf);
if (memcmp(buf, "****", 4) == 0)
return TRUE;
ptr = strchr(buf, ';');
if (ptr)
{
*ptr++ = 13;
*ptr = 0;
}
if ((_memicmp(buf, "CAPTURE", 7) == 0) || (_memicmp(buf, "PLAYBACK", 8) == 0))
{} // Ignore
else
/*
if (_memicmp(buf, "PATH", 4) == 0)
{
char * Context;
p_cmd = strtok_s(&buf[5], "\n\r", &Context);
if (p_cmd) TNC->ProgramPath = _strdup(p_cmd);
}
else
*/
if (_memicmp(buf, "WL2KREPORT", 10) == 0)
TNC->WL2K = DecodeWL2KReportLine(buf);
else
if (_memicmp(buf, "BUSYHOLD", 8) == 0) // Hold Time for Busy Detect
TNC->BusyHold = atoi(&buf[8]);
else
if (_memicmp(buf, "BUSYWAIT", 8) == 0) // Wait time beofre failing connect if busy
TNC->BusyWait = atoi(&buf[8]);
else
if (_memicmp(buf, "STARTINROBUST", 13) == 0)
TNC->StartInRobust = TRUE;
else
if (_memicmp(buf, "ROBUST", 6) == 0)
{
if (_memicmp(&buf[7], "TRUE", 4) == 0)
TNC->Robust = TRUE;
strcat (TNC->InitScript, buf);
}
else
strcat (TNC->InitScript, buf);
}
return (TRUE);
}
void WINMORThread(int port);
VOID ProcessDataSocketData(int port);
int ConnecttoWINMOR();
static int ProcessReceivedData(struct TNCINFO * TNC);
int V4ProcessReceivedData(struct TNCINFO * TNC);
VOID ReleaseTNC(struct TNCINFO * TNC);
VOID SuspendOtherPorts(struct TNCINFO * ThisTNC);
VOID ReleaseOtherPorts(struct TNCINFO * ThisTNC);
VOID WritetoTrace(struct TNCINFO * TNC, char * Msg, int Len);
#define MAXBPQPORTS 32
static time_t ltime;
static SOCKADDR_IN sinx;
static SOCKADDR_IN rxaddr;
static int addrlen=sizeof(sinx);
VOID ChangeMYC(struct TNCINFO * TNC, char * Call)
{
UCHAR TXMsg[100];
int datalen;
if (strcmp(Call, TNC->CurrentMYC) == 0)
return; // No Change
strcpy(TNC->CurrentMYC, Call);
// send(TNC->TCPSock, "CODEC FALSE\r\n", 13, 0);
datalen = sprintf(TXMsg, "MYC %s\r\n", Call);
send(TNC->TCPSock,TXMsg, datalen, 0);
// send(TNC->TCPSock, "CODEC TRUE\r\n", 12, 0);
// TNC->StartSent = TRUE;
send(TNC->TCPSock, "MYC\r\n", 5, 0);
}
static int ExtProc(int fn, int port,unsigned char * buff)
{
int i,winerr;
int datalen;
UINT * buffptr;
char txbuff[500];
unsigned int bytes,txlen=0;
char ErrMsg[255];
int Param;
HKEY hKey=0;
struct TNCINFO * TNC = TNCInfo[port];
struct STREAMINFO * STREAM = &TNC->Streams[0];
struct ScanEntry * Scan;
fd_set readfs;
fd_set writefs;
fd_set errorfs;
struct timeval timeout;
if (TNC == NULL)
return 0; // Port not defined
switch (fn)
{
case 1: // poll
while (TNC->PortRecord->UI_Q) // Release anything accidentally put on UI_Q
{
buffptr = Q_REM(&TNC->PortRecord->UI_Q);
ReleaseBuffer(buffptr);
}
if (TNC->Busy) // Count down to clear
{
if ((TNC->BusyFlags & CDBusy) == 0) // TNC Has reported not busy
{
TNC->Busy--;
if (TNC->Busy == 0)
SetWindowText(TNC->xIDC_CHANSTATE, "Clear");
strcpy(TNC->WEB_CHANSTATE, "Clear");
}
}
if (TNC->BusyDelay)
{
// Still Busy?
if (InterlockedCheckBusy(TNC) == FALSE)
{
// No, so send
send(TNC->TCPSock, TNC->ConnectCmd, strlen(TNC->ConnectCmd), 0);
TNC->Streams[0].Connecting = TRUE;
memset(TNC->Streams[0].RemoteCall, 0, 10);
memcpy(TNC->Streams[0].RemoteCall, &TNC->ConnectCmd[8], strlen(TNC->ConnectCmd)-10);
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s", TNC->Streams[0].MyCall, TNC->Streams[0].RemoteCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
free(TNC->ConnectCmd);
TNC->BusyDelay = 0;
}
else
{
// Wait Longer
TNC->BusyDelay--;
if (TNC->BusyDelay == 0)
{
// Timed out - Send Error Response
UINT * buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
buffptr[1]=39;
memcpy(buffptr+2,"Sorry, Can't Connect - Channel is busy\r", 39);
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
free(TNC->ConnectCmd);
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
}
}
}
if (TNC->HeartBeat++ > 600 || (TNC->Streams[0].Connected && TNC->HeartBeat > 50)) // Every Minute unless connected
{
if (TNC->HeartBeat > 600 && TNC->hWnd)
{
char wtext[100];
sprintf (wtext, "WINMOR Sound Card TNC - BPQ %s", TNC->PortRecord->PORTCONTROL.PORTDESCRIPTION);
MySetWindowText(TNC->hWnd, wtext);
}
TNC->HeartBeat = 0;
if (TNC->CONNECTED)
{
// Probe link
if (TNC->Streams[0].Connecting || TNC->Streams[0].Connected)
send(TNC->TCPSock, "MODE\r\n", 6, 0);
else
{
if (time(NULL) - TNC->WinmorRestartCodecTimer > 900) // 15 mins
{
send(TNC->TCPSock, "CODEC FALSE\r\n", 13, 0);
send(TNC->TCPSock, "CODEC TRUE\r\n", 12, 0);
}
else
send(TNC->TCPSock, "STATE\r\n", 7, 0);
}
}
}
if (TNC->FECMode)
{
if (TNC->FECIDTimer++ > 6000) // ID every 10 Mins
{
if (!TNC->Busy)
{
TNC->FECIDTimer = 0;
send(TNC->TCPSock, "SENDID 0\r\n", 10, 0);
}
}
if (TNC->FECPending) // Check if FEC Send needed
{
if (!TNC->Busy)
{
TNC->FECPending = 0;
if (TNC->FEC1600)
send(TNC->TCPSock,"FECSEND 1600\r\n", 14, 0);
else
send(TNC->TCPSock,"FECSEND 500\r\n", 13, 0);
}
}
}
if (STREAM->NeedDisc)
{
STREAM->NeedDisc--;
if (STREAM->NeedDisc == 0)
{
// Send the DISCONNECT
send(TNC->TCPSock, "DISCONNECT\r\n", 12, 0);
}
}
if (TNC->DiscPending)
{
TNC->DiscPending--;
if (TNC->DiscPending == 0)
{
// Too long in Disc Pending - Kill and Restart TNC
if (TNC->PID)
{
KillTNC(TNC);
RestartTNC(TNC);
}
}
}
if (TNC->TimeSinceLast++ > 800) // Allow 10 secs for Keepalive
{
// Restart TNC
if (TNC->ProgramPath && TNC->CONNECTED)
{
if (strstr(TNC->ProgramPath, "WINMOR TNC"))
{
struct tm * tm;
char Time[80];
TNC->Restarts++;
TNC->LastRestart = time(NULL);
tm = gmtime(&TNC->LastRestart);
sprintf_s(Time, sizeof(Time),"%04d/%02d/%02d %02d:%02dZ",
tm->tm_year +1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
SetWindowText(TNC->xIDC_RESTARTTIME, Time);
strcpy(TNC->WEB_RESTARTTIME, Time);
sprintf_s(Time, sizeof(Time),"%d", TNC->Restarts);
SetWindowText(TNC->xIDC_RESTARTS, Time);
strcpy(TNC->WEB_RESTARTS, Time);
KillTNC(TNC);
RestartTNC(TNC);
TNC->TimeSinceLast = 0;
}
}
}
if (TNC->PortRecord->ATTACHEDSESSIONS[0] && TNC->Streams[0].Attached == 0)
{
// New Attach
int calllen;
char Msg[80];
TNC->Streams[0].Attached = TRUE;
calllen = ConvFromAX25(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4USER, TNC->Streams[0].MyCall);
TNC->Streams[0].MyCall[calllen] = 0;
// Stop Listening, and set MYCALL to user's call
send(TNC->TCPSock, "LISTEN FALSE\r\n", 14, 0);
ChangeMYC(TNC, TNC->Streams[0].MyCall);
// Stop other ports in same group
SuspendOtherPorts(TNC);
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
// Stop Scanning
sprintf(Msg, "%d SCANSTOP", TNC->Port);
Rig_Command(-1, Msg);
}
if (TNC->Streams[0].Attached)
CheckForDetach(TNC, 0, &TNC->Streams[0], TidyClose, ForcedClose, CloseComplete);
if (TNC->Streams[0].ReportDISC)
{
TNC->Streams[0].ReportDISC = FALSE;
buff[4] = 0;
return -1;
}
if (TNC->CONNECTED == FALSE && TNC->CONNECTING == FALSE)
{
// See if time to reconnect
time(<ime);
if (ltime - TNC->lasttime > 9 )
{
ConnecttoWINMOR(port);
TNC->lasttime = ltime;
}
}
FD_ZERO(&readfs);
if (TNC->CONNECTED) FD_SET(TNC->TCPDataSock,&readfs);
FD_ZERO(&writefs);
if (TNC->BPQtoWINMOR_Q) FD_SET(TNC->TCPDataSock,&writefs); // Need notification of busy clearing
FD_ZERO(&errorfs);
if (TNC->CONNECTING || TNC->CONNECTED) FD_SET(TNC->TCPDataSock,&errorfs);
timeout.tv_sec = 0;
timeout.tv_usec = 0; // poll
if (select(TNC->TCPDataSock + 1, &readfs, &writefs, &errorfs, &timeout) > 0)
{
// See what happened
if (FD_ISSET(TNC->TCPDataSock, &readfs))
ProcessDataSocketData(port);
if (FD_ISSET(TNC->TCPDataSock, &writefs))
{
// Write block has cleared. Send rest of packet
buffptr=Q_REM(&TNC->BPQtoWINMOR_Q);
txlen=buffptr[1];
memcpy(txbuff,buffptr+2,txlen);
bytes=send(TNC->TCPSock,(const char FAR *)&txbuff,txlen,0);
ReleaseBuffer(buffptr);
}
if (FD_ISSET(TNC->TCPDataSock, &errorfs))
{
i=sprintf(ErrMsg, "WINMOR Data Connection lost for BPQ Port %d\r\n", port);
WritetoConsole(ErrMsg);
TNC->CONNECTING = FALSE;
TNC->CONNECTED = FALSE;
TNC->Streams[0].ReportDISC = TRUE;
}
}
// See if any frames for this port
if (TNC->WINMORtoBPQ_Q != 0)
{
buffptr=Q_REM(&TNC->WINMORtoBPQ_Q);
datalen=buffptr[1];
buff[4] = 0; // Compatibility with Kam Driver
buff[7] = 0xf0;
memcpy(&buff[8],buffptr+2,datalen); // Data goes to +7, but we have an extra byte
datalen+=8;
buff[5]=(datalen & 0xff);
buff[6]=(datalen >> 8);
ReleaseBuffer(buffptr);
return (1);
}
return (0);
case 2: // send
if (!TNC->CONNECTED)
{
// Send Error Response
UINT * buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
buffptr[1]=36;
memcpy(buffptr+2,"No Connection to WINMOR Virtual TNC\r", 36);
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
return 0; // Don't try if not connected
}
if (TNC->Streams[0].BPQtoPACTOR_Q) //Used for CTEXT
{
UINT * buffptr = Q_REM(&TNC->Streams[0].BPQtoPACTOR_Q);
txlen=buffptr[1];
memcpy(txbuff,buffptr+2,txlen);
bytes = send(TNC->TCPDataSock,(const char FAR *)&txbuff,txlen,0);
STREAM->BytesTXed += bytes;
WritetoTrace(TNC, txbuff, txlen);
ReleaseBuffer(buffptr);
}
if (TNC->SwallowSignon)
{
TNC->SwallowSignon = FALSE; // Discard *** connected
return 0;
}
txlen=(buff[6]<<8) + buff[5]-8;
if (TNC->Streams[0].Connected)
{
STREAM->PacketsSent++;
if (STREAM->PacketsSent == 3)
{
if (TNC->Robust)
send(TNC->TCPSock, "ROBUST TRUE\r\n", 13, 0);
else
send(TNC->TCPSock, "ROBUST FALSE\r\n", 14, 0);
}
bytes=send(TNC->TCPDataSock,(const char FAR *)&buff[8],txlen,0);
STREAM->BytesTXed += bytes;
WritetoTrace(TNC, &buff[8], txlen);
}
else
{
if (_memicmp(&buff[8], "D\r", 2) == 0)
{
TNC->Streams[0].ReportDISC = TRUE; // Tell Node
return 0;
}
if (TNC->FECMode)
{
char Buffer[300];
int len;
// Send FEC Data
buff[8 + txlen] = 0;
len = sprintf(Buffer, "%-9s: %s", TNC->Streams[0].MyCall, &buff[8]);
send(TNC->TCPDataSock, Buffer, len, 0);
if (TNC->BusyFlags)
{
TNC->FECPending = 1;
}
else
{
if (TNC->FEC1600)
send(TNC->TCPSock,"FECSEND 1600\r\n", 14, 0);
else
send(TNC->TCPSock,"FECSEND 500\r\n", 13, 0);
}
return 0;
}
// See if Local command (eg RADIO)
if (_memicmp(&buff[8], "RADIO ", 6) == 0)
{
sprintf(&buff[8], "%d %s", TNC->Port, &buff[14]);
if (Rig_Command(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4CROSSLINK->CIRCUITINDEX, &buff[8]))
{
}
else
{
UINT * buffptr = GetBuff();
if (buffptr == 0) return 1; // No buffers, so ignore
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "%s", &buff[8]);
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
}
return 1;
}
if (_memicmp(&buff[8], "OVERRIDEBUSY", 12) == 0)
{
UINT * buffptr = GetBuff();
TNC->OverrideBusy = TRUE;
if (buffptr)
{
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "Winmor} OK\r");
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
}
return 0;
}
if (_memicmp(&buff[8], "MAXCONREQ", 9) == 0)
{
if (buff[17] != 13)
{
// Limit connects
int tries = atoi(&buff[18]);
if (tries > 10) tries = 10;
sprintf(&buff[8], "MAXCONREQ %d\r\nMAXCONREQ\r\n", tries);
send(TNC->TCPSock,&buff[8],strlen(&buff[8]), 0);
return 0;
}
}
if ((_memicmp(&buff[8], "BW 500", 6) == 0) || (_memicmp(&buff[8], "BW 1600", 7) == 0))
{
// Generate a local response
UINT * buffptr = GetBuff();
if (_memicmp(&buff[8], "BW 500", 6) == 0)
TNC->WL2KMode = 21;
else
TNC->WL2KMode = 22;
if (buffptr)
{
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "Winmor} OK\r");