2016-10-10 16:59:30 -04:00
|
|
|
/*
|
2020-04-03 16:28:45 -04:00
|
|
|
* Copyright (C) 2016-2020 by Jonathan Naylor G4KLX
|
2016-10-10 16:59:30 -04:00
|
|
|
*
|
|
|
|
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "P25Gateway.h"
|
2020-11-04 06:50:57 -05:00
|
|
|
#include "RptNetwork.h"
|
|
|
|
#include "P25Network.h"
|
2016-10-10 16:59:30 -04:00
|
|
|
#include "Reflectors.h"
|
|
|
|
#include "StopWatch.h"
|
|
|
|
#include "DMRLookup.h"
|
|
|
|
#include "Version.h"
|
2016-10-18 02:47:37 -04:00
|
|
|
#include "Thread.h"
|
2019-02-04 09:57:20 -05:00
|
|
|
#include "Voice.h"
|
2017-03-18 03:13:14 -04:00
|
|
|
#include "Timer.h"
|
2020-04-03 16:28:45 -04:00
|
|
|
#include "Utils.h"
|
2016-10-10 16:59:30 -04:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2020-09-20 10:47:58 -04:00
|
|
|
#include <WS2tcpip.h>
|
2016-10-10 16:59:30 -04:00
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2022-01-23 13:07:55 -05:00
|
|
|
#include <netdb.h>
|
2016-10-10 16:59:30 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
const char* DEFAULT_INI_FILE = "P25Gateway.ini";
|
|
|
|
#else
|
|
|
|
const char* DEFAULT_INI_FILE = "/etc/P25Gateway.ini";
|
|
|
|
#endif
|
|
|
|
|
2019-02-04 09:57:20 -05:00
|
|
|
const unsigned P25_VOICE_ID = 10999U;
|
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
class CStaticTG {
|
|
|
|
public:
|
|
|
|
unsigned int m_tg;
|
|
|
|
sockaddr_storage m_addr;
|
|
|
|
unsigned int m_addrLen;
|
|
|
|
};
|
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
const char* iniFile = DEFAULT_INI_FILE;
|
|
|
|
if (argc > 1) {
|
|
|
|
for (int currentArg = 1; currentArg < argc; ++currentArg) {
|
|
|
|
std::string arg = argv[currentArg];
|
|
|
|
if ((arg == "-v") || (arg == "--version")) {
|
|
|
|
::fprintf(stdout, "P25Gateway version %s\n", VERSION);
|
|
|
|
return 0;
|
|
|
|
} else if (arg.substr(0, 1) == "-") {
|
|
|
|
::fprintf(stderr, "Usage: P25Gateway [-v|--version] [filename]\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
iniFile = argv[currentArg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CP25Gateway* gateway = new CP25Gateway(std::string(iniFile));
|
|
|
|
gateway->run();
|
|
|
|
delete gateway;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CP25Gateway::CP25Gateway(const std::string& file) :
|
|
|
|
m_conf(file)
|
|
|
|
{
|
2020-09-20 16:49:25 -04:00
|
|
|
CUDPSocket::startup();
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CP25Gateway::~CP25Gateway()
|
|
|
|
{
|
2020-09-20 16:49:25 -04:00
|
|
|
CUDPSocket::shutdown();
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CP25Gateway::run()
|
|
|
|
{
|
|
|
|
bool ret = m_conf.read();
|
|
|
|
if (!ret) {
|
|
|
|
::fprintf(stderr, "P25Gateway: cannot read the .ini file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
bool m_daemon = m_conf.getDaemon();
|
|
|
|
if (m_daemon) {
|
|
|
|
// Create new process
|
|
|
|
pid_t pid = ::fork();
|
|
|
|
if (pid == -1) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Couldn't fork() , exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
2018-06-18 14:04:37 -04:00
|
|
|
} else if (pid != 0) {
|
2016-10-10 16:59:30 -04:00
|
|
|
exit(EXIT_SUCCESS);
|
2018-06-18 14:04:37 -04:00
|
|
|
}
|
2016-10-10 16:59:30 -04:00
|
|
|
|
|
|
|
// Create new session and process group
|
|
|
|
if (::setsid() == -1) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Couldn't setsid(), exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the working directory to the root directory
|
|
|
|
if (::chdir("/") == -1) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Couldn't cd /, exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 14:04:37 -04:00
|
|
|
// If we are currently root...
|
2016-10-10 16:59:30 -04:00
|
|
|
if (getuid() == 0) {
|
|
|
|
struct passwd* user = ::getpwnam("mmdvm");
|
|
|
|
if (user == NULL) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Could not get the mmdvm user, exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t mmdvm_uid = user->pw_uid;
|
|
|
|
gid_t mmdvm_gid = user->pw_gid;
|
|
|
|
|
2018-06-18 14:04:37 -04:00
|
|
|
// Set user and group ID's to mmdvm:mmdvm
|
2016-10-10 16:59:30 -04:00
|
|
|
if (setgid(mmdvm_gid) != 0) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Could not set mmdvm GID, exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setuid(mmdvm_uid) != 0) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "Could not set mmdvm UID, exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:12:42 -05:00
|
|
|
// Double check it worked (AKA Paranoia)
|
2016-10-10 16:59:30 -04:00
|
|
|
if (setuid(0) != -1) {
|
2018-06-18 14:04:37 -04:00
|
|
|
::fprintf(stderr, "It's possible to regain root - something is wrong!, exiting\n");
|
2016-10-10 16:59:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 08:16:21 -04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2020-11-01 07:54:51 -05:00
|
|
|
ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate());
|
2020-10-05 08:16:21 -04:00
|
|
|
#else
|
2020-11-01 07:54:51 -05:00
|
|
|
ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate());
|
2020-10-05 08:16:21 -04:00
|
|
|
#endif
|
2018-06-18 14:04:37 -04:00
|
|
|
if (!ret) {
|
|
|
|
::fprintf(stderr, "P25Gateway: unable to open the log file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 15:23:42 -04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
if (m_daemon) {
|
|
|
|
::close(STDIN_FILENO);
|
|
|
|
::close(STDOUT_FILENO);
|
|
|
|
::close(STDERR_FILENO);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-03 09:00:48 -04:00
|
|
|
sockaddr_storage rptAddr;
|
|
|
|
unsigned int rptAddrLen;
|
2020-09-06 08:59:52 -04:00
|
|
|
if (CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen) != 0) {
|
|
|
|
LogError("Unable to resolve the address of the host");
|
|
|
|
return;
|
|
|
|
}
|
2016-10-10 16:59:30 -04:00
|
|
|
|
2020-11-04 07:14:31 -05:00
|
|
|
CRptNetwork localNetwork(m_conf.getMyPort(), rptAddr, rptAddrLen, m_conf.getCallsign(), m_conf.getDebug());
|
2016-10-10 16:59:30 -04:00
|
|
|
ret = localNetwork.open();
|
|
|
|
if (!ret) {
|
|
|
|
::LogFinalise();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-04 06:50:57 -05:00
|
|
|
CP25Network remoteNetwork(m_conf.getNetworkPort(), m_conf.getCallsign(), m_conf.getNetworkDebug());
|
2016-10-17 12:33:49 -04:00
|
|
|
ret = remoteNetwork.open();
|
|
|
|
if (!ret) {
|
|
|
|
localNetwork.close();
|
|
|
|
::LogFinalise();
|
|
|
|
return;
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 07:34:08 -04:00
|
|
|
CUDPSocket* remoteSocket = NULL;
|
|
|
|
if (m_conf.getRemoteCommandsEnabled()) {
|
|
|
|
remoteSocket = new CUDPSocket(m_conf.getRemoteCommandsPort());
|
|
|
|
ret = remoteSocket->open();
|
|
|
|
if (!ret) {
|
|
|
|
delete remoteSocket;
|
|
|
|
remoteSocket = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:54:06 -05:00
|
|
|
CReflectors reflectors(m_conf.getNetworkHosts1(), m_conf.getNetworkHosts2(), m_conf.getNetworkReloadTime());
|
2016-10-10 16:59:30 -04:00
|
|
|
if (m_conf.getNetworkParrotPort() > 0U)
|
|
|
|
reflectors.setParrot(m_conf.getNetworkParrotAddress(), m_conf.getNetworkParrotPort());
|
2020-06-18 00:56:34 -04:00
|
|
|
if (m_conf.getNetworkP252DMRPort() > 0U)
|
|
|
|
reflectors.setP252DMR(m_conf.getNetworkP252DMRAddress(), m_conf.getNetworkP252DMRPort());
|
2016-10-10 16:59:30 -04:00
|
|
|
reflectors.load();
|
|
|
|
|
|
|
|
CDMRLookup* lookup = new CDMRLookup(m_conf.getLookupName(), m_conf.getLookupTime());
|
|
|
|
lookup->read();
|
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
unsigned int rfHangTime = m_conf.getNetworkRFHangTime();
|
|
|
|
unsigned int netHangTime = m_conf.getNetworkNetHangTime();
|
|
|
|
|
|
|
|
CTimer hangTimer(1000U);
|
|
|
|
|
2016-10-13 13:39:50 -04:00
|
|
|
CTimer pollTimer(1000U, 5U);
|
2020-10-26 13:12:28 -04:00
|
|
|
pollTimer.start();
|
2016-10-11 12:43:37 -04:00
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
CStopWatch stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
|
2019-02-04 09:57:20 -05:00
|
|
|
CVoice* voice = NULL;
|
|
|
|
if (m_conf.getVoiceEnabled()) {
|
|
|
|
voice = new CVoice(m_conf.getVoiceDirectory(), m_conf.getVoiceLanguage(), P25_VOICE_ID);
|
|
|
|
bool ok = voice->open();
|
|
|
|
if (!ok) {
|
|
|
|
delete voice;
|
|
|
|
voice = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 18:44:45 -04:00
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
LogMessage("Starting P25Gateway-%s", VERSION);
|
|
|
|
|
2016-10-11 02:45:50 -04:00
|
|
|
unsigned int srcId = 0U;
|
2020-10-26 13:12:28 -04:00
|
|
|
unsigned int dstTG = 0U;
|
2016-10-10 16:59:30 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
bool currentIsStatic = false;
|
|
|
|
unsigned int currentTG = 0U;
|
2020-09-03 09:00:48 -04:00
|
|
|
unsigned int currentAddrLen = 0U;
|
|
|
|
sockaddr_storage currentAddr;
|
2016-10-11 02:45:50 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
std::vector<unsigned int> staticIds = m_conf.getNetworkStatic();
|
2016-10-11 12:43:37 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
std::vector<CStaticTG> staticTGs;
|
|
|
|
for (std::vector<unsigned int>::const_iterator it = staticIds.cbegin(); it != staticIds.cend(); ++it) {
|
|
|
|
CP25Reflector* reflector = reflectors.find(*it);
|
|
|
|
if (reflector != NULL) {
|
|
|
|
CStaticTG staticTG;
|
|
|
|
staticTG.m_tg = *it;
|
|
|
|
staticTG.m_addr = reflector->m_addr;
|
|
|
|
staticTG.m_addrLen = reflector->m_addrLen;
|
|
|
|
staticTGs.push_back(staticTG);
|
2016-10-11 12:43:37 -04:00
|
|
|
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.poll(staticTG.m_addr, staticTG.m_addrLen);
|
|
|
|
remoteNetwork.poll(staticTG.m_addr, staticTG.m_addrLen);
|
|
|
|
remoteNetwork.poll(staticTG.m_addr, staticTG.m_addrLen);
|
2016-10-11 13:18:14 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
LogMessage("Statically linked to reflector %u", *it);
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
unsigned char buffer[200U];
|
2020-09-03 09:00:48 -04:00
|
|
|
sockaddr_storage addr;
|
2020-10-26 13:12:28 -04:00
|
|
|
unsigned int addrLen;
|
2016-10-10 16:59:30 -04:00
|
|
|
|
|
|
|
// From the reflector to the MMDVM
|
2020-11-04 06:50:57 -05:00
|
|
|
unsigned int len = remoteNetwork.read(buffer, 200U, addr, addrLen);
|
2016-10-17 12:33:49 -04:00
|
|
|
if (len > 0U) {
|
|
|
|
// If we're linked and it's from the right place, send it on
|
2020-10-27 06:59:29 -04:00
|
|
|
if (currentAddrLen > 0U && CUDPSocket::match(currentAddr, addr)) {
|
2016-10-17 12:33:49 -04:00
|
|
|
// Don't pass reflector control data through to the MMDVM
|
|
|
|
if (buffer[0U] != 0xF0U && buffer[0U] != 0xF1U) {
|
|
|
|
// Rewrite the LCF and the destination TG
|
|
|
|
if (buffer[0U] == 0x64U) {
|
|
|
|
buffer[1U] = 0x00U; // LCF is for TGs
|
|
|
|
} else if (buffer[0U] == 0x65U) {
|
2020-10-26 13:12:28 -04:00
|
|
|
buffer[1U] = (currentTG >> 16) & 0xFFU;
|
|
|
|
buffer[2U] = (currentTG >> 8) & 0xFFU;
|
|
|
|
buffer[3U] = (currentTG >> 0) & 0xFFU;
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 06:50:57 -05:00
|
|
|
localNetwork.write(buffer, len);
|
2020-10-26 13:12:28 -04:00
|
|
|
|
|
|
|
hangTimer.start();
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
2020-10-26 13:12:28 -04:00
|
|
|
} else if (currentTG == 0U) {
|
2022-01-24 23:12:42 -05:00
|
|
|
bool poll = false;
|
|
|
|
unsigned char pollReply[11U] = { 0xF0U };
|
|
|
|
std::string callsign = m_conf.getCallsign();
|
|
|
|
|
|
|
|
callsign.resize(10U, ' ');
|
|
|
|
|
|
|
|
// Build poll reply data
|
|
|
|
for (unsigned int i = 0U; i < 10U; i++)
|
|
|
|
pollReply[i + 1U] = callsign.at(i);
|
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
// Don't pass reflector control data through to the MMDVM
|
2022-01-24 23:12:42 -05:00
|
|
|
if ((buffer[0U] != 0xF0U && buffer[0U] != 0xF1U) || (poll = (::memcmp(buffer, pollReply, std::min(11U, len)) == 0))) {
|
2020-10-26 13:12:28 -04:00
|
|
|
// Find the static TG that this audio data belongs to
|
|
|
|
for (std::vector<CStaticTG>::const_iterator it = staticTGs.cbegin(); it != staticTGs.cend(); ++it) {
|
|
|
|
if (CUDPSocket::match(addr, (*it).m_addr)) {
|
|
|
|
currentTG = (*it).m_tg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentTG > 0U) {
|
|
|
|
currentAddr = addr;
|
|
|
|
currentAddrLen = addrLen;
|
|
|
|
currentIsStatic = true;
|
|
|
|
|
|
|
|
// Rewrite the LCF and the destination TG
|
|
|
|
if (buffer[0U] == 0x64U) {
|
|
|
|
buffer[1U] = 0x00U; // LCF is for TGs
|
|
|
|
} else if (buffer[0U] == 0x65U) {
|
|
|
|
buffer[1U] = (currentTG >> 16) & 0xFFU;
|
|
|
|
buffer[2U] = (currentTG >> 8) & 0xFFU;
|
|
|
|
buffer[3U] = (currentTG >> 0) & 0xFFU;
|
|
|
|
}
|
|
|
|
|
2022-01-25 13:44:37 -05:00
|
|
|
if (!poll)
|
2022-01-24 23:12:42 -05:00
|
|
|
localNetwork.write(buffer, len);
|
2020-10-26 13:12:28 -04:00
|
|
|
|
|
|
|
LogMessage("Switched to reflector %u due to network activity", currentTG);
|
2016-10-17 12:33:49 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
hangTimer.setTimeout(netHangTime);
|
|
|
|
hangTimer.start();
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From the MMDVM to the reflector or control data
|
2020-11-04 06:50:57 -05:00
|
|
|
len = localNetwork.read(buffer, 200U);
|
2016-10-10 16:59:30 -04:00
|
|
|
if (len > 0U) {
|
|
|
|
if (buffer[0U] == 0x65U) {
|
2020-10-26 13:12:28 -04:00
|
|
|
dstTG = (buffer[1U] << 16) & 0xFF0000U;
|
|
|
|
dstTG |= (buffer[2U] << 8) & 0x00FF00U;
|
|
|
|
dstTG |= (buffer[3U] << 0) & 0x0000FFU;
|
2016-10-11 02:45:50 -04:00
|
|
|
} else if (buffer[0U] == 0x66U) {
|
|
|
|
srcId = (buffer[1U] << 16) & 0xFF0000U;
|
|
|
|
srcId |= (buffer[2U] << 8) & 0x00FF00U;
|
|
|
|
srcId |= (buffer[3U] << 0) & 0x0000FFU;
|
2022-01-24 23:12:42 -05:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
if (dstTG != currentTG) {
|
2020-10-27 06:59:29 -04:00
|
|
|
if (currentAddrLen > 0U) {
|
2016-10-11 02:45:50 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-10-26 13:12:28 -04:00
|
|
|
LogMessage("Unlinking from reflector %u by %s", currentTG, callsign.c_str());
|
2016-10-18 12:52:29 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
if (!currentIsStatic) {
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
2016-10-11 12:43:37 -04:00
|
|
|
}
|
2016-10-13 16:18:48 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
hangTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStaticTG* found = NULL;
|
|
|
|
for (std::vector<CStaticTG>::const_iterator it = staticTGs.cbegin(); it != staticTGs.cend(); ++it) {
|
|
|
|
if (dstTG == (*it).m_tg) {
|
|
|
|
found = &(*it);
|
|
|
|
break;
|
2019-02-04 09:57:20 -05:00
|
|
|
}
|
2020-10-26 13:12:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found == NULL) {
|
|
|
|
CP25Reflector* refl = reflectors.find(dstTG);
|
|
|
|
if (refl != NULL) {
|
|
|
|
currentTG = dstTG;
|
|
|
|
currentAddr = refl->m_addr;
|
|
|
|
currentAddrLen = refl->m_addrLen;
|
|
|
|
currentIsStatic = false;
|
2020-10-27 06:59:29 -04:00
|
|
|
} else {
|
|
|
|
currentTG = dstTG;
|
|
|
|
currentAddrLen = 0U;
|
|
|
|
currentIsStatic = false;
|
2020-10-26 13:12:28 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentTG = found->m_tg;
|
|
|
|
currentAddr = found->m_addr;
|
|
|
|
currentAddrLen = found->m_addrLen;
|
|
|
|
currentIsStatic = true;
|
2016-10-11 17:46:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link to the new reflector
|
2020-10-26 13:12:28 -04:00
|
|
|
if (currentAddrLen > 0U) {
|
2016-10-11 17:46:28 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-10-26 13:12:28 -04:00
|
|
|
LogMessage("Switched to reflector %u due to RF activity from %s", currentTG, callsign.c_str());
|
2016-10-11 17:46:28 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
if (!currentIsStatic) {
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
2020-10-26 13:12:28 -04:00
|
|
|
}
|
2016-10-17 12:33:49 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
hangTimer.setTimeout(rfHangTime);
|
|
|
|
hangTimer.start();
|
2020-10-27 07:34:08 -04:00
|
|
|
} else {
|
|
|
|
hangTimer.stop();
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
2020-10-26 13:12:28 -04:00
|
|
|
|
|
|
|
if (voice != NULL) {
|
2020-10-27 06:59:29 -04:00
|
|
|
if (currentAddrLen == 0U)
|
2020-10-26 13:12:28 -04:00
|
|
|
voice->unlinked();
|
|
|
|
else
|
|
|
|
voice->linkedTo(dstTG);
|
|
|
|
}
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
2020-10-26 13:12:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[0U] == 0x80U) {
|
2019-02-04 09:57:20 -05:00
|
|
|
if (voice != NULL)
|
|
|
|
voice->eof();
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're linked and we have a network, send it on
|
2020-10-27 06:59:29 -04:00
|
|
|
if (currentAddrLen > 0U) {
|
2016-10-11 02:45:50 -04:00
|
|
|
// Rewrite the LCF and the destination TG
|
|
|
|
if (buffer[0U] == 0x64U) {
|
|
|
|
buffer[1U] = 0x00U; // LCF is for TGs
|
|
|
|
} else if (buffer[0U] == 0x65U) {
|
2020-10-26 13:12:28 -04:00
|
|
|
buffer[1U] = (currentTG >> 16) & 0xFFU;
|
|
|
|
buffer[2U] = (currentTG >> 8) & 0xFFU;
|
|
|
|
buffer[3U] = (currentTG >> 0) & 0xFFU;
|
2016-10-11 02:45:50 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.write(buffer, len, currentAddr, currentAddrLen);
|
2020-10-26 13:12:28 -04:00
|
|
|
hangTimer.start();
|
2016-10-11 02:45:50 -04:00
|
|
|
}
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
2019-02-04 09:57:20 -05:00
|
|
|
if (voice != NULL) {
|
|
|
|
unsigned int length = voice->read(buffer);
|
|
|
|
if (length > 0U)
|
2020-11-04 06:50:57 -05:00
|
|
|
localNetwork.write(buffer, length);
|
2019-02-04 09:57:20 -05:00
|
|
|
}
|
|
|
|
|
2020-10-27 07:34:08 -04:00
|
|
|
if (remoteSocket != NULL) {
|
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
int res = remoteSocket->read(buffer, 200U, addr, addrLen);
|
|
|
|
if (res > 0) {
|
|
|
|
buffer[res] = '\0';
|
|
|
|
if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) {
|
2022-01-20 06:55:53 -05:00
|
|
|
unsigned int tg = ((strlen((char*)buffer + 0U) > 10) ? (unsigned int)::atoi((char*)(buffer + 10U)) : 9999);
|
2020-10-27 07:34:08 -04:00
|
|
|
|
|
|
|
if (tg != currentTG) {
|
|
|
|
if (currentAddrLen > 0U) {
|
|
|
|
LogMessage("Unlinked from reflector %u by remote command", currentTG);
|
|
|
|
|
|
|
|
if (!currentIsStatic) {
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
2020-10-27 07:34:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hangTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStaticTG* found = NULL;
|
|
|
|
for (std::vector<CStaticTG>::const_iterator it = staticTGs.cbegin(); it != staticTGs.cend(); ++it) {
|
|
|
|
if (tg == (*it).m_tg) {
|
|
|
|
found = &(*it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found == NULL) {
|
|
|
|
CP25Reflector* refl = reflectors.find(tg);
|
|
|
|
if (refl != NULL) {
|
|
|
|
currentTG = tg;
|
|
|
|
currentAddr = refl->m_addr;
|
|
|
|
currentAddrLen = refl->m_addrLen;
|
|
|
|
currentIsStatic = false;
|
|
|
|
} else {
|
|
|
|
currentTG = tg;
|
|
|
|
currentAddrLen = 0U;
|
|
|
|
currentIsStatic = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentTG = found->m_tg;
|
|
|
|
currentAddr = found->m_addr;
|
|
|
|
currentAddrLen = found->m_addrLen;
|
|
|
|
currentIsStatic = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link to the new reflector
|
|
|
|
if (currentAddrLen > 0U) {
|
|
|
|
LogMessage("Switched to reflector %u by remote command", currentTG);
|
|
|
|
|
|
|
|
if (!currentIsStatic) {
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
2020-10-27 07:34:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hangTimer.setTimeout(rfHangTime);
|
|
|
|
hangTimer.start();
|
|
|
|
} else {
|
|
|
|
hangTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (voice != NULL) {
|
|
|
|
if (currentAddrLen == 0U)
|
|
|
|
voice->unlinked();
|
|
|
|
else
|
|
|
|
voice->linkedTo(currentTG);
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 13:07:55 -05:00
|
|
|
} else if (::memcmp(buffer + 0U, "status", 6U) == 0) {
|
|
|
|
std::string state = std::string("p25:") + ((currentAddrLen > 0) ? "conn" : "disc");
|
|
|
|
remoteSocket->write((unsigned char*)state.c_str(), (unsigned int)state.length(), addr, addrLen);
|
|
|
|
} else if (::memcmp(buffer + 0U, "host", 4U) == 0) {
|
|
|
|
std::string ref;
|
|
|
|
|
|
|
|
if (currentAddrLen > 0) {
|
|
|
|
char buffer[INET6_ADDRSTRLEN];
|
|
|
|
if (getnameinfo((struct sockaddr*)¤tAddr, currentAddrLen, buffer, sizeof(buffer), 0, 0, NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
|
|
|
|
ref = std::string(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string host = std::string("p25:\"") + ((ref.length() == 0) ? "NONE" : ref) + "\"";
|
|
|
|
remoteSocket->write((unsigned char*)host.c_str(), (unsigned int)host.length(), addr, addrLen);
|
2020-10-27 07:34:08 -04:00
|
|
|
} else {
|
|
|
|
CUtils::dump("Invalid remote command received", buffer, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
unsigned int ms = stopWatch.elapsed();
|
|
|
|
stopWatch.start();
|
|
|
|
|
|
|
|
reflectors.clock(ms);
|
|
|
|
|
2019-02-04 09:57:20 -05:00
|
|
|
if (voice != NULL)
|
|
|
|
voice->clock(ms);
|
2016-10-17 18:44:45 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
hangTimer.clock(ms);
|
|
|
|
if (hangTimer.isRunning() && hangTimer.hasExpired()) {
|
2020-10-27 06:59:29 -04:00
|
|
|
if (currentAddrLen > 0U) {
|
2020-10-26 13:12:28 -04:00
|
|
|
LogMessage("Unlinking from %u due to inactivity", currentTG);
|
2018-04-09 14:31:24 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
if (!currentIsStatic) {
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
|
|
|
remoteNetwork.unlink(currentAddr, currentAddrLen);
|
2018-04-09 14:31:24 -04:00
|
|
|
}
|
2017-03-18 03:13:14 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
if (voice != NULL)
|
|
|
|
voice->unlinked();
|
|
|
|
|
|
|
|
currentAddrLen = 0U;
|
|
|
|
|
|
|
|
hangTimer.stop();
|
2018-03-29 15:30:05 -04:00
|
|
|
}
|
2020-11-02 06:36:38 -05:00
|
|
|
|
|
|
|
currentTG = 0U;
|
2018-04-09 14:31:24 -04:00
|
|
|
}
|
2017-03-18 03:13:14 -04:00
|
|
|
|
2020-11-04 06:50:57 -05:00
|
|
|
localNetwork.clock(ms);
|
|
|
|
|
2016-10-11 12:43:37 -04:00
|
|
|
pollTimer.clock(ms);
|
|
|
|
if (pollTimer.isRunning() && pollTimer.hasExpired()) {
|
2020-10-26 13:12:28 -04:00
|
|
|
// Poll the static TGs
|
|
|
|
for (std::vector<CStaticTG>::const_iterator it = staticTGs.cbegin(); it != staticTGs.cend(); ++it)
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.poll((*it).m_addr, (*it).m_addrLen);
|
2016-10-11 12:43:37 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
// Poll the dynamic TG
|
|
|
|
if (!currentIsStatic && currentAddrLen > 0U)
|
2020-11-04 06:50:57 -05:00
|
|
|
remoteNetwork.poll(currentAddr, currentAddrLen);
|
2016-10-17 12:33:49 -04:00
|
|
|
|
2020-10-26 13:12:28 -04:00
|
|
|
pollTimer.start();
|
2016-10-11 12:43:37 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 02:47:37 -04:00
|
|
|
if (ms < 5U)
|
|
|
|
CThread::sleep(5U);
|
2016-10-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
2019-02-04 09:57:20 -05:00
|
|
|
delete voice;
|
2016-10-17 18:44:45 -04:00
|
|
|
|
2016-10-10 16:59:30 -04:00
|
|
|
localNetwork.close();
|
|
|
|
|
2020-10-27 07:34:08 -04:00
|
|
|
if (remoteSocket != NULL) {
|
|
|
|
remoteSocket->close();
|
|
|
|
delete remoteSocket;
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:33:49 -04:00
|
|
|
remoteNetwork.close();
|
2016-10-10 16:59:30 -04:00
|
|
|
|
|
|
|
lookup->stop();
|
|
|
|
|
|
|
|
::LogFinalise();
|
|
|
|
}
|