diff --git a/Conf.cpp b/Conf.cpp index a454a6e..99ba846 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -60,7 +60,6 @@ enum SECTION { SECTION_OLED, SECTION_LCDPROC, SECTION_LOCK_FILE, - SECTION_GPSD, SECTION_REMOTE_CONTROL }; @@ -75,12 +74,6 @@ m_daemon(false), m_rxFrequency(0U), m_txFrequency(0U), m_power(0U), -m_latitude(0.0F), -m_longitude(0.0F), -m_height(0), -m_location(), -m_description(), -m_url(), m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), @@ -309,10 +302,8 @@ m_lcdprocUTC(false), m_lcdprocDimOnIdle(false), m_lockFileEnabled(false), m_lockFileName(), -m_gpsdEnabled(false), -m_gpsdAddress(), -m_gpsdPort(), m_remoteControlEnabled(false), +m_remoteControlAddress("127.0.0.1"), m_remoteControlPort(0U) { } @@ -399,8 +390,6 @@ bool CConf::read() section = SECTION_LCDPROC; else if (::strncmp(buffer, "[Lock File]", 11U) == 0) section = SECTION_LOCK_FILE; - else if (::strncmp(buffer, "[GPSD]", 6U) == 0) - section = SECTION_GPSD; else if (::strncmp(buffer, "[Remote Control]", 16U) == 0) section = SECTION_REMOTE_CONTROL; else @@ -457,18 +446,6 @@ bool CConf::read() m_rxFrequency = (unsigned int)::atoi(value); else if (::strcmp(key, "Power") == 0) m_power = (unsigned int)::atoi(value); - else if (::strcmp(key, "Latitude") == 0) - m_latitude = float(::atof(value)); - else if (::strcmp(key, "Longitude") == 0) - m_longitude = float(::atof(value)); - else if (::strcmp(key, "Height") == 0) - m_height = ::atoi(value); - else if (::strcmp(key, "Location") == 0) - m_location = value; - else if (::strcmp(key, "Description") == 0) - m_description = value; - else if (::strcmp(key, "URL") == 0) - m_url = value; } else if (section == SECTION_LOG) { if (::strcmp(key, "FilePath") == 0) m_logFilePath = value; @@ -1033,13 +1010,6 @@ bool CConf::read() m_lockFileEnabled = ::atoi(value) == 1; else if (::strcmp(key, "File") == 0) m_lockFileName = value; - } else if (section == SECTION_GPSD) { - if (::strcmp(key, "Enable") == 0) - m_gpsdEnabled = ::atoi(value) == 1; - else if (::strcmp(key, "Address") == 0) - m_gpsdAddress = value; - else if (::strcmp(key, "Port") == 0) - m_gpsdPort = value; } else if (section == SECTION_REMOTE_CONTROL) { if (::strcmp(key, "Enable") == 0) m_remoteControlEnabled = ::atoi(value) == 1; @@ -1098,36 +1068,6 @@ unsigned int CConf::getPower() const return m_power; } -float CConf::getLatitude() const -{ - return m_latitude; -} - -float CConf::getLongitude() const -{ - return m_longitude; -} - -int CConf::getHeight() const -{ - return m_height; -} - -std::string CConf::getLocation() const -{ - return m_location; -} - -std::string CConf::getDescription() const -{ - return m_description; -} - -std::string CConf::getURL() const -{ - return m_url; -} - unsigned int CConf::getLogDisplayLevel() const { return m_logDisplayLevel; @@ -2268,26 +2208,16 @@ std::string CConf::getLockFileName() const return m_lockFileName; } -bool CConf::getGPSDEnabled() const -{ - return m_gpsdEnabled; -} - -std::string CConf::getGPSDAddress() const -{ - return m_gpsdAddress; -} - -std::string CConf::getGPSDPort() const -{ - return m_gpsdPort; -} - bool CConf::getRemoteControlEnabled() const { return m_remoteControlEnabled; } +std::string CConf::getRemoteControlAddress() const +{ + return m_remoteControlAddress; +} + unsigned int CConf::getRemoteControlPort() const { return m_remoteControlPort; diff --git a/Conf.h b/Conf.h index 30663ff..9e2302c 100644 --- a/Conf.h +++ b/Conf.h @@ -42,12 +42,6 @@ public: unsigned int getRXFrequency() const; unsigned int getTXFrequency() const; unsigned int getPower() const; - float getLatitude() const; - float getLongitude() const; - int getHeight() const; - std::string getLocation() const; - std::string getDescription() const; - std::string getURL() const; // The Log section unsigned int getLogDisplayLevel() const; @@ -335,13 +329,9 @@ public: bool getLockFileEnabled() const; std::string getLockFileName() const; - // The GPSD section - bool getGPSDEnabled() const; - std::string getGPSDAddress() const; - std::string getGPSDPort() const; - // The Remote Control section bool getRemoteControlEnabled() const; + std::string getRemoteControlAddress() const; unsigned int getRemoteControlPort() const; private: @@ -356,12 +346,6 @@ private: unsigned int m_rxFrequency; unsigned int m_txFrequency; unsigned int m_power; - float m_latitude; - float m_longitude; - int m_height; - std::string m_location; - std::string m_description; - std::string m_url; unsigned int m_logDisplayLevel; unsigned int m_logFileLevel; @@ -620,11 +604,8 @@ private: bool m_lockFileEnabled; std::string m_lockFileName; - bool m_gpsdEnabled; - std::string m_gpsdAddress; - std::string m_gpsdPort; - bool m_remoteControlEnabled; + std::string m_remoteControlAddress; unsigned int m_remoteControlPort; }; diff --git a/DMRNetwork.cpp b/DMRNetwork.cpp index fece49c..b21b179 100644 --- a/DMRNetwork.cpp +++ b/DMRNetwork.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2019 by Jonathan Naylor G4KLX + * Copyright (C) 2015-2020 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 @@ -19,7 +19,6 @@ #include "DMRNetwork.h" #include "StopWatch.h" -#include "SHA256.h" #include "Utils.h" #include "Log.h" @@ -33,12 +32,12 @@ const unsigned int BUFFER_LENGTH = 500U; const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U; -CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType) : +CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType) : m_addressStr(address), m_address(), +m_addrlen(), m_port(port), m_id(NULL), -m_password(password), m_duplex(duplex), m_version(version), m_debug(debug), @@ -47,37 +46,25 @@ m_enabled(false), m_slot1(slot1), m_slot2(slot2), m_hwType(hwType), -m_status(WAITING_CONNECT), -m_retryTimer(1000U, 10U), -m_timeoutTimer(1000U, 60U), m_buffer(NULL), -m_salt(NULL), m_streamId(NULL), m_rxData(1000U, "DMR Network"), -m_options(), +m_beacon(false), +m_random(), m_callsign(), m_rxFrequency(0U), m_txFrequency(0U), m_power(0U), m_colorCode(0U), -m_latitude(0.0F), -m_longitude(0.0F), -m_height(0), -m_location(), -m_description(), -m_url(), -m_beacon(false), -m_random() +m_pingTimer(1000U, 10U) { assert(!address.empty()); assert(port > 0U); assert(id > 1000U); - assert(!password.empty()); - m_address = CUDPSocket::lookup(address); + CUDPSocket::lookup(m_addressStr, m_port, m_address, m_addrlen); m_buffer = new unsigned char[BUFFER_LENGTH]; - m_salt = new unsigned char[sizeof(uint32_t)]; m_id = new uint8_t[4U]; m_streamId = new uint32_t[2U]; @@ -98,43 +85,31 @@ m_random() CDMRNetwork::~CDMRNetwork() { delete[] m_buffer; - delete[] m_salt; delete[] m_streamId; delete[] m_id; } -void CDMRNetwork::setOptions(const std::string& options) -{ - m_options = options; -} - -void CDMRNetwork::setConfig(const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, unsigned int power, unsigned int colorCode, float latitude, float longitude, int height, const std::string& location, const std::string& description, const std::string& url) +void CDMRNetwork::setConfig(const std::string & callsign, unsigned int rxFrequency, unsigned int txFrequency, unsigned int power, unsigned int colorCode) { m_callsign = callsign; m_rxFrequency = rxFrequency; m_txFrequency = txFrequency; m_power = power; m_colorCode = colorCode; - m_latitude = latitude; - m_longitude = longitude; - m_height = height; - m_location = location; - m_description = description; - m_url = url; } bool CDMRNetwork::open() { LogMessage("DMR, Opening DMR Network"); - if (m_address.s_addr == INADDR_NONE) - m_address = CUDPSocket::lookup(m_addressStr); + if (CUDPSocket::isnone(m_address)) + CUDPSocket::lookup(m_addressStr, m_port, m_address, m_addrlen); - m_status = WAITING_CONNECT; - m_timeoutTimer.stop(); - m_retryTimer.start(); + bool ret = m_socket.open(); + if (ret) + m_pingTimer.start(); - return true; + return ret; } void CDMRNetwork::enable(bool enabled) @@ -147,9 +122,6 @@ void CDMRNetwork::enable(bool enabled) bool CDMRNetwork::read(CDMRData& data) { - if (m_status != RUNNING) - return false; - if (m_rxData.isEmpty()) return false; @@ -211,9 +183,6 @@ bool CDMRNetwork::read(CDMRData& data) bool CDMRNetwork::write(const CDMRData& data) { - if (m_status != RUNNING) - return false; - unsigned char buffer[HOMEBREW_DATA_PACKET_LENGTH]; ::memset(buffer, 0x00U, HOMEBREW_DATA_PACKET_LENGTH); @@ -275,9 +244,6 @@ bool CDMRNetwork::write(const CDMRData& data) buffer[54U] = data.getRSSI(); - if (m_debug) - CUtils::dump(1U, "Network Transmitted", buffer, HOMEBREW_DATA_PACKET_LENGTH); - write(buffer, HOMEBREW_DATA_PACKET_LENGTH); return true; @@ -285,264 +251,79 @@ bool CDMRNetwork::write(const CDMRData& data) bool CDMRNetwork::writeRadioPosition(unsigned int id, const unsigned char* data) { - if (m_status != RUNNING) - return false; - unsigned char buffer[20U]; ::memcpy(buffer + 0U, "DMRG", 4U); - ::memcpy(buffer + 4U, m_id, 4U); + buffer[4U] = id >> 16; + buffer[5U] = id >> 8; + buffer[6U] = id >> 0; - buffer[8U] = id >> 16; - buffer[9U] = id >> 8; - buffer[10U] = id >> 0; + ::memcpy(buffer + 7U, data + 2U, 7U); - ::memcpy(buffer + 11U, data + 2U, 7U); - - return write(buffer, 18U); + return write(buffer, 14U); } bool CDMRNetwork::writeTalkerAlias(unsigned int id, unsigned char type, const unsigned char* data) { - if (m_status != RUNNING) - return false; - unsigned char buffer[20U]; ::memcpy(buffer + 0U, "DMRA", 4U); - ::memcpy(buffer + 4U, m_id, 4U); + buffer[4U] = id >> 16; + buffer[5U] = id >> 8; + buffer[6U] = id >> 0; - buffer[8U] = id >> 16; - buffer[9U] = id >> 8; - buffer[10U] = id >> 0; + buffer[7U] = type; - buffer[11U] = type; + ::memcpy(buffer + 8U, data + 2U, 7U); - ::memcpy(buffer + 12U, data + 2U, 7U); - - return write(buffer, 19U); -} - -bool CDMRNetwork::writeHomePosition(float latitude, float longitude) -{ - m_latitude = latitude; - m_longitude = longitude; - - if (m_status != RUNNING) - return false; - - char buffer[50U]; - - ::memcpy(buffer + 0U, "RPTG", 4U); - ::memcpy(buffer + 4U, m_id, 4U); - - ::sprintf(buffer + 8U, "%08f%09f", latitude, longitude); - - return write((unsigned char*)buffer, 25U); + return write(buffer, 15U); } void CDMRNetwork::close() { LogMessage("DMR, Closing DMR Network"); - if (m_status == RUNNING) { - unsigned char buffer[9U]; - ::memcpy(buffer + 0U, "RPTCL", 5U); - ::memcpy(buffer + 5U, m_id, 4U); - write(buffer, 9U); - } - m_socket.close(); - - m_retryTimer.stop(); - m_timeoutTimer.stop(); } void CDMRNetwork::clock(unsigned int ms) { - if (m_status == WAITING_CONNECT) { - m_retryTimer.clock(ms); - if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) { - bool ret = m_socket.open(); - if (ret) { - ret = writeLogin(); - if (!ret) - return; + m_pingTimer.clock(ms); + if (m_pingTimer.isRunning() && m_pingTimer.hasExpired()) { + writeConfig(); + m_pingTimer.start(); + } - m_status = WAITING_LOGIN; - m_timeoutTimer.start(); - } - - m_retryTimer.start(); - } + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0) + return; + if (!CUDPSocket::match(m_address, address)) { + LogMessage("DMR, packet received from an invalid source"); return; } - in_addr address; - unsigned int port; - int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, port); - if (length < 0) { - LogError("DMR, Socket has failed, retrying connection to the master"); - close(); - open(); + if (!m_enabled) return; + + if (m_debug) + CUtils::dump(1U, "Network Received", m_buffer, length); + + if (::memcmp(m_buffer, "DMRD", 4U) == 0) { + unsigned char len = length; + m_rxData.addData(&len, 1U); + m_rxData.addData(m_buffer, len); + } else if (::memcmp(m_buffer, "DMRP", 4U) == 0) { + ; + } else if (::memcmp(m_buffer, "DMRB", 4U) == 0) { + m_beacon = true; + } else { + CUtils::dump("DMR, unknown packet from the DMR Gateway", m_buffer, length); } - - // if (m_debug && length > 0) - // CUtils::dump(1U, "Network Received", m_buffer, length); - - if (length > 0 && m_address.s_addr == address.s_addr && m_port == port) { - if (::memcmp(m_buffer, "DMRD", 4U) == 0) { - if (m_enabled) { - if (m_debug) - CUtils::dump(1U, "Network Received", m_buffer, length); - - unsigned char len = length; - m_rxData.addData(&len, 1U); - m_rxData.addData(m_buffer, len); - } - } else if (::memcmp(m_buffer, "MSTNAK", 6U) == 0) { - if (m_status == RUNNING) { - LogWarning("DMR, Login to the master has failed, retrying login ..."); - m_status = WAITING_LOGIN; - m_timeoutTimer.start(); - m_retryTimer.start(); - } else { - /* Once the modem death spiral has been prevented in Modem.cpp - the Network sometimes times out and reaches here. - We want it to reconnect so... */ - LogError("DMR, Login to the master has failed, retrying network ..."); - close(); - open(); - return; - } - } else if (::memcmp(m_buffer, "RPTACK", 6U) == 0) { - switch (m_status) { - case WAITING_LOGIN: - LogDebug("DMR, Sending authorisation"); - ::memcpy(m_salt, m_buffer + 6U, sizeof(uint32_t)); - writeAuthorisation(); - m_status = WAITING_AUTHORISATION; - m_timeoutTimer.start(); - m_retryTimer.start(); - break; - case WAITING_AUTHORISATION: - LogDebug("DMR, Sending configuration"); - writeConfig(); - m_status = WAITING_CONFIG; - m_timeoutTimer.start(); - m_retryTimer.start(); - break; - case WAITING_CONFIG: - if (m_options.empty()) { - LogMessage("DMR, Logged into the master successfully"); - m_status = RUNNING; - } else { - LogDebug("DMR, Sending options"); - writeOptions(); - m_status = WAITING_OPTIONS; - } - m_timeoutTimer.start(); - m_retryTimer.start(); - break; - case WAITING_OPTIONS: - LogMessage("DMR, Logged into the master successfully"); - m_status = RUNNING; - m_timeoutTimer.start(); - m_retryTimer.start(); - break; - default: - break; - } - } else if (::memcmp(m_buffer, "MSTCL", 5U) == 0) { - LogError("DMR, Master is closing down"); - close(); - open(); - } else if (::memcmp(m_buffer, "MSTPONG", 7U) == 0) { - m_timeoutTimer.start(); - } else if (::memcmp(m_buffer, "RPTSBKN", 7U) == 0) { - m_beacon = true; - } else { - CUtils::dump("Unknown packet from the master", m_buffer, length); - } - } - - m_retryTimer.clock(ms); - if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) { - switch (m_status) { - case WAITING_LOGIN: - writeLogin(); - break; - case WAITING_AUTHORISATION: - writeAuthorisation(); - break; - case WAITING_OPTIONS: - writeOptions(); - break; - case WAITING_CONFIG: - writeConfig(); - break; - case RUNNING: - writePing(); - break; - default: - break; - } - - m_retryTimer.start(); - } - - m_timeoutTimer.clock(ms); - if (m_timeoutTimer.isRunning() && m_timeoutTimer.hasExpired()) { - LogError("DMR, Connection to the master has timed out, retrying connection"); - close(); - open(); - } -} - -bool CDMRNetwork::writeLogin() -{ - unsigned char buffer[8U]; - - ::memcpy(buffer + 0U, "RPTL", 4U); - ::memcpy(buffer + 4U, m_id, 4U); - - return write(buffer, 8U); -} - -bool CDMRNetwork::writeAuthorisation() -{ - size_t size = m_password.size(); - - unsigned char* in = new unsigned char[size + sizeof(uint32_t)]; - ::memcpy(in, m_salt, sizeof(uint32_t)); - for (size_t i = 0U; i < size; i++) - in[i + sizeof(uint32_t)] = m_password.at(i); - - unsigned char out[40U]; - ::memcpy(out + 0U, "RPTK", 4U); - ::memcpy(out + 4U, m_id, 4U); - - CSHA256 sha256; - sha256.buffer(in, (unsigned int)(size + sizeof(uint32_t)), out + 8U); - - delete[] in; - - return write(out, 40U); -} - -bool CDMRNetwork::writeOptions() -{ - char buffer[300U]; - - ::memcpy(buffer + 0U, "RPTO", 4U); - ::memcpy(buffer + 4U, m_id, 4U); - ::strcpy(buffer + 8U, m_options.c_str()); - - return write((unsigned char*)buffer, (unsigned int)m_options.length() + 8U); } bool CDMRNetwork::writeConfig() @@ -614,40 +395,19 @@ bool CDMRNetwork::writeConfig() } } - char buffer[400U]; - - ::memcpy(buffer + 0U, "RPTC", 4U); - ::memcpy(buffer + 4U, m_id, 4U); - - char latitude[20U]; - ::sprintf(latitude, "%08f", m_latitude); - - char longitude[20U]; - ::sprintf(longitude, "%09f", m_longitude); - unsigned int power = m_power; if (power > 99U) power = 99U; - int height = m_height; - if (height > 999) - height = 999; + char buffer[150U]; - ::sprintf(buffer + 8U, "%-8.8s%09u%09u%02u%02u%8.8s%9.9s%03d%-20.20s%-19.19s%c%-124.124s%-40.40s%-40.40s", m_callsign.c_str(), - m_rxFrequency, m_txFrequency, power, m_colorCode, latitude, longitude, height, m_location.c_str(), - m_description.c_str(), slots, m_url.c_str(), m_version, software); + ::memcpy(buffer + 0U, "DMRC", 4U); + ::memcpy(buffer + 4U, m_id, 4U); + ::sprintf(buffer + 8U, "%-8.8s%09u%09u%02u%02u%c%-40.40s%-40.40s", + m_callsign.c_str(), m_rxFrequency, m_txFrequency, power, m_colorCode, slots, m_version, + software); - return write((unsigned char*)buffer, 302U); -} - -bool CDMRNetwork::writePing() -{ - unsigned char buffer[11U]; - - ::memcpy(buffer + 0U, "RPTPING", 7U); - ::memcpy(buffer + 7U, m_id, 4U); - - return write(buffer, 11U); + return write((unsigned char*)buffer, 119U); } bool CDMRNetwork::wantsBeacon() @@ -664,14 +424,12 @@ bool CDMRNetwork::write(const unsigned char* data, unsigned int length) assert(data != NULL); assert(length > 0U); - // if (m_debug) - // CUtils::dump(1U, "Network Transmitted", data, length); + if (m_debug) + CUtils::dump(1U, "Network Transmitted", data, length); - bool ret = m_socket.write(data, length, m_address, m_port); + bool ret = m_socket.write(data, length, m_address, m_addrlen); if (!ret) { - LogError("DMR, Socket has failed when writing data to the master, retrying connection"); - m_socket.close(); - open(); + LogError("DMR, socket error when writing to the DMR Gateway"); return false; } diff --git a/DMRNetwork.h b/DMRNetwork.h index 754c970..0f1368a 100644 --- a/DMRNetwork.h +++ b/DMRNetwork.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2017,2018,2020 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 @@ -32,12 +32,10 @@ class CDMRNetwork { public: - CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType); + CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType); ~CDMRNetwork(); - void setOptions(const std::string& options); - - void setConfig(const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, unsigned int power, unsigned int colorCode, float latitude, float longitude, int height, const std::string& location, const std::string& description, const std::string& url); + void setConfig(const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, unsigned int power, unsigned int colorCode); bool open(); @@ -51,8 +49,6 @@ public: bool writeTalkerAlias(unsigned int id, unsigned char type, const unsigned char* data); - bool writeHomePosition(float latitude, float longitude); - bool wantsBeacon(); void clock(unsigned int ms); @@ -61,10 +57,10 @@ public: private: std::string m_addressStr; - in_addr m_address; + sockaddr_storage m_address; + unsigned int m_addrlen; unsigned int m_port; uint8_t* m_id; - std::string m_password; bool m_duplex; const char* m_version; bool m_debug; @@ -73,47 +69,19 @@ private: bool m_slot1; bool m_slot2; HW_TYPE m_hwType; - - enum STATUS { - WAITING_CONNECT, - WAITING_LOGIN, - WAITING_AUTHORISATION, - WAITING_CONFIG, - WAITING_OPTIONS, - RUNNING - }; - - STATUS m_status; - CTimer m_retryTimer; - CTimer m_timeoutTimer; - unsigned char* m_buffer; - unsigned char* m_salt; - uint32_t* m_streamId; - + unsigned char* m_buffer; + uint32_t* m_streamId; CRingBuffer m_rxData; + bool m_beacon; + std::mt19937 m_random; + std::string m_callsign; + unsigned int m_rxFrequency; + unsigned int m_txFrequency; + unsigned int m_power; + unsigned int m_colorCode; + CTimer m_pingTimer; - std::string m_options; - - std::string m_callsign; - unsigned int m_rxFrequency; - unsigned int m_txFrequency; - unsigned int m_power; - unsigned int m_colorCode; - float m_latitude; - float m_longitude; - int m_height; - std::string m_location; - std::string m_description; - std::string m_url; - - bool m_beacon; - std::mt19937 m_random; - - bool writeLogin(); - bool writeAuthorisation(); - bool writeOptions(); bool writeConfig(); - bool writePing(); bool write(const unsigned char* data, unsigned int length); }; diff --git a/DStarNetwork.cpp b/DStarNetwork.cpp index d13d773..551a1c5 100644 --- a/DStarNetwork.cpp +++ b/DStarNetwork.cpp @@ -33,7 +33,7 @@ const unsigned int BUFFER_LENGTH = 100U; CDStarNetwork::CDStarNetwork(const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int localPort, bool duplex, const char* version, bool debug) : m_socket(localPort), m_address(), -m_port(gatewayPort), +m_addrlen(), m_duplex(duplex), m_version(version), m_debug(debug), @@ -47,7 +47,7 @@ m_linkStatus(LS_NONE), m_linkReflector(NULL), m_random() { - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen); m_linkReflector = new unsigned char[DSTAR_LONG_CALLSIGN_LENGTH]; @@ -65,12 +65,12 @@ bool CDStarNetwork::open() { LogMessage("Opening D-Star network connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_address)) return false; m_pollTimer.start(); - return m_socket.open(); + return m_socket.open(m_address.ss_family); } bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length, bool busy) @@ -103,7 +103,7 @@ bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length CUtils::dump(1U, "D-Star Network Header Sent", buffer, 49U); for (unsigned int i = 0U; i < 2U; i++) { - bool ret = m_socket.write(buffer, 49U, m_address, m_port); + bool ret = m_socket.write(buffer, 49U, m_address, m_addrlen); if (!ret) return false; } @@ -146,7 +146,7 @@ bool CDStarNetwork::writeData(const unsigned char* data, unsigned int length, un if (m_debug) CUtils::dump(1U, "D-Star Network Data Sent", buffer, length + 9U); - return m_socket.write(buffer, length + 9U, m_address, m_port); + return m_socket.write(buffer, length + 9U, m_address, m_addrlen); } bool CDStarNetwork::writePoll(const char* text) @@ -170,7 +170,7 @@ bool CDStarNetwork::writePoll(const char* text) // if (m_debug) // CUtils::dump(1U, "D-Star Network Poll Sent", buffer, 6U + length); - return m_socket.write(buffer, 6U + length, m_address, m_port); + return m_socket.write(buffer, 6U + length, m_address, m_addrlen); } void CDStarNetwork::clock(unsigned int ms) @@ -195,18 +195,12 @@ void CDStarNetwork::clock(unsigned int ms) unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match(m_address, address)) return; - // Check if the data is for us - if (m_address.s_addr != address.s_addr || m_port != port) { - LogMessage("D-Star packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); - return; - } - // Invalid packet type? if (::memcmp(buffer, "DSRP", 4U) != 0) return; diff --git a/DStarNetwork.h b/DStarNetwork.h index 991c031..fc1fd68 100644 --- a/DStarNetwork.h +++ b/DStarNetwork.h @@ -52,8 +52,8 @@ public: private: CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; + sockaddr_storage m_address; + unsigned int m_addrlen; bool m_duplex; const char* m_version; bool m_debug; diff --git a/Display.cpp b/Display.cpp index cded1ed..23f75c3 100644 --- a/Display.cpp +++ b/Display.cpp @@ -556,14 +556,14 @@ CDisplay* CDisplay::createDisplay(const CConf& conf, CUMP* ump, IModem* modem) if (port == "modem") { ISerialPort* serial = new IModemSerialPort(modem); - display = new CNextion(conf.getCallsign(), dmrid, serial, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF, conf.getLocation()); + display = new CNextion(conf.getCallsign(), dmrid, serial, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF); } else if (port == "ump") { if (ump != NULL) { - display = new CNextion(conf.getCallsign(), dmrid, ump, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF, conf.getLocation()); - } else { - LogInfo(" NullDisplay loaded"); - display = new CNullDisplay; - } + display = new CNextion(conf.getCallsign(), dmrid, ump, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF); + } else { + LogInfo(" NullDisplay loaded"); + display = new CNullDisplay; + } } else { unsigned int baudrate = 9600U; if (screenLayout == 4U) @@ -571,7 +571,7 @@ CDisplay* CDisplay::createDisplay(const CConf& conf, CUMP* ump, IModem* modem) LogInfo(" Display baudrate: %u ", baudrate); ISerialPort* serial = new CSerialController(port, baudrate); - display = new CNextion(conf.getCallsign(), dmrid, serial, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF, conf.getLocation()); + display = new CNextion(conf.getCallsign(), dmrid, serial, brightness, displayClock, utc, idleBrightness, screenLayout, txFrequency, rxFrequency, displayTempInF); } } else if (type == "LCDproc") { std::string address = conf.getLCDprocAddress(); diff --git a/FMNetwork.cpp b/FMNetwork.cpp index fd13df4..590a793 100644 --- a/FMNetwork.cpp +++ b/FMNetwork.cpp @@ -29,8 +29,8 @@ const unsigned int BUFFER_LENGTH = 500U; CFMNetwork::CFMNetwork(const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int sampleRate, bool debug) : m_socket(localAddress, localPort), -m_address(), -m_port(gatewayPort), +m_addr(), +m_addrLen(), m_sampleRate(sampleRate), m_debug(debug), m_enabled(false), @@ -41,7 +41,7 @@ m_pollTimer(1000U, 5U) assert(!gatewayAddress.empty()); assert(sampleRate > 0U); - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_addr, m_addrLen); int error; m_incoming = ::src_new(SRC_SINC_FASTEST, 1, &error); @@ -61,9 +61,6 @@ bool CFMNetwork::open() { LogMessage("Opening FM network connection"); - if (m_address.s_addr == INADDR_NONE) - return false; - m_pollTimer.start(); return m_socket.open(); @@ -115,7 +112,7 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples) if (m_debug) CUtils::dump(1U, "FM Network Data Sent", buffer, length); - return m_socket.write(buffer, length, m_address, m_port); + return m_socket.write(buffer, length, m_addr, m_addrLen); } bool CFMNetwork::writeEOT() @@ -130,7 +127,7 @@ bool CFMNetwork::writeEOT() if (m_debug) CUtils::dump(1U, "FM Network End of Transmission Sent", buffer, 3U); - return m_socket.write(buffer, 3U, m_address, m_port); + return m_socket.write(buffer, 3U, m_addr, m_addrLen); } void CFMNetwork::clock(unsigned int ms) @@ -143,15 +140,15 @@ void CFMNetwork::clock(unsigned int ms) unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); + sockaddr_storage addr; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrlen); if (length <= 0) return; // Check if the data is for us - if (m_address.s_addr != address.s_addr || port != m_port) { - LogMessage("FM packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); + if (!CUDPSocket::match(addr, m_addr)) { + LogMessage("FM packet received from an invalid source"); return; } @@ -263,5 +260,6 @@ bool CFMNetwork::writePoll() if (m_debug) CUtils::dump(1U, "FM Network Poll Sent", buffer, 3U); - return m_socket.write(buffer, 3U, m_address, m_port); + return m_socket.write(buffer, 3U, m_addr, m_addrLen); } + diff --git a/FMNetwork.h b/FMNetwork.h index f0d5d2a..c4314fd 100644 --- a/FMNetwork.h +++ b/FMNetwork.h @@ -50,16 +50,16 @@ public: void clock(unsigned int ms); private: - CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; - unsigned int m_sampleRate; - bool m_debug; - bool m_enabled; + CUDPSocket m_socket; + sockaddr_storage m_addr; + unsigned int m_addrLen; + unsigned int m_sampleRate; + bool m_debug; + bool m_enabled; CRingBuffer m_buffer; - CTimer m_pollTimer; - SRC_STATE* m_incoming; - SRC_STATE* m_outgoing; + CTimer m_pollTimer; + SRC_STATE* m_incoming; + SRC_STATE* m_outgoing; bool writePoll(); }; diff --git a/GPSD.cpp b/GPSD.cpp deleted file mode 100644 index c06321a..0000000 --- a/GPSD.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2018,2020 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 "GPSD.h" - -#if defined(USE_GPSD) - -#include -#include -#include -#include - -CGPSD::CGPSD(const std::string& address, const std::string& port, CDMRNetwork* network) : -m_gpsdAddress(address), -m_gpsdPort(port), -m_network(network), -m_gpsdData(), -m_idTimer(1000U, 60U) -{ - assert(!address.empty()); - assert(!port.empty()); - assert(network != NULL); -} - -CGPSD::~CGPSD() -{ -} - -bool CGPSD::open() -{ - int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData); - if (ret != 0) { - LogError("Error when opening access to gpsd - %d - %s", errno, ::gps_errstr(errno)); - return false; - } - - ::gps_stream(&m_gpsdData, WATCH_ENABLE | WATCH_JSON, NULL); - - LogMessage("Connected to GPSD"); - - m_idTimer.start(); - - return true; -} - -void CGPSD::clock(unsigned int ms) -{ - m_idTimer.clock(ms); - - if (m_idTimer.hasExpired()) { - sendReport(); - m_idTimer.start(); - } -} - -void CGPSD::close() -{ - ::gps_stream(&m_gpsdData, WATCH_DISABLE, NULL); - ::gps_close(&m_gpsdData); -} - -void CGPSD::sendReport() -{ - if (!::gps_waiting(&m_gpsdData, 0)) - return; - -#if GPSD_API_MAJOR_VERSION >= 7 - if (::gps_read(&m_gpsdData, NULL, 0) <= 0) - return; -#else - if (::gps_read(&m_gpsdData) <= 0) - return; -#endif - - if (m_gpsdData.status != STATUS_FIX) - return; - - bool latlonSet = (m_gpsdData.set & LATLON_SET) == LATLON_SET; - if (!latlonSet) - return; - - float latitude = float(m_gpsdData.fix.latitude); - float longitude = float(m_gpsdData.fix.longitude); - - m_network->writeHomePosition(latitude, longitude); -} - -#endif diff --git a/GPSD.h b/GPSD.h deleted file mode 100644 index cef93b5..0000000 --- a/GPSD.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2018,2020 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. - */ - -#ifndef GPSD_H -#define GPSD_H - -#if defined(USE_GPSD) - -#include "DMRNetwork.h" -#include "Timer.h" - -#include - -#include - -class CGPSD { -public: - CGPSD(const std::string& address, const std::string& port, CDMRNetwork* network); - ~CGPSD(); - - bool open(); - - void clock(unsigned int ms); - - void close(); - -private: - std::string m_gpsdAddress; - std::string m_gpsdPort; - CDMRNetwork* m_network; - struct gps_data_t m_gpsdData; - CTimer m_idTimer; - - void sendReport(); -}; - -#endif - -#endif diff --git a/LCDproc.cpp b/LCDproc.cpp index c043813..5f072e9 100644 --- a/LCDproc.cpp +++ b/LCDproc.cpp @@ -121,44 +121,53 @@ CLCDproc::~CLCDproc() bool CLCDproc::open() { - const char *server; - unsigned int port, localPort; - struct sockaddr_in serverAddress, clientAddress; - struct hostent *h; + int err; + unsigned int addrlen; + std::string port, localPort; + struct sockaddr_storage serverAddress, clientAddress; + struct addrinfo hints, *res; - server = m_address.c_str(); - port = m_port; - localPort = m_localPort; + port = std::to_string(m_port); + localPort = std::to_string(m_localPort); + memset(&hints, 0, sizeof(hints)); + /* Lookup the hostname address */ + hints.ai_flags = AI_NUMERICSERV; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo(m_address.c_str(), port.c_str(), &hints, &res); + if (err) { + LogError("LCDproc, cannot lookup server"); + return false; + } + memcpy(&serverAddress, res->ai_addr, addrlen = res->ai_addrlen); + freeaddrinfo(res); + + /* Lookup the client address (random port - need to specify manual port from ini file) */ + hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE; + hints.ai_family = serverAddress.ss_family; + err = getaddrinfo(NULL, localPort.c_str(), &hints, &res); + if (err) { + LogError("LCDproc, cannot lookup client"); + return false; + } + memcpy(&clientAddress, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); /* Create TCP socket */ - m_socketfd = socket(AF_INET, SOCK_STREAM, 0); + m_socketfd = socket(clientAddress.ss_family, SOCK_STREAM, 0); if (m_socketfd == -1) { LogError("LCDproc, failed to create socket"); return false; } - /* Sets client address (random port - need to specify manual port from ini file?) */ - clientAddress.sin_family = AF_INET; - clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); - //clientAddress.sin_port = htons(0); - clientAddress.sin_port = htons(localPort); - /* Bind the address to the socket */ - if (bind(m_socketfd, (struct sockaddr *)&clientAddress, sizeof(clientAddress)) == -1) { + if (bind(m_socketfd, (struct sockaddr *)&clientAddress, addrlen) == -1) { LogError("LCDproc, error whilst binding address"); return false; } - /* Lookup the hostname address */ - h = gethostbyname(server); - - /* Sets server address */ - serverAddress.sin_family = h->h_addrtype; - memcpy((char*)&serverAddress.sin_addr.s_addr, h->h_addr_list[0], h->h_length); - serverAddress.sin_port = htons(port); - - if (connect(m_socketfd, (struct sockaddr * )&serverAddress, sizeof(serverAddress))==-1) { + /* Connect to server */ + if (connect(m_socketfd, (struct sockaddr *)&serverAddress, addrlen) == -1) { LogError("LCDproc, cannot connect to server"); return false; } diff --git a/MMDVM.ini b/MMDVM.ini index ca21105..1561541 100644 --- a/MMDVM.ini +++ b/MMDVM.ini @@ -13,12 +13,6 @@ Daemon=0 RXFrequency=435000000 TXFrequency=435000000 Power=1 -Latitude=0.0 -Longitude=0.0 -Height=0 -Location=Nowhere -Description=Multi-Mode Repeater -URL=www.google.co.uk [Log] # Logging levels, 0=No logging @@ -202,10 +196,8 @@ Debug=0 Enable=1 Address=44.131.4.1 Port=62031 +Local=62032 Jitter=360 -# Local=62032 -Password=PASSWORD -# Options= Slot1=1 Slot2=1 # ModeHang=3 @@ -319,11 +311,7 @@ UTC=0 Enable=0 File=/tmp/MMDVM_Active.lck -[GPSD] -Enable=0 -Address=127.0.0.1 -Port=2947 - [Remote Control] Enable=0 +Address=127.0.0.1 Port=7642 diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index fc74ff3..70de9d9 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -170,9 +170,6 @@ m_id(0U), m_cwCallsign(), m_lockFileEnabled(false), m_lockFileName(), -#if defined(USE_GPSD) -m_gpsd(NULL), -#endif m_remoteControl(NULL), m_fixedMode(false) { @@ -345,8 +342,8 @@ int CMMDVMHost::run() return 1; } - in_addr transparentAddress; - unsigned int transparentPort = 0U; + sockaddr_storage transparentAddress; + unsigned int transparentAddrLen; CUDPSocket* transparentSocket = NULL; unsigned int sendFrameType = 0U; @@ -362,11 +359,10 @@ int CMMDVMHost::run() LogInfo(" Local Port: %u", localPort); LogInfo(" Send Frame Type: %u", sendFrameType); - transparentAddress = CUDPSocket::lookup(remoteAddress); - transparentPort = remotePort; + CUDPSocket::lookup(remoteAddress, remotePort, transparentAddress, transparentAddrLen); transparentSocket = new CUDPSocket(localPort); - ret = transparentSocket->open(); + ret = transparentSocket->open(transparentAddress.ss_family); if (!ret) { LogWarning("Could not open the Transparent data socket, disabling"); delete transparentSocket; @@ -657,12 +653,14 @@ int CMMDVMHost::run() bool remoteControlEnabled = m_conf.getRemoteControlEnabled(); if (remoteControlEnabled) { + std::string address = m_conf.getRemoteControlAddress(); unsigned int port = m_conf.getRemoteControlPort(); LogInfo("Remote Control Parameters"); + LogInfo(" Address: %s", address.c_str()); LogInfo(" Port: %u", port); - m_remoteControl = new CRemoteControl(port); + m_remoteControl = new CRemoteControl(address, port); ret = m_remoteControl->open(); if (!ret) { @@ -869,7 +867,7 @@ int CMMDVMHost::run() len = m_modem->readTransparentData(data); if (transparentSocket != NULL && len > 0U) - transparentSocket->write(data, len, transparentAddress, transparentPort); + transparentSocket->write(data, len, transparentAddress, transparentAddrLen); if (!m_fixedMode) { if (m_modeTimer.isRunning() && m_modeTimer.hasExpired()) @@ -1052,9 +1050,9 @@ int CMMDVMHost::run() } if (transparentSocket != NULL) { - in_addr address; - unsigned int port = 0U; - len = transparentSocket->read(data, 200U, address, port); + sockaddr_storage address; + unsigned int addrlen; + len = transparentSocket->read(data, 200U, address, addrlen); if (len > 0U) m_modem->writeTransparentData(data, len); } @@ -1101,11 +1099,6 @@ int CMMDVMHost::run() if (m_fmNetwork != NULL) m_fmNetwork->clock(ms); -#if defined(USE_GPSD) - if (m_gpsd != NULL) - m_gpsd->clock(ms); -#endif - m_cwIdTimer.clock(ms); if (m_cwIdTimer.isRunning() && m_cwIdTimer.hasExpired()) { if (!m_modem->hasTX()){ @@ -1181,13 +1174,6 @@ int CMMDVMHost::run() m_display->close(); delete m_display; -#if defined(USE_GPSD) - if (m_gpsd != NULL) { - m_gpsd->close(); - delete m_gpsd; - } -#endif - if (m_ump != NULL) { m_ump->close(); delete m_ump; @@ -1500,38 +1486,20 @@ bool CMMDVMHost::createDMRNetwork() LogInfo(" Slot 2: %s", slot2 ? "enabled" : "disabled"); LogInfo(" Mode Hang: %us", m_dmrNetModeHang); - m_dmrNetwork = new CDMRNetwork(address, port, local, id, password, m_duplex, VERSION, debug, slot1, slot2, hwType); - - std::string options = m_conf.getDMRNetworkOptions(); - if (!options.empty()) { - LogInfo(" Options: %s", options.c_str()); - m_dmrNetwork->setOptions(options); - } + m_dmrNetwork = new CDMRNetwork(address, port, local, id, m_duplex, VERSION, debug, slot1, slot2, hwType); unsigned int rxFrequency = m_conf.getRXFrequency(); unsigned int txFrequency = m_conf.getTXFrequency(); unsigned int power = m_conf.getPower(); unsigned int colorCode = m_conf.getDMRColorCode(); - float latitude = m_conf.getLatitude(); - float longitude = m_conf.getLongitude(); - int height = m_conf.getHeight(); - std::string location = m_conf.getLocation(); - std::string description = m_conf.getDescription(); - std::string url = m_conf.getURL(); LogInfo("Info Parameters"); LogInfo(" Callsign: %s", m_callsign.c_str()); LogInfo(" RX Frequency: %uHz", rxFrequency); LogInfo(" TX Frequency: %uHz", txFrequency); LogInfo(" Power: %uW", power); - LogInfo(" Latitude: %fdeg N", latitude); - LogInfo(" Longitude: %fdeg E", longitude); - LogInfo(" Height: %um", height); - LogInfo(" Location: \"%s\"", location.c_str()); - LogInfo(" Description: \"%s\"", description.c_str()); - LogInfo(" URL: \"%s\"", url.c_str()); - m_dmrNetwork->setConfig(m_callsign, rxFrequency, txFrequency, power, colorCode, latitude, longitude, height, location, description, url); + m_dmrNetwork->setConfig(m_callsign, rxFrequency, txFrequency, power, colorCode); bool ret = m_dmrNetwork->open(); if (!ret) { @@ -1540,26 +1508,6 @@ bool CMMDVMHost::createDMRNetwork() return false; } -#if defined(USE_GPSD) - bool gpsdEnabled = m_conf.getGPSDEnabled(); - if (gpsdEnabled) { - std::string gpsdAddress = m_conf.getGPSDAddress(); - std::string gpsdPort = m_conf.getGPSDPort(); - - LogInfo("GPSD Parameters"); - LogInfo(" Address: %s", gpsdAddress.c_str()); - LogInfo(" Port: %s", gpsdPort.c_str()); - - m_gpsd = new CGPSD(gpsdAddress, gpsdPort, m_dmrNetwork); - - ret = m_gpsd->open(); - if (!ret) { - delete m_gpsd; - m_gpsd = NULL; - } - } -#endif - m_dmrNetwork->enable(true); return true; @@ -1567,12 +1515,12 @@ bool CMMDVMHost::createDMRNetwork() bool CMMDVMHost::createYSFNetwork() { - std::string myAddress = m_conf.getFusionNetworkMyAddress(); - unsigned int myPort = m_conf.getFusionNetworkMyPort(); + std::string myAddress = m_conf.getFusionNetworkMyAddress(); + unsigned int myPort = m_conf.getFusionNetworkMyPort(); std::string gatewayAddress = m_conf.getFusionNetworkGatewayAddress(); unsigned int gatewayPort = m_conf.getFusionNetworkGatewayPort(); - m_ysfNetModeHang = m_conf.getFusionNetworkModeHang(); - bool debug = m_conf.getFusionNetworkDebug(); + m_ysfNetModeHang = m_conf.getFusionNetworkModeHang(); + bool debug = m_conf.getFusionNetworkDebug(); LogInfo("System Fusion Network Parameters"); LogInfo(" Local Address: %s", myAddress.c_str()); diff --git a/MMDVMHost.h b/MMDVMHost.h index 9cb28cb..4f23a39 100644 --- a/MMDVMHost.h +++ b/MMDVMHost.h @@ -42,13 +42,11 @@ #include "Timer.h" #include "Modem.h" #include "Conf.h" -#include "GPSD.h" #include "UMP.h" #include - class CMMDVMHost { public: @@ -113,9 +111,6 @@ private: std::string m_cwCallsign; bool m_lockFileEnabled; std::string m_lockFileName; -#if defined(USE_GPSD) - CGPSD* m_gpsd; -#endif CRemoteControl* m_remoteControl; bool m_fixedMode; diff --git a/MMDVMHost.vcxproj b/MMDVMHost.vcxproj index 96f4c96..f857633 100644 --- a/MMDVMHost.vcxproj +++ b/MMDVMHost.vcxproj @@ -188,7 +188,6 @@ - @@ -238,7 +237,6 @@ - @@ -292,7 +290,6 @@ - @@ -337,7 +334,6 @@ - diff --git a/MMDVMHost.vcxproj.filters b/MMDVMHost.vcxproj.filters index 0757e74..e17fa03 100644 --- a/MMDVMHost.vcxproj.filters +++ b/MMDVMHost.vcxproj.filters @@ -77,9 +77,6 @@ Header Files - - Header Files - Header Files @@ -320,9 +317,6 @@ Header Files - - Header Files - Header Files @@ -382,9 +376,6 @@ Source Files - - Source Files - Source Files @@ -607,11 +598,8 @@ Source Files - - Source Files - Source Files - \ No newline at end of file + diff --git a/Makefile b/Makefile index df54350..809fc7c 100644 --- a/Makefile +++ b/Makefile @@ -2,26 +2,19 @@ CC = cc CXX = c++ - -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread LIBS = -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPSD -std=c++0x -pthread -#LIBS = -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/Makefile.Pi b/Makefile.Pi index 3b14a13..2d77806 100644 --- a/Makefile.Pi +++ b/Makefile.Pi @@ -2,25 +2,19 @@ CC = gcc CXX = g++ -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread -DRASPBERRY_PI -I/usr/local/include LIBS = -lwiringPi -lwiringPiDev -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPSD -std=c++0x -pthread -DRASPBERRY_PI -I/usr/local/include -#LIBS = -lwiringPi -lwiringPiDev -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g -L/usr/local/lib OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/Makefile.Pi.Adafruit b/Makefile.Pi.Adafruit index 149d9ba..a87a1b2 100644 --- a/Makefile.Pi.Adafruit +++ b/Makefile.Pi.Adafruit @@ -3,25 +3,19 @@ CC = gcc CXX = g++ -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread -DHD44780 -DADAFRUIT_DISPLAY -I/usr/local/include LIBS = -lwiringPi -lwiringPiDev -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPSD -std=c++0x -pthread -DHD44780 -DADAFRUIT_DISPLAY -I/usr/local/include -#LIBS = -lwiringPi -lwiringPiDev -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g -L/usr/local/lib OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o HD44780.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o HD44780.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/Makefile.Pi.HD44780 b/Makefile.Pi.HD44780 index 85044ef..e8814e8 100644 --- a/Makefile.Pi.HD44780 +++ b/Makefile.Pi.HD44780 @@ -2,26 +2,18 @@ CC = gcc CXX = g++ - -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread -DHD44780 -I/usr/local/include -LIBS = -lwiringPi -lwiringPiDev -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPSD -std=c++0x -pthread -DHD44780 -I/usr/local/include -#LIBS = -lwiringPi -lwiringPiDev -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g -L/usr/local/lib OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o HD44780.o I2CController.o IIDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o HD44780.o I2CController.o IIDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/Makefile.Pi.OLED b/Makefile.Pi.OLED index 0a3d7bb..9e260ac 100644 --- a/Makefile.Pi.OLED +++ b/Makefile.Pi.OLED @@ -2,26 +2,19 @@ CC = gcc CXX = g++ - -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread -DOLED -I/usr/local/include LIBS = -lArduiPi_OLED -lwiringPi -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPS -std=c++0x -pthread -DOLED -I/usr/local/include -#LIBS = -lArduiPi_OLED -lwiringPi -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g -L/usr/local/lib OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o I2CController.o IIRDirectForm1Filter.o OLED.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o I2CController.o IIRDirectForm1Filter.o OLED.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/Makefile.Pi.PCF8574 b/Makefile.Pi.PCF8574 index deda9e3..637dc9d 100644 --- a/Makefile.Pi.PCF8574 +++ b/Makefile.Pi.PCF8574 @@ -3,26 +3,19 @@ CC = gcc CXX = g++ - -# Use the following CFLAGS and LIBS if you don't want to use gpsd. CFLAGS = -g -O3 -Wall -std=c++0x -pthread -DHD44780 -DPCF8574_DISPLAY -I/usr/local/include LIBS = -lwiringPi -lwiringPiDev -lpthread -lutil -lsamplerate - -# Use the following CFLAGS and LIBS if you do want to use gpsd. -#CFLAGS = -g -O3 -Wall -DUSE_GPSD -std=c++0x -pthread -DHD44780 -DPCF8574_DISPLAY -I/usr/local/include -#LIBS = -lwiringPi -lwiringPiDev -lpthread -lgps -lutil -lsamplerate - LDFLAGS = -g -L/usr/local/lib OBJECTS = AMBEFEC.o AX25Control.o AX25Network.o BCH.o BPTC19696.o CASTInfo.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o \ DMRDataHeader.o DMREMB.o DMREmbeddedData.o DMRFullLC.o DMRLookup.o DMRLC.o DMRNetwork.o DMRShortLC.o DMRSlot.o DMRSlotType.o \ DMRAccessControl.o DMRTA.o DMRTrellis.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o FMControl.o FMNetwork.o \ - Golay2087.o Golay24128.o GPSD.o Hamming.o HD44780.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ + Golay2087.o Golay24128.o Hamming.o HD44780.o I2CController.o IIRDirectForm1Filter.o LCDproc.o Log.o MMDVMHost.o Modem.o \ ModemSerialPort.o Mutex.o NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o \ NXDNCRC.o NXDNFACCH1.o NXDNIcomNetwork.o NXDNKenwoodNetwork.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o NXDNSACCH.o \ NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \ PseudoTTYController.o POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o \ - SerialController.o SerialModem.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ + SerialController.o SerialModem.o SerialPort.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \ UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o all: MMDVMHost RemoteCommand diff --git a/NXDNIcomNetwork.cpp b/NXDNIcomNetwork.cpp index 2f3767b..3b340cc 100644 --- a/NXDNIcomNetwork.cpp +++ b/NXDNIcomNetwork.cpp @@ -31,7 +31,7 @@ const unsigned int BUFFER_LENGTH = 200U; CNXDNIcomNetwork::CNXDNIcomNetwork(const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) : m_socket(localAddress, localPort), m_address(), -m_port(gatewayPort), +m_addrlen(), m_debug(debug), m_enabled(false), m_buffer(1000U, "NXDN Network") @@ -39,7 +39,7 @@ m_buffer(1000U, "NXDN Network") assert(gatewayPort > 0U); assert(!gatewayAddress.empty()); - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen); } CNXDNIcomNetwork::~CNXDNIcomNetwork() @@ -50,7 +50,7 @@ bool CNXDNIcomNetwork::open() { LogMessage("Opening NXDN network connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_address)) return false; return m_socket.open(); @@ -100,25 +100,19 @@ bool CNXDNIcomNetwork::write(const unsigned char* data, NXDN_NETWORK_MESSAGE_TYP if (m_debug) CUtils::dump(1U, "NXDN Network Data Sent", buffer, 102U); - return m_socket.write(buffer, 102U, m_address, m_port); + return m_socket.write(buffer, 102U, m_address, m_addrlen); } void CNXDNIcomNetwork::clock(unsigned int ms) { unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match(m_address, address)) return; - // Check if the data is for us - if (m_address.s_addr != address.s_addr || port != m_port) { - LogMessage("NXDN packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); - return; - } - // Invalid packet type? if (::memcmp(buffer, "ICOM", 4U) != 0) return; diff --git a/NXDNIcomNetwork.h b/NXDNIcomNetwork.h index 05fb023..18bdd97 100644 --- a/NXDNIcomNetwork.h +++ b/NXDNIcomNetwork.h @@ -49,8 +49,8 @@ public: private: CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; + sockaddr_storage m_address; + unsigned int m_addrlen; bool m_debug; bool m_enabled; CRingBuffer m_buffer; diff --git a/NXDNKenwoodNetwork.cpp b/NXDNKenwoodNetwork.cpp index e873bd3..f84af6e 100644 --- a/NXDNKenwoodNetwork.cpp +++ b/NXDNKenwoodNetwork.cpp @@ -36,9 +36,9 @@ const unsigned int BUFFER_LENGTH = 200U; CNXDNKenwoodNetwork::CNXDNKenwoodNetwork(const std::string& localAddress, unsigned int localPort, const std::string& gwyAddress, unsigned int gwyPort, bool debug) : m_rtpSocket(localAddress, localPort + 0U), m_rtcpSocket(localAddress, localPort + 1U), -m_address(), -m_rtcpPort(gwyPort + 1U), -m_rtpPort(gwyPort + 0U), +m_rtcpaddress(), +m_rtpaddress(), +m_addrlen(), m_enabled(false), m_headerSeen(false), m_seen1(false), @@ -65,7 +65,8 @@ m_random() m_sacch = new unsigned char[10U]; - m_address = CUDPSocket::lookup(gwyAddress); + CUDPSocket::lookup(gwyAddress, gwyPort + 1, m_rtcpaddress, m_addrlen); + CUDPSocket::lookup(gwyAddress, gwyPort, m_rtpaddress, m_addrlen); std::random_device rd; std::mt19937 mt(rd()); @@ -81,7 +82,8 @@ bool CNXDNKenwoodNetwork::open() { LogMessage("Opening Kenwood connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_rtpaddress) || + CUDPSocket::isnone(m_rtcpaddress)) return false; if (!m_rtcpSocket.open()) @@ -363,7 +365,7 @@ bool CNXDNKenwoodNetwork::writeRTPVoiceHeader(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U); - return m_rtpSocket.write(buffer, 47U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 47U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data) @@ -409,7 +411,7 @@ bool CNXDNKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U); - return m_rtpSocket.write(buffer, 47U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 47U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTPVoiceData(const unsigned char* data) @@ -455,7 +457,7 @@ bool CNXDNKenwoodNetwork::writeRTPVoiceData(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 59U); - return m_rtpSocket.write(buffer, 59U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 59U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTPDataHeader(const unsigned char* data) @@ -497,7 +499,7 @@ bool CNXDNKenwoodNetwork::writeRTPDataHeader(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 42U); - return m_rtpSocket.write(buffer, 42U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 42U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTPDataTrailer(const unsigned char* data) @@ -539,7 +541,7 @@ bool CNXDNKenwoodNetwork::writeRTPDataTrailer(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 42U); - return m_rtpSocket.write(buffer, 42U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 42U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTPDataData(const unsigned char* data) @@ -581,7 +583,7 @@ bool CNXDNKenwoodNetwork::writeRTPDataData(const unsigned char* data) if (m_debug) CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 42U); - return m_rtpSocket.write(buffer, 42U, m_address, m_rtpPort); + return m_rtpSocket.write(buffer, 42U, m_rtpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTCPStart() @@ -641,7 +643,7 @@ bool CNXDNKenwoodNetwork::writeRTCPStart() if (m_debug) CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U); - return m_rtcpSocket.write(buffer, 28U, m_address, m_rtcpPort); + return m_rtcpSocket.write(buffer, 28U, m_rtcpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTCPPing() @@ -683,7 +685,7 @@ bool CNXDNKenwoodNetwork::writeRTCPPing() if (m_debug) CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U); - return m_rtcpSocket.write(buffer, 28U, m_address, m_rtcpPort); + return m_rtcpSocket.write(buffer, 28U, m_rtcpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::writeRTCPHang(unsigned char type, unsigned short src, unsigned short dst) @@ -726,7 +728,7 @@ bool CNXDNKenwoodNetwork::writeRTCPHang() if (m_debug) CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 20U); - return m_rtcpSocket.write(buffer, 20U, m_address, m_rtcpPort); + return m_rtcpSocket.write(buffer, 20U, m_rtcpaddress, m_addrlen); } bool CNXDNKenwoodNetwork::read(unsigned char* data) @@ -761,18 +763,12 @@ unsigned int CNXDNKenwoodNetwork::readRTP(unsigned char* data) unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match_addr(m_rtpaddress, address)) return 0U; - // Check if the data is for us - if (m_address.s_addr != address.s_addr) { - LogMessage("Kenwood RTP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr); - return 0U; - } - if (!m_enabled) return 0U; @@ -790,18 +786,12 @@ unsigned int CNXDNKenwoodNetwork::readRTCP(unsigned char* data) unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match_addr(m_rtcpaddress, address)) return 0U; - // Check if the data is for us - if (m_address.s_addr != address.s_addr) { - LogMessage("Kenwood RTCP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr); - return 0U; - } - if (!m_enabled) return 0U; diff --git a/NXDNKenwoodNetwork.h b/NXDNKenwoodNetwork.h index 6645c23..3466569 100644 --- a/NXDNKenwoodNetwork.h +++ b/NXDNKenwoodNetwork.h @@ -49,9 +49,9 @@ public: private: CUDPSocket m_rtpSocket; CUDPSocket m_rtcpSocket; - in_addr m_address; - unsigned int m_rtcpPort; - unsigned int m_rtpPort; + sockaddr_storage m_rtcpaddress; + sockaddr_storage m_rtpaddress; + unsigned int m_addrlen; bool m_enabled; bool m_headerSeen; bool m_seen1; diff --git a/Nextion.cpp b/Nextion.cpp index ac6e8d7..fa6851e 100644 --- a/Nextion.cpp +++ b/Nextion.cpp @@ -25,7 +25,6 @@ #include #include #include -//#include const unsigned int DSTAR_RSSI_COUNT = 3U; // 3 * 420ms = 1260ms const unsigned int DSTAR_BER_COUNT = 63U; // 63 * 20ms = 1260ms @@ -48,7 +47,7 @@ const unsigned int NXDN_BER_COUNT = 28U; // 28 * 40ms = 1120ms // 00:low, others:high-speed. bit[2] is overlapped with LAYOUT_COMPAT_MASK. #define LAYOUT_HIGHSPEED (3 << 2) -CNextion::CNextion(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness, unsigned int screenLayout, unsigned int txFrequency, unsigned int rxFrequency, bool displayTempInF, const std::string& location) : +CNextion::CNextion(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness, unsigned int screenLayout, unsigned int txFrequency, unsigned int rxFrequency, bool displayTempInF) : CDisplay(), m_callsign(callsign), m_ipaddress("(ip unknown)"), @@ -73,8 +72,7 @@ m_txFrequency(txFrequency), m_rxFrequency(rxFrequency), m_fl_txFrequency(0.0F), m_fl_rxFrequency(0.0F), -m_displayTempInF(displayTempInF), -m_location(location) +m_displayTempInF(displayTempInF) { assert(serial != NULL); assert(brightness >= 0U && brightness <= 100U); @@ -179,10 +177,6 @@ void CNextion::setIdleInt() sendCommandAction(22U); } } - - ::sprintf(command, "t31.txt=\"%s\"", m_location.c_str()); // location - sendCommand(command); - sendCommandAction(23U); } else { sendCommandAction(17U); } diff --git a/Nextion.h b/Nextion.h index 7217d2a..488372f 100644 --- a/Nextion.h +++ b/Nextion.h @@ -29,7 +29,7 @@ class CNextion : public CDisplay { public: - CNextion(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness, unsigned int screenLayout, unsigned int txFrequency, unsigned int rxFrequency, bool displayTempInF, const std::string& location); + CNextion(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness, unsigned int screenLayout, unsigned int txFrequency, unsigned int rxFrequency, bool displayTempInF); virtual ~CNextion(); virtual bool open(); @@ -103,7 +103,6 @@ private: double m_fl_txFrequency; double m_fl_rxFrequency; bool m_displayTempInF; - std::string m_location; void sendCommand(const char* command); void sendCommandAction(unsigned int status); diff --git a/P25Network.cpp b/P25Network.cpp index 14a0fd5..ed28fa6 100644 --- a/P25Network.cpp +++ b/P25Network.cpp @@ -90,13 +90,13 @@ const unsigned int BUFFER_LENGTH = 100U; CP25Network::CP25Network(const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int localPort, bool debug) : m_socket(localPort), m_address(), -m_port(gatewayPort), +m_addrlen(), m_debug(debug), m_enabled(false), m_buffer(1000U, "P25 Network"), m_audio() { - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen); } CP25Network::~CP25Network() @@ -107,10 +107,10 @@ bool CP25Network::open() { LogMessage("Opening P25 network connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_address)) return false; - return m_socket.open(); + return m_socket.open(m_address.ss_family); } bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, const CP25LowSpeedData& lsd, bool end) @@ -126,7 +126,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 22U); - bool ret = m_socket.write(buffer, 22U, m_address, m_port); + bool ret = m_socket.write(buffer, 22U, m_address, m_addrlen); if (!ret) return false; @@ -137,7 +137,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 14U); - ret = m_socket.write(buffer, 14U, m_address, m_port); + ret = m_socket.write(buffer, 14U, m_address, m_addrlen); if (!ret) return false; @@ -150,7 +150,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -165,7 +165,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -180,7 +180,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -191,7 +191,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -202,7 +202,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -213,7 +213,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -226,7 +226,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU1 Sent", buffer, 16U); - ret = m_socket.write(buffer, 16U, m_address, m_port); + ret = m_socket.write(buffer, 16U, m_address, m_addrlen); if (!ret) return false; @@ -234,7 +234,7 @@ bool CP25Network::writeLDU1(const unsigned char* ldu1, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network END Sent", REC80, 17U); - ret = m_socket.write(REC80, 17U, m_address, m_port); + ret = m_socket.write(REC80, 17U, m_address, m_addrlen); if (!ret) return false; } @@ -255,7 +255,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 22U); - bool ret = m_socket.write(buffer, 22U, m_address, m_port); + bool ret = m_socket.write(buffer, 22U, m_address, m_addrlen); if (!ret) return false; @@ -266,7 +266,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 14U); - ret = m_socket.write(buffer, 14U, m_address, m_port); + ret = m_socket.write(buffer, 14U, m_address, m_addrlen); if (!ret) return false; @@ -283,7 +283,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -297,7 +297,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -311,7 +311,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -326,7 +326,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -337,7 +337,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -348,7 +348,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 17U); - ret = m_socket.write(buffer, 17U, m_address, m_port); + ret = m_socket.write(buffer, 17U, m_address, m_addrlen); if (!ret) return false; @@ -361,7 +361,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network LDU2 Sent", buffer, 16U); - ret = m_socket.write(buffer, 16U, m_address, m_port); + ret = m_socket.write(buffer, 16U, m_address, m_addrlen); if (!ret) return false; @@ -369,7 +369,7 @@ bool CP25Network::writeLDU2(const unsigned char* ldu2, const CP25Data& control, if (m_debug) CUtils::dump(1U, "P25 Network END Sent", REC80, 17U); - ret = m_socket.write(REC80, 17U, m_address, m_port); + ret = m_socket.write(REC80, 17U, m_address, m_addrlen); if (!ret) return false; } @@ -381,18 +381,12 @@ void CP25Network::clock(unsigned int ms) { unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match(m_address, address)) return; - // Check if the data is for us - if (m_address.s_addr != address.s_addr || m_port != port) { - LogMessage("P25 packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); - return; - } - if (!m_enabled) return; diff --git a/P25Network.h b/P25Network.h index 054c772..009c994 100644 --- a/P25Network.h +++ b/P25Network.h @@ -49,8 +49,8 @@ public: private: CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; + sockaddr_storage m_address; + unsigned int m_addrlen; bool m_debug; bool m_enabled; CRingBuffer m_buffer; diff --git a/POCSAGNetwork.cpp b/POCSAGNetwork.cpp index 682fdd1..e6b755a 100644 --- a/POCSAGNetwork.cpp +++ b/POCSAGNetwork.cpp @@ -31,12 +31,12 @@ const unsigned int BUFFER_LENGTH = 200U; CPOCSAGNetwork::CPOCSAGNetwork(const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) : m_socket(myAddress, myPort), m_address(), -m_port(gatewayPort), +m_addrlen(), m_debug(debug), m_enabled(false), m_buffer(1000U, "POCSAG Network") { - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen); } CPOCSAGNetwork::~CPOCSAGNetwork() @@ -47,7 +47,7 @@ bool CPOCSAGNetwork::open() { LogMessage("Opening POCSAG network connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_address)) return false; return m_socket.open(); @@ -57,18 +57,12 @@ void CPOCSAGNetwork::clock(unsigned int ms) { unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match(m_address, address)) return; - // Check if the data is for us - if (m_address.s_addr != address.s_addr || m_port != port) { - LogMessage("POCSAG packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); - return; - } - // Invalid packet type? if (::memcmp(buffer, "POCSAG", 6U) != 0) return; @@ -118,7 +112,7 @@ void CPOCSAGNetwork::enable(bool enabled) unsigned char c = enabled ? 0x00U : 0xFFU; - m_socket.write(&c, 1U, m_address, m_port); + m_socket.write(&c, 1U, m_address, m_addrlen); m_enabled = enabled; } diff --git a/POCSAGNetwork.h b/POCSAGNetwork.h index fe95203..dd3945b 100644 --- a/POCSAGNetwork.h +++ b/POCSAGNetwork.h @@ -46,8 +46,8 @@ public: private: CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; + sockaddr_storage m_address; + unsigned int m_addrlen; bool m_debug; bool m_enabled; CRingBuffer m_buffer; diff --git a/RemoteCommand.cpp b/RemoteCommand.cpp index fd96e46..25b07fd 100644 --- a/RemoteCommand.cpp +++ b/RemoteCommand.cpp @@ -61,15 +61,17 @@ CRemoteCommand::~CRemoteCommand() int CRemoteCommand::send(const std::string& command) { + sockaddr_storage address; + unsigned int addrlen; + CUDPSocket::lookup("127.0.0.1", m_port, address, addrlen); + CUDPSocket socket(0U); - bool ret = socket.open(); + bool ret = socket.open(address.ss_family); if (!ret) return 1; - in_addr address = CUDPSocket::lookup("localhost"); - - ret = socket.write((unsigned char*)command.c_str(), command.length(), address, m_port); + ret = socket.write((unsigned char*)command.c_str(), command.length(), address, addrlen); if (!ret) { socket.close(); return 1; diff --git a/RemoteControl.cpp b/RemoteControl.cpp index bf400ad..eea8f4f 100644 --- a/RemoteControl.cpp +++ b/RemoteControl.cpp @@ -32,8 +32,8 @@ const unsigned int CW_ARGS = 2U; const unsigned int BUFFER_LENGTH = 100U; -CRemoteControl::CRemoteControl(unsigned int port) : -m_socket(port), +CRemoteControl::CRemoteControl(const std::string address, unsigned int port) : +m_socket(address, port), m_command(RCD_NONE), m_args() { @@ -56,9 +56,9 @@ REMOTE_COMMAND CRemoteControl::getCommand() char command[BUFFER_LENGTH]; char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int ret = m_socket.read((unsigned char*)buffer, BUFFER_LENGTH, address, port); + sockaddr_storage address; + unsigned int addrlen; + int ret = m_socket.read((unsigned char*)buffer, BUFFER_LENGTH, address, addrlen); if (ret > 0) { buffer[ret] = '\0'; diff --git a/RemoteControl.h b/RemoteControl.h index 3825dee..422ce6e 100644 --- a/RemoteControl.h +++ b/RemoteControl.h @@ -54,7 +54,7 @@ enum REMOTE_COMMAND { class CRemoteControl { public: - CRemoteControl(unsigned int port); + CRemoteControl(const std::string address, unsigned int port); ~CRemoteControl(); bool open(); diff --git a/SHA256.cpp b/SHA256.cpp deleted file mode 100644 index b3366e0..0000000 --- a/SHA256.cpp +++ /dev/null @@ -1,373 +0,0 @@ -/* - * Copyright (C) 2005, 2006, 2008 Free Software Foundation, Inc. - * Copyright (C) 2011,2015 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 "SHA256.h" - -#include -#include -#include - -#ifdef WORDS_BIGENDIAN -# define SWAP(n) (n) -#else -# define SWAP(n) \ - (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) -#endif - -#define BLOCKSIZE 4096 -#if BLOCKSIZE % 64 != 0 -# error "invalid BLOCKSIZE" -#endif - -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - - -/* - Takes a pointer to a 256 bit block of data (eight 32 bit ints) and - intializes it to the start constants of the SHA256 algorithm. This - must be called before using hash in the call to sha256_hash -*/ -CSHA256::CSHA256() : -m_state(NULL), -m_total(NULL), -m_buflen(0U), -m_buffer(NULL) -{ - m_state = new uint32_t[8U]; - m_total = new uint32_t[2U]; - m_buffer = new uint32_t[32U]; - - init(); -} - -CSHA256::~CSHA256() -{ - delete[] m_state; - delete[] m_total; - delete[] m_buffer; -} - -void CSHA256::init() -{ - m_state[0] = 0x6a09e667UL; - m_state[1] = 0xbb67ae85UL; - m_state[2] = 0x3c6ef372UL; - m_state[3] = 0xa54ff53aUL; - m_state[4] = 0x510e527fUL; - m_state[5] = 0x9b05688cUL; - m_state[6] = 0x1f83d9abUL; - m_state[7] = 0x5be0cd19UL; - - m_total[0] = m_total[1] = 0; - m_buflen = 0; -} - -/* Copy the value from v into the memory location pointed to by *cp, - If your architecture allows unaligned access this is equivalent to - * (uint32_t *) cp = v */ -static inline void set_uint32(unsigned char* cp, uint32_t v) -{ - assert(cp != NULL); - - ::memcpy(cp, &v, sizeof v); -} - -/* Put result from CTX in first 32 bytes following RESBUF. The result - must be in little endian byte order. */ -unsigned char* CSHA256::read(unsigned char* resbuf) -{ - assert(resbuf != NULL); - - for (unsigned int i = 0U; i < 8U; i++) - set_uint32(resbuf + i * sizeof(m_state[0]), SWAP(m_state[i])); - - return resbuf; -} - -/* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. */ -void CSHA256::conclude() -{ - /* Take yet unprocessed bytes into account. */ - unsigned int bytes = m_buflen; - unsigned int size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; - - /* Now count remaining bytes. */ - m_total[0] += bytes; - if (m_total[0] < bytes) - ++m_total[1]; - - /* Put the 64-bit file length in *bits* at the end of the buffer. - Use set_uint32 rather than a simple assignment, to avoid risk of - unaligned access. */ - set_uint32((unsigned char*)&m_buffer[size - 2], SWAP((m_total[1] << 3) | (m_total[0] >> 29))); - set_uint32((unsigned char*)&m_buffer[size - 1], SWAP(m_total[0] << 3)); - - ::memcpy(&((char*)m_buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); - - /* Process last bytes. */ - processBlock((unsigned char*)m_buffer, size * 4); -} - -unsigned char* CSHA256::finish(unsigned char* resbuf) -{ - assert(resbuf != NULL); - - conclude(); - - return read(resbuf); -} - -/* Compute SHA256 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -unsigned char* CSHA256::buffer(const unsigned char* buffer, unsigned int len, unsigned char* resblock) -{ - assert(buffer != NULL); - assert(resblock != NULL); - - /* Initialize the computation context. */ - init(); - - /* Process whole buffer but last len % 64 bytes. */ - processBytes(buffer, len); - - /* Put result in desired memory area. */ - return finish(resblock); -} - -void CSHA256::processBytes(const unsigned char* buffer, unsigned int len) -{ - assert(buffer != NULL); - - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (m_buflen != 0U) { - unsigned int left_over = m_buflen; - unsigned int add = 128U - left_over > len ? len : 128U - left_over; - - ::memcpy(&((char*)m_buffer)[left_over], buffer, add); - m_buflen += add; - - if (m_buflen > 64U) { - processBlock((unsigned char*)m_buffer, m_buflen & ~63U); - - m_buflen &= 63U; - - /* The regions in the following copy operation cannot overlap. */ - ::memcpy(m_buffer, &((char*)m_buffer)[(left_over + add) & ~63U], m_buflen); - } - - buffer += add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64U) { -//#if !_STRING_ARCH_unaligned -//# define alignof(type) offsetof (struct { char c; type x; }, x) -//# define UNALIGNED_P(p) (((unsigned int) p) % alignof (uint32_t) != 0) -// if (UNALIGNED_P (buffer)) { -// while (len > 64U) { -// ::memcpy(m_buffer, buffer, 64U); -// processBlock((unsigned char*)m_buffer, 64U); -// buffer += 64U; -// len -= 64U; -// } -// } else -//#endif - { - processBlock(buffer, len & ~63U); - buffer += (len & ~63U); - len &= 63U; - } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0U) { - unsigned int left_over = m_buflen; - - ::memcpy(&((char*)m_buffer)[left_over], buffer, len); - left_over += len; - - if (left_over >= 64U) { - processBlock((unsigned char*)m_buffer, 64U); - left_over -= 64U; - ::memcpy(m_buffer, &m_buffer[16], left_over); - } - - m_buflen = left_over; - } -} - -/* --- Code below is the primary difference between sha1.c and sha256.c --- */ - -/* SHA256 round constants */ -#define K(I) roundConstants[I] -static const uint32_t roundConstants[64] = { - 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, - 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, - 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, - 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, - 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, - 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, - 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, - 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, - 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, - 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, - 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, - 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, - 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, - 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, - 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, - 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, -}; - -/* Round functions. */ -#define F2(A,B,C) ( ( A & B ) | ( C & ( A | B ) ) ) -#define F1(E,F,G) ( G ^ ( E & ( F ^ G ) ) ) - -/* Process LEN bytes of BUFFER, accumulating context into CTX. - It is assumed that LEN % 64 == 0. - Most of this code comes from GnuPG's cipher/sha1.c. */ - -void CSHA256::processBlock(const unsigned char* buffer, unsigned int len) -{ - assert(buffer != NULL); - - const uint32_t* words = (uint32_t*)buffer; - unsigned int nwords = len / sizeof(uint32_t); - const uint32_t* endp = words + nwords; - uint32_t x[16]; - uint32_t a = m_state[0]; - uint32_t b = m_state[1]; - uint32_t c = m_state[2]; - uint32_t d = m_state[3]; - uint32_t e = m_state[4]; - uint32_t f = m_state[5]; - uint32_t g = m_state[6]; - uint32_t h = m_state[7]; - - /* First increment the byte count. FIPS PUB 180-2 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - m_total[0] += len; - if (m_total[0] < len) - ++m_total[1]; - - #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) - #define S0(x) (rol(x,25)^rol(x,14)^(x>>3)) - #define S1(x) (rol(x,15)^rol(x,13)^(x>>10)) - #define SS0(x) (rol(x,30)^rol(x,19)^rol(x,10)) - #define SS1(x) (rol(x,26)^rol(x,21)^rol(x,7)) - - #define M(I) (tm = S1(x[(I-2)&0x0f]) + x[(I-7)&0x0f] + S0(x[(I-15)&0x0f]) + x[I&0x0f], x[I&0x0f] = tm) - - #define R(A,B,C,D,E,F,G,H,K,M) do { t0 = SS0(A) + F2(A,B,C); \ - t1 = H + SS1(E) + F1(E,F,G) + K + M; \ - D += t1; H = t0 + t1; \ - } while(0) - - while (words < endp) { - uint32_t tm; - uint32_t t0, t1; - /* FIXME: see sha1.c for a better implementation. */ - for (unsigned int t = 0U; t < 16U; t++) { - x[t] = SWAP(*words); - words++; - } - - R( a, b, c, d, e, f, g, h, K( 0), x[ 0] ); - R( h, a, b, c, d, e, f, g, K( 1), x[ 1] ); - R( g, h, a, b, c, d, e, f, K( 2), x[ 2] ); - R( f, g, h, a, b, c, d, e, K( 3), x[ 3] ); - R( e, f, g, h, a, b, c, d, K( 4), x[ 4] ); - R( d, e, f, g, h, a, b, c, K( 5), x[ 5] ); - R( c, d, e, f, g, h, a, b, K( 6), x[ 6] ); - R( b, c, d, e, f, g, h, a, K( 7), x[ 7] ); - R( a, b, c, d, e, f, g, h, K( 8), x[ 8] ); - R( h, a, b, c, d, e, f, g, K( 9), x[ 9] ); - R( g, h, a, b, c, d, e, f, K(10), x[10] ); - R( f, g, h, a, b, c, d, e, K(11), x[11] ); - R( e, f, g, h, a, b, c, d, K(12), x[12] ); - R( d, e, f, g, h, a, b, c, K(13), x[13] ); - R( c, d, e, f, g, h, a, b, K(14), x[14] ); - R( b, c, d, e, f, g, h, a, K(15), x[15] ); - R( a, b, c, d, e, f, g, h, K(16), M(16) ); - R( h, a, b, c, d, e, f, g, K(17), M(17) ); - R( g, h, a, b, c, d, e, f, K(18), M(18) ); - R( f, g, h, a, b, c, d, e, K(19), M(19) ); - R( e, f, g, h, a, b, c, d, K(20), M(20) ); - R( d, e, f, g, h, a, b, c, K(21), M(21) ); - R( c, d, e, f, g, h, a, b, K(22), M(22) ); - R( b, c, d, e, f, g, h, a, K(23), M(23) ); - R( a, b, c, d, e, f, g, h, K(24), M(24) ); - R( h, a, b, c, d, e, f, g, K(25), M(25) ); - R( g, h, a, b, c, d, e, f, K(26), M(26) ); - R( f, g, h, a, b, c, d, e, K(27), M(27) ); - R( e, f, g, h, a, b, c, d, K(28), M(28) ); - R( d, e, f, g, h, a, b, c, K(29), M(29) ); - R( c, d, e, f, g, h, a, b, K(30), M(30) ); - R( b, c, d, e, f, g, h, a, K(31), M(31) ); - R( a, b, c, d, e, f, g, h, K(32), M(32) ); - R( h, a, b, c, d, e, f, g, K(33), M(33) ); - R( g, h, a, b, c, d, e, f, K(34), M(34) ); - R( f, g, h, a, b, c, d, e, K(35), M(35) ); - R( e, f, g, h, a, b, c, d, K(36), M(36) ); - R( d, e, f, g, h, a, b, c, K(37), M(37) ); - R( c, d, e, f, g, h, a, b, K(38), M(38) ); - R( b, c, d, e, f, g, h, a, K(39), M(39) ); - R( a, b, c, d, e, f, g, h, K(40), M(40) ); - R( h, a, b, c, d, e, f, g, K(41), M(41) ); - R( g, h, a, b, c, d, e, f, K(42), M(42) ); - R( f, g, h, a, b, c, d, e, K(43), M(43) ); - R( e, f, g, h, a, b, c, d, K(44), M(44) ); - R( d, e, f, g, h, a, b, c, K(45), M(45) ); - R( c, d, e, f, g, h, a, b, K(46), M(46) ); - R( b, c, d, e, f, g, h, a, K(47), M(47) ); - R( a, b, c, d, e, f, g, h, K(48), M(48) ); - R( h, a, b, c, d, e, f, g, K(49), M(49) ); - R( g, h, a, b, c, d, e, f, K(50), M(50) ); - R( f, g, h, a, b, c, d, e, K(51), M(51) ); - R( e, f, g, h, a, b, c, d, K(52), M(52) ); - R( d, e, f, g, h, a, b, c, K(53), M(53) ); - R( c, d, e, f, g, h, a, b, K(54), M(54) ); - R( b, c, d, e, f, g, h, a, K(55), M(55) ); - R( a, b, c, d, e, f, g, h, K(56), M(56) ); - R( h, a, b, c, d, e, f, g, K(57), M(57) ); - R( g, h, a, b, c, d, e, f, K(58), M(58) ); - R( f, g, h, a, b, c, d, e, K(59), M(59) ); - R( e, f, g, h, a, b, c, d, K(60), M(60) ); - R( d, e, f, g, h, a, b, c, K(61), M(61) ); - R( c, d, e, f, g, h, a, b, K(62), M(62) ); - R( b, c, d, e, f, g, h, a, K(63), M(63) ); - - a = m_state[0] += a; - b = m_state[1] += b; - c = m_state[2] += c; - d = m_state[3] += d; - e = m_state[4] += e; - f = m_state[5] += f; - g = m_state[6] += g; - h = m_state[7] += h; - } -} diff --git a/SHA256.h b/SHA256.h deleted file mode 100644 index 7c48f19..0000000 --- a/SHA256.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2005, 2006, 2008, 2009 Free Software Foundation, Inc. - * Copyright (C) 2011,2015,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. - */ - -#ifndef SHA256_H -#define SHA256_H - -#include - -enum { - SHA256_DIGEST_SIZE = 256 / 8 -}; - -class CSHA256 { -public: - CSHA256(); - ~CSHA256(); - - /* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is necessary that LEN is a multiple of 64!!! */ - void processBlock(const unsigned char* buffer, unsigned int len); - - /* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is NOT required that LEN is a multiple of 64. */ - void processBytes(const unsigned char* buffer, unsigned int len); - - /* Process the remaining bytes in the buffer and put result from CTX - in first 32 bytes following RESBUF. The result is always in little - endian byte order, so that a byte-wise output yields to the wanted - ASCII representation of the message digest. */ - unsigned char* finish(unsigned char* resbuf); - - /* Put result from CTX in first 32 bytes following RESBUF. The result is - always in little endian byte order, so that a byte-wise output yields - to the wanted ASCII representation of the message digest. */ - unsigned char* read(unsigned char* resbuf); - - /* Compute SHA256 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ - unsigned char* buffer(const unsigned char* buffer, unsigned int len, unsigned char* resblock); - -private: - uint32_t* m_state; - uint32_t* m_total; - unsigned int m_buflen; - uint32_t* m_buffer; - - void init(); - void conclude(); -}; - -#endif diff --git a/UDPSocket.cpp b/UDPSocket.cpp index ba0e35f..0bbe27b 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -60,49 +60,117 @@ CUDPSocket::~CUDPSocket() #endif } -in_addr CUDPSocket::lookup(const std::string& hostname) +int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length) { - in_addr addr; -#if defined(_WIN32) || defined(_WIN64) - unsigned long address = ::inet_addr(hostname.c_str()); - if (address != INADDR_NONE && address != INADDR_ANY) { - addr.s_addr = address; - return addr; + struct addrinfo hints; + + ::memset(&hints, 0, sizeof(hints)); + + return lookup(hostname, port, addr, address_length, hints); +} + +int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints) +{ + int err; + std::string portstr = std::to_string(port); + struct addrinfo *res; + + /* port is always digits, no needs to lookup service */ + hints.ai_flags |= AI_NUMERICSERV; + + err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res); + if (err) { + sockaddr_in *paddr = (sockaddr_in *)&addr; + ::memset(paddr, 0, address_length = sizeof(sockaddr_in)); + paddr->sin_family = AF_INET; + paddr->sin_port = htons(port); + paddr->sin_addr.s_addr = htonl(INADDR_NONE); + LogError("Cannot find address for host %s", hostname.c_str()); + return err; } - struct hostent* hp = ::gethostbyname(hostname.c_str()); - if (hp != NULL) { - ::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); - return addr; + ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen); + + freeaddrinfo(res); + return 0; +} + +bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2) +{ + if (addr1.ss_family != addr2.ss_family) + return false; + + switch (addr1.ss_family) { + case AF_INET: + struct sockaddr_in *in_1, *in_2; + in_1 = (struct sockaddr_in *)&addr1; + in_2 = (struct sockaddr_in *)&addr2; + return ( (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && + (in_1->sin_port == in_2->sin_port) ); + case AF_INET6: + struct sockaddr_in6 *in6_1, *in6_2; + in6_1 = (struct sockaddr_in6 *)&addr1; + in6_2 = (struct sockaddr_in6 *)&addr2; + return ( IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && + (in6_1->sin6_port == in6_2->sin6_port) ); + default: + return false; } +} - LogError("Cannot find address for host %s", hostname.c_str()); +bool CUDPSocket::match_addr(const sockaddr_storage &addr1, const sockaddr_storage &addr2) +{ + if (addr1.ss_family != addr2.ss_family) + return false; - addr.s_addr = INADDR_NONE; - return addr; -#else - in_addr_t address = ::inet_addr(hostname.c_str()); - if (address != in_addr_t(-1)) { - addr.s_addr = address; - return addr; + switch (addr1.ss_family) { + case AF_INET: + struct sockaddr_in *in_1, *in_2; + in_1 = (struct sockaddr_in *)&addr1; + in_2 = (struct sockaddr_in *)&addr2; + return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr); + case AF_INET6: + struct sockaddr_in6 *in6_1, *in6_2; + in6_1 = (struct sockaddr_in6 *)&addr1; + in6_2 = (struct sockaddr_in6 *)&addr2; + return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr); + default: + return false; } +} - struct hostent* hp = ::gethostbyname(hostname.c_str()); - if (hp != NULL) { - ::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); - return addr; - } +bool CUDPSocket::isnone(const sockaddr_storage &addr) +{ + struct sockaddr_in *in = (struct sockaddr_in *)&addr; - LogError("Cannot find address for host %s", hostname.c_str()); - - addr.s_addr = INADDR_NONE; - return addr; -#endif + return ( (addr.ss_family == AF_INET) && + (in->sin_addr.s_addr == htonl(INADDR_NONE)) ); } bool CUDPSocket::open() { - m_fd = ::socket(PF_INET, SOCK_DGRAM, 0); + return open(AF_UNSPEC); +} + +bool CUDPSocket::open(const unsigned int af) +{ + int err; + sockaddr_storage addr; + unsigned int addrlen; + struct addrinfo hints; + + ::memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = af; + + /* to determine protocol family, call lookup() first. */ + err = lookup(m_address, m_port, addr, addrlen, hints); + if (err) { + LogError("The local address is invalid - %s", m_address.c_str()); + return false; + } + + m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); if (m_fd < 0) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); @@ -113,24 +181,6 @@ bool CUDPSocket::open() } if (m_port > 0U) { - sockaddr_in addr; - ::memset(&addr, 0x00, sizeof(sockaddr_in)); - addr.sin_family = AF_INET; - addr.sin_port = htons(m_port); - addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (!m_address.empty()) { -#if defined(_WIN32) || defined(_WIN64) - addr.sin_addr.s_addr = ::inet_addr(m_address.c_str()); -#else - addr.sin_addr.s_addr = ::inet_addr(m_address.c_str()); -#endif - if (addr.sin_addr.s_addr == INADDR_NONE) { - LogError("The local address is invalid - %s", m_address.c_str()); - return false; - } - } - int reuse = 1; if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { #if defined(_WIN32) || defined(_WIN64) @@ -141,7 +191,7 @@ bool CUDPSocket::open() return false; } - if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) { + if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); #else @@ -154,7 +204,7 @@ bool CUDPSocket::open() return true; } -int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port) +int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length) { assert(buffer != NULL); assert(length > 0U); @@ -186,17 +236,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres if (ret == 0) return 0; - sockaddr_in addr; #if defined(_WIN32) || defined(_WIN64) - int size = sizeof(sockaddr_in); + int size = sizeof(sockaddr_storage); #else - socklen_t size = sizeof(sockaddr_in); + socklen_t size = sizeof(sockaddr_storage); #endif #if defined(_WIN32) || defined(_WIN64) - int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size); + int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #else - ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size); + ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #endif if (len <= 0) { #if defined(_WIN32) || defined(_WIN64) @@ -207,28 +256,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres return -1; } - address = addr.sin_addr; - port = ntohs(addr.sin_port); - + address_length = size; return len; } -bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port) +bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length) { assert(buffer != NULL); assert(length > 0U); - sockaddr_in addr; - ::memset(&addr, 0x00, sizeof(sockaddr_in)); - - addr.sin_family = AF_INET; - addr.sin_addr = address; - addr.sin_port = htons(port); - #if defined(_WIN32) || defined(_WIN64) - int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in)); + int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length); #else - ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in)); + ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length); #endif if (ret < 0) { #if defined(_WIN32) || defined(_WIN64) diff --git a/UDPSocket.h b/UDPSocket.h index 3437b53..ad5b2a1 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -31,7 +31,8 @@ #include #include #else -#include +#include +#include #endif class CUDPSocket { @@ -41,13 +42,18 @@ public: ~CUDPSocket(); bool open(); + bool open(const unsigned int af); - int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port); - bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port); + int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length); + bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length); void close(); - static in_addr lookup(const std::string& hostName); + static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length); + static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints); + static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2); + static bool match_addr(const sockaddr_storage &addr1, const sockaddr_storage &addr2); + static bool isnone(const sockaddr_storage &addr); private: std::string m_address; diff --git a/Version.h b/Version.h index 4d2faa4..23ae81e 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200809"; +const char* VERSION = "20200830"; #endif diff --git a/YSFNetwork.cpp b/YSFNetwork.cpp index a8effba..ff5cab8 100644 --- a/YSFNetwork.cpp +++ b/YSFNetwork.cpp @@ -31,7 +31,7 @@ const unsigned int BUFFER_LENGTH = 200U; CYSFNetwork::CYSFNetwork(const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, const std::string& callsign, bool debug) : m_socket(myAddress, myPort), m_address(), -m_port(gatewayPort), +m_addrlen(), m_callsign(), m_debug(debug), m_enabled(false), @@ -42,7 +42,7 @@ m_tag(NULL) m_callsign = callsign; m_callsign.resize(YSF_CALLSIGN_LENGTH, ' '); - m_address = CUDPSocket::lookup(gatewayAddress); + CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen); m_tag = new unsigned char[YSF_CALLSIGN_LENGTH]; ::memset(m_tag, ' ', YSF_CALLSIGN_LENGTH); @@ -57,7 +57,7 @@ bool CYSFNetwork::open() { LogMessage("Opening YSF network connection"); - if (m_address.s_addr == INADDR_NONE) + if (CUDPSocket::isnone(m_address)) return false; m_pollTimer.start(); @@ -97,7 +97,7 @@ bool CYSFNetwork::write(const unsigned char* src, const unsigned char* dest, con if (m_debug) CUtils::dump(1U, "YSF Network Data Sent", buffer, 155U); - return m_socket.write(buffer, 155U, m_address, m_port); + return m_socket.write(buffer, 155U, m_address, m_addrlen); } bool CYSFNetwork::writePoll() @@ -115,7 +115,7 @@ bool CYSFNetwork::writePoll() if (m_debug) CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U); - return m_socket.write(buffer, 14U, m_address, m_port); + return m_socket.write(buffer, 14U, m_address, m_addrlen); } void CYSFNetwork::clock(unsigned int ms) @@ -128,18 +128,12 @@ void CYSFNetwork::clock(unsigned int ms) unsigned char buffer[BUFFER_LENGTH]; - in_addr address; - unsigned int port; - int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); - if (length <= 0) + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen); + if (length <= 0 || !CUDPSocket::match(m_address, address)) return; - // Check if the data is for us - if (m_address.s_addr != address.s_addr || m_port != port) { - LogMessage("YSF packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port); - return; - } - // Ignore incoming polls if (::memcmp(buffer, "YSFP", 4U) == 0) return; diff --git a/YSFNetwork.h b/YSFNetwork.h index e9a430f..292364d 100644 --- a/YSFNetwork.h +++ b/YSFNetwork.h @@ -48,8 +48,8 @@ public: private: CUDPSocket m_socket; - in_addr m_address; - unsigned int m_port; + sockaddr_storage m_address; + unsigned int m_addrlen; std::string m_callsign; bool m_debug; bool m_enabled;