2018-03-13 18:16:48 -04:00
|
|
|
/*
|
2021-02-13 10:34:18 -05:00
|
|
|
* Copyright (C) 2016,2018,2020,2021 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "NXDNReflector.h"
|
|
|
|
#include "NXDNNetwork.h"
|
|
|
|
#include "NXDNLookup.h"
|
|
|
|
#include "StopWatch.h"
|
|
|
|
#include "Version.h"
|
|
|
|
#include "Thread.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#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 = "NXDNReflector.ini";
|
|
|
|
#else
|
|
|
|
const char* DEFAULT_INI_FILE = "/etc/NXDNReflector.ini";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
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, "NXDNReflector version %s\n", VERSION);
|
|
|
|
return 0;
|
|
|
|
} else if (arg.substr(0, 1) == "-") {
|
|
|
|
::fprintf(stderr, "Usage: NXDNReflector [-v|--version] [filename]\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
iniFile = argv[currentArg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNReflector* reflector = new CNXDNReflector(std::string(iniFile));
|
|
|
|
reflector->run();
|
|
|
|
delete reflector;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNReflector::CNXDNReflector(const std::string& file) :
|
|
|
|
m_conf(file),
|
2020-08-22 09:26:44 -04:00
|
|
|
m_icomNetwork(NULL),
|
|
|
|
m_kenwoodNetwork(NULL),
|
2018-03-13 18:16:48 -04:00
|
|
|
m_repeaters()
|
|
|
|
{
|
2020-09-20 16:43:46 -04:00
|
|
|
CUDPSocket::startup();
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNReflector::~CNXDNReflector()
|
|
|
|
{
|
2020-09-20 16:43:46 -04:00
|
|
|
CUDPSocket::shutdown();
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNReflector::run()
|
|
|
|
{
|
|
|
|
bool ret = m_conf.read();
|
|
|
|
if (!ret) {
|
|
|
|
::fprintf(stderr, "NXDNReflector: 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;
|
2018-06-18 13:50:47 -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
|
|
|
|
|
2020-10-05 08:20:18 -04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2020-11-01 07:45: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:20:18 -04:00
|
|
|
#else
|
2020-11-01 07:45: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:20:18 -04:00
|
|
|
#endif
|
2018-06-18 13:50:47 -04:00
|
|
|
if (!ret) {
|
|
|
|
::fprintf(stderr, "NXDNReflector: unable to open the log file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 15:43:27 -04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
if (m_daemon) {
|
|
|
|
::close(STDIN_FILENO);
|
|
|
|
::close(STDOUT_FILENO);
|
|
|
|
::close(STDERR_FILENO);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-26 15:21:32 -04:00
|
|
|
unsigned short tg = m_conf.getTG();
|
|
|
|
|
2020-09-08 04:29:17 -04:00
|
|
|
CNXDNNetwork nxdnNetwork(m_conf.getNetworkPort(), m_conf.getNetworkDebug());
|
2018-03-13 18:16:48 -04:00
|
|
|
ret = nxdnNetwork.open();
|
|
|
|
if (!ret) {
|
|
|
|
::LogFinalise();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
bool icomEnabled = m_conf.getIcomEnabled();
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
unsigned short icomTGEnable = 0U;
|
|
|
|
unsigned short icomTGDisable = 0U;
|
2018-03-21 15:19:02 -04:00
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (icomEnabled) {
|
2020-08-22 09:26:44 -04:00
|
|
|
ret = openIcomNetwork();
|
2018-03-13 18:16:48 -04:00
|
|
|
if (!ret) {
|
|
|
|
nxdnNetwork.close();
|
|
|
|
::LogFinalise();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-21 15:19:02 -04:00
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
icomTGEnable = m_conf.getIcomTGEnable();
|
|
|
|
icomTGDisable = m_conf.getIcomTGDisable();
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
bool kenwoodEnabled = m_conf.getKenwoodEnabled();
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
unsigned short kenwoodTGEnable = 0U;
|
|
|
|
unsigned short kenwoodTGDisable = 0U;
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (kenwoodEnabled) {
|
2020-08-22 09:26:44 -04:00
|
|
|
ret = openKenwoodNetwork();
|
|
|
|
if (!ret) {
|
|
|
|
nxdnNetwork.close();
|
|
|
|
::LogFinalise();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kenwoodTGEnable = m_conf.getKenwoodTGEnable();
|
|
|
|
kenwoodTGDisable = m_conf.getKenwoodTGDisable();
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNLookup* lookup = new CNXDNLookup(m_conf.getLookupName(), m_conf.getLookupTime());
|
|
|
|
lookup->read();
|
|
|
|
|
|
|
|
CStopWatch stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
|
|
|
|
CTimer dumpTimer(1000U, 120U);
|
|
|
|
dumpTimer.start();
|
|
|
|
|
|
|
|
LogMessage("Starting NXDNReflector-%s", VERSION);
|
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
enum {
|
|
|
|
ACTIVE_NONE,
|
2021-08-24 13:46:27 -04:00
|
|
|
ACTIVE_NXDN,
|
2020-08-23 08:22:18 -04:00
|
|
|
ACTIVE_ICOM,
|
|
|
|
ACTIVE_KENWOOD
|
|
|
|
} active = ACTIVE_NONE;
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
CNXDNRepeater* current = NULL;
|
|
|
|
|
2018-03-19 15:03:06 -04:00
|
|
|
unsigned short srcId = 0U;
|
|
|
|
unsigned short dstId = 0U;
|
|
|
|
bool grp = false;
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
CTimer watchdogTimer(1000U, 0U, 1500U);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
unsigned char buffer[200U];
|
2020-09-03 10:12:03 -04:00
|
|
|
sockaddr_storage address;
|
|
|
|
unsigned int addressLen;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-09-03 10:12:03 -04:00
|
|
|
unsigned int len = nxdnNetwork.read(buffer, 200U, address, addressLen);
|
2018-03-13 18:16:48 -04:00
|
|
|
if (len > 0U) {
|
2020-09-03 10:12:03 -04:00
|
|
|
CNXDNRepeater* rpt = findRepeater(address);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-27 03:13:54 -04:00
|
|
|
if (::memcmp(buffer, "NXDNP", 5U) == 0 && len == 17U) {
|
|
|
|
unsigned short id = (buffer[15U] << 8) | buffer[16U];
|
|
|
|
if (id == tg) {
|
|
|
|
if (rpt == NULL) {
|
|
|
|
rpt = new CNXDNRepeater;
|
|
|
|
rpt->m_timer.start();
|
2021-02-13 10:34:18 -05:00
|
|
|
::memcpy(&rpt->m_addr, &address, sizeof(struct sockaddr_storage));
|
2020-09-03 10:12:03 -04:00
|
|
|
rpt->m_addrLen = addressLen;
|
2018-03-27 03:13:54 -04:00
|
|
|
rpt->m_callsign = std::string((char*)(buffer + 5U), 10U);
|
|
|
|
m_repeaters.push_back(rpt);
|
|
|
|
|
2020-11-24 07:51:39 -05:00
|
|
|
char buff[80U];
|
|
|
|
LogMessage("Adding %s (%s)", rpt->m_callsign.c_str(), CUDPSocket::display(address, buff, 80U));
|
2018-03-27 03:13:54 -04:00
|
|
|
} else {
|
|
|
|
rpt->m_timer.start();
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-27 03:13:54 -04:00
|
|
|
// Return the poll
|
2020-09-03 10:12:03 -04:00
|
|
|
nxdnNetwork.write(buffer, len, address, addressLen);
|
2018-03-27 03:13:54 -04:00
|
|
|
}
|
|
|
|
} else if (::memcmp(buffer, "NXDNU", 5U) == 0 && len == 17U) {
|
|
|
|
unsigned short id = (buffer[15U] << 8) | buffer[16U];
|
|
|
|
if (id == tg) {
|
|
|
|
if (rpt != NULL) {
|
|
|
|
std::string callsign = std::string((char*)(buffer + 5U), 10U);
|
2020-11-24 07:51:39 -05:00
|
|
|
|
|
|
|
char buff[80U];
|
|
|
|
LogMessage("Removing %s (%s) unlinked", callsign.c_str(), CUDPSocket::display(address, buff, 80U));
|
2018-03-27 03:13:54 -04:00
|
|
|
|
|
|
|
for (std::vector<CNXDNRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
|
|
|
|
if (*it == rpt) {
|
|
|
|
m_repeaters.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2018-03-27 03:13:54 -04:00
|
|
|
delete rpt;
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
2018-03-15 14:49:15 -04:00
|
|
|
} else if (::memcmp(buffer, "NXDND", 5U) == 0 && len == 43U) {
|
2018-03-13 18:16:48 -04:00
|
|
|
if (rpt != NULL) {
|
2018-03-21 15:19:02 -04:00
|
|
|
unsigned short srcId = (buffer[5U] << 8) | buffer[6U];
|
|
|
|
unsigned short dstId = (buffer[7U] << 8) | buffer[8U];
|
|
|
|
bool grp = (buffer[9U] & 0x01U) == 0x01U;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (icomEnabled && icomTGEnable != 0U && grp && dstId == icomTGEnable) {
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_icomNetwork == NULL) {
|
|
|
|
std::string callsign = lookup->find(srcId);
|
|
|
|
LogMessage("Icom Network link enabled by %s at %s", callsign.c_str(), current->m_callsign.c_str());
|
|
|
|
bool ok = openIcomNetwork();
|
|
|
|
if (!ok)
|
|
|
|
LogWarning("Unable to open the Icom Network link");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (kenwoodEnabled && kenwoodTGEnable != 0U && grp && dstId == kenwoodTGEnable) {
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_kenwoodNetwork == NULL) {
|
2018-03-21 15:19:02 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-08-22 09:26:44 -04:00
|
|
|
LogMessage("Kenwood Network link enabled by %s at %s", callsign.c_str(), current->m_callsign.c_str());
|
|
|
|
bool ok = openKenwoodNetwork();
|
2018-03-21 16:12:08 -04:00
|
|
|
if (!ok)
|
2020-08-22 09:26:44 -04:00
|
|
|
LogWarning("Unable to open the Kenwood Network link");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (icomEnabled && icomTGDisable != 0U && grp && dstId == icomTGDisable) {
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_icomNetwork != NULL) {
|
|
|
|
std::string callsign = lookup->find(srcId);
|
|
|
|
LogMessage("Icom Network link disabled by %s at %s", callsign.c_str(), current->m_callsign.c_str());
|
|
|
|
closeIcomNetwork();
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
2020-08-22 09:26:44 -04:00
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
if (kenwoodEnabled && kenwoodTGDisable != 0U && grp && dstId == kenwoodTGDisable) {
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_kenwoodNetwork != NULL) {
|
2018-03-21 15:19:02 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-08-22 09:26:44 -04:00
|
|
|
LogMessage("Kenwood Network link disabled by %s at %s", callsign.c_str(), current->m_callsign.c_str());
|
|
|
|
closeKenwoodNetwork();
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
2020-08-22 09:26:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (grp && dstId == tg) {
|
2018-03-21 15:19:02 -04:00
|
|
|
rpt->m_timer.start();
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
if (current == NULL && active == ACTIVE_NONE) {
|
2018-03-21 15:19:02 -04:00
|
|
|
current = rpt;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-21 15:19:02 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
|
|
|
LogMessage("Transmission from %s at %s to %s%u", callsign.c_str(), current->m_callsign.c_str(), grp ? "TG " : "", dstId);
|
2020-08-23 08:22:18 -04:00
|
|
|
|
2021-08-24 13:46:27 -04:00
|
|
|
active = ACTIVE_NXDN;
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2021-08-24 13:46:27 -04:00
|
|
|
if (active == ACTIVE_NXDN) {
|
2018-03-21 15:19:02 -04:00
|
|
|
watchdogTimer.start();
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-21 15:19:02 -04:00
|
|
|
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
|
2021-02-13 10:34:18 -05:00
|
|
|
if (!CUDPSocket::match((*it)->m_addr, address))
|
|
|
|
nxdnNetwork.write(buffer, len, (*it)->m_addr, (*it)->m_addrLen);
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_icomNetwork != NULL)
|
|
|
|
m_icomNetwork->write(buffer, len);
|
|
|
|
|
|
|
|
if (m_kenwoodNetwork != NULL)
|
|
|
|
m_kenwoodNetwork->write(buffer, len);
|
2018-03-22 15:18:07 -04:00
|
|
|
|
2018-03-21 15:19:02 -04:00
|
|
|
if ((buffer[9U] & 0x08U) == 0x08U) {
|
|
|
|
LogMessage("Received end of transmission");
|
|
|
|
current = NULL;
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
|
|
|
watchdogTimer.stop();
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-03 10:12:03 -04:00
|
|
|
LogMessage("Data received from an unknown source");
|
2018-03-13 18:16:48 -04:00
|
|
|
CUtils::dump(2U, "Data", buffer, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_icomNetwork != NULL) {
|
|
|
|
len = m_icomNetwork->read(buffer);
|
2018-03-13 18:16:48 -04:00
|
|
|
if (len > 0U) {
|
|
|
|
if (current == NULL) {
|
2020-08-23 08:22:18 -04:00
|
|
|
if (active == ACTIVE_NONE) {
|
2018-03-13 18:16:48 -04:00
|
|
|
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && buffer[5U] == 0x01U) {
|
2018-03-26 15:21:32 -04:00
|
|
|
bool tempGrp = (buffer[7U] & 0x20U) == 0x20U;
|
|
|
|
unsigned short tempSrcId = (buffer[8U] << 8) | buffer[9U];
|
|
|
|
unsigned short tempDstId = (buffer[10U] << 8) | buffer[11U];
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-26 15:21:32 -04:00
|
|
|
if (tempGrp && tempDstId == tg) {
|
|
|
|
// Save the grp, src and dest for use in the NXDN Protocol messages
|
|
|
|
grp = tempGrp;
|
|
|
|
srcId = tempSrcId;
|
|
|
|
dstId = tempDstId;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-26 15:21:32 -04:00
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-08-22 09:26:44 -04:00
|
|
|
LogMessage("Transmission from %s on Icom Network to %s%u", callsign.c_str(), grp ? "TG " : "", dstId);
|
2018-03-26 15:21:32 -04:00
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_ICOM;
|
2018-05-17 15:32:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((buffer[0U] & 0xF0U) == 0x90U && buffer[2U] == 0x09U) {
|
|
|
|
bool tempGrp = (buffer[4U] & 0x20U) == 0x20U;
|
|
|
|
unsigned short tempSrcId = (buffer[5U] << 8) | buffer[6U];
|
|
|
|
unsigned short tempDstId = (buffer[7U] << 8) | buffer[8U];
|
|
|
|
|
|
|
|
if (tempGrp && tempDstId == tg) {
|
|
|
|
// Save the grp, src and dest for use in the NXDN Protocol messages
|
2021-08-24 13:46:27 -04:00
|
|
|
grp = tempGrp;
|
2018-05-17 15:32:00 -04:00
|
|
|
srcId = tempSrcId;
|
|
|
|
dstId = tempDstId;
|
|
|
|
|
|
|
|
std::string callsign = lookup->find(srcId);
|
2020-08-22 09:26:44 -04:00
|
|
|
LogMessage("Transmission from %s on Icom Network to %s%u", callsign.c_str(), grp ? "TG " : "", dstId);
|
2018-05-17 15:32:00 -04:00
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_ICOM;
|
2018-03-26 15:21:32 -04:00
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
if (active == ACTIVE_ICOM) {
|
2018-03-13 18:16:48 -04:00
|
|
|
watchdogTimer.start();
|
|
|
|
|
2021-02-13 10:34:18 -05:00
|
|
|
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it)
|
|
|
|
nxdnNetwork.write(buffer, len, srcId, dstId, grp, (*it)->m_addr, (*it)->m_addrLen);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_kenwoodNetwork != NULL)
|
|
|
|
m_kenwoodNetwork->write(buffer, len);
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && buffer[5U] == 0x08U) {
|
|
|
|
LogMessage("Received end of transmission");
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
2018-03-13 18:16:48 -04:00
|
|
|
watchdogTimer.stop();
|
|
|
|
}
|
2018-05-17 15:32:00 -04:00
|
|
|
if ((buffer[0U] & 0xF0U) == 0x90U && buffer[2U] == 0x08U) {
|
|
|
|
LogMessage("Received end of transmission");
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
2020-08-22 09:26:44 -04:00
|
|
|
watchdogTimer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_kenwoodNetwork != NULL) {
|
|
|
|
len = m_kenwoodNetwork->read(buffer);
|
|
|
|
if (len > 0U) {
|
|
|
|
if (current == NULL) {
|
2020-08-23 08:22:18 -04:00
|
|
|
if (active == ACTIVE_NONE) {
|
2020-08-22 09:26:44 -04:00
|
|
|
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && buffer[5U] == 0x01U) {
|
|
|
|
bool tempGrp = (buffer[7U] & 0x20U) == 0x20U;
|
|
|
|
unsigned short tempSrcId = (buffer[8U] << 8) | buffer[9U];
|
|
|
|
unsigned short tempDstId = (buffer[10U] << 8) | buffer[11U];
|
|
|
|
|
|
|
|
if (tempGrp && tempDstId == tg) {
|
|
|
|
// Save the grp, src and dest for use in the NXDN Protocol messages
|
2021-08-24 13:46:27 -04:00
|
|
|
grp = tempGrp;
|
2020-08-22 09:26:44 -04:00
|
|
|
srcId = tempSrcId;
|
|
|
|
dstId = tempDstId;
|
|
|
|
|
|
|
|
std::string callsign = lookup->find(srcId);
|
|
|
|
LogMessage("Transmission from %s on Kenwood Network to %s%u", callsign.c_str(), grp ? "TG " : "", dstId);
|
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_KENWOOD;
|
2020-08-22 09:26:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((buffer[0U] & 0xF0U) == 0x90U && buffer[2U] == 0x09U) {
|
|
|
|
bool tempGrp = (buffer[4U] & 0x20U) == 0x20U;
|
|
|
|
unsigned short tempSrcId = (buffer[5U] << 8) | buffer[6U];
|
|
|
|
unsigned short tempDstId = (buffer[7U] << 8) | buffer[8U];
|
|
|
|
|
|
|
|
if (tempGrp && tempDstId == tg) {
|
|
|
|
// Save the grp, src and dest for use in the NXDN Protocol messages
|
2021-08-24 13:46:27 -04:00
|
|
|
grp = tempGrp;
|
2020-08-22 09:26:44 -04:00
|
|
|
srcId = tempSrcId;
|
|
|
|
dstId = tempDstId;
|
|
|
|
|
|
|
|
std::string callsign = lookup->find(srcId);
|
|
|
|
LogMessage("Transmission from %s on Kenwood Network to %s%u", callsign.c_str(), grp ? "TG " : "", dstId);
|
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_KENWOOD;
|
2020-08-22 09:26:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:22:18 -04:00
|
|
|
if (active == ACTIVE_KENWOOD) {
|
2020-08-22 09:26:44 -04:00
|
|
|
watchdogTimer.start();
|
|
|
|
|
2021-02-13 10:34:18 -05:00
|
|
|
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it)
|
|
|
|
nxdnNetwork.write(buffer, len, srcId, dstId, grp, (*it)->m_addr, (*it)->m_addrLen);
|
2020-08-22 09:26:44 -04:00
|
|
|
|
|
|
|
if (m_icomNetwork != NULL)
|
|
|
|
m_icomNetwork->write(buffer, len);
|
|
|
|
|
|
|
|
if ((buffer[0U] == 0x81U || buffer[0U] == 0x83U) && buffer[5U] == 0x08U) {
|
|
|
|
LogMessage("Received end of transmission");
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
2020-08-22 09:26:44 -04:00
|
|
|
watchdogTimer.stop();
|
|
|
|
}
|
|
|
|
if ((buffer[0U] & 0xF0U) == 0x90U && buffer[2U] == 0x08U) {
|
|
|
|
LogMessage("Received end of transmission");
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
2018-05-17 15:32:00 -04:00
|
|
|
watchdogTimer.stop();
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ms = stopWatch.elapsed();
|
|
|
|
stopWatch.start();
|
|
|
|
|
|
|
|
// Remove any repeaters that haven't reported for a while
|
|
|
|
for (std::vector<CNXDNRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it)
|
|
|
|
(*it)->m_timer.clock(ms);
|
|
|
|
|
|
|
|
for (std::vector<CNXDNRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
|
2021-02-13 10:34:18 -05:00
|
|
|
if ((*it)->m_timer.hasExpired()) {
|
2020-11-24 07:51:39 -05:00
|
|
|
char buff[80U];
|
2021-02-13 10:34:18 -05:00
|
|
|
LogMessage("Removing %s (%s) disappeared", (*it)->m_callsign.c_str(),
|
|
|
|
CUDPSocket::display((*it)->m_addr, buff, 80U));
|
2020-11-24 07:51:39 -05:00
|
|
|
|
2021-02-13 10:34:18 -05:00
|
|
|
delete *it;
|
2021-08-24 13:46:27 -04:00
|
|
|
m_repeaters.erase(it);
|
2018-03-13 18:16:48 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watchdogTimer.clock(ms);
|
|
|
|
if (watchdogTimer.isRunning() && watchdogTimer.hasExpired()) {
|
|
|
|
LogMessage("Network watchdog has expired");
|
|
|
|
watchdogTimer.stop();
|
|
|
|
current = NULL;
|
2020-08-23 08:22:18 -04:00
|
|
|
active = ACTIVE_NONE;
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dumpTimer.clock(ms);
|
|
|
|
if (dumpTimer.hasExpired()) {
|
|
|
|
dumpRepeaters();
|
|
|
|
dumpTimer.start();
|
|
|
|
}
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_icomNetwork != NULL)
|
|
|
|
m_icomNetwork->clock(ms);
|
|
|
|
|
|
|
|
if (m_kenwoodNetwork != NULL)
|
|
|
|
m_kenwoodNetwork->clock(ms);
|
2020-04-20 11:02:10 -04:00
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
if (ms < 5U)
|
|
|
|
CThread::sleep(5U);
|
|
|
|
}
|
|
|
|
|
|
|
|
nxdnNetwork.close();
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
closeIcomNetwork();
|
|
|
|
|
|
|
|
closeKenwoodNetwork();
|
2018-03-13 18:16:48 -04:00
|
|
|
|
|
|
|
lookup->stop();
|
|
|
|
|
|
|
|
::LogFinalise();
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:12:03 -04:00
|
|
|
CNXDNRepeater* CNXDNReflector::findRepeater(const sockaddr_storage& addr) const
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
|
2020-09-03 10:12:03 -04:00
|
|
|
if (CUDPSocket::match(addr, (*it)->m_addr))
|
2018-03-13 18:16:48 -04:00
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNReflector::dumpRepeaters() const
|
|
|
|
{
|
|
|
|
if (m_repeaters.size() == 0U) {
|
|
|
|
LogMessage("No repeaters linked");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogMessage("Currently linked repeaters:");
|
|
|
|
|
|
|
|
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
|
2020-11-24 07:51:39 -05:00
|
|
|
char buffer[80U];
|
2021-02-13 10:34:18 -05:00
|
|
|
LogMessage(" %s: %s %u/%u", (*it)->m_callsign.c_str(),
|
|
|
|
CUDPSocket::display((*it)->m_addr, buffer, 80U),
|
|
|
|
(*it)->m_timer.getTimer(),
|
|
|
|
(*it)->m_timer.getTimeout());
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 15:19:02 -04:00
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
bool CNXDNReflector::openIcomNetwork()
|
2018-03-21 15:19:02 -04:00
|
|
|
{
|
2020-08-22 09:26:44 -04:00
|
|
|
m_icomNetwork = new CIcomNetwork(m_conf.getIcomAddress(), m_conf.getIcomDebug());
|
|
|
|
bool ret = m_icomNetwork->open();
|
2018-03-21 15:19:02 -04:00
|
|
|
if (!ret) {
|
2020-08-22 09:26:44 -04:00
|
|
|
delete m_icomNetwork;
|
|
|
|
m_icomNetwork = NULL;
|
2018-03-21 15:19:02 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-22 09:26:44 -04:00
|
|
|
bool CNXDNReflector::openKenwoodNetwork()
|
|
|
|
{
|
|
|
|
m_kenwoodNetwork = new CKenwoodNetwork(m_conf.getKenwoodAddress(), m_conf.getKenwoodDebug());
|
|
|
|
bool ret = m_kenwoodNetwork->open();
|
|
|
|
if (!ret) {
|
|
|
|
delete m_kenwoodNetwork;
|
|
|
|
m_kenwoodNetwork = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNReflector::closeIcomNetwork()
|
|
|
|
{
|
|
|
|
if (m_icomNetwork != NULL) {
|
|
|
|
m_icomNetwork->close();
|
|
|
|
delete m_icomNetwork;
|
|
|
|
m_icomNetwork = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNReflector::closeKenwoodNetwork()
|
2018-03-21 15:19:02 -04:00
|
|
|
{
|
2020-08-22 09:26:44 -04:00
|
|
|
if (m_kenwoodNetwork != NULL) {
|
|
|
|
m_kenwoodNetwork->close();
|
|
|
|
delete m_kenwoodNetwork;
|
|
|
|
m_kenwoodNetwork = NULL;
|
2018-03-21 15:19:02 -04:00
|
|
|
}
|
|
|
|
}
|