1
0
Fork 0

Move DG-ID Gateway to Ipv6

ycs232-kbc
Jonathan Naylor 4 years ago
parent 3c84e47ea8
commit 4d986cbbd9

@ -36,8 +36,8 @@ m_longitude(0.0F),
m_height(0), m_height(0),
m_desc(), m_desc(),
m_suffix(suffix), m_suffix(suffix),
m_aprsAddress(), m_aprsAddr(),
m_aprsPort(port), m_aprsAddrLen(),
m_aprsSocket() m_aprsSocket()
#if defined(USE_GPSD) #if defined(USE_GPSD)
,m_gpsdEnabled(false), ,m_gpsdEnabled(false),
@ -55,7 +55,7 @@ m_gpsdData()
m_callsign.append(rptSuffix.substr(0U, 1U)); m_callsign.append(rptSuffix.substr(0U, 1U));
} }
m_aprsAddress = CUDPSocket::lookup(address); CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen);
} }
CAPRSWriter::~CAPRSWriter() CAPRSWriter::~CAPRSWriter()
@ -176,7 +176,7 @@ void CAPRSWriter::write(const unsigned char* source, const char* type, unsigned
if (m_debug) if (m_debug)
LogDebug("APRS ==> %s", output); LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort); m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
} }
void CAPRSWriter::clock(unsigned int ms) void CAPRSWriter::clock(unsigned int ms)
@ -275,7 +275,7 @@ void CAPRSWriter::sendIdFrameFixed()
if (m_debug) if (m_debug)
LogDebug("APRS ==> %s", output); LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort); m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
} }
#if defined(USE_GPSD) #if defined(USE_GPSD)
@ -376,6 +376,6 @@ void CAPRSWriter::sendIdFrameMobile()
if (m_debug) if (m_debug)
LogDebug("APRS ==> %s", output); LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort); m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
} }
#endif #endif

@ -70,8 +70,8 @@ private:
int m_height; int m_height;
std::string m_desc; std::string m_desc;
std::string m_suffix; std::string m_suffix;
in_addr m_aprsAddress; sockaddr_storage m_aprsAddr;
unsigned int m_aprsPort; unsigned int m_aprsAddrLen;
CUDPSocket m_aprsSocket; CUDPSocket m_aprsSocket;
#if defined(USE_GPSD) #if defined(USE_GPSD)
bool m_gpsdEnabled; bool m_gpsdEnabled;

