forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APRSCode.c
6300 lines (4614 loc) · 126 KB
/
APRSCode.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
*/
// Module to implement APRS "New Paradigm" Digipeater and APRS-IS Gateway
// First Version, November 2011
#pragma data_seg("_BPQDATA")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T // Until the ASM code switches to 64 bit time
#include <stdio.h>
#include "CHeaders.h"
#include "bpq32.h"
#include <time.h>
#include "kernelresource.h"
#include "tncinfo.h"
#include "BPQAPRS.h"
#ifndef WIN32
#include <unistd.h>
#include <sys/mman.h>
#include <sys/un.h>
int sfd;
struct sockaddr_un my_addr, peer_addr;
socklen_t peer_addr_size;
#endif
#define MAXAGE 3600 * 12 // 12 Hours
#define MAXCALLS 20 // Max Flood, Trace and Digi
#define GATETIMELIMIT 40 * 60 // Don't gate to RF if station not heard for this time (40 mins)
static BOOL APIENTRY GETSENDNETFRAMEADDR();
static VOID DoSecTimer();
static VOID DoMinTimer();
static APRSProcessLine(char * buf);
static BOOL APRSReadConfigFile();
VOID APRSISThread(BOOL Report);
unsigned long _beginthread( void( *start_address )(BOOL Report), unsigned stack_size, void * arglist);
VOID __cdecl Debugprintf(const char * format, ...);
VOID __cdecl Consoleprintf(const char * format, ...);
BOOL APIENTRY Send_AX(PMESSAGE Block, DWORD Len, UCHAR Port);
VOID Send_AX_Datagram(PDIGIMESSAGE Block, DWORD Len, UCHAR Port);
char * strlop(char * buf, char delim);
int APRSDecodeFrame(char * msg, char * buffer, int Stamp, UINT Mask); // Unsemaphored DecodeFrame
APRSSTATIONRECORD * UpdateHeard(UCHAR * Call, int Port);
BOOL CheckforDups(char * Call, char * Msg, int Len);
VOID ProcessQuery(char * Query);
VOID ProcessSpecificQuery(char * Query, int Port, char * Origin, char * DestPlusDigis);
VOID CheckandDigi(DIGIMESSAGE * Msg, int Port, int FirstUnused, int Digis, int Len);
VOID SendBeacon(int toPort, char * Msg, BOOL SendISStatus, BOOL SendSOGCOG);
Dll BOOL APIENTRY PutAPRSMessage(char * Frame, int Len);
VOID ProcessAPRSISMsg(char * APRSMsg);
static VOID SendtoDigiPorts(PDIGIMESSAGE Block, DWORD Len, UCHAR Port);
APRSSTATIONRECORD * LookupStation(char * call);
BOOL OpenGPSPort();
void PollGPSIn();
int CountLocalStations();
BOOL SendAPPLAPRSMessage(char * Frame);
VOID SendAPRSMessage(char * Message, int toPort);
static VOID TCPConnect();
struct STATIONRECORD * DecodeAPRSISMsg(char * msg);
struct STATIONRECORD * ProcessRFFrame(char * buffer, int len);
VOID APRSSecTimer();
double Distance(double laa, double loa);
struct STATIONRECORD * FindStation(char * Call, BOOL AddIfNotFound);
VOID DecodeAPRSPayload(char * Payload, struct STATIONRECORD * Station);
BOOL ProcessConfig();
extern int SemHeldByAPI;
extern int APRSMONDECODE();
extern struct ConsoleInfo MonWindow;
extern char VersionString[];
// All data should be initialised to force into shared segment
static char ConfigClassName[]="CONFIG";
BPQVECSTRUC * APRSMONVECPTR;
extern int MONDECODE();
extern VOID * zalloc(int len);
extern BOOL StartMinimized;
extern char * PortConfig[];
extern char TextVerstring[];
extern HWND hConsWnd;
extern HKEY REGTREE;
static int SecTimer = 10;
static int MinTimer = 60;
BOOL APRSApplConnected = FALSE;
BOOL APRSWeb = FALSE;
UINT APPL_Q = 0; // Queue of frames for APRS Appl
UINT APPLTX_Q = 0; // Queue of frames from APRS Appl
UINT APRSPortMask = 0;
char APRSCall[10] = "";
char APRSDest[10] = "APBPQ1";
UCHAR AXCall[7] = "";
char CallPadded[10] = " ";
int GPSPort = 0;
int GPSSpeed = 0;
char GPSRelay[80] = "";
BOOL GateLocal = FALSE;
double GateLocalDistance = 0.0;
int MaxDigisforIS = 7; // Dont send to IS if more digis uued to reach us
char WXFileName[MAX_PATH];
char WXComment[80];
BOOL SendWX = FALSE;
int WXInterval = 30;
int WXCounter = 29 * 60;
char APRSCall[10];
char LoppedAPRSCall[10];
BOOL WXPort[32]; // Ports to send WX to
BOOL GPSOK = 0;
char LAT[] = "0000.00N"; // in standard APRS Format
char LON[] = "00000.00W"; //in standard APRS Format
char HostName[80]; // for BlueNMEA
BOOL BlueNMEAOK = FALSE;
int BlueNMEATimer = 0;
double SOG, COG; // From GPS
double Lat = 0.0;
double Lon = 0.0;
BOOL PosnSet = FALSE;
/*
The null position should be include the \. symbol (unknown/indeterminate
position). For example, a Position Report for a station with unknown position
will contain the coordinates …0000.00N\00000.00W.…
*/
char * FloodCalls = 0; // Calls to relay using N-n without tracing
char * TraceCalls = 0; // Calls to relay using N-n with tracing
char * DigiCalls = 0; // Calls for normal relaying
UCHAR FloodAX[MAXCALLS][7] = {0};
UCHAR TraceAX[MAXCALLS][7] = {0};
UCHAR DigiAX[MAXCALLS][7] = {0};
int FloodLen[MAXCALLS];
int TraceLen[MAXCALLS];
int DigiLen[MAXCALLS];
int ISPort = 0;
char ISHost[256] = "";
int ISPasscode = 0;
char NodeFilter[1000] = "m/50"; // Filter when the isn't an application
char ISFilter[1000] = "m/50"; // Current Filter
char APPLFilter[1000] = ""; // Filter when an Applcation is running
extern BOOL IGateEnabled;
char StatusMsg[256] = ""; // Must be in shared segment
int StatusMsgLen = 0;
char * BeaconPath[33] = {0};
char CrossPortMap[33][33] = {0};
char APRSBridgeMap[33][33] = {0};
UCHAR BeaconHeader[33][10][7] = {""}; // Dest, Source and up to 8 digis
int BeaconHddrLen[33] = {0}; // Actual Length used
char CFGSYMBOL = 'a';
char CFGSYMSET = 'B';
char SYMBOL = '='; // Unknown Locaton
char SYMSET = '/';
BOOL TraceDigi = FALSE; // Add Trace to packets relayed on Digi Calls
int MaxTraceHops = 2;
int MaxFloodHops = 2;
int BeaconInterval = 0;
int MobileBeaconInterval = 0;
time_t LastMobileBeacon = 0;
int BeaconCounter = 0;
int IStatusCounter = 0; // Used to send ?ISTATUS? Responses
int StatusCounter = 0; // Used to send Status Messages
char RunProgram[128] = ""; // Program to start
BOOL APRSISOpen = FALSE;
int ISDelayTimer = 0; // Time before trying to reopen APRS-IS link
char APRSDESTS[][7] = {"AIR*", "ALL*", "AP*", "BEACON", "CQ*", "GPS*", "DF*", "DGPS*", "DRILL*",
"DX*", "ID*", "JAVA*", "MAIL*", "MICE*", "QST*", "QTH*", "RTCM*", "SKY*",
"SPACE*", "SPC*", "SYM*", "TEL*", "TEST*", "TLM*", "WX*", "ZIP"};
UCHAR AXDESTS[30][7] = {""};
int AXDESTLEN[30] = {0};
UCHAR axTCPIP[7];
UCHAR axRFONLY[7];
UCHAR axNOGATE[7];
int MessageCount = 0;
struct PortInfo
{
int Index;
int ComPort;
char PortType[2];
BOOL NewVCOM; // Using User Mode Virtual COM Driver
int ReopenTimer; // Retry if open failed delay
int RTS;
int CTS;
int DCD;
int DTR;
int DSR;
char Params[20]; // Init Params (eg 9600,n,8)
char PortLabel[20];
HANDLE hDevice;
BOOL Created;
BOOL PortEnabled;
int FLOWCTRL;
int gpsinptr;
#ifdef WIN32
OVERLAPPED Overlapped;
OVERLAPPED OverlappedRead;
#endif
char GPSinMsg[160];
int GPSTypeFlag; // GPS Source flags
BOOL RMCOnly; // Only send RMC msgs to this port
};
struct PortInfo InPorts[1] = {0};
// Heard Station info
#define MAXHEARD 1000
int HEARDENTRIES = 0;
int MAXHEARDENTRIES = 0;
int MHLEN = sizeof(APRSSTATIONRECORD);
// Area is allocated as needed
APRSSTATIONRECORD MHTABLE[MAXHEARD] = {0};
APRSSTATIONRECORD * MHDATA = &MHTABLE[0];
static SOCKET sock = (SOCKET) NULL;
//Duplicate suppression Code
#define MAXDUPS 100 // Number to keep
#define DUPSECONDS 28 // Time to Keep
struct DUPINFO
{
time_t DupTime;
int DupLen;
char DupUser[8]; // Call in ax.35 format
char DupText[100];
};
struct DUPINFO DupInfo[MAXDUPS];
struct OBJECT
{
struct OBJECT * Next;
UCHAR Path[10][7]; // Dest, Source and up to 8 digis
int PathLen; // Actual Length used
char Message[80];
char PortMap[33];
int Interval;
int Timer;
};
struct OBJECT * ObjectList; // List of objects to send;
int ObjectCount = 0;
#include <math.h>
#define M_PI 3.14159265358979323846
int RetryCount = 4;
int RetryTimer = 45;
int ExpireTime = 120;
int TrackExpireTime = 1440;
BOOL SuppressNullPosn = FALSE;
BOOL DefaultNoTracks = FALSE;
BOOL LocalTime = TRUE;
int MaxStations = 500;
RECT Rect, MsgRect, StnRect;
char Key[80];
// function prototypes
VOID RefreshMessages();
// a few global variables
char APRSDir[MAX_PATH] = "BPQAPRS";
char DF[MAX_PATH];
#define FEND 0xC0 // KISS CONTROL CODES
#define FESC 0xDB
#define TFEND 0xDC
#define TFESC 0xDD
int StationCount = 0;
UCHAR NextSeq = 1;
BOOL ImageChanged;
BOOL NeedRefresh = FALSE;
time_t LastRefresh = 0;
// Stationrecords are stored in a shared memory segment. based at APRSStationMemory (normally 0x43000000)
// A pointer to the first is placed at the start of this
struct STATIONRECORD ** StationRecords = NULL;
struct STATIONRECORD * StationRecordPool = NULL;
struct APRSMESSAGE * Messages = NULL;
struct APRSMESSAGE * OutstandingMsgs = NULL;
VOID SendObject(struct OBJECT * Object);
VOID MonitorAPRSIS(char * Msg, int MsgLen, BOOL TX);
#ifndef WIN32
#define WSAEWOULDBLOCK 11
#endif
HANDLE hMapFile;
UCHAR * APRSStationMemory = NULL;
int ISSend(SOCKET sock, char * Msg, int Len, int flags)
{
int Loops = 0;
int Sent;
MonitorAPRSIS(Msg, Len, TRUE);
Sent = send(sock, Msg, Len, flags);
while (Sent != Len && Loops++ < 300) // 10 secs max
{
if ((Sent == SOCKET_ERROR) && (WSAGetLastError() != WSAEWOULDBLOCK))
return SOCKET_ERROR;
if (Sent > 0) // something sent
{
Len -= Sent;
memmove(Msg, &Msg[Sent], Len);
}
Sleep(30);
Sent = send(sock, Msg, Len, flags);
}
return Sent;
}
Dll BOOL APIENTRY Init_APRS()
{
int i;
char * DCall;
#ifndef LINBPQ
HKEY hKey=0;
int retCode, Vallen, Type;
#else
#ifndef WIN32
int fd;
char RX_SOCK_PATH[] = "BPQAPRSrxsock";
char TX_SOCK_PATH[] = "BPQAPRStxsock";
#endif
#endif
struct STATIONRECORD * Stn1, * Stn2;
// CLear tables in case a restart
StationRecords = NULL;
Messages = NULL;
OutstandingMsgs = NULL;
StationCount = 0;
HEARDENTRIES = 0;
MAXHEARDENTRIES = 0;
memset(MHTABLE, 0, sizeof(MHTABLE));
ConvToAX25(MYNODECALL, MYCALL);
ConvToAX25("TCPIP", axTCPIP);
ConvToAX25("RFONLY", axRFONLY);
ConvToAX25("NOGATE", axNOGATE);
memset(&FloodAX[0][0], 0, sizeof(FloodAX));
memset(&TraceAX[0][0], 0, sizeof(TraceAX));
memset(&DigiAX[0][0], 0, sizeof(DigiAX));
APRSPortMask = 0;
memset(BeaconPath, sizeof(BeaconPath), 0);
memset(&CrossPortMap[0][0], 0, sizeof(CrossPortMap));
memset(&APRSBridgeMap[0][0], 0, sizeof(APRSBridgeMap));
for (i = 1; i <= NUMBEROFPORTS; i++)
{
CrossPortMap[i][i] = TRUE; // Set Defaults - Same Port
CrossPortMap[i][0] = TRUE; // and APRS-IS
}
PosnSet = 0;
ObjectList = NULL;
ObjectCount = 0;
ISPort = ISHost[0] = ISPasscode = 0;
if (APRSReadConfigFile() == 0)
return FALSE;
#ifdef LINBPQ
// Create a Shared Memory Object
APRSStationMemory = NULL;
#ifndef WIN32
fd = shm_open("/BPQAPRSSharedMem", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1)
{
perror("Create Shared Memory");
printf("Create APRS Shared Memory Failed\n");
}
else
{
if (ftruncate(fd, sizeof(struct STATIONRECORD) * (MaxStations + 1)) == -1)
{
perror("Extend Shared Memory");
printf("Extend APRS Shared Memory Failed\n");
}
else
{
// Map shared memory object
APRSStationMemory = mmap((void *)0x43000000, sizeof(struct STATIONRECORD) * (MaxStations + 1),
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
if (APRSStationMemory == MAP_FAILED)
{
perror("Map Shared Memory");
printf("Map APRS Shared Memory Failed\n");
APRSStationMemory = NULL;
}
}
}
#endif
if (APRSStationMemory == NULL)
{
printf("APRS not using shared memory\n");
APRSStationMemory = malloc(sizeof(struct STATIONRECORD) * (MaxStations + 1));
}
#else
retCode = RegOpenKeyEx (REGTREE,
"SOFTWARE\\G8BPQ\\BPQ32",
0,
KEY_QUERY_VALUE,
&hKey);
if (retCode == ERROR_SUCCESS)
{
Vallen = 4;
retCode = RegQueryValueEx(hKey, "IGateEnabled", 0, &Type, (UCHAR *)&IGateEnabled, &Vallen);
/*
// Restore GPS Position if GPS is configured and LAN/LON is not
if (GPSPort && PosnSet == 0)
{
char LATLON[20];
Vallen = 29;
retCode = RegQueryValueEx(hKey, "GPS", 0, &Type, LATLON, &Vallen);
if (retCode == 0)
{
memcpy(LAT, LATLON, 8);
memcpy(LON, &LATLON[10], 9);
PosnSet = TRUE;
}
}
*/
}
// Create Memory Mapping for Station List
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
sizeof(struct STATIONRECORD) * (MaxStations + 1), // maximum object size (low-order DWORD)
"BPQAPRSStationsMappingObject"); // name of mapping object
if (hMapFile == NULL)
{
Consoleprintf("Could not create file mapping object (%d).\n", GetLastError());
return 0;
}
UnmapViewOfFile((void *)0x43000000);
APRSStationMemory = (LPTSTR) MapViewOfFileEx(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(struct STATIONRECORD) * (MaxStations + 1),
(void *)0x43000000);
if (APRSStationMemory == NULL)
{
Consoleprintf("Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
return 0;
}
#endif
// First record has pointer to table
memset(APRSStationMemory, 0, sizeof(struct STATIONRECORD) * (MaxStations + 1));
Stn1 = (struct STATIONRECORD *)APRSStationMemory;
StationRecords = (struct STATIONRECORD **)Stn1;
Stn1++;
StationRecordPool = Stn1;
for (i = 1; i < MaxStations; i++) // Already have first
{
Stn2 = Stn1;
Stn2++;
Stn1->Next = Stn2;
Stn1 = Stn2;
}
if (PosnSet == 0)
{
SYMBOL = '.';
SYMSET = '\\'; // Undefined Posn Symbol
}
else
{
// Convert posn to floating degrees
char LatDeg[3], LonDeg[4];
memcpy(LatDeg, LAT, 2);
LatDeg[2]=0;
Lat=atof(LatDeg) + (atof(LAT+2)/60);
if (LAT[7] == 'S') Lat=-Lat;
memcpy(LonDeg, LON, 3);
LonDeg[3]=0;
Lon=atof(LonDeg) + (atof(LON+3)/60);
if (LON[8]== 'W') Lon=-Lon;
SYMBOL = CFGSYMBOL;
SYMSET = CFGSYMSET;
}
// First record has control info for APRS Mapping App
Stn1 = (struct STATIONRECORD *)APRSStationMemory;
memcpy(Stn1->Callsign, APRSCall, 10);
Stn1->Lat = Lat;
Stn1->Lon = Lon;
Stn1->LastPort = MaxStations;
#ifndef WIN32
// Open unix socket for messaging app
sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror("Socket");
}
else
{
u_long param=1;
ioctl(sfd, FIONBIO, ¶m); // Set non-blocking
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, TX_SOCK_PATH, sizeof(my_addr.sun_path) - 1);
memset(&peer_addr, 0, sizeof(struct sockaddr_un));
peer_addr.sun_family = AF_UNIX;
strncpy(peer_addr.sun_path, RX_SOCK_PATH, sizeof(peer_addr.sun_path) - 1);
unlink(TX_SOCK_PATH);
if (bind(sfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_un)) == -1)
perror("bind");
}
#endif
// Convert Dest ADDRS to AX.25
for (i = 0; i < 26; i++)
{
DCall = &APRSDESTS[i][0];
if (strchr(DCall, '*'))
AXDESTLEN[i] = strlen(DCall) - 1;
else
AXDESTLEN[i] = 6;
ConvToAX25(DCall, &AXDESTS[i][0]);
}
// Process any Object Definitions
// Setup Heard Data Area
HEARDENTRIES = 0;
MAXHEARDENTRIES = MAXHEARD;
APRSMONVECPTR->HOSTAPPLFLAGS = 0x80; // Request Monitoring
if (ISPort && IGateEnabled)
{
_beginthread(APRSISThread, 0, (VOID *) TRUE);
}
if (GPSPort)
OpenGPSPort();
WritetoConsole("APRS Digi/Gateway Enabled\n");
APRSWeb = TRUE;
// If a Run parameter was supplied, run the program
if (RunProgram[0] == 0)
return TRUE;
#ifndef WIN32
{
char * arg_list[] = {NULL, NULL};
pid_t child_pid;
signal(SIGCHLD, SIG_IGN); // Silently (and portably) reap children.
// Fork and Exec ARDOP
printf("Trying to start %s\n", RunProgram);
arg_list[0] = RunProgram;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid == -1)
{
printf ("APRS fork() Failed\n");
return 0;
}
if (child_pid == 0)
{
execvp (arg_list[0], arg_list);
/* The execvp function returns only if an error occurs. */
printf ("Failed to run %s\n", RunProgram);
exit(0); // Kill the new process
}
}
#else
{
int n = 0;
STARTUPINFO SInfo; // pointer to STARTUPINFO
PROCESS_INFORMATION PInfo; // pointer to PROCESS_INFORMATION
SInfo.cb=sizeof(SInfo);
SInfo.lpReserved=NULL;
SInfo.lpDesktop=NULL;
SInfo.lpTitle=NULL;
SInfo.dwFlags=0;
SInfo.cbReserved2=0;
SInfo.lpReserved2=NULL;
while (KillOldTNC(RunProgram) && n++ < 100)
{
Sleep(100);
}
if (!CreateProcess(RunProgram, NULL, NULL, NULL, FALSE,0 ,NULL ,NULL, &SInfo, &PInfo))
Debugprintf("Failed to Start %s Error %d ", RunProgram, GetLastError());
}
#endif
return TRUE;
}
#define SD_RECEIVE 0x00
#define SD_SEND 0x01
#define SD_BOTH 0x02
BOOL APRSActive;
VOID APRSClose()
{
APRSActive = FALSE;
if (sock)
{
shutdown(sock, SD_BOTH);
Sleep(50);
closesocket(sock);
}
#ifdef WIN32
if (InPorts[0].hDevice)
CloseHandle(InPorts[0].hDevice);
#endif
}
Dll VOID APIENTRY Poll_APRS()
{
char Msg[256];
int numBytes;
SecTimer--;
if (SecTimer == 0)
{
SecTimer = 10;
DoSecTimer();
MinTimer--;
if (MinTimer == 0)
{
MinTimer = 10;
DoMinTimer();
}
}
#ifdef LINBPQ
#ifndef WIN32
// Look for messages from App
numBytes = recvfrom(sfd, Msg, 256, 0, NULL, NULL);
if (numBytes > 0)
{
char To[10];
struct STATIONRECORD * Station;
memcpy(To, &Msg[1], 9);
Station = FindStation(To, TRUE);
if (Station)
{
Msg[numBytes] = 0;
SendAPPLAPRSMessage(Msg);
}
else
printf("Cant Send APRS Message - Station Table is full\n");
}
#endif
#endif
if (GPSPort)
PollGPSIn();
if (APPLTX_Q)
{
UINT * buffptr = Q_REM(&APPLTX_Q);
if (buffptr[2] == -1)
SendAPPLAPRSMessage((char *)&buffptr[3]);
else
SendAPRSMessage((char *)&buffptr[3], buffptr[2]);
ReleaseBuffer(buffptr);
}
while (APRSMONVECPTR->HOSTTRACEQ)
{
int stamp, len;
BOOL MonitorNODES = FALSE;
UINT * monbuff;
UCHAR * monchars;
MESSAGE * Orig;
int Digis = 0;
MESSAGE * AdjBuff; // Adjusted for digis
BOOL FirstUnused = FALSE;
int DigisUsed = 0; // Digis used to reach us
DIGIMESSAGE Msg = {0};
int Port, i;
char * DEST;
unsigned char buffer[1024];
char ISMsg[500];
char * ptr1;
char * Payload;
char * ptr3;
char * ptr4;
BOOL ThirdParty = FALSE;
BOOL NoGate = FALSE;
APRSSTATIONRECORD * MH;
char MsgCopy[500];
int toPort;
struct STATIONRECORD * Station;
#ifdef WIN32
struct _EXCEPTION_POINTERS exinfo;
char EXCEPTMSG[80] = "";
#endif
monbuff = Q_REM(&APRSMONVECPTR->HOSTTRACEQ);
monchars = (UCHAR *)monbuff;
AdjBuff = Orig = (MESSAGE *)monchars; // Adjusted for digis
Port = Orig->PORT;
if (Port & 0x80) // TX
{
ReleaseBuffer(monbuff);
continue;
}
// if (CompareCalls(Orig->ORIGIN, AXCall)) // Our Packet
// {
// ReleaseBuffer(monbuff);
// continue;
// }
if ((APRSPortMask & (1 << (Port - 1))) == 0)// Port in use for APRS?
{
ReleaseBuffer(monbuff);
continue;
}
stamp = monbuff[88];
if ((UCHAR)monchars[4] & 0x80) // TX
{
ReleaseBuffer(monbuff);
continue;
}
// See if digipeaters present.
while ((AdjBuff->ORIGIN[6] & 1) == 0 && Digis < 9)
{
UCHAR * temp = (UCHAR *)AdjBuff;
temp += 7;
AdjBuff = (MESSAGE *)temp;
// If we have already digi'ed it, ignore (Dup Check my fail on slow links)
if (AdjBuff->ORIGIN[6] & 0x80)
{
// Used Digi
if (memcmp(AdjBuff->ORIGIN, AXCall, 6) == 0)
{
ReleaseBuffer(monbuff);
return;
}
DigisUsed++;
}
if (memcmp(AdjBuff->ORIGIN, axTCPIP, 6) == 0)
ThirdParty = TRUE;
Digis ++;
if (FirstUnused == FALSE && (AdjBuff->ORIGIN[6] & 0x80) == 0)
{
// Unused Digi - see if we should digi it
FirstUnused = Digis;
// CheckDigi(buff, AdjBuff->ORIGIN);
}
}
if (Digis > 8)
{
ReleaseBuffer(monbuff);
continue; // Corrupt
}
if (Digis)
{
if (memcmp(AdjBuff->ORIGIN, axNOGATE, 6) == 0
|| memcmp(AdjBuff->ORIGIN, axRFONLY, 6) == 0
|| DigisUsed > MaxDigisforIS)
// TOo many digis or Last digis is NOGATE or RFONLY - dont send to IS
NoGate = TRUE;
}
if (AdjBuff->CTL != 3 || AdjBuff->PID != 0xf0) // Only UI
{
ReleaseBuffer(monbuff);
continue;
}
// Bridge if requested
for (toPort = 1; toPort <= NUMBEROFPORTS; toPort++)
{
if (APRSBridgeMap[Port][toPort])
{
MESSAGE * Buffer = GetBuff();
struct PORTCONTROL * PORT;
if (Buffer)
{
memcpy(Buffer, Orig, Orig->LENGTH);
Buffer->PORT = toPort;
PORT = GetPortTableEntryFromPortNum(toPort);
if (PORT)
PUT_ON_PORT_Q(PORT, Buffer);
else
ReleaseBuffer(Buffer);
}
}
}