Conversion to IPv6

This commit is contained in:
Jonathan Naylor 2020-09-03 14:00:48 +01:00
parent 05d56f078e
commit 59dd67a952
20 changed files with 450 additions and 405 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,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
@ -43,22 +43,19 @@ bool CNetwork::open()
return m_socket.open(); return m_socket.open();
} }
bool CNetwork::writeData(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port) bool CNetwork::writeData(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen)
{ {
assert(data != NULL); assert(data != NULL);
assert(length > 0U); assert(length > 0U);
assert(port > 0U);
if (m_debug) if (m_debug)
CUtils::dump(1U, "P25 Network Data Sent", data, length); CUtils::dump(1U, "P25 Network Data Sent", data, length);
return m_socket.write(data, length, address, port); return m_socket.write(data, length, addr, addrLen);
} }
bool CNetwork::writePoll(const in_addr& address, unsigned int port) bool CNetwork::writePoll(const sockaddr_storage& addr, unsigned int addrLen)
{ {
assert(port > 0U);
unsigned char data[15U]; unsigned char data[15U];
data[0U] = 0xF0U; data[0U] = 0xF0U;
@ -69,13 +66,11 @@ bool CNetwork::writePoll(const in_addr& address, unsigned int port)
if (m_debug) if (m_debug)
CUtils::dump(1U, "P25 Network Poll Sent", data, 11U); CUtils::dump(1U, "P25 Network Poll Sent", data, 11U);
return m_socket.write(data, 11U, address, port); return m_socket.write(data, 11U, addr, addrLen);
} }
bool CNetwork::writeUnlink(const in_addr& address, unsigned int port) bool CNetwork::writeUnlink(const sockaddr_storage& addr, unsigned int addrLen)
{ {
assert(port > 0U);
unsigned char data[15U]; unsigned char data[15U];
data[0U] = 0xF1U; data[0U] = 0xF1U;
@ -86,15 +81,15 @@ bool CNetwork::writeUnlink(const in_addr& address, unsigned int port)
if (m_debug) if (m_debug)
CUtils::dump(1U, "P25 Network Unlink Sent", data, 11U); CUtils::dump(1U, "P25 Network Unlink Sent", data, 11U);
return m_socket.write(data, 11U, address, port); return m_socket.write(data, 11U, addr, addrLen);
} }
unsigned int CNetwork::readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port) unsigned int CNetwork::readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen)
{ {
assert(data != NULL); assert(data != NULL);
assert(length > 0U); assert(length > 0U);
int len = m_socket.read(data, length, address, port); int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0) if (len <= 0)
return 0U; return 0U;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,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,13 +31,13 @@ public:
bool open(); bool open();
bool writeData(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port); bool writeData(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen);
unsigned int readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port); unsigned int readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen);
bool writePoll(const in_addr& address, unsigned int port); bool writePoll(const sockaddr_storage& addr, unsigned int addrLen);
bool writeUnlink(const in_addr& address, unsigned int port); bool writeUnlink(const sockaddr_storage& addr, unsigned int addrLen);
void close(); void close();

View File