@ -180,13 +180,15 @@ int CDGIdGateway::run()
m_callsign = m_conf.getCallsign(); m_callsign = m_conf.getCallsign();
m_suffix = m_conf.getSuffix(); m_suffix = m_conf.getSuffix();
sockaddr_storage rptAddr;
unsigned int rptAddrLen;
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
bool debug = m_conf.getDebug(); bool debug = m_conf.getDebug();
in_addr rptAddress = CUDPSocket::lookup(m_conf.getRptAddress());
unsigned int rptPort = m_conf.getRptPort();
std::string myAddress = m_conf.getMyAddress(); std::string myAddress = m_conf.getMyAddress();
unsigned int myPort = m_conf.getMyPort(); unsigned int myPort = m_conf.getMyPort();
CYSFNetwork rptNetwork(myAddress, myPort, "MMDVM", rptAddress, rptPort, m_callsign, debug); CYSFNetwork rptNetwork(myAddress, myPort, "MMDVM", rptAddr, rptAddrLen, m_callsign, debug);
ret = rptNetwork.open(); ret = rptNetwork.open();
if (!ret) { if (!ret) {
::LogError("Cannot open the repeater network port"); ::LogError("Cannot open the repeater network port");
@ -239,7 +241,7 @@ int CDGIdGateway::run()
CYSFReflector* reflector = reflectors->findByName(name); CYSFReflector* reflector = reflectors->findByName(name);
if (reflector != NULL) { if (reflector != NULL) {
dgIdNetwork[dgid] = new CYSFNetwork(local, reflector->m_name, reflector->m_address, reflector->m_port, m_callsign, debug);; dgIdNetwork[dgid] = new CYSFNetwork(local, reflector->m_name, reflector->m_addr, reflector->m_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;
@ -253,7 +255,7 @@ int CDGIdGateway::run()
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; IMRSDest* dest = new IMRSDest;
dest->m_address = CUDPSocket::lookup((*it)->m_address); CUDPSocket::lookup((*it)->m_address, IMRS_PORT, dest->m_addr, dest->m_addrLen);
dest->m_dgId = (*it)->m_dgId; dest->m_dgId = (*it)->m_dgId;
dests.push_back(dest); dests.push_back(dest);
} }
@ -267,54 +269,54 @@ int CDGIdGateway::run()
dgIdNetwork[dgid]->m_netHangTime = netHangTime; dgIdNetwork[dgid]->m_netHangTime = netHangTime;
} }
} else if (type == "Parrot") { } else if (type == "Parrot") {
in_addr address = CUDPSocket::lookup((*it)->m_address);
unsigned int port = (*it)->m_port;
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
if (address.s_addr != INADDR_NONE) { sockaddr_storage addr;
dgIdNetwork[dgid] = new CYSFNetwork(local, "PARROT", address, port, m_callsign, debug); unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
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 if (type == "YSF2DMR") { } else if (type == "YSF2DMR") {
in_addr address = CUDPSocket::lookup((*it)->m_address);
unsigned int port = (*it)->m_port;
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
if (address.s_addr != INADDR_NONE) { sockaddr_storage addr;
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2DMR", address, port, m_callsign, debug); unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
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 if (type == "YSF2NXDN") { } else if (type == "YSF2NXDN") {
in_addr address = CUDPSocket::lookup((*it)->m_address);
unsigned int port = (*it)->m_port;
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
if (address.s_addr != INADDR_NONE) { sockaddr_storage addr;
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2NXDN", address, port, m_callsign, debug); unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
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 if (type == "YSF2P25") { } else if (type == "YSF2P25") {
in_addr address = CUDPSocket::lookup((*it)->m_address);
unsigned int port = (*it)->m_port;
unsigned int local = (*it)->m_local; unsigned int local = (*it)->m_local;
if (address.s_addr != INADDR_NONE) { sockaddr_storage addr;
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2P25", address, port, m_callsign, debug); unsigned int addrLen;
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
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;
} }
}
if (dgIdNetwork[dgid] != NULL && dgIdNetwork[dgid] != imrs) { if (dgIdNetwork[dgid] != NULL && dgIdNetwork[dgid] != imrs) {
bool ret = dgIdNetwork[dgid]->open(); bool ret = dgIdNetwork[dgid]->open();

@ -32,7 +32,8 @@ 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, bool debug) :
m_socket(port), m_socket(port),
m_debug(debug), m_debug(debug),
m_address(), m_addr(),
m_addrLen(0U),
m_ping(NULL), m_ping(NULL),
m_info(NULL), m_info(NULL),
m_reflector(reflector), m_reflector(reflector),
@ -72,16 +73,48 @@ bool CFCSNetwork::open()
{ {
LogMessage("Resolving FCS00x addresses"); LogMessage("Resolving FCS00x addresses");
m_addresses["FCS001"] = CUDPSocket::lookup("fcs001.xreflector.net"); sockaddr_storage addr;
m_addresses["FCS002"] = CUDPSocket::lookup("fcs002.xreflector.net"); unsigned int addrLen;
m_addresses["FCS003"] = CUDPSocket::lookup("fcs003.xreflector.net");
m_addresses["FCS004"] = CUDPSocket::lookup("fcs004.xreflector.net"); CUDPSocket::lookup("fcs001.xreflector.net", FCS_PORT, addr, addrLen);
m_addresses["FCS005"] = CUDPSocket::lookup("fcs005.xreflector.net"); std::pair<sockaddr_storage, unsigned int> entry = std::make_pair(addr, addrLen);
m_addresses["FCS222"] = CUDPSocket::lookup("fcs222.xreflector.net"); m_addresses["FCS001"] = entry;
m_addresses["FCS224"] = CUDPSocket::lookup("fcs224.xreflector.net");
m_addresses["FCS232"] = CUDPSocket::lookup("fcs232.xreflector.net"); CUDPSocket::lookup("fcs002.xreflector.net", FCS_PORT, addr, addrLen);
m_addresses["FCS260"] = CUDPSocket::lookup("fcs260.xreflector.net"); entry = std::make_pair(addr, addrLen);
m_addresses["FCS262"] = CUDPSocket::lookup("fcs262.xreflector.net"); 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");
@ -103,7 +136,7 @@ void CFCSNetwork::write(unsigned int dgid, const unsigned char* data)
if (m_debug) if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U); CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U);
m_socket.write(buffer, 130U, m_address, FCS_PORT); m_socket.write(buffer, 130U, m_addr, m_addrLen);
} }
void CFCSNetwork::link() void CFCSNetwork::link()
@ -117,11 +150,9 @@ void CFCSNetwork::link()
return; return;
} }
m_address = m_addresses[name]; std::pair<sockaddr_storage, unsigned int> entry = m_addresses[name];
if (m_address.s_addr == INADDR_NONE) { m_addr = entry.first;
LogError("FCS reflector %s has no address", name.c_str()); m_addrLen = entry.second;
return;
}
m_state = FCS_LINKING; m_state = FCS_LINKING;
@ -135,7 +166,7 @@ void CFCSNetwork::unlink()
if (m_state != FCS_LINKED) if (m_state != FCS_LINKED)
return; return;
m_socket.write((unsigned char*)"CLOSE ", 11U, m_address, FCS_PORT); m_socket.write((unsigned char*)"CLOSE ", 11U, m_addr, m_addrLen);
} }
void CFCSNetwork::clock(unsigned int ms) void CFCSNetwork::clock(unsigned int ms)
@ -154,16 +185,16 @@ void CFCSNetwork::clock(unsigned int ms)
unsigned char buffer[BUFFER_LENGTH]; unsigned char buffer[BUFFER_LENGTH];
in_addr address; sockaddr_storage addr;
unsigned int port; unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0) if (length <= 0)
return; return;
if (m_state == FCS_UNLINKED) if (m_state == FCS_UNLINKED)
return; return;
if (address.s_addr != m_address.s_addr || port != FCS_PORT) if (!CUDPSocket::match(addr, m_addr))
return; return;
if (m_debug) if (m_debug)
@ -242,7 +273,7 @@ void CFCSNetwork::writeInfo()
if (m_debug) if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_info, 100U); CUtils::dump(1U, "FCS Network Data Sent", m_info, 100U);
m_socket.write(m_info, 100U, m_address, FCS_PORT); m_socket.write(m_info, 100U, m_addr, m_addrLen);
} }
void CFCSNetwork::writePing() void CFCSNetwork::writePing()
@ -253,5 +284,5 @@ void CFCSNetwork::writePing()
if (m_debug) if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_ping, 25U); CUtils::dump(1U, "FCS Network Data Sent", m_ping, 25U);
m_socket.write(m_ping, 25U, m_address, FCS_PORT); m_socket.write(m_ping, 25U, m_addr, m_addrLen);
} }

