1
0
Fork 0

IPv6 and FCS changes.

ycs232-kbc
Jonathan Naylor 4 years ago
parent bdf5195362
commit 241c76e606

@ -37,7 +37,7 @@ m_height(0),
m_desc(), m_desc(),
m_suffix(suffix), m_suffix(suffix),
m_aprsAddr(), m_aprsAddr(),
m_aprsAddrLen(), m_aprsAddrLen(0U),
m_aprsSocket() m_aprsSocket()
#if defined(USE_GPSD) #if defined(USE_GPSD)
,m_gpsdEnabled(false), ,m_gpsdEnabled(false),
@ -55,7 +55,8 @@ m_gpsdData()
m_callsign.append(rptSuffix.substr(0U, 1U)); m_callsign.append(rptSuffix.substr(0U, 1U));
} }
CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen); if (CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen) != 0)
m_aprsAddrLen = 0U;
} }
CAPRSWriter::~CAPRSWriter() CAPRSWriter::~CAPRSWriter()
@ -90,6 +91,11 @@ void CAPRSWriter::setGPSDLocation(const std::string& address, const std::string&
bool CAPRSWriter::open() bool CAPRSWriter::open()
{ {
if (m_aprsAddrLen == 0U) {
LogError("Unable to lookup the adress of the APRS-IS server");
return false;
}
#if defined(USE_GPSD) #if defined(USE_GPSD)
if (m_gpsdEnabled) { if (m_gpsdEnabled) {
int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData); int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData);
@ -103,7 +109,7 @@ bool CAPRSWriter::open()
LogMessage("Connected to GPSD"); LogMessage("Connected to GPSD");
} }
#endif #endif
bool ret = m_aprsSocket.open(); bool ret = m_aprsSocket.open(m_aprsAddr);
if (!ret) if (!ret)
return false; return false;

@ -128,13 +128,33 @@ bool CConf::read()
section = SECTION_NONE; section = SECTION_NONE;
continue; continue;
} }
char* key = ::strtok(buffer, " \t=\r\n"); char* key = ::strtok(buffer, " \t=\r\n");
if (key == NULL) if (key == NULL)
continue; continue;
char* value = ::strtok(NULL, "\r\n");
if (value == NULL)
continue;
// Remove quotes from the value
size_t len = ::strlen(value);
if (len > 1U && *value == '"' && value[len - 1U] == '"') {
value[len - 1U] = '\0';
value++;
} else {
char *p;
// if value is not quoted, remove after # (to make comment)
if ((p = strchr(value, '#')) != NULL)
*p = '\0';
// remove trailing tab/space
for (p = value + strlen(value) - 1U; p >= value && (*p == '\t' || *p == ' '); p--)
*p = '\0';
}
char* value = ::strtok(NULL, "\r\n");
if (section == SECTION_GENERAL) { if (section == SECTION_GENERAL) {
if (::strcmp(key, "Callsign") == 0) { if (::strcmp(key, "Callsign") == 0) {
// Convert the callsign to upper case // Convert the callsign to upper case
@ -241,6 +261,8 @@ bool CConf::read()
dgIdData->m_netHangTime = (unsigned int)::atoi(value); dgIdData->m_netHangTime = (unsigned int)::atoi(value);
else if (::strcmp(key, "Static") == 0) else if (::strcmp(key, "Static") == 0)
dgIdData->m_static = ::atoi(value) == 1; dgIdData->m_static = ::atoi(value) == 1;
else if (::strcmp(key, "Options") == 0)
dgIdData->m_options = value;
else if (::strcmp(key, "Address") == 0) else if (::strcmp(key, "Address") == 0)
dgIdData->m_address = value; dgIdData->m_address = value;
else if (::strcmp(key, "Name") == 0) else if (::strcmp(key, "Name") == 0)

@ -31,6 +31,7 @@ struct DGIdData {
unsigned int m_dgId; unsigned int m_dgId;
std::string m_type; std::string m_type;
bool m_static; bool m_static;
std::string m_options;
std::string m_name; std::string m_name;
std::string m_address; std::string m_address;
unsigned int m_port; unsigned int m_port;

@ -182,7 +182,10 @@ int CDGIdGateway::run()
sockaddr_storage rptAddr; sockaddr_storage rptAddr;
unsigned int rptAddrLen; unsigned int rptAddrLen;
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen); if (CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen) != 0) {
LogError("Unable to resolve the address of the host");
return 1;
}
bool debug = m_conf.getDebug(); bool debug = m_conf.getDebug();
std::string myAddress = m_conf.getMyAddress(); std::string myAddress = m_conf.getMyAddress();
@ -229,8 +232,9 @@ int CDGIdGateway::run()
unsigned int rxFrequency = m_conf.getRxFrequency(); unsigned int rxFrequency = m_conf.getRxFrequency();
std::string locator = calculateLocator(); std::string locator = calculateLocator();
unsigned int id = m_conf.getId(); unsigned int id = m_conf.getId();
std::string options = (*it)->m_options;
dgIdNetwork[dgid] = new CFCSNetwork(name, local, m_callsign, rxFrequency, txFrequency, locator, id, debug); dgIdNetwork[dgid] = new CFCSNetwork(name, local, m_callsign, rxFrequency, txFrequency, locator, id, options, debug);
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE; dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE;
dgIdNetwork[dgid]->m_static = statc; dgIdNetwork[dgid]->m_static = statc;
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime; dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
@ -254,10 +258,17 @@ int CDGIdGateway::run()
std::string name = (*it)->m_name; std::string name = (*it)->m_name;
for (std::vector<IMRSDestination*>::const_iterator it = destinations.begin(); it != destinations.end(); ++it) { for (std::vector<IMRSDestination*>::const_iterator it = destinations.begin(); it != destinations.end(); ++it) {
IMRSDest* dest = new IMRSDest; sockaddr_storage addr;
CUDPSocket::lookup((*it)->m_address, IMRS_PORT, dest->m_addr, dest->m_addrLen); unsigned int addrLen;
dest->m_dgId = (*it)->m_dgId; if (CUDPSocket::lookup((*it)->m_address, IMRS_PORT, addr, addrLen) == 0) {
dests.push_back(dest); IMRSDest* dest = new IMRSDest;
dest->m_dgId = (*it)->m_dgId;
dest->m_addr = addr;
dest->m_addrLen = addrLen;
dests.push_back(dest);
} else {
LogWarning("Unable to resolve the address for %s", (*it)->m_address.c_str());
}
} }
imrs->addDGId(dgid, name, dests, debug); imrs->addDGId(dgid, name, dests, debug);
@ -273,49 +284,57 @@ int CDGIdGateway::run()
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrLen; unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen); if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
dgIdNetwork[dgid] = new CYSFNetwork(local, "PARROT", addr, addrLen, m_callsign, debug);
dgIdNetwork[dgid] = new CYSFNetwork(local, "PARROT", addr, addrLen, m_callsign, debug); dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE;
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE; dgIdNetwork[dgid]->m_static = statc;
dgIdNetwork[dgid]->m_static = statc; dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime; dgIdNetwork[dgid]->m_netHangTime = netHangTime;
dgIdNetwork[dgid]->m_netHangTime = netHangTime; } else {
LogWarning("Unable to resolve the address for the YSF Parrot");
}
} else if (type == "YSF2DMR") { } else if (type == "YSF2DMR") {
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrLen; unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen); if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2DMR", addr, addrLen, m_callsign, debug);
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2DMR", addr, addrLen, m_callsign, debug); dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2; dgIdNetwork[dgid]->m_static = statc;
dgIdNetwork[dgid]->m_static = statc; dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime; dgIdNetwork[dgid]->m_netHangTime = netHangTime;
dgIdNetwork[dgid]->m_netHangTime = netHangTime; } else {
LogWarning("Unable to resolve the address for YSF2DMR");
}
} else if (type == "YSF2NXDN") { } else if (type == "YSF2NXDN") {
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrLen; unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen); if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2NXDN", addr, addrLen, m_callsign, debug);
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2NXDN", addr, addrLen, m_callsign, debug); dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2; dgIdNetwork[dgid]->m_static = statc;
dgIdNetwork[dgid]->m_static = statc; dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime; dgIdNetwork[dgid]->m_netHangTime = netHangTime;
dgIdNetwork[dgid]->m_netHangTime = netHangTime; } else {
LogWarning("Unable to resolve the address for YSF2NXDN");
}
} else if (type == "YSF2P25") { } else if (type == "YSF2P25") {
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrLen; unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen); if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2P25", addr, addrLen, m_callsign, debug);
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2P25", addr, addrLen, m_callsign, debug); dgIdNetwork[dgid]->m_modes = DT_VOICE_FR_MODE;
dgIdNetwork[dgid]->m_modes = DT_VOICE_FR_MODE; dgIdNetwork[dgid]->m_static = statc;
dgIdNetwork[dgid]->m_static = statc; dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime; dgIdNetwork[dgid]->m_netHangTime = netHangTime;
dgIdNetwork[dgid]->m_netHangTime = netHangTime; } else {
LogWarning("Unable to resolve the address for YSF2P25");
}
} }
if (dgIdNetwork[dgid] != NULL && dgIdNetwork[dgid] != imrs) { if (dgIdNetwork[dgid] != NULL && dgIdNetwork[dgid] != imrs) {

@ -118,6 +118,7 @@ Type=FCS
Static=0 Static=0
Name=FCS00101 Name=FCS00101
Local=42025 Local=42025
# Options=20;21;
#RFHangTime=120 #RFHangTime=120
#NetHangTime=60 #NetHangTime=60
Debug=0 Debug=0

@ -29,12 +29,14 @@ const char* FCS_VERSION = "MMDVM";
const unsigned int BUFFER_LENGTH = 200U; const unsigned int BUFFER_LENGTH = 200U;
CFCSNetwork::CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug) : CFCSNetwork::CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, const std::string& options, bool debug) :
m_socket(port), m_socket(port),
m_debug(debug), m_debug(debug),
m_addr(), m_addr(),
m_addrLen(0U), m_addrLen(0U),
m_ping(NULL), m_ping(NULL),
m_options(NULL),
m_opt(options),
m_info(NULL), m_info(NULL),
m_reflector(reflector), m_reflector(reflector),
m_print(), m_print(),
@ -56,12 +58,23 @@ m_state(FCS_UNLINKED)
::memcpy(m_ping + 10U, reflector.c_str(), 8U); ::memcpy(m_ping + 10U, reflector.c_str(), 8U);
m_print = reflector.substr(0U, 6U) + "-" + reflector.substr(6U); m_print = reflector.substr(0U, 6U) + "-" + reflector.substr(6U);
m_options = new unsigned char[50U];
::memcpy(m_options + 0U, "FCSO", 4U);
::memset(m_options + 4U, ' ', 46U);
::memcpy(m_options + 4U, callsign.c_str(), callsign.size());
char url[50U];
::sprintf(url, "%s.xreflector.net", reflector.c_str());
if (CUDPSocket::lookup(std::string(url), FCS_PORT, m_addr, m_addrLen) != 0)
m_addrLen = 0U;
} }
CFCSNetwork::~CFCSNetwork() CFCSNetwork::~CFCSNetwork()
{ {
delete[] m_info; delete[] m_info;
delete[] m_ping; delete[] m_ping;
delete[] m_options;
} }
std::string CFCSNetwork::getDesc(unsigned int dgId) std::string CFCSNetwork::getDesc(unsigned int dgId)
@ -71,54 +84,14 @@ std::string CFCSNetwork::getDesc(unsigned int dgId)
bool CFCSNetwork::open() bool CFCSNetwork::open()
{ {
LogMessage("Resolving FCS00x addresses"); if (m_addrLen == 0U) {
LogError("Unable to resolve the address of %s", m_reflector.c_str());
sockaddr_storage addr; return false;
unsigned int addrLen; }
CUDPSocket::lookup("fcs001.xreflector.net", FCS_PORT, addr, addrLen);
std::pair<sockaddr_storage, unsigned int> entry = std::make_pair(addr, addrLen);
m_addresses["FCS001"] = entry;
CUDPSocket::lookup("fcs002.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS002"] = entry;
CUDPSocket::lookup("fcs003.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS003"] = entry;
CUDPSocket::lookup("fcs004.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS004"] = entry;
CUDPSocket::lookup("fcs005.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS005"] = entry;
CUDPSocket::lookup("fcs222.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS222"] = entry;
CUDPSocket::lookup("fcs224.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS224"] = entry;
CUDPSocket::lookup("fcs232.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS232"] = entry;
CUDPSocket::lookup("fcs260.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS260"] = entry;
CUDPSocket::lookup("fcs262.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS262"] = entry;
LogMessage("Opening FCS network connection"); LogMessage("Opening FCS network connection");
return m_socket.open(); return m_socket.open(m_addr);
} }
void CFCSNetwork::write(unsigned int dgid, const unsigned char* data) void CFCSNetwork::write(unsigned int dgid, const unsigned char* data)
@ -131,6 +104,7 @@ void CFCSNetwork::write(unsigned int dgid, const unsigned char* data)
unsigned char buffer[130U]; unsigned char buffer[130U];
::memset(buffer + 0U, ' ', 130U); ::memset(buffer + 0U, ' ', 130U);
::memcpy(buffer + 0U, data + 35U, 120U); ::memcpy(buffer + 0U, data + 35U, 120U);
::memcpy(buffer + 120U, data + 34U, 1U);
::memcpy(buffer + 121U, m_reflector.c_str(), 8U); ::memcpy(buffer + 121U, m_reflector.c_str(), 8U);
if (m_debug) if (m_debug)
@ -144,16 +118,6 @@ void CFCSNetwork::link()
if (m_state == FCS_LINKING || m_state == FCS_LINKED) if (m_state == FCS_LINKING || m_state == FCS_LINKED)
return; return;
std::string name = m_reflector.substr(0U, 6U);
if (m_addresses.count(name) == 0U) {
LogError("Unknown FCS reflector - %s", name.c_str());
return;
}
std::pair<sockaddr_storage, unsigned int> entry = m_addresses[name];
m_addr = entry.first;
m_addrLen = entry.second;
m_state = FCS_LINKING; m_state = FCS_LINKING;
m_pingTimer.start(); m_pingTimer.start();
@ -205,12 +169,14 @@ void CFCSNetwork::clock(unsigned int ms)
LogMessage("Linked to %s", m_print.c_str()); LogMessage("Linked to %s", m_print.c_str());
m_state = FCS_LINKED; m_state = FCS_LINKED;
writeInfo(); writeInfo();
writeOptions(m_print);
} }
if (length == 10 && m_state == FCS_LINKING) { if (length == 10 && m_state == FCS_LINKING) {
LogMessage("Linked to %s", m_print.c_str()); LogMessage("Linked to %s", m_print.c_str());
m_state = FCS_LINKED; m_state = FCS_LINKED;
writeInfo(); writeInfo();
writeOptions(m_print);
} }
if (length == 7 || length == 10 || length == 130) { if (length == 7 || length == 10 || length == 130) {
@ -286,3 +252,22 @@ void CFCSNetwork::writePing()
m_socket.write(m_ping, 25U, m_addr, m_addrLen); m_socket.write(m_ping, 25U, m_addr, m_addrLen);
} }
void CFCSNetwork::writeOptions(const std::string& reflector)
{
if (m_state != FCS_LINKED)
return;
if (m_opt.size() < 1)
return;
::memset(m_options + 14U, 0x20U, 36U);
::memcpy(m_options + 4U, (reflector.substr(0, 6) + reflector.substr(7, 2)).c_str(), 8U);
::memcpy(m_options + 12U, m_opt.c_str(), m_opt.size());
if (m_debug)
CUtils::dump(1U, "FCS Network Options Sent", m_options, 50U);
m_socket.write(m_options, 50U, m_addr, m_addrLen);
}