@ -167,8 +167,9 @@ void CP25Gateway::run()
} }
#endif #endif
in_addr rptAddr = CUDPSocket::lookup(m_conf.getRptAddress()); sockaddr_storage rptAddr;
unsigned int rptPort = m_conf.getRptPort(); unsigned int rptAddrLen;
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
CUDPSocket* remoteSocket = NULL; CUDPSocket* remoteSocket = NULL;
if (m_conf.getRemoteCommandsEnabled()) { if (m_conf.getRemoteCommandsEnabled()) {
@ -228,8 +229,8 @@ void CP25Gateway::run()
unsigned int dstId = 0U; unsigned int dstId = 0U;
unsigned int currentId = 9999U; unsigned int currentId = 9999U;
in_addr currentAddr; unsigned int currentAddrLen = 0U;
unsigned int currentPort = 0U; sockaddr_storage currentAddr;
unsigned int startupId = m_conf.getNetworkStartup(); unsigned int startupId = m_conf.getNetworkStartup();
bool p252dmr_enabled = (startupId == 20) ? true : false; bool p252dmr_enabled = (startupId == 20) ? true : false;
@ -238,16 +239,16 @@ void CP25Gateway::run()
CP25Reflector* reflector = reflectors.find(startupId); CP25Reflector* reflector = reflectors.find(startupId);
if (reflector != NULL) { if (reflector != NULL) {
currentId = startupId; currentId = startupId;
currentAddr = reflector->m_address; currentAddr = reflector->m_addr;
currentPort = reflector->m_port; currentAddrLen = reflector->m_addrLen;
inactivityTimer.start(); inactivityTimer.start();
pollTimer.start(); pollTimer.start();
lostTimer.start(); lostTimer.start();
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
LogMessage("Linked at startup to reflector %u", currentId); LogMessage("Linked at startup to reflector %u", currentId);
} }
@ -255,14 +256,14 @@ void CP25Gateway::run()
for (;;) { for (;;) {
unsigned char buffer[200U]; unsigned char buffer[200U];
in_addr address; sockaddr_storage addr;
unsigned int port; unsigned int addrlen;
// From the reflector to the MMDVM // From the reflector to the MMDVM
unsigned int len = remoteNetwork.readData(buffer, 200U, address, port); unsigned int len = remoteNetwork.readData(buffer, 200U, addr, addrlen);
if (len > 0U) { if (len > 0U) {
// If we're linked and it's from the right place, send it on // If we're linked and it's from the right place, send it on
if (currentId != 9999U && currentAddr.s_addr == address.s_addr && currentPort == port) { if (currentId != 9999U && CUDPSocket::match(currentAddr, addr)) {
// Don't pass reflector control data through to the MMDVM // Don't pass reflector control data through to the MMDVM
if (buffer[0U] != 0xF0U && buffer[0U] != 0xF1U) { if (buffer[0U] != 0xF0U && buffer[0U] != 0xF1U) {
// Rewrite the LCF and the destination TG // Rewrite the LCF and the destination TG
@ -274,7 +275,7 @@ void CP25Gateway::run()
buffer[3U] = (currentId >> 0) & 0xFFU; buffer[3U] = (currentId >> 0) & 0xFFU;
} }
localNetwork.writeData(buffer, len, rptAddr, rptPort); localNetwork.writeData(buffer, len, rptAddr, rptAddrLen);
} }
// Any network activity is proof that the reflector is alive // Any network activity is proof that the reflector is alive
@ -283,7 +284,7 @@ void CP25Gateway::run()
} }
// From the MMDVM to the reflector or control data // From the MMDVM to the reflector or control data
len = localNetwork.readData(buffer, 200U, address, port); len = localNetwork.readData(buffer, 200U, addr, addrlen);
if (len > 0U) { if (len > 0U) {
if (buffer[0U] == 0x65U) { if (buffer[0U] == 0x65U) {
dstId = (buffer[1U] << 16) & 0xFF0000U; dstId = (buffer[1U] << 16) & 0xFF0000U;
@ -309,9 +310,9 @@ void CP25Gateway::run()
if (currentId != 9999U) { if (currentId != 9999U) {
LogMessage("Unlinked from reflector %u by %s", currentId, callsign.c_str()); LogMessage("Unlinked from reflector %u by %s", currentId, callsign.c_str());
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
inactivityTimer.stop(); inactivityTimer.stop();
pollTimer.stop(); pollTimer.stop();
@ -331,15 +332,15 @@ void CP25Gateway::run()
// Link to the new reflector // Link to the new reflector
if (reflector != NULL) { if (reflector != NULL) {
currentId = dstId; currentId = dstId;
currentAddr = reflector->m_address; currentAddr = reflector->m_addr;
currentPort = reflector->m_port; currentAddrLen = reflector->m_addrLen;
std::string callsign = lookup->find(srcId); std::string callsign = lookup->find(srcId);
LogMessage("Linked to reflector %u by %s", currentId, callsign.c_str()); LogMessage("Linked to reflector %u by %s", currentId, callsign.c_str());
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start(); inactivityTimer.start();
pollTimer.start(); pollTimer.start();
@ -362,7 +363,7 @@ void CP25Gateway::run()
buffer[3U] = (currentId >> 0) & 0xFFU; buffer[3U] = (currentId >> 0) & 0xFFU;
} }
remoteNetwork.writeData(buffer, len, currentAddr, currentPort); remoteNetwork.writeData(buffer, len, currentAddr, currentAddrLen);
inactivityTimer.start(); inactivityTimer.start();
} }
} }
@ -370,7 +371,7 @@ void CP25Gateway::run()
if (voice != NULL) { if (voice != NULL) {
unsigned int length = voice->read(buffer); unsigned int length = voice->read(buffer);
if (length > 0U) if (length > 0U)
localNetwork.writeData(buffer, length, rptAddr, rptPort); localNetwork.writeData(buffer, length, rptAddr, rptAddrLen);
} }
unsigned int ms = stopWatch.elapsed(); unsigned int ms = stopWatch.elapsed();
@ -379,7 +380,7 @@ void CP25Gateway::run()
reflectors.clock(ms); reflectors.clock(ms);
if (remoteSocket != NULL) { if (remoteSocket != NULL) {
int res = remoteSocket->read(buffer, 200U, address, port); int res = remoteSocket->read(buffer, 200U, addr, addrlen);
if (res > 0) { if (res > 0) {
buffer[res] = '\0'; buffer[res] = '\0';
if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) { if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) {
@ -395,9 +396,9 @@ void CP25Gateway::run()
if (voice != NULL) if (voice != NULL)
voice->unlinked(); voice->unlinked();
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
inactivityTimer.stop(); inactivityTimer.stop();
pollTimer.stop(); pollTimer.stop();
@ -406,17 +407,17 @@ void CP25Gateway::run()
currentId = 9999U; currentId = 9999U;
} else if (reflector != NULL && currentId == 9999U) { } else if (reflector != NULL && currentId == 9999U) {
currentId = tg; currentId = tg;
currentAddr = reflector->m_address; currentAddr = reflector->m_addr;
currentPort = reflector->m_port; currentAddrLen = reflector->m_addrLen;
LogMessage("Linked to reflector %u by remote command", currentId); LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL) if (voice != NULL)
voice->linkedTo(currentId); voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start(); inactivityTimer.start();
pollTimer.start(); pollTimer.start();
@ -424,22 +425,22 @@ void CP25Gateway::run()
} else if (reflector != NULL && currentId != 9999U) { } else if (reflector != NULL && currentId != 9999U) {
LogMessage("Unlinked from reflector %u by remote command", currentId); LogMessage("Unlinked from reflector %u by remote command", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
currentId = tg; currentId = tg;
currentAddr = reflector->m_address; currentAddr = reflector->m_addr;
currentPort = reflector->m_port; currentAddrLen = reflector->m_addrLen;
LogMessage("Linked to reflector %u by remote command", currentId); LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL) if (voice != NULL)
voice->linkedTo(currentId); voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start(); inactivityTimer.start();
pollTimer.start(); pollTimer.start();
@ -459,9 +460,9 @@ void CP25Gateway::run()
if (currentId != 9999U && startupId == 9999U) { if (currentId != 9999U && startupId == 9999U) {
LogMessage("Unlinking from %u due to inactivity", currentId); LogMessage("Unlinking from %u due to inactivity", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
if (voice != NULL) if (voice != NULL)
voice->unlinked(); voice->unlinked();
@ -472,16 +473,16 @@ void CP25Gateway::run()
inactivityTimer.stop(); inactivityTimer.stop();
} else if (currentId != startupId) { } else if (currentId != startupId) {
if (currentId != 9999U) { if (currentId != 9999U) {
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentPort); remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
} }
CP25Reflector* reflector = reflectors.find(startupId); CP25Reflector* reflector = reflectors.find(startupId);
if (reflector != NULL) { if (reflector != NULL) {
currentId = startupId; currentId = startupId;
currentAddr = reflector->m_address; currentAddr = reflector->m_addr;
currentPort = reflector->m_port; currentAddrLen = reflector->m_addrLen;
inactivityTimer.start(); inactivityTimer.start();
pollTimer.start(); pollTimer.start();
@ -492,9 +493,9 @@ void CP25Gateway::run()
if (voice != NULL) if (voice != NULL)
voice->linkedTo(currentId); voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
} else { } else {
startupId = 9999U; startupId = 9999U;
inactivityTimer.stop(); inactivityTimer.stop();
@ -507,7 +508,7 @@ void CP25Gateway::run()
pollTimer.clock(ms); pollTimer.clock(ms);
if (pollTimer.isRunning() && pollTimer.hasExpired()) { if (pollTimer.isRunning() && pollTimer.hasExpired()) {
if (currentId != 9999U) if (currentId != 9999U)
remoteNetwork.writePoll(currentAddr, currentPort); remoteNetwork.writePoll(currentAddr, currentAddrLen);
pollTimer.start(); pollTimer.start();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2016,2018,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,6 +31,8 @@ m_hostsFile1(hostsFile1),
m_hostsFile2(hostsFile2), m_hostsFile2(hostsFile2),
m_parrotAddress(), m_parrotAddress(),
m_parrotPort(0U), m_parrotPort(0U),
m_p252dmrAddress(),
m_p252dmrPort(0U),
m_reflectors(), m_reflectors(),
m_timer(1000U, reloadTime * 60U) m_timer(1000U, reloadTime * 60U)
{ {
@ -79,18 +81,14 @@ bool CReflectors::load()
if (p1 != NULL && p2 != NULL && p3 != NULL) { if (p1 != NULL && p2 != NULL && p3 != NULL) {
std::string host = std::string(p2); std::string host = std::string(p2);
unsigned int port = (unsigned int)::atoi(p3);
in_addr address = CUDPSocket::lookup(host);
if (address.s_addr != INADDR_NONE) {
CP25Reflector* refl = new CP25Reflector; CP25Reflector* refl = new CP25Reflector;
refl->m_id = (unsigned int)::atoi(p1); refl->m_id = (unsigned int)::atoi(p1);
refl->m_address = address; CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl); m_reflectors.push_back(refl);
} }
} }
}
::fclose(fp); ::fclose(fp);
} }
@ -111,19 +109,15 @@ bool CReflectors::load()
unsigned int id = (unsigned int)::atoi(p1); unsigned int id = (unsigned int)::atoi(p1);
if (find(id) == NULL) { if (find(id) == NULL) {
std::string host = std::string(p2); std::string host = std::string(p2);
unsigned int port = (unsigned int)::atoi(p3);
in_addr address = CUDPSocket::lookup(host);
if (address.s_addr != INADDR_NONE) {
CP25Reflector* refl = new CP25Reflector; CP25Reflector* refl = new CP25Reflector;
refl->m_id = id; refl->m_id = id;
refl->m_address = address; CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl); m_reflectors.push_back(refl);
} }
} }
} }
}
::fclose(fp); ::fclose(fp);
} }
@ -135,8 +129,7 @@ bool CReflectors::load()
if (m_parrotPort > 0U) { if (m_parrotPort > 0U) {
CP25Reflector* refl = new CP25Reflector; CP25Reflector* refl = new CP25Reflector;
refl->m_id = 10U; refl->m_id = 10U;
refl->m_address = CUDPSocket::lookup(m_parrotAddress); CUDPSocket::lookup(m_parrotAddress, m_parrotPort, refl->m_addr, refl->m_addrLen);
refl->m_port = m_parrotPort;
m_reflectors.push_back(refl); m_reflectors.push_back(refl);
LogInfo("Loaded P25 parrot (TG%u)", refl->m_id); LogInfo("Loaded P25 parrot (TG%u)", refl->m_id);
} }
@ -145,8 +138,7 @@ bool CReflectors::load()
if (m_p252dmrPort > 0U) { if (m_p252dmrPort > 0U) {
CP25Reflector* refl = new CP25Reflector; CP25Reflector* refl = new CP25Reflector;
refl->m_id = 20U; refl->m_id = 20U;
refl->m_address = CUDPSocket::lookup(m_p252dmrAddress); CUDPSocket::lookup(m_p252dmrAddress, m_p252dmrPort, refl->m_addr, refl->m_addrLen);
refl->m_port = m_p252dmrPort;
m_reflectors.push_back(refl); m_reflectors.push_back(refl);
LogInfo("Loaded P252DMR (TG%u)", refl->m_id); LogInfo("Loaded P252DMR (TG%u)", refl->m_id);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2016,2018,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
@ -29,14 +29,14 @@ class CP25Reflector {
public: public:
CP25Reflector() : CP25Reflector() :
m_id(), m_id(),
m_address(), m_addr(),
m_port(0U) m_addrLen(0U)
{ {
} }
unsigned int m_id; unsigned int m_id;
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_port; unsigned int m_addrLen;
}; };
class CReflectors { class CReflectors {

View File

@ -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) { return lookup(hostname, port, addr, address_length, hints);
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
if (hp != NULL) { {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); std::string portstr = std::to_string(port);
return addr; 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;
return addr;
#else
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) {
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); freeaddrinfo(res);
return addr;
return 0;
} }
LogError("Cannot find address for host %s", hostname.c_str()); bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
addr.s_addr = INADDR_NONE; switch (addr1.ss_family) {
return addr; case AF_INET:
#endif 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;
}
}
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
} }
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
@ -151,12 +175,14 @@ bool CUDPSocket::open()
#endif #endif
return false; return false;
} }
LogInfo("Opening UDP port on %u", m_port);
} }
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);
@ -188,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)
@ -209,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)

