/
aprs.c
196 lines (165 loc) · 6.56 KB
/
aprs.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
/*
* Linux DMR Master server
Copyright (C) 2014 Wim Hofman
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "master_server.h"
sqlite3 *openDatabase();
void closeDatabase();
char aprsUrl[100];
char aprsPort[7];
int openAprsSock(){
int rv;
int sockfd;
struct addrinfo hints, *servinfo, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
char ipstr[INET_ADDRSTRLEN];
syslog(LOG_NOTICE,"Opening APRS socket");
if ((rv = getaddrinfo(aprsUrl, aprsPort, &hints, &servinfo)) != 0) {
syslog(LOG_NOTICE,"getaddrinfo: %s\n", gai_strerror(rv));
return;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
struct in_addr *addr;
struct sockaddr_in *ipv = (struct sockaddr_in *) p->ai_addr;
addr = &(ipv->sin_addr);
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
syslog(LOG_NOTICE,"address: %s",ipstr);
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
syslog(LOG_NOTICE,"Not able to create APRS sock");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
syslog(LOG_NOTICE,"Error to connect to APRS");
continue;
}
break;
}
if (p == NULL) syslog(LOG_NOTICE,"Failed to open APRS socket");
freeaddrinfo(servinfo); // all done with this structure
}
void sendAprsBeacon(char callsign[10],char pass[6],char loc[20],char phg[7],char text[100]){
char toSend[300];
char ipstr[INET_ADDRSTRLEN];
int sockfd;
sprintf(toSend,"user %s pass %s vers DMRgate 1.0\n%s>APRS,%s,qAR,%s:!%s&%s%s",callsign,pass,callsign,callsign,callsign,loc,phg,text);
openAprsSock();
send(sockfd,toSend,strlen(toSend),0);
close(sockfd);
}
void sendAprs(struct gpsCoordinates gpsData, int radioId, struct repeater repeater){
char toSend[300];
char timeString[7];
char SQLQUERY[200];
int sockfd;
struct idInfo radioIdent = {0};
sqlite3 *dbase;
sqlite3_stmt *stmt;
unsigned char aprsCor[30];
dbase = openDatabase();
sprintf(SQLQUERY,"SELECT callsign,aprsSuffix,aprsBeacon,aprsSymbol,lastAprsTime FROM callsigns WHERE radioId = %i",radioId);
if (sqlite3_prepare_v2(dbase,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
sprintf(radioIdent.callsign,"%s",sqlite3_column_text(stmt,0));
sprintf(radioIdent.aprsSuffix,"%s",sqlite3_column_text(stmt,1));
sprintf(radioIdent.aprsBeacon,"%s",sqlite3_column_text(stmt,2));
radioIdent.aprsSymbol = sqlite3_column_int(stmt,3);
radioIdent.aprsTimeStamp = sqlite3_column_int(stmt,4);
sqlite3_finalize(stmt);
}
else{
sqlite3_finalize(stmt);
syslog(LOG_NOTICE,"[%s]DMR ID %i not found in database, not sending to aprs.fi",repeater.callsign,radioId);
closeDatabase(dbase);
return;
}
}
else{
syslog(LOG_NOTICE,"[%s]Bad query %s",repeater.callsign,SQLQUERY);
closeDatabase(dbase);
return;
}
if (time(NULL) - radioIdent.aprsTimeStamp < 5){
syslog(LOG_NOTICE,"[%s]Preventing aprs.fi flooding for %s",repeater.callsign,radioIdent.callsign);
closeDatabase(dbase);
return;
}
sprintf(SQLQUERY,"UPDATE callsigns SET hasSendAprs = 1, lastAprsTime = %lu where radioId = %i",time(NULL),radioId);
sqlite3_exec(dbase,SQLQUERY,0,0,0);
closeDatabase(dbase);
sprintf(aprsCor,"%s/%s>%s/%s",gpsData.latitude,gpsData.longitude,gpsData.heading,gpsData.speed);
aprsCor[18] = radioIdent.aprsSymbol;
sprintf(toSend,"user %s pass %s vers DMRgate 1.0\n%s%s>APRS,%s,qAR,%s:!%s %s\n",repeater.callsign,repeater.aprsPass,radioIdent.callsign,radioIdent.aprsSuffix,repeater.callsign,repeater.callsign,aprsCor,radioIdent.aprsBeacon);
//sockfd = openAprsSock();
//send(sockfd,toSend,strlen(toSend),0);
//close(sockfd);
syslog(LOG_NOTICE,"[%s]Would send info to APRS network for %s [%s]",repeater.callsign,radioIdent.callsign,toSend);
}
int checkCoordinates(struct gpsCoordinates gpsData, struct repeater repeater){
regex_t regex;
int reti;
reti = regcomp(®ex, "^[0-9][0-9][0-9][0-9][.][0-9][0-9][NZ]$", 0);
if(reti){
syslog(LOG_NOTICE,"[%s]Hyt GPS decode,could not compile regex latitude",repeater.callsign);
return 0;
}
reti = regexec(®ex,gpsData.latitude,0,NULL,0);
if(reti == REG_NOMATCH){
syslog(LOG_NOTICE,"[%s]Corrupt latitude received",repeater.callsign);
regfree(®ex);
return 0;
}
regfree(®ex);
reti = regcomp(®ex, "^[0-9][0-9][0-9][0-9][0-9][.][0-9][0-9][EW]$", 0);
if(reti){
syslog(LOG_NOTICE,"[%s]Hyt GPS decode,could not compile regex longitude",repeater.callsign);
regfree(®ex);
return 0;
}
reti = regexec(®ex,gpsData.longitude,0,NULL,0);
if(reti == REG_NOMATCH){
syslog(LOG_NOTICE,"[%s]Corrupt longitude received",repeater.callsign);
regfree(®ex);
return 0;
}
reti = regcomp(®ex, "^[0-9][0-9][0-9]$", 0);
if(reti){
syslog(LOG_NOTICE,"[%s]Hyt GPS decode,could not compile regex heading",repeater.callsign);
regfree(®ex);
return 0;
}
reti = regexec(®ex,gpsData.heading,0,NULL,0);
if(reti == REG_NOMATCH){
syslog(LOG_NOTICE,"[%s]Corrupt heading received",repeater.callsign);
regfree(®ex);
return 0;
}
reti = regcomp(®ex, "^[0-9.][0-9.][0-9.]$", 0);
if(reti){
syslog(LOG_NOTICE,"[%s]Hyt GPS decode,could not compile regex speed",repeater.callsign);
regfree(®ex);
return 0;
}
reti = regexec(®ex,gpsData.speed,0,NULL,0);
if(reti == REG_NOMATCH){
syslog(LOG_NOTICE,"[%s]Corrupt speed received",repeater.callsign);
regfree(®ex);
return 0;
}
regfree(®ex);
return 1;
}