@ -27,7 +27,6 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <map>
enum FCS_STATE { enum FCS_STATE {
FCS_UNLINKED, FCS_UNLINKED,
@ -37,7 +36,7 @@ enum FCS_STATE {
class CFCSNetwork : public CDGIdNetwork { class CFCSNetwork : public CDGIdNetwork {
public: public:
CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug); CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, const std::string& options, bool debug);
virtual ~CFCSNetwork(); virtual ~CFCSNetwork();
virtual std::string getDesc(unsigned int dgId); virtual std::string getDesc(unsigned int dgId);
@ -62,16 +61,18 @@ private:
sockaddr_storage m_addr; sockaddr_storage m_addr;
unsigned int m_addrLen; unsigned int m_addrLen;
unsigned char* m_ping; unsigned char* m_ping;
unsigned char* m_options;
std::string m_opt;
unsigned char* m_info; unsigned char* m_info;
std::string m_reflector; std::string m_reflector;
std::string m_print; std::string m_print;
CRingBuffer<unsigned char> m_buffer; CRingBuffer<unsigned char> m_buffer;
std::map<std::string, std::pair<sockaddr_storage, unsigned int>> m_addresses;
unsigned char m_n; unsigned char m_n;
CTimer m_pingTimer; CTimer m_pingTimer;
CTimer m_resetTimer; CTimer m_resetTimer;
FCS_STATE m_state; FCS_STATE m_state;
void writeOptions(const std::string& reflector);
void writeInfo(); void writeInfo();
void writePing(); void writePing();
}; };

@ -1,5 +1,5 @@
CC = gcc CC = cc
CXX = g++ CXX = c++
# Use the following CFLAGS and LIBS if you don't want to use gpsd. # Use the following CFLAGS and LIBS if you don't want to use gpsd.
CFLAGS = -g -O3 -Wall -std=c++0x -pthread CFLAGS = -g -O3 -Wall -std=c++0x -pthread

@ -17,7 +17,6 @@
*/ */
#include "UDPSocket.h" #include "UDPSocket.h"
#include "Log.h"
#include <cassert> #include <cassert>
@ -26,26 +25,36 @@
#include <cstring> #include <cstring>
#endif #endif
#if defined(HAVE_LOG_H)
#include "Log.h"
#else
#define LogError(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
#define LogInfo(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
#endif
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) : CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
m_address(address), m_address_save(address),
m_port(port), m_port_save(port),
m_fd(-1) m_counter(0U)
{ {
assert(!address.empty());
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
WSAData data; WSAData data;
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data); int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
if (wsaRet != 0) if (wsaRet != 0)
LogError("Error from WSAStartup"); LogError("Error from WSAStartup");
#endif #endif
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
m_address[i] = "";
m_port[i] = 0U;
m_af[i] = 0U;
m_fd[i] = -1;
}
} }
CUDPSocket::CUDPSocket(unsigned int port) : CUDPSocket::CUDPSocket(unsigned int port) :
m_address(), m_address_save(),
m_port(port), m_port_save(port),
m_fd(-1) m_counter(0U)
{ {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
WSAData data; WSAData data;
@ -53,6 +62,12 @@ m_fd(-1)
if (wsaRet != 0) if (wsaRet != 0)
LogError("Error from WSAStartup"); LogError("Error from WSAStartup");
#endif #endif
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
m_address[i] = "";
m_port[i] = 0U;
m_af[i] = 0U;
m_fd[i] = -1;
}
} }
CUDPSocket::~CUDPSocket() CUDPSocket::~CUDPSocket()
@ -96,58 +111,82 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
return 0; return 0;
} }
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2) bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
{ {
if (addr1.ss_family != addr2.ss_family) if (addr1.ss_family != addr2.ss_family)
return false; return false;
switch (addr1.ss_family) { if (type == IMT_ADDRESS_AND_PORT) {
case AF_INET: switch (addr1.ss_family) {
struct sockaddr_in *in_1, *in_2; case AF_INET:
in_1 = (struct sockaddr_in*)&addr1; struct sockaddr_in *in_1, *in_2;
in_2 = (struct sockaddr_in*)&addr2; in_1 = (struct sockaddr_in*)&addr1;
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port)); in_2 = (struct sockaddr_in*)&addr2;
case AF_INET6: return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
struct sockaddr_in6 *in6_1, *in6_2; case AF_INET6:
in6_1 = (struct sockaddr_in6*)&addr1; struct sockaddr_in6 *in6_1, *in6_2;
in6_2 = (struct sockaddr_in6*)&addr2; in6_1 = (struct sockaddr_in6*)&addr1;
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port)); in6_2 = (struct sockaddr_in6*)&addr2;
default: return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port);
default:
return false;
}
} else if (type == IMT_ADDRESS_ONLY) {
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;
}
} else {
return false; return false;
} }
} }
bool CUDPSocket::isnone(const sockaddr_storage& addr) bool CUDPSocket::isNone(const sockaddr_storage& addr)
{ {
struct sockaddr_in *in = (struct sockaddr_in *)&addr; struct sockaddr_in *in = (struct sockaddr_in *)&addr;
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE))); return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
} }
bool CUDPSocket::open() bool CUDPSocket::open(const sockaddr_storage& address)
{ {
return open(AF_UNSPEC); return open(address.ss_family);
} }
bool CUDPSocket::open(const unsigned int af) bool CUDPSocket::open(unsigned int af)
{
return open(0, af, m_address_save, m_port_save);
}
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int port)
{ {
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrlen; unsigned int addrlen;
struct addrinfo hints; struct addrinfo hints;
::memset(&hints, 0, sizeof(hints)); ::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
hints.ai_family = af; hints.ai_family = af;
/* to determine protocol family, call lookup() first. */ /* to determine protocol family, call lookup() first. */
int err = lookup(m_address, m_port, addr, addrlen, hints); int err = lookup(address, port, addr, addrlen, hints);
if (err != 0) { if (err != 0) {
LogError("The local address is invalid - %s", m_address.c_str()); LogError("The local address is invalid - %s", address.c_str());
return false; return false;
} }
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
if (m_fd < 0) { if (fd < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
#else #else
@ -156,9 +195,14 @@ bool CUDPSocket::open(const unsigned int af)
return false; return false;
} }
if (m_port > 0U) { m_address[index] = address;
m_port[index] = port;
m_af[index] = addr.ss_family;
m_fd[index] = fd;
if (port > 0U) {
int reuse = 1; int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError()); LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
#else #else
@ -167,7 +211,7 @@ bool CUDPSocket::open(const unsigned int af)
return false; return false;
} }
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) { if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else #else
@ -176,7 +220,7 @@ bool CUDPSocket::open(const unsigned int af)
return false; return false;
} }
LogInfo("Opening UDP port on %u", m_port); LogInfo("Opening UDP port on %u", port);
} }
return true; return true;
@ -188,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
assert(length > 0U); assert(length > 0U);
// Check that the readfrom() won't block // Check that the readfrom() won't block
fd_set readFds; int i, n;
FD_ZERO(&readFds); struct pollfd pfd[UDP_SOCKET_MAX];
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
if (m_fd[i] >= 0) {
pfd[n].fd = m_fd[i];
pfd[n].events = POLLIN;
n++;
}
}
// no socket descriptor to receive
if (n == 0)
return 0;
// Return immediately
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
FD_SET((unsigned int)m_fd, &readFds); int ret = WSAPoll(pfd, n, 0);
#else #else
FD_SET(m_fd, &readFds); int ret = ::poll(pfd, n, 0);
#endif #endif
// Return immediately
timeval tv;
tv.tv_sec = 0L;
tv.tv_usec = 0L;
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from UDP select, err: %lu", ::GetLastError()); LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
#else #else
LogError("Error returned from UDP select, err: %d", errno); LogError("Error returned from UDP poll, err: %d", errno);
#endif #endif
return -1; return -1;
} }
if (ret == 0) int index;
for (i = 0; i < n; i++) {
// round robin
index = (i + m_counter) % n;
if (pfd[index].revents & POLLIN)
break;
}
if (i == n)
return 0; return 0;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -221,9 +278,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else #else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); ssize_t len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif #endif
if (len <= 0) { if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -234,6 +291,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
return -1; return -1;
} }
m_counter++;
address_length = size; address_length = size;
return len; return len;
} }
@ -243,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
assert(buffer != NULL); assert(buffer != NULL);
assert(length > 0U); assert(length > 0U);
bool result = false;
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
if (m_fd[i] < 0 || m_af[i] != address.ss_family)
continue;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length); int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else #else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length); ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif #endif
if (ret < 0) {
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from sendto, err: %lu", ::GetLastError()); LogError("Error returned from sendto, err: %lu", ::GetLastError());
#else #else
LogError("Error returned from sendto, err: %d", errno); LogError("Error returned from sendto, err: %d", errno);
#endif #endif
return false; } else {
}
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
if (ret != int(length)) if (ret == int(length))
return false; result = true;
#else #else
if (ret != ssize_t(length)) if (ret == ssize_t(length))
return false; result = true;
#endif #endif
}
}
return true; return result;
} }
void CUDPSocket::close() void CUDPSocket::close()
{ {
for (int i = 0; i < UDP_SOCKET_MAX; i++)
close(m_fd[i]);
}
void CUDPSocket::close(const unsigned int index)
{
if (m_fd[index] >= 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
::closesocket(m_fd); ::closesocket(m_fd[index]);
#else #else
::close(m_fd); ::close(m_fd[index]);
#endif #endif
m_fd[index] = -1;
}
} }

