2018-03-13 18:16:48 -04:00
|
|
|
/*
|
2020-09-03 10:12:03 -04:00
|
|
|
* Copyright (C) 2016,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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Reflectors.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cctype>
|
|
|
|
|
2018-06-19 15:35:32 -04:00
|
|
|
CReflectors::CReflectors(const std::string& hostsFile1, const std::string& hostsFile2, unsigned int reloadTime) :
|
|
|
|
m_hostsFile1(hostsFile1),
|
|
|
|
m_hostsFile2(hostsFile2),
|
2018-03-13 18:16:48 -04:00
|
|
|
m_parrotAddress(),
|
|
|
|
m_parrotPort(0U),
|
|
|
|
m_reflectors(),
|
|
|
|
m_timer(1000U, reloadTime * 60U)
|
|
|
|
{
|
|
|
|
if (reloadTime > 0U)
|
|
|
|
m_timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
CReflectors::~CReflectors()
|
|
|
|
{
|
|
|
|
for (std::vector<CNXDNReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
|
|
|
|
delete *it;
|
|
|
|
|
|
|
|
m_reflectors.clear();
|
|
|
|
}
|
|
|
|
|
2021-04-25 01:48:10 -04:00
|
|
|
void CReflectors::setParrot(const std::string& address, unsigned short port)
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
m_parrotAddress = address;
|
|
|
|
m_parrotPort = port;
|
|
|
|
}
|
|
|
|
|
2021-04-25 01:48:10 -04:00
|
|
|
void CReflectors::setNXDN2DMR(const std::string& address, unsigned short port)
|
2018-03-31 21:30:38 -04:00
|
|
|
{
|
|
|
|
m_nxdn2dmrAddress = address;
|
|
|
|
m_nxdn2dmrPort = port;
|
|
|
|
}
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
bool CReflectors::load()
|
|
|
|
{
|
|
|
|
// Clear out the old reflector list
|
|
|
|
for (std::vector<CNXDNReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
|
|
|
|
delete *it;
|
|
|
|
|
|
|
|
m_reflectors.clear();
|
|
|
|
|
2018-06-19 15:35:32 -04:00
|
|
|
FILE* fp = ::fopen(m_hostsFile1.c_str(), "rt");
|
2018-03-13 18:16:48 -04:00
|
|
|
if (fp != NULL) {
|
|
|
|
char buffer[100U];
|
|
|
|
while (::fgets(buffer, 100U, fp) != NULL) {
|
|
|
|
if (buffer[0U] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char* p1 = ::strtok(buffer, " \t\r\n");
|
|
|
|
char* p2 = ::strtok(NULL, " \t\r\n");
|
|
|
|
char* p3 = ::strtok(NULL, " \t\r\n");
|
|
|
|
|
|
|
|
if (p1 != NULL && p2 != NULL && p3 != NULL) {
|
2020-09-03 10:12:03 -04:00
|
|
|
std::string host = std::string(p2);
|
2021-04-25 01:48:10 -04:00
|
|
|
unsigned short port = (unsigned short)::atoi(p3);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-09-06 08:45:06 -04:00
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
|
|
|
|
CNXDNReflector* refl = new CNXDNReflector;
|
|
|
|
refl->m_id = (unsigned short)::atoi(p1);
|
|
|
|
refl->m_addr = addr;
|
|
|
|
refl->m_addrLen = addrLen;
|
|
|
|
m_reflectors.push_back(refl);
|
|
|
|
} else {
|
|
|
|
LogWarning("Unable to resolve the address of %s", host.c_str());
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::fclose(fp);
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:35:32 -04:00
|
|
|
fp = ::fopen(m_hostsFile2.c_str(), "rt");
|
|
|
|
if (fp != NULL) {
|
|
|
|
char buffer[100U];
|
|
|
|
while (::fgets(buffer, 100U, fp) != NULL) {
|
|
|
|
if (buffer[0U] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char* p1 = ::strtok(buffer, " \t\r\n");
|
|
|
|
char* p2 = ::strtok(NULL, " \t\r\n");
|
|
|
|
char* p3 = ::strtok(NULL, " \t\r\n");
|
|
|
|
|
|
|
|
if (p1 != NULL && p2 != NULL && p3 != NULL) {
|
|
|
|
// Don't allow duplicate reflector ids from the secondary hosts file.
|
|
|
|
unsigned int id = (unsigned int)::atoi(p1);
|
|
|
|
if (find(id) == NULL) {
|
2020-09-03 10:12:03 -04:00
|
|
|
std::string host = std::string(p2);
|
2021-04-25 01:48:10 -04:00
|
|
|
unsigned short port = (unsigned short)::atoi(p3);
|
2018-06-19 15:35:32 -04:00
|
|
|
|
2020-09-06 08:45:06 -04:00
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
|
|
|
|
CNXDNReflector* refl = new CNXDNReflector;
|
|
|
|
refl->m_id = id;
|
|
|
|
refl->m_addr = addr;
|
|
|
|
refl->m_addrLen = addrLen;
|
|
|
|
m_reflectors.push_back(refl);
|
|
|
|
} else {
|
|
|
|
LogWarning("Unable to resolve the address of %s", host.c_str());
|
|
|
|
}
|
2018-06-19 15:35:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::fclose(fp);
|
|
|
|
}
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
size_t size = m_reflectors.size();
|
|
|
|
LogInfo("Loaded %u NXDN reflectors", size);
|
|
|
|
|
|
|
|
// Add the Parrot entry
|
|
|
|
if (m_parrotPort > 0U) {
|
2020-09-06 08:45:06 -04:00
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
if (CUDPSocket::lookup(m_parrotAddress, m_parrotPort, addr, addrLen) == 0) {
|
|
|
|
CNXDNReflector* refl = new CNXDNReflector;
|
|
|
|
refl->m_id = 10U;
|
|
|
|
refl->m_addr = addr;
|
|
|
|
refl->m_addrLen = addrLen;
|
|
|
|
m_reflectors.push_back(refl);
|
|
|
|
LogInfo("Loaded NXDN parrot (TG%u)", refl->m_id);
|
|
|
|
} else {
|
|
|
|
LogWarning("Unable to resolve the address of the NXDN Parrot");
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:30:38 -04:00
|
|
|
// Add the NXDN2DMR entry
|
|
|
|
if (m_nxdn2dmrPort > 0U) {
|
2020-09-06 08:45:06 -04:00
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
if (CUDPSocket::lookup(m_nxdn2dmrAddress, m_nxdn2dmrPort, addr, addrLen) == 0) {
|
|
|
|
CNXDNReflector* refl = new CNXDNReflector;
|
|
|
|
refl->m_id = 20U;
|
|
|
|
refl->m_addr = addr;
|
|
|
|
refl->m_addrLen = addrLen;
|
|
|
|
m_reflectors.push_back(refl);
|
|
|
|
LogInfo("Loaded NXDN2DMR (TG%u)", refl->m_id);
|
|
|
|
} else {
|
|
|
|
LogWarning("Unable to resolve the address of NXDN2DMR");
|
|
|
|
}
|
2018-03-31 21:30:38 -04:00
|
|
|
}
|
|
|
|
|
2018-03-13 18:16:48 -04:00
|
|
|
size = m_reflectors.size();
|
|
|
|
if (size == 0U)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-27 03:13:54 -04:00
|
|
|
CNXDNReflector* CReflectors::find(unsigned short id)
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
for (std::vector<CNXDNReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) {
|
|
|
|
if (id == (*it)->m_id)
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CReflectors::clock(unsigned int ms)
|
|
|
|
{
|
|
|
|
m_timer.clock(ms);
|
|
|
|
|
|
|
|
if (m_timer.isRunning() && m_timer.hasExpired()) {
|
|
|
|
load();
|
|
|
|
m_timer.start();
|
|
|
|
}
|
|
|
|
}
|