diff --git a/YSFGateway/Conf.cpp b/YSFGateway/Conf.cpp index 006bf17..198fc0e 100644 --- a/YSFGateway/Conf.cpp +++ b/YSFGateway/Conf.cpp @@ -57,7 +57,8 @@ m_aprsHostname(), m_aprsPort(0U), m_aprsPassword(), m_networkEnabled(false), -m_networkPort(0U), +m_networkDataPort(0U), +m_networkStatusPort(0U), m_networkHosts(), m_networkDebug(false) { @@ -152,8 +153,10 @@ bool CConf::read() } else if (section == SECTION_NETWORK) { if (::strcmp(key, "Enable") == 0) m_networkEnabled = ::atoi(value) == 1; - else if (::strcmp(key, "Port") == 0) - m_networkPort = (unsigned int)::atoi(value); + else if (::strcmp(key, "DataPort") == 0) + m_networkDataPort = (unsigned int)::atoi(value); + else if (::strcmp(key, "StatusPort") == 0) + m_networkStatusPort = (unsigned int)::atoi(value); else if (::strcmp(key, "Hosts") == 0) m_networkHosts = value; else if (::strcmp(key, "Debug") == 0) @@ -266,9 +269,14 @@ bool CConf::getNetworkEnabled() const return m_networkEnabled; } -unsigned int CConf::getNetworkPort() const +unsigned int CConf::getNetworkDataPort() const +{ + return m_networkDataPort; +} + +unsigned int CConf::getNetworkStatusPort() const { - return m_networkPort; + return m_networkStatusPort; } std::string CConf::getNetworkHosts() const diff --git a/YSFGateway/Conf.h b/YSFGateway/Conf.h index 052b3d1..f35bc02 100644 --- a/YSFGateway/Conf.h +++ b/YSFGateway/Conf.h @@ -59,7 +59,8 @@ public: // The Network section bool getNetworkEnabled() const; - unsigned int getNetworkPort() const; + unsigned int getNetworkDataPort() const; + unsigned int getNetworkStatusPort() const; std::string getNetworkHosts() const; bool getNetworkDebug() const; @@ -89,7 +90,8 @@ private: std::string m_aprsPassword; bool m_networkEnabled; - unsigned int m_networkPort; + unsigned int m_networkDataPort; + unsigned int m_networkStatusPort; std::string m_networkHosts; bool m_networkDebug; }; diff --git a/YSFGateway/Hosts.cpp b/YSFGateway/Hosts.cpp index c0d713b..ac267ff 100644 --- a/YSFGateway/Hosts.cpp +++ b/YSFGateway/Hosts.cpp @@ -24,7 +24,8 @@ #include CHosts::CHosts(const std::string& filename) : -m_filename(filename) +m_filename(filename), +m_hosts() { } @@ -47,24 +48,22 @@ bool CHosts::read() 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) { - std::string name = std::string(p1); - std::string address = std::string(p2); - unsigned int port = (unsigned int)::atoi(p3); + if (p1 != NULL && p2 != NULL) { + std::string address = std::string(p1); + unsigned int port = (unsigned int)::atoi(p2); CYSFHost* host = new CYSFHost; host->m_address = address; host->m_port = port; - m_table[p1] = host; + m_hosts.push_back(host); } } ::fclose(fp); - size_t size = m_table.size(); + size_t size = m_hosts.size(); if (size == 0U) return false; @@ -73,27 +72,7 @@ bool CHosts::read() return true; } -CYSFHost* CHosts::find(const std::string& name) const +std::vector& CHosts::list() { - CYSFHost* host = NULL; - - try { - host = m_table.at(name); - } catch (...) { - // Nothing to do - } - - return host; -} - -std::vector& CHosts::list() const -{ - std::vector hosts; - - for (std::map::const_iterator it = m_table.begin(); it != m_table.end(); ++it) { - std::pair t = *it; - hosts.push_back(t.first); - } - - return hosts; + return m_hosts; } diff --git a/YSFGateway/Hosts.h b/YSFGateway/Hosts.h index 8a2e8e0..2d76c14 100644 --- a/YSFGateway/Hosts.h +++ b/YSFGateway/Hosts.h @@ -21,7 +21,6 @@ #include #include -#include class CYSFHost { public: @@ -36,13 +35,11 @@ public: bool read(); - CYSFHost* find(const std::string& name) const; - - std::vector& list() const; + std::vector& list(); private: - std::string m_filename; - std::map m_table; + std::string m_filename; + std::vector m_hosts; }; #endif diff --git a/YSFGateway/Reflectors.cpp b/YSFGateway/Reflectors.cpp new file mode 100644 index 0000000..f0cdb74 --- /dev/null +++ b/YSFGateway/Reflectors.cpp @@ -0,0 +1,128 @@ +/* +* Copyright (C) 2016 by Jonathan Naylor G4KLX +* +* 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 "Hosts.h" +#include "Log.h" + +#include +#include +#include + +CReflectors::CReflectors(const std::string& hostsFile, unsigned int statusPort) : +m_hostsFile(hostsFile), +m_socket(statusPort), +m_reflectors(), +m_it(), +m_timer(1000U, 60U) +{ + assert(statusPort > 0U); +} + +CReflectors::~CReflectors() +{ +} + +bool CReflectors::load() +{ + bool ret = m_socket.open(); + if (!ret) + return false; + + CHosts hosts(m_hostsFile); + ret = hosts.read(); + if (!ret) + return false; + + std::vector& hostList = hosts.list(); + + for (std::vector::const_iterator it = hostList.begin(); it != hostList.end(); ++it) { + CYSFReflector* reflector = new CYSFReflector; + reflector->m_address = CUDPSocket::lookup((*it)->m_address); + reflector->m_port = (*it)->m_port; + m_reflectors.push_back(reflector); + } + + m_it = m_reflectors.begin(); + + m_timer.start(); + + return true; +} + +CYSFReflector* CReflectors::find(const std::string& id) +{ + for (std::vector::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) { + if (id == (*it)->m_id) + if ((*it)->m_timer.isRunning() && (*it)->m_timer.hasExpired()) { + LogDebug("Found id %s, but it has expired", id.c_str()); + return NULL; + } else { + return *it; + } + } + + return NULL; +} + +void CReflectors::clock(unsigned int ms) +{ + m_timer.clock(ms); + if (m_timer.isRunning() && m_timer.hasExpired()) { + m_socket.write((unsigned char*)"YSFS", 4U, (*m_it)->m_address, (*m_it)->m_port); + + ++m_it; + if (m_it == m_reflectors.end()) + m_it = m_reflectors.begin(); + + m_timer.start(); + } + + in_addr address; + unsigned int port; + unsigned char buffer[200U]; + int ret = m_socket.read(buffer, 200U, address, port); + + if (ret > 0) { + if (::memcmp(buffer + 0U, "YSFS", 4U) == 0) { + buffer[42U] = 0x00U; + + std::string id = std::string((char*)(buffer + 4U), 5U); + std::string name = std::string((char*)(buffer + 9U), 16U); + std::string desc = std::string((char*)(buffer + 25U), 14U); + unsigned int cnt = ::atoi((char*)(buffer + 39U)); + + for (std::vector::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) { + in_addr itAddr = (*it)->m_address; + unsigned int itPort = (*it)->m_port; + + if (itAddr.s_addr == address.s_addr && itPort == port) { + (*it)->m_id = id; + (*it)->m_name = name; + (*it)->m_desc = desc; + (*it)->m_count = cnt; + (*it)->m_timer.start(); + break; + } + } + } + } + + for (std::vector::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) + (*it)->m_timer.clock(ms); +} diff --git a/YSFGateway/Reflectors.h b/YSFGateway/Reflectors.h new file mode 100644 index 0000000..90d0915 --- /dev/null +++ b/YSFGateway/Reflectors.h @@ -0,0 +1,69 @@ +/* +* Copyright (C) 2016 by Jonathan Naylor G4KLX +* +* 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. +*/ + +#if !defined(Reflectors_H) +#define Reflectors_H + +#include "UDPSocket.h" +#include "Timer.h" +#include "Hosts.h" + +#include + +class CYSFReflector { +public: + CYSFReflector() : + m_id(), + m_name(), + m_desc(), + m_count(0U), + m_address(), + m_port(0U), + m_timer(1000U, 700U) + { + } + + std::string m_id; + std::string m_name; + std::string m_desc; + unsigned int m_count; + in_addr m_address; + unsigned int m_port; + CTimer m_timer; +}; + +class CReflectors { +public: + CReflectors(const std::string& hostsFile, unsigned int statusPort); + ~CReflectors(); + + bool load(); + + CYSFReflector* find(const std::string& id); + + void clock(unsigned int ms); + +private: + std::string m_hostsFile; + CUDPSocket m_socket; + std::vector m_reflectors; + std::vector ::iterator m_it; + CTimer m_timer; +}; + +#endif diff --git a/YSFGateway/WiresX.cpp b/YSFGateway/WiresX.cpp index 121837c..1d387bf 100644 --- a/YSFGateway/WiresX.cpp +++ b/YSFGateway/WiresX.cpp @@ -40,15 +40,22 @@ const unsigned char DEFAULT_FICH[] = {0x20U, 0x00U, 0x01U, 0x00U}; const unsigned char NET_HEADER[] = "YSFDGATEWAY ALL "; -CWiresX::CWiresX(CNetwork* network) : +CWiresX::CWiresX(CNetwork* network, const std::string& hostsFile, unsigned int statusPort) : m_network(network), -m_reflector(), +m_reflectors(hostsFile, statusPort), +m_reflector(NULL), +m_id(), +m_name(), +m_description(), +m_txFrequency(0U), +m_rxFrequency(0U), m_timer(1000U, 0U, 100U + 750U), m_seqNo(0U), m_csd1(NULL), m_status(WXSI_NONE) { assert(network != NULL); + assert(statusPort > 0U); m_csd1 = new unsigned char[20U]; } @@ -58,6 +65,42 @@ CWiresX::~CWiresX() delete[] m_csd1; } +void CWiresX::setInfo(const std::string& name, const std::string& description, unsigned int txFrequency, unsigned int rxFrequency) +{ + assert(txFrequency > 0U); + assert(rxFrequency > 0U); + + m_name = name; + m_description = description; + m_txFrequency = txFrequency; + m_rxFrequency = rxFrequency; + + unsigned int hash = 0U; + + for (unsigned int i = 0U; i < name.size(); i++) { + hash += name.at(i); + hash += (hash << 10); + hash ^= (hash >> 6); + } + + // Final avalanche + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + + char id[10U]; + ::sprintf(id, "%05u", hash % 100000U); + + LogInfo("The ID of this repeater is %s", id); + + m_id = std::string(id); +} + +bool CWiresX::start() +{ + return m_reflectors.load(); +} + WX_STATUS CWiresX::process(const unsigned char* data, unsigned char fi, unsigned char dt, unsigned char fn) { assert(data != NULL); @@ -99,8 +142,7 @@ WX_STATUS CWiresX::process(const unsigned char* data, unsigned char fi, unsigned processAll(); return WXS_NONE; } else if (::memcmp(buffer + 1U, CONN_REQ, 3U) == 0) { - processConnect(buffer + 5U); - return WXS_CONNECT; + return processConnect(buffer + 5U); } else if (::memcmp(buffer + 1U, DISC_REQ, 3U) == 0) { processDisconnect(); return WXS_DISCONNECT; @@ -113,7 +155,7 @@ WX_STATUS CWiresX::process(const unsigned char* data, unsigned char fi, unsigned return WXS_NONE; } -std::string CWiresX::getReflector() const +CYSFReflector* CWiresX::getReflector() const { return m_reflector; } @@ -132,14 +174,20 @@ void CWiresX::processAll() m_timer.start(); } -void CWiresX::processConnect(const unsigned char* data) +WX_STATUS CWiresX::processConnect(const unsigned char* data) { ::LogDebug("Received Connect to %5.5s from %10.10s", data + 5U, m_csd1 + 10U); - m_reflector = std::string((char*)(data + 5U), 5U); + std::string id = std::string((char*)(data + 4U), 5U); + + m_reflector = m_reflectors.find(id); + if (m_reflector == NULL) + return WXS_NONE; m_status = WXSI_CONNECT; m_timer.start(); + + return WXS_CONNECT; } void CWiresX::processDisconnect() diff --git a/YSFGateway/WiresX.h b/YSFGateway/WiresX.h index 352ad95..ce90e2b 100644 --- a/YSFGateway/WiresX.h +++ b/YSFGateway/WiresX.h @@ -19,6 +19,7 @@ #if !defined(WIRESX_H) #define WIRESX_H +#include "Reflectors.h" #include "Network.h" #include "Timer.h" @@ -40,24 +41,34 @@ enum WXSI_STATUS { class CWiresX { public: - CWiresX(CNetwork* network); + CWiresX(CNetwork* network, const std::string& hostsFile, unsigned int statusPort); ~CWiresX(); + void setInfo(const std::string& name, const std::string& description, unsigned int txFrequency, unsigned int rxFrequency); + + bool start(); + WX_STATUS process(const unsigned char* data, unsigned char fi, unsigned char dt, unsigned char fn); - std::string getReflector() const; + CYSFReflector* getReflector() const; void clock(unsigned int ms); private: CNetwork* m_network; - std::string m_reflector; + CReflectors m_reflectors; + CYSFReflector* m_reflector; + std::string m_id; + std::string m_name; + std::string m_description; + unsigned int m_txFrequency; + unsigned int m_rxFrequency; CTimer m_timer; unsigned char m_seqNo; unsigned char* m_csd1; WXSI_STATUS m_status; - void processConnect(const unsigned char* data); + WX_STATUS processConnect(const unsigned char* data); void processDisconnect(); void processDX(); void processAll(); diff --git a/YSFGateway/YSFGateway.cpp b/YSFGateway/YSFGateway.cpp index a061c25..1e9a1fc 100644 --- a/YSFGateway/YSFGateway.cpp +++ b/YSFGateway/YSFGateway.cpp @@ -17,6 +17,7 @@ */ #include "YSFGateway.h" +#include "Reflectors.h" #include "StopWatch.h" #include "Version.h" #include "YSFFICH.h" @@ -74,7 +75,6 @@ int main(int argc, char** argv) CYSFGateway::CYSFGateway(const std::string& configFile) : m_conf(configFile), m_gps(NULL), -m_hosts(NULL), m_wiresX(NULL), m_netNetwork(NULL), m_linked(false) @@ -157,13 +157,9 @@ int CYSFGateway::run() } #endif - std::string fileName = m_conf.getNetworkHosts(); - m_hosts = new CHosts(fileName); - m_hosts->read(); - bool debug = m_conf.getNetworkDebug(); unsigned int rptPort = m_conf.getPort(); - unsigned int netPort = m_conf.getNetworkPort(); + unsigned int netPort = m_conf.getNetworkDataPort(); CNetwork rptNetwork(rptPort, debug); m_netNetwork = new CNetwork(netPort, debug); @@ -181,9 +177,12 @@ int CYSFGateway::run() } bool networkEnabled = m_conf.getNetworkEnabled(); + if (networkEnabled) { + std::string fileName = m_conf.getNetworkHosts(); + unsigned int port = m_conf.getNetworkStatusPort(); - if (networkEnabled) - m_wiresX = new CWiresX(&rptNetwork); + m_wiresX = new CWiresX(&rptNetwork, fileName, port); + } CStopWatch stopWatch; stopWatch.start(); @@ -211,8 +210,12 @@ int CYSFGateway::run() if (m_wiresX != NULL) { WX_STATUS status = m_wiresX->process(buffer + 35U, fi, dt, fn); switch (status) { - case WXS_CONNECT: - connect(buffer + 14U); + case WXS_CONNECT: { + CYSFReflector* reflector = m_wiresX->getReflector(); + LogMessage("Connect to %05u has been requested by %10.10s", reflector->m_id, buffer + 14U); + m_netNetwork->setDestination(reflector->m_address, reflector->m_port); + m_linked = true; + } break; case WXS_DISCONNECT: LogMessage("Disconnect has been requested by %10.10s", buffer + 14U); @@ -270,7 +273,6 @@ int CYSFGateway::run() m_netNetwork->close(); delete m_gps; - delete m_hosts; delete m_netNetwork; delete m_wiresX; @@ -290,26 +292,3 @@ void CYSFGateway::createGPS() m_gps = new CGPS(hostname, port, password); } - -bool CYSFGateway::connect(const unsigned char* source) -{ - std::string reflector = m_wiresX->getReflector(); - - CYSFHost* host = m_hosts->find(reflector); - if (host == NULL) { - LogMessage("Request made for invalid reflector %s by %10.10s", reflector.c_str(), source); - return false; - } - - std::string address = host->m_address; - unsigned int port = host->m_port; - - in_addr addr = CUDPSocket::lookup(address); - if (addr.s_addr == INADDR_NONE) - return false; - - LogMessage("Connect to %s has been requested by %10.10s", reflector.c_str(), source); - - m_netNetwork->setDestination(addr, port); - m_linked = true; -} diff --git a/YSFGateway/YSFGateway.h b/YSFGateway/YSFGateway.h index 648623b..0233502 100644 --- a/YSFGateway/YSFGateway.h +++ b/YSFGateway/YSFGateway.h @@ -21,7 +21,6 @@ #include "Network.h" #include "WiresX.h" -#include "Hosts.h" #include "Conf.h" #include "GPS.h" @@ -38,13 +37,11 @@ public: private: CConf m_conf; CGPS* m_gps; - CHosts* m_hosts; CWiresX* m_wiresX; CNetwork* m_netNetwork; bool m_linked; void createGPS(); - bool connect(const unsigned char* source); }; #endif diff --git a/YSFGateway/YSFGateway.ini b/YSFGateway/YSFGateway.ini index f3593ef..d2a5b33 100644 --- a/YSFGateway/YSFGateway.ini +++ b/YSFGateway/YSFGateway.ini @@ -30,6 +30,7 @@ Password=9999 [Network] Enable=1 -Port=32767 +DataPort=42000 +StatusPort=42001 Hosts=./YSFHosts.txt Debug=0 diff --git a/YSFGateway/YSFGateway.vcxproj b/YSFGateway/YSFGateway.vcxproj index f567fd3..d2442ec 100644 --- a/YSFGateway/YSFGateway.vcxproj +++ b/YSFGateway/YSFGateway.vcxproj @@ -154,6 +154,7 @@ + @@ -179,6 +180,7 @@ + diff --git a/YSFGateway/YSFGateway.vcxproj.filters b/YSFGateway/YSFGateway.vcxproj.filters index c26c43a..caec1ea 100644 --- a/YSFGateway/YSFGateway.vcxproj.filters +++ b/YSFGateway/YSFGateway.vcxproj.filters @@ -80,6 +80,9 @@ Header Files + + Header Files + @@ -142,5 +145,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/YSFGateway/YSFHosts.txt b/YSFGateway/YSFHosts.txt index 3423d98..cffe7f4 100644 --- a/YSFGateway/YSFHosts.txt +++ b/YSFGateway/YSFHosts.txt @@ -10,3 +10,4 @@ c4fm.sustrai.org 42000 ysf.ve2mrc.com 42000 ysf.hamlabs.no 42000 ysf007.spain-dmr.es 42000 +ysfreflector.dvswitch.org 42000