@ -26,6 +26,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <poll.h>
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -35,29 +36,45 @@
#include <ws2tcpip.h> #include <ws2tcpip.h>
#endif #endif
#if !defined(UDP_SOCKET_MAX)
#define UDP_SOCKET_MAX 1
#endif
enum IPMATCHTYPE {
IMT_ADDRESS_AND_PORT,
IMT_ADDRESS_ONLY
};
class CUDPSocket { class CUDPSocket {
public: public:
CUDPSocket(const std::string& address, unsigned int port = 0U); CUDPSocket(const std::string& address, unsigned int port = 0U);
CUDPSocket(unsigned int port = 0U); CUDPSocket(unsigned int port = 0U);
~CUDPSocket(); ~CUDPSocket();
bool open(); bool open(unsigned int af = AF_UNSPEC);
bool open(const unsigned int af); bool open(const sockaddr_storage& address);
bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length); 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); bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close(); void close();
void close(const unsigned int index);
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);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints); 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(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT);
static bool isnone(const sockaddr_storage& addr);
static bool isNone(const sockaddr_storage& addr);
private: private:
std::string m_address; std::string m_address_save;
unsigned short m_port; unsigned short m_port_save;
int m_fd; std::string m_address[UDP_SOCKET_MAX];
unsigned short m_port[UDP_SOCKET_MAX];
unsigned int m_af[UDP_SOCKET_MAX];
int m_fd[UDP_SOCKET_MAX];
unsigned int m_counter;
}; };
#endif #endif

