-
Notifications
You must be signed in to change notification settings - Fork 26
/
smaster.c
254 lines (238 loc) · 9.47 KB
/
smaster.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
/*
* 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"
#define NUMSLOTS 2 //DMR IS 2 SLOT
#define SLOT1 4369 //HEX 1111
#define SLOT2 8738 //HEX 2222
#define VCALL 4369 //HEX 1111
#define DCALL 26214 //HEX 6666
#define ISSYNC 61166 //HEX EEEE
#define VCALLEND 8738 //HEX 2222
//#define CALL 2
//#define CALLEND 3
#define PTYPE_ACTIVE1 2
#define PTYPE_END1 3
#define PTYPE_ACTIVE2 66
#define PTYPE_END2 67
#define VFRAMESIZE 103 //UDP PAYLOAD SIZE OF sMaster
#define SYNC_OFFSET1 18 //UDP OFFSETS FOR VARIOUS BYTES IN THE DATA STREAM
#define SYNC_OFFSET2 19 //
//#define SYNC_OFFSET3 18 //Look for EEEE
//#define SYNC_OFFSET4 19 //Look for EEEE
#define SLOT_OFFSET1 16 //
#define SLOT_OFFSET2 17
#define PTYPE_OFFSET 8
#define SRC_OFFSET1 68
#define SRC_OFFSET2 69
#define SRC_OFFSET3 70
#define DST_OFFSET1 64
#define DST_OFFSET2 65
#define DST_OFFSET3 66
#define TYP_OFFSET1 62
struct masterData sMaster = {0};
struct allow{
bool repeater;
bool sMaster;
bool isRange;
};
struct allow checkTalkGroup();
void *sMasterThread(){
int sockfd = 0;
char ping[50];
unsigned char buffer[VFRAMESIZE];
unsigned char repInfo[200];
int n,rc,i;
fd_set fdMaster;
struct timeval timeout;
time_t timeNow,needPingTime,reportTime,pongTime;
int packetType = 0;
unsigned char slot = 0;
int sync = 0;
int srcId = 0;
int dstId = 0;
int callType = 0;
unsigned char origC[5][3];
struct sockaddr_in servaddr,cliaddr;
struct allow toSend = {0};
bool block[3];
syslog(LOG_NOTICE,"Starting sMaster thread");
block[1] = false;
block[2] = false;
sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(atoi(master.sMasterPort));
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
servaddr.sin_addr.s_addr=inet_addr(master.sMasterIp);
sMaster.address = servaddr;
sMaster.sockfd = sockfd;
sMaster.online = true;
sprintf(ping,"PING%s%s%s ",master.ownCountryCode,master.ownRegion,version);
time(&needPingTime);
sendto(sockfd,ping,strlen(ping),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
time(&reportTime);
FD_ZERO(&fdMaster);
for(;;){
FD_SET(sockfd, &fdMaster);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if (rc = select(sockfd+1, &fdMaster, NULL, NULL, &timeout) == -1) {
syslog(LOG_NOTICE,"Select error in sMaster thread");
close(sockfd);
//need to reconnect here !!!!
}
if (FD_ISSET(sockfd,&fdMaster)) {
n = recv(sockfd,buffer,VFRAMESIZE,0);
if (n<VFRAMESIZE){
if (strstr(buffer,"RREQ")){
syslog(LOG_NOTICE,"RREQ from sMaster");
char rreqResponse[200];
char myCCInfo[21];
sprintf(myCCInfo,"%s%s",master.ownCountryCode,master.ownRegion);
sprintf(rreqResponse,"TS1R%-20s%-20s%s",myCCInfo,master.ownName,master.announcedCC1);
syslog(LOG_NOTICE,"RREQ response %s",rreqResponse);
sendto(sockfd,rreqResponse,strlen(rreqResponse),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
sprintf(rreqResponse,"TS2R%-20s%-20s%s",myCCInfo,master.ownName,master.announcedCC2);
syslog(LOG_NOTICE,"RREQ response %s",rreqResponse);
sendto(sockfd,rreqResponse,strlen(rreqResponse),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
unsigned char ip[INET_ADDRSTRLEN];
for (i=0;i<highestRepeater;i++){
if (repeaterList[i].address.sin_addr.s_addr !=0){
inet_ntop(AF_INET, &(repeaterList[i].address.sin_addr), ip, INET_ADDRSTRLEN);
sprintf(repInfo,"RPT%02i%-15s%i%i%i%i%-4s%-16s%-9s%-6s0 0 %-10s%-12s%-3s",i,ip,repeaterList[i].id,baseDmrPort+i,
master.ownCCInt,master.ownRegionInt,version,repeaterList[i].callsign,repeaterList[i].txFreq,repeaterList[i].shift,
repeaterList[i].hardware,repeaterList[i].firmware,repeaterList[i].mode);
sendto(sockfd,repInfo,strlen(repInfo),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
syslog(LOG_NOTICE,"RREQ response %s",repInfo);
usleep(60000);
}
}
time(&reportTime);
}
if (strstr(buffer,"PONG")){
time(&pongTime);
}
}
else{
slot = buffer[SLOT_OFFSET1] / 16;
if (dmrState[slot] == IDLE || sMaster.sending[slot]){
packetType = buffer[PTYPE_OFFSET];
sync = buffer[SYNC_OFFSET1] << 8 | buffer[SYNC_OFFSET2];
switch (packetType){
case 0x01:
if (sync == VCALL && dmrState[slot] != VOICE && block[slot] == false){
srcId = buffer[SRC_OFFSET3] << 16 | buffer[SRC_OFFSET2] << 8 | buffer[SRC_OFFSET1];
dstId = buffer[DST_OFFSET3] << 16 | buffer[DST_OFFSET2] << 8 | buffer[DST_OFFSET1];
callType = buffer[TYP_OFFSET1];
toSend = checkTalkGroup(dstId,slot,callType);
if (toSend.repeater == false){
block[slot] = true;
break;
}
sMaster.sending[slot] = true;
dmrState[slot] = VOICE;
syslog(LOG_NOTICE,"[sMaster]Voice call started on slot %i src %i dst %i type %i",slot,srcId,dstId,callType);
if (buffer[90] !=0) {
memcpy(origC[slot],buffer+90,4);
syslog(LOG_NOTICE,"[sMaster]Replacing country code to %s",origC[slot]);
}
else{
memcpy(origC[slot],buffer+64,4);
}
break;
}
if (sync == DCALL && dmrState[slot] != DATA){
srcId = buffer[SRC_OFFSET3] << 16 | buffer[SRC_OFFSET2] << 8 | buffer[SRC_OFFSET1];
dstId = buffer[DST_OFFSET3] << 16 | buffer[DST_OFFSET2] << 8 | buffer[DST_OFFSET1];
callType = buffer[TYP_OFFSET1];
syslog(LOG_NOTICE,"[sMaster]Data on slot %i src %i dst %i type %i",slot,srcId,dstId,callType);
break;
}
break;
case 0x03:
if (sync == VCALLEND){
dmrState[slot] = IDLE;
sMaster.sending[slot] = false;
block[slot] = false;
syslog(LOG_NOTICE,"[sMaster]Voice call ended on slot %i",slot);
}
break;
}
memcpy(buffer+64,origC[slot],4);
if (!block[slot]){
for (i=0;i<highestRepeater;i++){
if (repeaterList[i].address.sin_addr.s_addr !=0 && !repeaterList[i].sending[slot]){
sendto(repeaterList[i].sockfd,buffer,72,0,(struct sockaddr *)&repeaterList[i].address,sizeof(repeaterList[i].address));
}
}
}
}
else{
syslog(LOG_NOTICE,"[sMaster]Incomming traffic on slot %i, but DMR not IDLE",slot);
}
}
}
else{
if (restart){
syslog(LOG_NOTICE,"Exiting sMaster thread (restart)");
close(sockfd);
pthread_exit(NULL);
}
time(&timeNow);
if (sMaster.sending[1] && dmrState[1] != IDLE){
dmrState[1] = IDLE;
sMaster.sending[1] = false;
block[1] = false;
syslog(LOG_NOTICE,"[sMaster]Slot 1 IDLE");
}
if (sMaster.sending[2] && dmrState[2] != IDLE){
dmrState[2] = IDLE;
sMaster.sending[2] = false;
block[2] = false;
syslog(LOG_NOTICE,"[sMaster]Slot 2 IDLE");
}
if (difftime(timeNow,needPingTime) > 5){
time(&needPingTime);
sendto(sockfd,ping,strlen(ping),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
}
if (difftime(timeNow,reportTime) > 320){
char rreqResponse[200];
char myCCInfo[21];
sprintf(myCCInfo,"%s%s",master.ownCountryCode,master.ownRegion);
sprintf(rreqResponse,"TS1R%-20s%-20s%s",myCCInfo,master.ownName,master.announcedCC1);
syslog(LOG_NOTICE,"Report to sMaster %s",rreqResponse);
sendto(sockfd,rreqResponse,strlen(rreqResponse),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
sprintf(rreqResponse,"TS2R%-20s%-20s%s",myCCInfo,master.ownName,master.announcedCC2);
syslog(LOG_NOTICE,"Report to sMaster %s",rreqResponse);
sendto(sockfd,rreqResponse,strlen(rreqResponse),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
unsigned char ip[INET_ADDRSTRLEN];
for (i=0;i<highestRepeater;i++){
if (repeaterList[i].address.sin_addr.s_addr !=0){
inet_ntop(AF_INET, &(repeaterList[i].address.sin_addr), ip, INET_ADDRSTRLEN);
sprintf(repInfo,"RPT%02i%-15s%i%i%i%i%-4s%-16s%-9s%-6s0 0 %-10s%-12s%-3s",i,ip,repeaterList[i].id,baseDmrPort+i,
master.ownCCInt,master.ownRegionInt,version,repeaterList[i].callsign,repeaterList[i].txFreq,repeaterList[i].shift,
repeaterList[i].hardware,repeaterList[i].firmware,repeaterList[i].mode);
sendto(sockfd,repInfo,strlen(repInfo),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
syslog(LOG_NOTICE,"Report to sMaster %s",repInfo);
usleep(60000);
}
}
time(&reportTime);
}
}
}
}