View File

@ -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;

View File

@ -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

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,2016,2018,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
@ -26,8 +26,8 @@ const unsigned int BUFFER_LENGTH = 200U;
CNetwork::CNetwork(unsigned int port) : CNetwork::CNetwork(unsigned int port) :
m_socket(port), m_socket(port),
m_address(), m_addr(),
m_port(0U) m_addrLen(0U)
{ {
} }
@ -44,24 +44,24 @@ bool CNetwork::open()
bool CNetwork::write(const unsigned char* data, unsigned int length) bool CNetwork::write(const unsigned char* data, unsigned int length)
{ {
if (m_port == 0U) if (m_addrLen == 0U)
return true; return true;
assert(data != NULL); assert(data != NULL);
return m_socket.write(data, length, m_address, m_port); return m_socket.write(data, length, m_addr, m_addrLen);
} }
unsigned int CNetwork::read(unsigned char* data) unsigned int CNetwork::read(unsigned char* data)
{ {
in_addr address; sockaddr_storage addr;
unsigned int port; unsigned int addrlen;
int length = m_socket.read(data, BUFFER_LENGTH, address, port); int length = m_socket.read(data, BUFFER_LENGTH, addr, addrlen);
if (length <= 0) if (length <= 0)
return 0U; return 0U;
m_address.s_addr = address.s_addr; m_addr = addr;
m_port = port; m_addrLen = addrlen;
if (data[0U] == 0xF0U) { // A poll if (data[0U] == 0xF0U) { // A poll
write(data, length); write(data, length);
@ -76,7 +76,7 @@ unsigned int CNetwork::read(unsigned char* data)
void CNetwork::end() void CNetwork::end()
{ {
m_port = 0U; m_addrLen = 0U;
} }
void CNetwork::close() void CNetwork::close()

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,2016,2018,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
@ -41,8 +41,8 @@ public:
private: private:
CUDPSocket m_socket; CUDPSocket m_socket;
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_port; unsigned int m_addrLen;
}; };
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2006-2016 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
@ -61,49 +61,95 @@ 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) { return lookup(hostname, port, addr, address_length, hints);
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints)
if (hp != NULL) { {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); int err;
return addr; 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);
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str()); ::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
return err;
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;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); freeaddrinfo(res);
return addr; return 0;
} }
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str()); bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
addr.s_addr = INADDR_NONE; switch (addr1.ss_family) {
return addr; case AF_INET:
#endif 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;
}
}
bool CUDPSocket::isnone(const sockaddr_storage &addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
return ( (addr.ss_family == AF_INET) &&
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
} }
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)
{
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) {
::fprintf(stderr, "The local address is invalid - %s\n", 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)
::fprintf(stderr, "Cannot create the UDP socket, err: %lu\n", ::GetLastError()); ::fprintf(stderr, "Cannot create the UDP socket, err: %lu\n", ::GetLastError());
@ -114,24 +160,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) {
::fprintf(stderr, "The local address is invalid - %s\n", 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)
@ -142,7 +170,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)
::fprintf(stderr, "Cannot bind the UDP address, err: %lu\n", ::GetLastError()); ::fprintf(stderr, "Cannot bind the UDP address, err: %lu\n", ::GetLastError());
#else #else
@ -157,7 +185,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);
@ -189,17 +217,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)
@ -210,28 +237,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)