@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20200903"; const char* VERSION = "20200906";
#endif #endif

@ -93,9 +93,14 @@ std::string CYSFNetwork::getDesc(unsigned int dgId)
bool CYSFNetwork::open() bool CYSFNetwork::open()
{ {
if (m_addrLen == 0U) {
LogError("Unable to resolve the address of the YSF network");
return false;
}
LogMessage("Opening YSF network connection"); LogMessage("Opening YSF network connection");
return m_socket.open(); return m_socket.open(m_addr);
} }
void CYSFNetwork::write(unsigned int dgid, const unsigned char* data) void CYSFNetwork::write(unsigned int dgid, const unsigned char* data)

@ -60,12 +60,18 @@ bool CYSFReflectors::load()
std::string host = std::string(p4); std::string host = std::string(p4);
unsigned int port = (unsigned int)::atoi(p5); unsigned int port = (unsigned int)::atoi(p5);
CYSFReflector* refl = new CYSFReflector; sockaddr_storage addr;
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen); unsigned int addrLen;
refl->m_id = std::string(p1); if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
refl->m_name = std::string(p2); CYSFReflector* refl = new CYSFReflector;
refl->m_id = std::string(p1);
m_reflectors.push_back(refl); refl->m_name = std::string(p2);
refl->m_addr = addr;
refl->m_addrLen = addrLen;
m_reflectors.push_back(refl);
} else {
LogWarning("Unable to resolve the address for %s", host.c_str());
}
} }
} }

Loading…
Cancel
Save