@ -59,13 +59,14 @@ public:
private: private:
CUDPSocket m_socket; CUDPSocket m_socket;
bool m_debug; bool m_debug;
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_addrLen;
unsigned char* m_ping; unsigned char* m_ping;
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, in_addr> m_addresses; 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;

@ -26,8 +26,6 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
const unsigned int IMRS_PORT = 21110U;
CIMRSNetwork::CIMRSNetwork() : CIMRSNetwork::CIMRSNetwork() :
m_socket(IMRS_PORT), m_socket(IMRS_PORT),
@ -134,7 +132,7 @@ void CIMRSNetwork::write(unsigned int dgId, const unsigned char* data)
if (ptr->m_debug) if (ptr->m_debug)
CUtils::dump(1U, "IMRS Network Data Sent", buffer, 165U); CUtils::dump(1U, "IMRS Network Data Sent", buffer, 165U);
m_socket.write(buffer, 165U, (*it)->m_address, IMRS_PORT); m_socket.write(buffer, 165U, (*it)->m_addr, (*it)->m_addrLen);
} }
} }
@ -150,20 +148,19 @@ void CIMRSNetwork::clock(unsigned int ms)
{ {
unsigned char buffer[500U]; unsigned char buffer[500U];
in_addr address; sockaddr_storage addr;
unsigned int port; unsigned int addrLen;
int length = m_socket.read(buffer, 500U, address, port); int length = m_socket.read(buffer, 500U, addr, addrLen);
if (length <= 0) if (length <= 0)
return; return;
LogDebug("IMRS Network Data Reecived from port %u", port);
CUtils::dump(1U, "IMRS Network Data Received", buffer, length); CUtils::dump(1U, "IMRS Network Data Received", buffer, length);
return; return;
if (port != IMRS_PORT) // if (port != IMRS_PORT)
return; // return;
IMRSDGId* ptr = find(address); IMRSDGId* ptr = find(addr);
if (ptr == NULL) if (ptr == NULL)
return; return;
@ -201,11 +198,11 @@ void CIMRSNetwork::close()
m_socket.close(); m_socket.close();
} }
IMRSDGId* CIMRSNetwork::find(in_addr address) const IMRSDGId* CIMRSNetwork::find(const sockaddr_storage& addr) const
{ {
for (std::vector<IMRSDGId*>::const_iterator it1 = m_dgIds.begin(); it1 != m_dgIds.end(); ++it1) { for (std::vector<IMRSDGId*>::const_iterator it1 = m_dgIds.begin(); it1 != m_dgIds.end(); ++it1) {
for (std::vector<IMRSDest*>::const_iterator it2 = (*it1)->m_destinations.begin(); it2 != (*it1)->m_destinations.end(); ++it2) { for (std::vector<IMRSDest*>::const_iterator it2 = (*it1)->m_destinations.begin(); it2 != (*it1)->m_destinations.end(); ++it2) {
if (address.s_addr == (*it2)->m_address.s_addr) if (CUDPSocket::match(addr, (*it2)->m_addr))
return *it1; return *it1;
} }
} }

@ -28,7 +28,8 @@
#include <string> #include <string>
struct IMRSDest { struct IMRSDest {
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_addrLen;
unsigned int m_dgId; unsigned int m_dgId;
}; };
@ -76,7 +77,7 @@ private:
CUDPSocket m_socket; CUDPSocket m_socket;
std::vector<IMRSDGId*> m_dgIds; std::vector<IMRSDGId*> m_dgIds;
IMRSDGId* find(in_addr address) const; IMRSDGId* find(const sockaddr_storage& address) const;
IMRSDGId* find(unsigned int dgId) const; IMRSDGId* find(unsigned int dgId) const;
}; };

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2016 by Jonathan Naylor G4KLX * Copyright (C) 2006-2016,2020 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
#endif #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; struct addrinfo hints;
#if defined(_WIN32) || defined(_WIN64) ::memset(&hints, 0, sizeof(hints));
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
}
struct hostent* hp = ::gethostbyname(hostname.c_str()); return lookup(hostname, port, addr, address_length, hints);
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
} }
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
std::string portstr = std::to_string(port);
struct addrinfo *res;
/* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV;
int err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err != 0) {
sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0x00U, 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()); LogError("Cannot find address for host %s", hostname.c_str());
return err;
}
addr.s_addr = INADDR_NONE; ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
return addr;
#else freeaddrinfo(res);
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) { return 0;
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
if (hp != NULL) { {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); if (addr1.ss_family != addr2.ss_family)
return addr; 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::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
addr.s_addr = INADDR_NONE; return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
return addr;
#endif
} }
bool CUDPSocket::open() bool CUDPSocket::open()
{ {
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0); return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
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. */
int err = lookup(m_address, m_port, addr, addrlen, hints);
if (err != 0) {
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 (m_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());
@ -115,24 +157,6 @@ bool CUDPSocket::open()
} }
if (m_port > 0U) { 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; int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -143,7 +167,7 @@ bool CUDPSocket::open()
return false; 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) #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
@ -158,7 +182,7 @@ bool CUDPSocket::open()
return true; 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(buffer != NULL);
assert(length > 0U); assert(length > 0U);
@ -190,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0) if (ret == 0)
return 0; return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in); int size = sizeof(sockaddr_storage);
#else #else
socklen_t size = sizeof(sockaddr_in); socklen_t size = sizeof(sockaddr_storage);
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #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 #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 #endif
if (len <= 0) { if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -211,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1; return -1;
} }
address = addr.sin_addr; address_length = size;
port = ntohs(addr.sin_port);
return len; 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(buffer != NULL);
assert(length > 0U); 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) #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 #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 #endif
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -31,7 +31,8 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#else #else
#include <winsock.h> #include <winsock2.h>
#include <ws2tcpip.h>
#endif #endif
class CUDPSocket { class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket(); ~CUDPSocket();
bool open(); bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, 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 in_addr& address, unsigned int port); bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close(); 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 isnone(const sockaddr_storage& addr);
private: private:
std::string m_address; std::string m_address;

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