View File

@ -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;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX * Copyright (C) 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
@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20161021"; const char* VERSION = "20200903";
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,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
@ -41,24 +41,23 @@ bool CNetwork::open()
return m_socket.open(); return m_socket.open();
} }
bool CNetwork::writeData(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port) bool CNetwork::writeData(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen)
{ {
assert(data != NULL); assert(data != NULL);
assert(length > 0U); assert(length > 0U);
assert(port > 0U);
if (m_debug) if (m_debug)
CUtils::dump(1U, "P25 Network Data Sent", data, length); CUtils::dump(1U, "P25 Network Data Sent", data, length);
return m_socket.write(data, length, address, port); return m_socket.write(data, length, addr, addrLen);
} }
unsigned int CNetwork::readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port) unsigned int CNetwork::readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen)
{ {
assert(data != NULL); assert(data != NULL);
assert(length > 0U); assert(length > 0U);
int len = m_socket.read(data, length, address, port); int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0) if (len <= 0)
return 0U; return 0U;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,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,9 +31,9 @@ public:
bool open(); bool open();
bool writeData(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port); bool writeData(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen);
unsigned int readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port); unsigned int readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen);
void close(); void close();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX * Copyright (C) 2016,2018,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
@ -196,34 +196,34 @@ void CP25Reflector::run()
for (;;) { for (;;) {
unsigned char buffer[200U]; unsigned char buffer[200U];
in_addr address; sockaddr_storage addr;
unsigned int port; unsigned int addrLen;
unsigned int len = network.readData(buffer, 200U, address, port); unsigned int len = network.readData(buffer, 200U, addr, addrLen);
if (len > 0U) { if (len > 0U) {
CP25Repeater* rpt = findRepeater(address, port); CP25Repeater* rpt = findRepeater(addr);
if (buffer[0U] == 0xF0U) { if (buffer[0U] == 0xF0U) {
if (rpt == NULL) { if (rpt == NULL) {
rpt = new CP25Repeater; rpt = new CP25Repeater;
rpt->m_timer.start(); rpt->m_timer.start();
rpt->m_address = address; rpt->m_addr = addr;
rpt->m_port = port; rpt->m_addrLen = addrLen;
rpt->m_callsign = std::string((char*)(buffer + 1U), 10U); rpt->m_callsign = std::string((char*)(buffer + 1U), 10U);
m_repeaters.push_back(rpt); m_repeaters.push_back(rpt);
LogMessage("Adding %s (%s:%u)", rpt->m_callsign.c_str(), ::inet_ntoa(address), port); LogMessage("Adding %s", rpt->m_callsign.c_str());
} else { } else {
rpt->m_timer.start(); rpt->m_timer.start();
} }
// Return the poll // Return the poll
network.writeData(buffer, len, address, port); network.writeData(buffer, len, addr, addrLen);
} else if (buffer[0U] == 0xF1U && rpt != NULL) { } else if (buffer[0U] == 0xF1U && rpt != NULL) {
LogMessage("Removing %s (%s:%u) unlinked", rpt->m_callsign.c_str(), ::inet_ntoa(address), port); LogMessage("Removing %s unlinked", rpt->m_callsign.c_str());
for (std::vector<CP25Repeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CP25Repeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CP25Repeater* itRpt = *it; CP25Repeater* itRpt = *it;
if (itRpt->m_address.s_addr == rpt->m_address.s_addr && itRpt->m_port == rpt->m_port) { if (CUDPSocket::match(itRpt->m_addr, rpt->m_addr)) {
m_repeaters.erase(it); m_repeaters.erase(it);
delete itRpt; delete itRpt;
break; break;
@ -237,7 +237,7 @@ void CP25Reflector::run()
displayed = false; displayed = false;
seen64 = false; seen64 = false;
seen65 = false; seen65 = false;
LogMessage("Transmission started from %s (%s:%u)", current->m_callsign.c_str(), ::inet_ntoa(address), port); LogMessage("Transmission started from %s", current->m_callsign.c_str());
} }
if (current == rpt) { if (current == rpt) {
@ -266,10 +266,8 @@ void CP25Reflector::run()
} }
for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
in_addr addr = (*it)->m_address; if (!CUDPSocket::match(addr, (*it)->m_addr))
unsigned int prt = (*it)->m_port; network.writeData(buffer, len, (*it)->m_addr, (*it)->m_addrLen);
if (addr.s_addr != address.s_addr || prt != port)
network.writeData(buffer, len, addr, prt);
} }
if (buffer[0U] == 0x80U) { if (buffer[0U] == 0x80U) {
@ -279,7 +277,7 @@ void CP25Reflector::run()
} }
} }
} else { } else {
LogMessage("Data received from an unknown source - %s:%u", ::inet_ntoa(address), port); LogMessage("Data received from an unknown source");
CUtils::dump(2U, "Data", buffer, len); CUtils::dump(2U, "Data", buffer, len);
} }
} }
@ -294,10 +292,7 @@ void CP25Reflector::run()
for (std::vector<CP25Repeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CP25Repeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CP25Repeater* itRpt = *it; CP25Repeater* itRpt = *it;
if (itRpt->m_timer.hasExpired()) { if (itRpt->m_timer.hasExpired()) {
in_addr address = itRpt->m_address; LogMessage("Removing %s disappeared", itRpt->m_callsign.c_str());
unsigned int port = itRpt->m_port;
std::string callsign = itRpt->m_callsign;
LogMessage("Removing %s (%s:%u) disappeared", callsign.c_str(), ::inet_ntoa(address), port);
m_repeaters.erase(it); m_repeaters.erase(it);
delete itRpt; delete itRpt;
break; break;
@ -328,10 +323,10 @@ void CP25Reflector::run()
::LogFinalise(); ::LogFinalise();
} }
CP25Repeater* CP25Reflector::findRepeater(const in_addr& address, unsigned int port) const CP25Repeater* CP25Reflector::findRepeater(const sockaddr_storage& addr) const
{ {
for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if (address.s_addr == (*it)->m_address.s_addr && (*it)->m_port == port) if (CUDPSocket::match(addr, (*it)->m_addr))
return *it; return *it;
} }
@ -348,11 +343,9 @@ void CP25Reflector::dumpRepeaters() const
LogMessage("Currently linked repeaters:"); LogMessage("Currently linked repeaters:");
for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
in_addr address = (*it)->m_address;
unsigned int port = (*it)->m_port;
std::string callsign = (*it)->m_callsign; std::string callsign = (*it)->m_callsign;
unsigned int timer = (*it)->m_timer.getTimer(); unsigned int timer = (*it)->m_timer.getTimer();
unsigned int timeout = (*it)->m_timer.getTimeout(); unsigned int timeout = (*it)->m_timer.getTimeout();
LogMessage(" %s (%s:%u) %u/%u", callsign.c_str(), ::inet_ntoa(address), port, timer, timeout); LogMessage(" %s %u/%u", callsign.c_str(), timer, timeout);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 by Jonathan Naylor G4KLX * Copyright (C) 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
@ -41,15 +41,15 @@
class CP25Repeater { class CP25Repeater {
public: public:
CP25Repeater() : CP25Repeater() :
m_address(), m_addr(),
m_port(0U), m_addrLen(0U),
m_callsign(), m_callsign(),
m_timer(1000U, 120U) m_timer(1000U, 120U)
{ {
} }
in_addr m_address; sockaddr_storage m_addr;
unsigned int m_port; unsigned int m_addrLen;
std::string m_callsign; std::string m_callsign;
CTimer m_timer; CTimer m_timer;
}; };
@ -66,7 +66,7 @@ private:
CConf m_conf; CConf m_conf;
std::vector<CP25Repeater*> m_repeaters; std::vector<CP25Repeater*> m_repeaters;
CP25Repeater* findRepeater(const in_addr& address, unsigned int port) const; CP25Repeater* findRepeater(const sockaddr_storage& addr) const;
void dumpRepeaters() const; void dumpRepeaters() const;
}; };

View File

@ -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) { return lookup(hostname, port, addr, address_length, hints);
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
if (hp != NULL) { {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); std::string portstr = std::to_string(port);
return addr; 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;
return addr;
#else
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) {
addr.s_addr = address;
return addr;
} }
struct hostent* hp = ::gethostbyname(hostname.c_str()); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr)); freeaddrinfo(res);
return addr;
return 0;
} }
LogError("Cannot find address for host %s", hostname.c_str()); bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
addr.s_addr = INADDR_NONE; switch (addr1.ss_family) {
return addr; case AF_INET:
#endif 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;
}
}
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
} }
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
@ -151,12 +175,14 @@ bool CUDPSocket::open()
#endif #endif
return false; return false;
} }
LogInfo("Opening UDP port on %u", m_port);
} }
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);
@ -188,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)
@ -209,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)

View File

@ -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;

View File

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