-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGWAPI.c
1637 lines (1120 loc) · 34.6 KB
/
AGWAPI.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
*/
/*
AGWPE emulation Interface for BPQ32
Based on AGWtoBPQ
*/
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "CHeaders.h"
#include "bpq32.h"
// Internal AGW Interface
//#define VVICON 400
struct AGWHeader
{
int Port;
unsigned char DataKind;
unsigned char filler2;
unsigned char PID;
unsigned char filler3;
unsigned char callfrom[10];
unsigned char callto[10];
int DataLength;
int reserved;
};
struct AGWSocketConnectionInfo
{
int Number; // Number of record - for AGWConnections display
SOCKET socket;
SOCKADDR_IN sin;
BOOL SocketActive;
BOOL RawFlag;
BOOL MonFlag;
unsigned char CallSign1[10];
unsigned char CallSign2[10];
BOOL GotHeader;
int MsgDataLength;
struct AGWHeader AGWRXHeader;
};
struct BPQConnectionInfo
{
struct AGWSocketConnectionInfo * SocketIndex;
int BPQStream;
unsigned char CallKey[21]; // Port + two calls
BOOL Connecting; // Set while waiting for connection to complete
BOOL Listening;
int ApplMask;
} ConInfoRec;
char AGWPorts[1000];
byte AGWMessage[1000];
struct AGWHeader AGWTXHeader;
char SessionList[100];
struct BPQConnectionInfo AGWConnections[65];
#define MaxSockets 64
static struct AGWSocketConnectionInfo Sockets[MaxSockets+1];
int CurrentConnections;
static int CurrentSockets=0;
int AGWPort = 0;
int AGWSessions = 0;
int AGWMask = 0;
BOOL LoopMonFlag = FALSE;
BOOL Loopflag = FALSE;
extern char pgm[256];
SOCKET agwsock;
extern BPQVECSTRUC * AGWMONVECPTR;
extern int SemHeldByAPI;
char szBuff[ 80 ];
BOOL Initialise();
int SetUpHostSessions();
int DisplaySessions();
int AGWDoStateChange(int Stream);
int AGWDoReceivedData(int Stream);
int AGWDoMonitorData();
int AGWConnected(struct BPQConnectionInfo * Con, int Stream);
int AGWDisconnected(struct BPQConnectionInfo * Con, int Stream);
int DeleteConnection(struct BPQConnectionInfo * Con);
int SendConMsgtoAppl(BOOL Incomming, struct BPQConnectionInfo * Con, char * CallSign);
int SendDisMsgtoAppl(char * Msg, struct AGWSocketConnectionInfo * sockptr);
int AGWSocket_Accept(int SocketId);
int Socket_Data(int SocketId,int error, int eventcode);
int AGWDataSocket_Read(struct AGWSocketConnectionInfo * sockptr, SOCKET sock);
int DataSocket_Write(struct AGWSocketConnectionInfo * sockptr, SOCKET sock);
int AGWGetSessionKey(char * key, struct AGWSocketConnectionInfo * sockptr);
int ProcessAGWCommand(struct AGWSocketConnectionInfo * sockptr);
int SendDataToAppl(int Stream, byte * Buffer, int Length);
int InternalAGWDecodeFrame(char * msg, char * buffer, int Stamp, int * FrameType);
int AGWDataSocket_Disconnect( struct AGWSocketConnectionInfo * sockptr);
int SendRawPacket(struct AGWSocketConnectionInfo * sockptr, char *txmsg, int Length);
int ShowApps();
int Terminate();
int SendtoSocket(SOCKET sock,char * Msg);
VOID Poll_AGW()
{
int state, change, i;
int Stream;
struct BPQConnectionInfo * Con;
struct AGWSocketConnectionInfo * sockptr;
// Look for incoming connects
fd_set readfd, writefd, exceptfd;
struct timeval timeout;
int retval;
int n;
int Active = 0;
SOCKET maxsock;
timeout.tv_sec = 0;
timeout.tv_usec = 0; // poll
FD_ZERO(&readfd);
FD_SET(agwsock, &readfd);
retval = select(agwsock + 1, &readfd, NULL, NULL, &timeout);
if (retval == -1)
{
retval = WSAGetLastError();
perror("listen select");
}
if (retval)
if (FD_ISSET(agwsock, &readfd))
AGWSocket_Accept(agwsock);
// look for data on any active sockets
maxsock = 0;
FD_ZERO(&readfd);
FD_ZERO(&writefd);
FD_ZERO(&exceptfd);
// Check for data on active streams
for (i = 0; i < CurrentConnections; i++)
{
Con = &AGWConnections[i];
Stream = Con->BPQStream;
SessionState(Stream, &state, &change);
if (change == 1)
{
if (state == 1)
// Connected
AGWConnected(Con, Stream);
else
AGWDisconnected(Con, Stream);
}
if (Con->SocketIndex) // Active Session
{
AGWDoReceivedData(Stream);
// Get current Session State. Any state changed is ACK'ed
// automatically. See BPQHOST functions 4 and 5.
SessionState(Stream, &state, &change);
if (change == 1)
{
if (state == 1)
// Connected
AGWConnected(Con, Stream);
else
AGWDisconnected(Con, Stream);
}
}
}
for (n = 1; n <= MaxSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive)
{
SOCKET sock = sockptr->socket;
FD_SET(sock, &readfd);
FD_SET(sock, &exceptfd);
Active++;
if (sock > maxsock)
maxsock = sock;
}
}
if (Active)
{
retval = select(maxsock + 1, &readfd, &writefd, &exceptfd, &timeout);
if (retval == -1)
{
perror("data select yy");
Debugprintf("Select Error %d", WSAGetLastError());
}
else
{
if (retval)
{
// see who has data
for (n = 1; n <= MaxSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive)
{
SOCKET sock = sockptr->socket;
if (FD_ISSET(sock, &exceptfd))
AGWDataSocket_Disconnect(sockptr);
if (FD_ISSET(sock, &readfd))
AGWDataSocket_Read(sockptr, sock);
}
}
}
}
}
AGWDoMonitorData();
}
SOCKADDR_IN local_sin; /* Local socket - internet style */
PSOCKADDR_IN psin;
BOOL AGWAPIInit()
{
struct PORTCONTROL * PORT = PORTTABLE;
int i = 1;
char * ptr;
BOOL opt=TRUE;
if (AGWPort == 0)
return FALSE;
// Create listening socket
agwsock = socket( AF_INET, SOCK_STREAM, 0);
if (agwsock == INVALID_SOCKET)
{
sprintf(szBuff, "socket() failed error %d", WSAGetLastError());
WritetoConsole(szBuff);
return FALSE;
}
setsockopt (agwsock, SOL_SOCKET, SO_REUSEADDR, (const char FAR *)&opt,4);
psin=&local_sin;
psin->sin_family = AF_INET;
psin->sin_addr.s_addr = INADDR_ANY;
psin->sin_port = htons(AGWPort); /* Convert to network ordering */
if (bind(agwsock, (struct sockaddr FAR *) &local_sin, sizeof(local_sin)) == SOCKET_ERROR)
{
sprintf(szBuff, "bind(sock) failed Error %d", WSAGetLastError());
WritetoConsole(szBuff);
closesocket(agwsock);
return FALSE;
}
if (listen(agwsock, 5) < 0)
{
sprintf(szBuff, "listen(sock) failed Error %d", WSAGetLastError());
WritetoConsole(szBuff);
return FALSE;
}
SetUpHostSessions();
// Set up port List
ptr = &AGWPorts[0];
ptr += sprintf(ptr, "%d", NUMBEROFPORTS);
*(ptr)=';';
*(++ptr)=0;
while (PORT)
{
memcpy(ptr,"Port",4);
ptr += sprintf(ptr, "%d", i);
memcpy(ptr, " with ", 6);
ptr+=6;
memcpy(ptr, PORT->PORTDESCRIPTION, 29); // ";"
ptr+=29;
while (*(--ptr) == ' ') {}
ptr++;
*(ptr++)=';';
i++;
PORT=PORT->PORTPOINTER;
}
*(ptr)=0;
AGWMONVECPTR->HOSTAPPLFLAGS = 0x80; // Requext Monitoring
return TRUE;
}
int SetUpHostSessions()
{
int Stream, i;
if (AGWMask == 0) return 0;
for (i = 1; i <= AGWSessions; i++)
{
strcpy(pgm, "AGW");
Stream = FindFreeStream();
if (Stream == 255) break;
SetAppl(Stream, 2, AGWMask);
strcpy(pgm, "bpq32.exe");
AGWConnections[CurrentConnections].CallKey[0] = 0;
AGWConnections[CurrentConnections].BPQStream = Stream;
AGWConnections[CurrentConnections].SocketIndex = 0;
AGWConnections[CurrentConnections].Connecting = FALSE;
AGWConnections[CurrentConnections].Listening = TRUE;
AGWConnections[CurrentConnections].ApplMask = AGWMask;
CurrentConnections++;
}
return 0;
}
extern struct DATAMESSAGE * REPLYBUFFER;
extern BOOL AGWActive;
VOID SHOWAGW(TRANSPORTENTRY * Session, char * Bufferptr, char * CmdTail, CMDX * CMD)
{
// DISPLAY AGW Session Status
int i, con;
struct BPQConnectionInfo * Con;
byte key[21];
struct AGWSocketConnectionInfo * sockptr;
char IPAddr[20];
if (AGWActive == FALSE)
{
Bufferptr += sprintf(Bufferptr, "\rAGW Interface is not enabled\r");
SendCommandReply(Session, REPLYBUFFER, Bufferptr - (char *)REPLYBUFFER);
return;
}
Bufferptr += sprintf(Bufferptr, "\rSockets\r");
for (i = 1; i <= CurrentSockets; i++)
{
sockptr=&Sockets[i];
if (sockptr->SocketActive)
{
unsigned char work[4];
memcpy(work, &sockptr->sin.sin_addr.s_addr, 4);
sprintf(IPAddr, "%d.%d.%d.%d", work[0], work[1], work[2], work[3]);
Bufferptr = CHECKBUFFER(Session, Bufferptr);
Bufferptr += sprintf(Bufferptr, "%2d %-16s %5d %-10s %-10s\r", i, IPAddr, htons(sockptr->sin.sin_port), &sockptr->CallSign1, &sockptr->CallSign2);
}
else
{
Bufferptr = CHECKBUFFER(Session, Bufferptr);
Bufferptr += sprintf(Bufferptr, "%2d Idle\r", 1);
}
}
Bufferptr += sprintf(Bufferptr, "\rPort Calls Stream Socket Connecting Listening Mask\r");
for (con = 0; con < CurrentConnections; con++)
{
Con = &AGWConnections[con];
memcpy(key,Con->CallKey,21);
if (key[0] == 0) key[0] = 32;
key[10]=0;
key[20]=0;
Bufferptr = CHECKBUFFER(Session, Bufferptr);
Bufferptr += sprintf(Bufferptr, "%2c ", key[0]);
Bufferptr += sprintf(Bufferptr, "%-10s%-10s ",&key[1],&key[11]);
Bufferptr += sprintf(Bufferptr, "%2d %2d %s %s %X\r",
Con->BPQStream,
(Con->SocketIndex == 0) ? 0 : AGWConnections[con].SocketIndex->Number,
(Con->Connecting == 0) ? "FALSE" : "TRUE ",
(Con->Listening == 0) ? "FALSE" : "TRUE ",
Con->ApplMask);
}
SendCommandReply(Session, REPLYBUFFER, Bufferptr - (char *)REPLYBUFFER);
}
int DisplaySessions()
{
/* char * ptr;
int i, con;
byte key[21]; // As String, char As String
strcpy (SessionList," Port Calls Stream Socket Connecting Listening Mask");
SendDlgItemMessage(MainWnd,IDC_TEXTBOX3,LB_RESETCONTENT,0,0);
SendDlgItemMessage(MainWnd,IDC_TEXTBOX3,LB_ADDSTRING,0,(LPARAM) SessionList);
for (con = 0; con < CurrentConnections; con++)
{
memcpy(key,AGWConnections[con].CallKey,21);
if (key[0] == 0) key[0] = 32;
key[10]=0;
key[20]=0;
ptr=&SessionList[0];
i=sprintf(ptr,"%2d %2c ",con,key[0]);
ptr+=i;
i=sprintf(ptr,"%-10s%-10s ",&key[1],&key[11]);
ptr+=i;
i=sprintf(ptr,"%2d %2d %s %s %x",
AGWConnections[con].BPQStream,
(AGWConnections[con].SocketIndex == 0) ? 0 : AGWConnections[con].SocketIndex->Number,
(AGWConnections[con].Connecting == 0) ? "FALSE" : "TRUE ",
(AGWConnections[con].Listening == 0) ? "FALSE" : "TRUE ",
AGWConnections[con].ApplMask);
SendDlgItemMessage(MainWnd,IDC_TEXTBOX3,LB_ADDSTRING,0,(LPARAM) SessionList);
}
*/
return (0);
}
int AGWConnected(struct BPQConnectionInfo * Con, int Stream)
{
byte ConnectingCall[10];
byte * ApplCallPtr;
byte * keyptr;
byte ApplCall[10]="";
int i;
int ApplNum;
struct AGWSocketConnectionInfo * sockptr;
GetCallsign(Stream, ConnectingCall);
for (i=9;i>0;i--)
if (ConnectingCall[i]==32)
ConnectingCall[i]=0;
ApplNum = GetApplNum(Stream);
if (ApplNum == 0)
{
return 0; // Cant be an incomming connect
}
ApplCallPtr = GetApplCall(ApplNum);
if (ApplCallPtr != 0) memcpy(ApplCall,ApplCallPtr,10);
// Convert trailing spaces to nulls
for (i=9;i>0;i--)
if (ApplCall[i]==32)
ApplCall[i]=0;
// See if incomming connection
if (Con->Listening)
{
// Allocate Session and send "c" Message
//
// Find an AGW session
for (sockptr=&Sockets[1]; sockptr <= &Sockets[CurrentSockets]; sockptr++)
{
if (sockptr->SocketActive &&
(memcmp(sockptr->CallSign1, ApplCall, 10) == 0) || (memcmp(sockptr->CallSign2, ApplCall, 10) == 0))
{
// Create Key
keyptr=(byte *)&Con->CallKey;
*(keyptr++)='1';
memcpy(keyptr, ApplCall, 10);
keyptr+=10;
memcpy(keyptr,ConnectingCall, 10);
// Make sure key is not already in use
for (i = 0; i < CurrentConnections; i++)
{
if (Con->BPQStream == Stream)
continue; // Dont Check ourself!
if (AGWConnections[i].SocketIndex == sockptr &&
memcmp(&Con->CallKey, &AGWConnections[i].CallKey,21) == 0)
{
SendMsg(Stream, "AGWtoBPQ - Callsign is already connected\r", 43);
Sleep (500);
Disconnect(Stream);
Con->CallKey[0]=0;
return 0;
}
}
Con->Listening = FALSE;
Con->SocketIndex = sockptr;
DisplaySessions();
SendConMsgtoAppl(TRUE, Con, ConnectingCall);
return 0;
}
}
SendMsg(Stream, "No AGWPE Host Sessions available\r", 33);
Sleep (500);
Disconnect(Stream); // disconnect
return (0);
}
// Not listening ??
OutputDebugString("Inbound Connection on Outgoing Stream");
SendMsg(Stream, "AGWtoBPQ - Inbound Connection on Outgoing Stream\r", 49);
Sleep (500);
Disconnect(Stream); // disconnect
return (0);
}
int AGWDisconnected(struct BPQConnectionInfo * Con, int Stream)
{
struct AGWSocketConnectionInfo * sockptr;
char key[21];
memcpy(key,Con->CallKey,21);
sockptr = Con->SocketIndex;
if (sockptr != 0)
{
AGWTXHeader.Port = key[0] - 49;
memcpy(&AGWTXHeader.callfrom,&key[11],10);
memcpy(&AGWTXHeader.callto,&key[1],10);
// Send a "d" Message
// DisMsg = "*** DISCONNECTED From Station "
SendDisMsgtoAppl("*** DISCONNECTED From Station ", sockptr);
}
if (Con->ApplMask != 0)
{
Con->Listening = TRUE;
Con->SocketIndex = 0;
memset(&Con->CallKey ,0 ,21);
DisplaySessions();
}
else
{
DeleteConnection(Con);
}
return 0;
}
int AGWDoReceivedData(int Stream)
{
byte Buffer[400];
int len,count;
//Dim n As Integer, i As Integer, j As Integer, portcount As Integer
//Dim start As Integer
do
{
GetMsg(Stream, Buffer,&len, &count);
if (len > 0)
SendDataToAppl(Stream, Buffer,len);
}
while (count > 0);
return 0;
}
int AGWDoMonitorData()
{
byte Buffer[500];
int RawLen, Length;
byte Port;
struct AGWSocketConnectionInfo * sockptr;
byte AGWBuffer[1000];
int n;
int Stamp, Frametype;
BOOL RXFlag, NeedAGW;
// Look for Monitor Data
while (AGWMONVECPTR->HOSTTRACEQ)
{
MESSAGE * monbuff;
GetSemaphore(&Semaphore, 99);
monbuff = Q_REM((UINT *)&AGWMONVECPTR->HOSTTRACEQ);
RawLen = monbuff->LENGTH;
if (RawLen < 7 || RawLen > 350)
{
ReleaseBuffer(monbuff);
FreeSemaphore(&Semaphore);
return 0;
}
Stamp = (UINT)monbuff->Timestamp;
memcpy(Buffer, monbuff, RawLen);
ReleaseBuffer(monbuff);
FreeSemaphore(&Semaphore);
//' 4 byte chain
//' 1 byte port - top bit = transmit
//' 2 byte length (LO-HI)
Port = Buffer[4];
if (Port > 127)
{
RXFlag = FALSE;
Port = Port - 128;
}
else
{
RXFlag = TRUE;
}
NeedAGW = FALSE;
for (n = 1; n<= CurrentSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive && sockptr->MonFlag) NeedAGW = TRUE;
}
if (NeedAGW)
{
if (RXFlag || LoopMonFlag) // only send txed frames if requested
{
Length = InternalAGWDecodeFrame(Buffer, AGWBuffer,Stamp, &Frametype);
//
// Decode frame and send to applications which have requested monitoring
//
if (Length > 0)
{
AGWTXHeader.Port = Port - 1; // AGW Ports start from 0
if (Frametype == 3)
{
AGWTXHeader.DataKind = 'U';
}
else
{
if (Frametype && 1 == 0)
{
AGWTXHeader.DataKind = 'I';
}
else
{
AGWTXHeader.DataKind = 'S';
}
}
/* For i = 8 To 17
cChar = Asc(Mid(AGWBuffer, i - 1))
If cChar = 32 Then Exit For
AGWTXHeader(i) = cChar
Next i
j = i + 3
For i = 18 To 27
cChar = Asc(Mid(AGWBuffer, j))
If cChar = 32 Then Exit For
AGWTXHeader(i) = cChar
j = j + 1
Next i
*/
AGWTXHeader.DataLength = Length;
memset(AGWTXHeader.callfrom, 0,10);
ConvFromAX25(monbuff->ORIGIN, AGWTXHeader.callfrom);
for (n = 1; n<= CurrentSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive && sockptr->MonFlag)
SendRawPacket(sockptr, AGWBuffer, Length);
}
}
}
}
RawLen = RawLen - 6;
if (RXFlag || Loopflag) // Send transmitted frames if requested
{
//
// Send raw data to any sockets that have requested Raw frames
//
Buffer[6]=0;
AGWTXHeader.Port = Port - 1; // AGW Ports start from 0
AGWTXHeader.DataKind = 'K';
AGWTXHeader.DataLength = RawLen;
for (n = 1; n<= CurrentSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive && sockptr->RawFlag)
SendRawPacket(sockptr, &Buffer[6], RawLen);
}
}
}
return 0;
}
int DeleteConnection(struct BPQConnectionInfo * Con)
{
int i;
int con;
//
// remove specified session
//
SetAppl(Con->BPQStream, 0, 0);
Disconnect(Con->BPQStream);
DeallocateStream(Con->BPQStream);
// move all down one
con = (Con - &AGWConnections[0]);
for (i = con; i <= CurrentConnections - 1; i++)
{
memcpy(&AGWConnections[i],&AGWConnections[i + 1],sizeof ConInfoRec);
}
CurrentConnections--;
return 0;
}
int SendConMsgtoAppl(BOOL Incomming, struct BPQConnectionInfo * Con, char * CallSign)
{
char key[21];
char ConMsg[80]="*** CONNECTED ";
struct AGWSocketConnectionInfo * sockptr;
memcpy(key,&Con->CallKey,21);
sockptr = Con->SocketIndex;
AGWTXHeader.Port = key[0] - 49;
memcpy(AGWTXHeader.callfrom, &key[11],10);
memcpy(AGWTXHeader.callto, &key[1],10);
/* '
' Send a "C" Message
'
'01 00 00 00 43 00 00 00 47 4D 38 42 50 51 2D 34 C GM8BPQ-4
'00 EA 47 4D 38 42 50 51 2D 34 00 FF 25 00 00 00 êGM8BPQ-4 ÿ%
'00 00 00 00 2A 2A 2A 20 43 4F 4E 4E 45 43 54 45 *** CONNECTE
'44 20 57 69 74 68 20 53 74 61 74 69 6F 6E 20 47 D With Station G
'4D 38 42 50 51 2D 34 0D 00 M8BPQ-4
*/
AGWTXHeader.DataKind = 'C';
AGWTXHeader.PID = 0;
if (Incomming)
strcat(ConMsg,"To");
else
strcat(ConMsg,"With");
strcat(ConMsg," Station ");
strcat(ConMsg,CallSign);
AGWTXHeader.DataLength = strlen(ConMsg)+1;
SendtoSocket(sockptr->socket, ConMsg);
return 0;
}
int SendDisMsgtoAppl(char * Msg, struct AGWSocketConnectionInfo * sockptr)
{
byte DisMsg[100];
strcpy(DisMsg,Msg);
strcat(DisMsg,(const char *)&AGWTXHeader.callfrom);
AGWTXHeader.Port = sockptr->AGWRXHeader.Port;
AGWTXHeader.DataKind = 'd';
strcat(DisMsg,"\r\0");
AGWTXHeader.DataLength = strlen(DisMsg)+1;
SendtoSocket(sockptr->socket, DisMsg);
return 0;
}
int AGWSocket_Accept(int SocketId)
{
int n,addrlen;
struct AGWSocketConnectionInfo * sockptr;
SOCKET sock;
// Find a free Socket
for (n = 1; n <= MaxSockets; n++)
{
sockptr=&Sockets[n];
if (sockptr->SocketActive == FALSE)
{
addrlen=sizeof(struct sockaddr);
sock = accept(SocketId, (struct sockaddr *)&sockptr->sin, &addrlen);
if (sock == INVALID_SOCKET)
{
sprintf(szBuff, "AGW accept() failed Error %d\r", WSAGetLastError());
WritetoConsole(szBuff);
return FALSE;
}
sockptr->socket = sock;
sockptr->SocketActive = TRUE;
sockptr->GotHeader = FALSE;
sockptr->MsgDataLength = 0;
sockptr->Number = n;
if (CurrentSockets < n) CurrentSockets=n; //Record max used to save searching all entries
ShowApps();
return 0;
}
}
// Should accept, then immediately close
return 0;
}
int SendtoSocket(SOCKET sock,char * Msg)
{
int len;
len=AGWTXHeader.DataLength;
send(sock,(char *)&AGWTXHeader, 36,0);
if (len > 0) send(sock, Msg, len,0);
return 0;
}