@ -49,4 +49,6 @@ const unsigned char YSF_MR_BUSY = 0x02U;
const unsigned int FCS_PORT = 62500U; const unsigned int FCS_PORT = 62500U;
const unsigned int IMRS_PORT = 21110U;
#endif #endif

@ -27,11 +27,11 @@
const unsigned int BUFFER_LENGTH = 200U; const unsigned int BUFFER_LENGTH = 200U;
CYSFNetwork::CYSFNetwork(const std::string& localAddress, unsigned int localPort, const std::string& name, const in_addr& address, unsigned int port, const std::string& callsign, bool debug) : CYSFNetwork::CYSFNetwork(const std::string& localAddress, unsigned int localPort, const std::string& name, const sockaddr_storage& addr, unsigned int addrLen, const std::string& callsign, bool debug) :
m_socket(localAddress, localPort), m_socket(localAddress, localPort),
m_debug(debug), m_debug(debug),
m_address(address), m_addr(addr),
m_port(port), m_addrLen(addrLen),
m_poll(NULL), m_poll(NULL),
m_unlink(NULL), m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer"), m_buffer(1000U, "YSF Network Buffer"),
@ -54,11 +54,11 @@ m_linked(true)
} }
} }
CYSFNetwork::CYSFNetwork(unsigned int localPort, const std::string& name, const in_addr& address, unsigned int port, const std::string& callsign, bool debug) : CYSFNetwork::CYSFNetwork(unsigned int localPort, const std::string& name, const sockaddr_storage& addr, unsigned int addrLen, const std::string& callsign, bool debug) :
m_socket(localPort), m_socket(localPort),
m_debug(debug), m_debug(debug),
m_address(address), m_addr(addr),
m_port(port), m_addrLen(addrLen),
m_poll(NULL), m_poll(NULL),
m_unlink(NULL), m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer"), m_buffer(1000U, "YSF Network Buffer"),
@ -108,7 +108,7 @@ void CYSFNetwork::write(unsigned int dgid, const unsigned char* data)
if (m_debug) if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", data, 155U); CUtils::dump(1U, "YSF Network Data Sent", data, 155U);
m_socket.write(data, 155U, m_address, m_port); m_socket.write(data, 155U, m_addr, m_addrLen);
} }
void CYSFNetwork::link() void CYSFNetwork::link()
@ -120,36 +120,35 @@ void CYSFNetwork::writePoll()
{ {
m_pollTimer.start(); m_pollTimer.start();
m_socket.write(m_poll, 14U, m_address, m_port); m_socket.write(m_poll, 14U, m_addr, m_addrLen);
} }
void CYSFNetwork::unlink() void CYSFNetwork::unlink()
{ {
m_pollTimer.stop(); m_pollTimer.stop();
m_socket.write(m_unlink, 14U, m_address, m_port); m_socket.write(m_unlink, 14U, m_addr, m_addrLen);
m_linked = false; m_linked = false;
} }
void CYSFNetwork::clock(unsigned int ms) void CYSFNetwork::clock(unsigned int ms)
{ {
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
m_pollTimer.clock(ms); m_pollTimer.clock(ms);
if (m_pollTimer.isRunning() && m_pollTimer.hasExpired()) if (m_pollTimer.isRunning() && m_pollTimer.hasExpired())
writePoll(); writePoll();
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); unsigned char buffer[BUFFER_LENGTH];
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0) if (length <= 0)
return; return;
if (m_port == 0U) if (m_addrLen == 0U)
return; return;
if (address.s_addr != m_address.s_addr || port != m_port) if (!CUDPSocket::match(addr, m_addr))
return; return;
if (::memcmp(buffer, "YSFP", 4U) == 0 && !m_linked) { if (::memcmp(buffer, "YSFP", 4U) == 0 && !m_linked) {

@ -30,8 +30,8 @@
class CYSFNetwork : public CDGIdNetwork { class CYSFNetwork : public CDGIdNetwork {
public: public:
CYSFNetwork(const std::string& localAddress, unsigned int localPort, const std::string& name, const in_addr& address, unsigned int port, const std::string& callsign, bool debug); CYSFNetwork(const std::string& localAddress, unsigned int localPort, const std::string& name, const sockaddr_storage& addr, unsigned int addrLen, const std::string& callsign, bool debug);
CYSFNetwork(unsigned int localPort, const std::string& name, const in_addr& address, unsigned int port, const std::string& callsign, bool debug); CYSFNetwork(unsigned int localPort, const std::string& name, const sockaddr_storage& addr, unsigned int addrLen, const std::string& callsign, bool debug);
virtual ~CYSFNetwork(); virtual ~CYSFNetwork();
virtual std::string getDesc(unsigned int dgId); virtual std::string getDesc(unsigned int dgId);
@ -53,8 +53,8 @@ public:
private: private:
CUDPSocket m_socket; CUDPSocket m_socket;
bool m_debug; bool m_debug;
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_port; unsigned int m_addrLen;
unsigned char* m_poll; unsigned char* m_poll;
unsigned char* m_unlink; unsigned char* m_unlink;
CRingBuffer<unsigned char> m_buffer; CRingBuffer<unsigned char> m_buffer;

@ -58,19 +58,16 @@ bool CYSFReflectors::load()
if (p1 != NULL && p2 != NULL && p3 != NULL && p4 != NULL && p5 != NULL && p6 != NULL) { if (p1 != NULL && p2 != NULL && p3 != NULL && p4 != NULL && p5 != NULL && p6 != NULL) {
std::string host = std::string(p4); std::string host = std::string(p4);
unsigned int port = (unsigned int)::atoi(p5);
in_addr address = CUDPSocket::lookup(host);
if (address.s_addr != INADDR_NONE) {
CYSFReflector* refl = new CYSFReflector; CYSFReflector* refl = new CYSFReflector;
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
refl->m_id = std::string(p1); refl->m_id = std::string(p1);
refl->m_name = std::string(p2); refl->m_name = std::string(p2);
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p5);
m_reflectors.push_back(refl); m_reflectors.push_back(refl);
} }
} }
}
::fclose(fp); ::fclose(fp);
} }

@ -29,15 +29,15 @@ public:
CYSFReflector() : CYSFReflector() :
m_id(), m_id(),
m_name(), m_name(),
m_address(), m_addr(),
m_port(0U) m_addrLen(0U)
{ {
} }
std::string m_id; std::string m_id;
std::string m_name; std::string m_name;
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_port; unsigned int m_addrLen;
}; };
class CYSFReflectors { class CYSFReflectors {

Loading…
Cancel
Save