NXDNClients/NXDNGateway/NXDNGateway.cpp

631 lines
17 KiB
C++
Raw Normal View History

2018-03-13 18:16:48 -04:00
/*
* Copyright (C) 2016,2017,2018,2020 by Jonathan Naylor G4KLX
2018-03-13 18:16:48 -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.
*/
2020-04-06 16:32:50 -04:00
#include "KenwoodNetwork.h"
2018-03-13 18:16:48 -04:00
#include "IcomNetwork.h"
#include "NXDNNetwork.h"
#include "NXDNGateway.h"
#include "RptNetwork.h"
2018-03-13 18:16:48 -04:00
#include "NXDNLookup.h"
#include "Reflectors.h"
2018-05-22 15:43:04 -04:00
#include "GPSHandler.h"
2018-03-13 18:16:48 -04:00
#include "StopWatch.h"
#include "Version.h"
#include "Thread.h"
#include "Voice.h"
#include "Timer.h"
#include "Utils.h"
2018-03-13 18:16:48 -04:00
#include "Log.h"
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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 = "NXDNGateway.ini";
#else
const char* DEFAULT_INI_FILE = "/etc/NXDNGateway.ini";
#endif
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <ctime>
#include <cstring>
2018-05-22 15:43:04 -04:00
const unsigned char NXDN_TYPE_DCALL_HDR = 0x09U;
const unsigned char NXDN_TYPE_DCALL = 0x0BU;
const unsigned char NXDN_TYPE_TX_REL = 0x08U;
2018-03-20 16:09:31 -04:00
const unsigned short NXDN_VOICE_ID = 9999U;
2018-03-13 18:16:48 -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, "NXDNGateway version %s\n", VERSION);
return 0;
} else if (arg.substr(0, 1) == "-") {
::fprintf(stderr, "Usage: NXDNGateway [-v|--version] [filename]\n");
return 1;
} else {
iniFile = argv[currentArg];
}
}
}
CNXDNGateway* gateway = new CNXDNGateway(std::string(iniFile));
gateway->run();
delete gateway;
return 0;
}
CNXDNGateway::CNXDNGateway(const std::string& file) :
2018-11-06 06:27:36 -05:00
m_conf(file),
m_writer(NULL),
m_gps(NULL)
2018-03-13 18:16:48 -04:00
{
}
CNXDNGateway::~CNXDNGateway()
{
}
void CNXDNGateway::run()
{
bool ret = m_conf.read();
if (!ret) {
::fprintf(stderr, "NXDNGateway: 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 13:50:47 -04:00
::fprintf(stderr, "Couldn't fork() , exiting\n");
2018-03-13 18:16:48 -04:00
return;
2020-04-06 16:32:50 -04:00
}
else if (pid != 0) {
2018-03-13 18:16:48 -04:00
exit(EXIT_SUCCESS);
2018-06-18 13:50:47 -04:00
}
2018-03-13 18:16:48 -04:00
// Create new session and process group
if (::setsid() == -1) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "Couldn't setsid(), exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
// Set the working directory to the root directory
if (::chdir("/") == -1) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "Couldn't cd /, exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
2018-06-18 13:50:47 -04:00
// If we are currently root...
2018-03-13 18:16:48 -04:00
if (getuid() == 0) {
struct passwd* user = ::getpwnam("mmdvm");
if (user == NULL) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "Could not get the mmdvm user, exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
uid_t mmdvm_uid = user->pw_uid;
gid_t mmdvm_gid = user->pw_gid;
2018-06-18 13:50:47 -04:00
// Set user and group ID's to mmdvm:mmdvm
2018-03-13 18:16:48 -04:00
if (setgid(mmdvm_gid) != 0) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "Could not set mmdvm GID, exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
if (setuid(mmdvm_uid) != 0) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "Could not set mmdvm UID, exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
2018-06-18 13:50:47 -04:00
// Double check it worked (AKA Paranoia)
2018-03-13 18:16:48 -04:00
if (setuid(0) != -1) {
2018-06-18 13:50:47 -04:00
::fprintf(stderr, "It's possible to regain root - something is wrong!, exiting\n");
2018-03-13 18:16:48 -04:00
return;
}
}
}
#endif
2018-06-18 13:50:47 -04:00
ret = ::LogInitialise(m_conf.getLogFilePath(), m_conf.getLogFileRoot(), 1U, 1U);
if (!ret) {
::fprintf(stderr, "NXDNGateway: unable to open the log file\n");
return;
}
#if !defined(_WIN32) && !defined(_WIN64)
if (m_daemon) {
::close(STDIN_FILENO);
::close(STDOUT_FILENO);
::close(STDERR_FILENO);
}
#endif
2018-05-22 15:43:04 -04:00
createGPS();
2020-04-06 16:32:50 -04:00
IRptNetwork* localNetwork = NULL;
std::string protocol = m_conf.getRptProtocol();
if (protocol == "Kenwood")
localNetwork = new CKenwoodNetwork(m_conf.getMyPort(), m_conf.getRptAddress(), m_conf.getRptPort(), m_conf.getRptDebug());
else
localNetwork = new CIcomNetwork(m_conf.getMyPort(), m_conf.getRptAddress(), m_conf.getRptPort(), m_conf.getRptDebug());
ret = localNetwork->open();
2018-03-13 18:16:48 -04:00
if (!ret) {
::LogFinalise();
return;
}
CNXDNNetwork remoteNetwork(m_conf.getNetworkPort(), m_conf.getCallsign(), m_conf.getNetworkDebug());
ret = remoteNetwork.open();
if (!ret) {
localNetwork->close();
delete localNetwork;
2018-03-13 18:16:48 -04:00
::LogFinalise();
return;
}
2020-05-03 13:12:27 -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-06-19 15:35:32 -04:00
CReflectors reflectors(m_conf.getNetworkHosts1(), m_conf.getNetworkHosts2(), m_conf.getNetworkReloadTime());
2018-03-13 18:16:48 -04:00
if (m_conf.getNetworkParrotPort() > 0U)
reflectors.setParrot(m_conf.getNetworkParrotAddress(), m_conf.getNetworkParrotPort());
2018-03-31 21:30:38 -04:00
if (m_conf.getNetworkNXDN2DMRPort() > 0U)
reflectors.setNXDN2DMR(m_conf.getNetworkNXDN2DMRAddress(), m_conf.getNetworkNXDN2DMRPort());
2018-03-13 18:16:48 -04:00
reflectors.load();
CNXDNLookup* lookup = new CNXDNLookup(m_conf.getLookupName(), m_conf.getLookupTime());
lookup->read();
CTimer inactivityTimer(1000U, m_conf.getNetworkInactivityTimeout() * 60U);
CTimer lostTimer(1000U, 120U);
CTimer pollTimer(1000U, 5U);
CStopWatch stopWatch;
stopWatch.start();
CVoice* voice = NULL;
2018-03-15 14:44:59 -04:00
if (m_conf.getVoiceEnabled()) {
2018-03-20 16:09:31 -04:00
voice = new CVoice(m_conf.getVoiceDirectory(), m_conf.getVoiceLanguage(), NXDN_VOICE_ID);
2018-03-15 14:44:59 -04:00
bool ok = voice->open();
if (!ok) {
delete voice;
voice = NULL;
}
}
2018-03-13 18:16:48 -04:00
LogMessage("Starting NXDNGateway-%s", VERSION);
unsigned short srcId = 0U;
unsigned short dstId = 0U;
bool grp = false;
2018-03-27 03:13:54 -04:00
unsigned short currentId = 9999U;
2018-03-13 18:16:48 -04:00
in_addr currentAddr;
unsigned int currentPort = 0U;
2018-04-09 14:08:19 -04:00
unsigned short startupId = m_conf.getNetworkStartup();
if (startupId != 9999U) {
CNXDNReflector* reflector = reflectors.find(startupId);
2018-03-13 18:16:48 -04:00
if (reflector != NULL) {
2018-04-09 14:08:19 -04:00
currentId = startupId;
2018-03-13 18:16:48 -04:00
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
2018-03-27 03:13:54 -04:00
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
2018-03-13 18:16:48 -04:00
LogMessage("Linked at startup to reflector %u", currentId);
2018-04-09 14:08:19 -04:00
} else {
2020-05-03 13:12:27 -04:00
startupId = 9999U;
2018-03-13 18:16:48 -04:00
}
}
for (;;) {
unsigned char buffer[200U];
in_addr address;
unsigned int port;
// From the reflector to the MMDVM
unsigned int len = remoteNetwork.readData(buffer, 200U, address, port);
if (len > 0U) {
// If we're linked and it's from the right place, send it on
if (currentId != 9999U && currentAddr.s_addr == address.s_addr && currentPort == port) {
// Don't pass reflector control data through to the MMDVM
if (::memcmp(buffer, "NXDND", 5U) == 0) {
unsigned short dstId = 0U;
dstId |= (buffer[7U] << 8) & 0xFF00U;
dstId |= (buffer[8U] << 0) & 0x00FFU;
bool grp = (buffer[9U] & 0x01U) == 0x01U;
if (grp && currentId == dstId)
2020-04-06 16:32:50 -04:00
localNetwork->write(buffer + 10U, len - 10U);
}
2018-03-13 18:16:48 -04:00
// Any network activity is proof that the reflector is alive
lostTimer.start();
}
}
// From the MMDVM to the reflector or control data
2020-04-06 16:32:50 -04:00
len = localNetwork->read(buffer);
2018-03-13 18:16:48 -04:00
if (len > 0U) {
// Only process the beginning and ending voice blocks here
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && (buffer[5U] == 0x01U || buffer[5U] == 0x08U)) {
2018-03-13 18:16:48 -04:00
grp = (buffer[7U] & 0x20U) == 0x20U;
2018-05-22 15:43:04 -04:00
srcId = (buffer[8U] << 8) & 0xFF00U;
2018-03-13 18:16:48 -04:00
srcId |= (buffer[9U] << 0) & 0x00FFU;
2018-05-22 15:43:04 -04:00
dstId = (buffer[10U] << 8) & 0xFF00U;
2018-03-13 18:16:48 -04:00
dstId |= (buffer[11U] << 0) & 0x00FFU;
if (dstId != currentId) {
CNXDNReflector* reflector = NULL;
if (dstId != 9999U)
reflector = reflectors.find(dstId);
// If we're unlinking or changing reflectors, unlink from the current one
if (dstId == 9999U || reflector != NULL) {
if (currentId != 9999U) {
2018-03-20 16:09:31 -04:00
std::string callsign = lookup->find(srcId);
2018-03-13 18:16:48 -04:00
LogMessage("Unlinked from reflector %u by %s", currentId, callsign.c_str());
2018-03-14 15:24:18 -04:00
if (voice != NULL && dstId == 9999U)
voice->unlinked();
2018-03-27 03:13:54 -04:00
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
2018-03-13 18:16:48 -04:00
inactivityTimer.stop();
pollTimer.stop();
lostTimer.stop();
}
currentId = dstId;
}
// Link to the new reflector
if (reflector != NULL) {
2018-05-22 15:43:04 -04:00
currentId = dstId;
2018-03-13 18:16:48 -04:00
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
std::string callsign = lookup->find(srcId);
LogMessage("Linked to reflector %u by %s", currentId, callsign.c_str());
2018-03-14 15:24:18 -04:00
if (voice != NULL)
voice->linkedTo(currentId);
2018-03-27 03:13:54 -04:00
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
2018-03-13 18:16:48 -04:00
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
}
}
// If it's the end of the voice transmission, start the voice prompt
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && buffer[5U] == 0x08U) {
if (voice != NULL)
voice->eof();
}
2018-03-13 18:16:48 -04:00
}
2018-05-22 15:43:04 -04:00
if (m_gps != NULL) {
if ((buffer[0U] & 0xF0U) == 0x90U) {
switch (buffer[2U] & 0x3FU) {
case NXDN_TYPE_DCALL_HDR: {
unsigned short srcId = 0U;
srcId |= (buffer[5U] << 8) & 0xFF00U;
srcId |= (buffer[6U] << 0) & 0x00FFU;
std::string callsign = lookup->find(srcId);
m_gps->processHeader(callsign);
}
break;
case NXDN_TYPE_DCALL:
m_gps->processData(buffer + 3U);
break;
case NXDN_TYPE_TX_REL:
m_gps->processEnd();
break;
default:
break;
}
}
}
2018-03-13 18:16:48 -04:00
// If we're linked and we have a network, send it on
if (currentId != 9999U) {
remoteNetwork.writeData(buffer, len, srcId, dstId, grp, currentAddr, currentPort);
inactivityTimer.start();
}
}
if (voice != NULL) {
unsigned int length = voice->read(buffer);
if (length > 0U)
2020-04-06 16:32:50 -04:00
localNetwork->write(buffer, length);
2018-03-13 18:16:48 -04:00
}
if (remoteSocket != NULL) {
int res = remoteSocket->read(buffer, 200U, address, port);
if (res > 0) {
buffer[res] = '\0';
if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) {
unsigned int tg = (unsigned int)::atoi((char*)(buffer + 9U));
CNXDNReflector* reflector = NULL;
if (tg != 9999U)
reflector = reflectors.find(tg);
if (reflector == NULL && currentId != 9999U) {
LogMessage("Unlinked from reflector %u by remote command", currentId);
if (voice != NULL)
voice->unlinked();
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
inactivityTimer.stop();
pollTimer.stop();
lostTimer.stop();
currentId = 9999U;
} else if (reflector != NULL && currentId == 9999U) {
currentId = tg;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
} else if (reflector != NULL && currentId != 9999U) {
LogMessage("Unlinked from reflector %u by remote command", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
currentId = tg;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
}
} else {
CUtils::dump("Invalid remote command received", buffer, res);
}
}
}
2018-03-13 18:16:48 -04:00
unsigned int ms = stopWatch.elapsed();
stopWatch.start();
reflectors.clock(ms);
localNetwork->clock(ms);
2018-03-13 18:16:48 -04:00
if (voice != NULL)
voice->clock(ms);
inactivityTimer.clock(ms);
if (inactivityTimer.isRunning() && inactivityTimer.hasExpired()) {
2018-04-09 14:08:19 -04:00
if (currentId != 9999U && startupId == 9999U) {
2018-03-13 18:16:48 -04:00
LogMessage("Unlinking from %u due to inactivity", currentId);
2018-03-27 03:13:54 -04:00
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
2018-03-13 18:16:48 -04:00
if (voice != NULL)
voice->unlinked();
currentId = 9999U;
2018-04-09 14:08:19 -04:00
inactivityTimer.stop();
2018-03-13 18:16:48 -04:00
pollTimer.stop();
lostTimer.stop();
2018-04-09 14:08:19 -04:00
} else if (currentId != startupId) {
if (currentId != 9999U) {
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
}
2018-03-13 18:16:48 -04:00
2018-04-09 14:08:19 -04:00
CNXDNReflector* reflector = reflectors.find(startupId);
if (reflector != NULL) {
currentId = startupId;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
LogMessage("Relinked to reflector %u due to inactivity", currentId);
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
} else {
startupId = 9999U;
inactivityTimer.stop();
pollTimer.stop();
lostTimer.stop();
}
}
2018-03-13 18:16:48 -04:00
}
pollTimer.clock(ms);
if (pollTimer.isRunning() && pollTimer.hasExpired()) {
if (currentId != 9999U)
2018-03-27 03:13:54 -04:00
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
2018-03-13 18:16:48 -04:00
pollTimer.start();
}
lostTimer.clock(ms);
if (lostTimer.isRunning() && lostTimer.hasExpired()) {
if (currentId != 9999U) {
LogWarning("No response from %u, unlinking", currentId);
currentId = 9999U;
}
inactivityTimer.stop();
lostTimer.stop();
}
2018-11-06 06:27:36 -05:00
if (m_writer != NULL)
m_writer->clock(ms);
2018-05-22 15:43:04 -04:00
2018-03-13 18:16:48 -04:00
if (ms < 5U)
CThread::sleep(5U);
}
delete voice;
localNetwork->close();
delete localNetwork;
2018-03-13 18:16:48 -04:00
if (remoteSocket != NULL) {
remoteSocket->close();
delete remoteSocket;
}
2018-03-13 18:16:48 -04:00
remoteNetwork.close();
lookup->stop();
2018-05-22 15:43:04 -04:00
if (m_gps != NULL) {
2018-11-06 06:27:36 -05:00
m_writer->close();
delete m_writer;
2018-05-22 15:43:04 -04:00
delete m_gps;
}
2018-03-13 18:16:48 -04:00
::LogFinalise();
}
2018-05-22 15:43:04 -04:00
void CNXDNGateway::createGPS()
{
if (!m_conf.getAPRSEnabled())
return;
std::string callsign = m_conf.getCallsign();
std::string rptSuffix = m_conf.getSuffix();
std::string hostname = m_conf.getAPRSServer();
unsigned int port = m_conf.getAPRSPort();
std::string password = m_conf.getAPRSPassword();
std::string suffix = m_conf.getAPRSSuffix();
2018-11-06 06:27:36 -05:00
m_writer = new CAPRSWriter(callsign, rptSuffix, password, hostname, port);
2018-05-22 15:43:04 -04:00
unsigned int txFrequency = m_conf.getTxFrequency();
unsigned int rxFrequency = m_conf.getRxFrequency();
2018-11-06 06:27:36 -05:00
std::string desc = m_conf.getAPRSDescription();
2018-05-22 15:43:04 -04:00
2018-11-06 06:27:36 -05:00
m_writer->setInfo(txFrequency, rxFrequency, desc);
2018-05-22 15:43:04 -04:00
2018-11-06 06:27:36 -05:00
bool enabled = m_conf.getMobileGPSEnabled();
if (enabled) {
std::string address = m_conf.getMobileGPSAddress();
unsigned int port = m_conf.getMobileGPSPort();
m_writer->setMobileLocation(address, port);
} else {
float latitude = m_conf.getLatitude();
float longitude = m_conf.getLongitude();
int height = m_conf.getHeight();
m_writer->setStaticLocation(latitude, longitude, height);
}
bool ret = m_writer->open();
2018-05-22 15:43:04 -04:00
if (!ret) {
2018-11-06 06:27:36 -05:00
delete m_writer;
m_writer = NULL;
return;
2018-05-22 15:43:04 -04:00
}
2018-11-06 06:27:36 -05:00
m_gps = new CGPSHandler(callsign, suffix, m_writer);
2018-05-22 15:43:04 -04:00
}
2018-11-06 06:27:36 -05:00