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
* it under the terms of the GNU General Public License as published by
@ -43,22 +43,19 @@ bool CNetwork::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(length > 0U);
assert(port > 0U);
if (m_debug)
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];
data[0U] = 0xF0U;
@ -69,13 +66,11 @@ bool CNetwork::writePoll(const in_addr& address, unsigned int port)
if (m_debug)
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];
data[0U] = 0xF1U;
@ -86,15 +81,15 @@ bool CNetwork::writeUnlink(const in_addr& address, unsigned int port)
if (m_debug)
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(length > 0U);
int len = m_socket.read(data, length, address, port);
int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0)
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
* it under the terms of the GNU General Public License as published by
@ -31,13 +31,13 @@ public:
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();

View File

@ -167,8 +167,9 @@ void CP25Gateway::run()
}
#endif
in_addr rptAddr = CUDPSocket::lookup(m_conf.getRptAddress());
unsigned int rptPort = m_conf.getRptPort();
sockaddr_storage rptAddr;
unsigned int rptAddrLen;
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
CUDPSocket* remoteSocket = NULL;
if (m_conf.getRemoteCommandsEnabled()) {
@ -228,8 +229,8 @@ void CP25Gateway::run()
unsigned int dstId = 0U;
unsigned int currentId = 9999U;
in_addr currentAddr;
unsigned int currentPort = 0U;
unsigned int currentAddrLen = 0U;
sockaddr_storage currentAddr;
unsigned int startupId = m_conf.getNetworkStartup();
bool p252dmr_enabled = (startupId == 20) ? true : false;
@ -237,17 +238,17 @@ void CP25Gateway::run()
if (startupId != 9999U) {
CP25Reflector* reflector = reflectors.find(startupId);
if (reflector != NULL) {
currentId = startupId;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
currentId = startupId;
currentAddr = reflector->m_addr;
currentAddrLen = reflector->m_addrLen;
inactivityTimer.start();
pollTimer.start();
lostTimer.start();
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
LogMessage("Linked at startup to reflector %u", currentId);
}
@ -255,14 +256,14 @@ void CP25Gateway::run()
for (;;) {
unsigned char buffer[200U];
in_addr address;
unsigned int port;
sockaddr_storage addr;
unsigned int addrlen;
// 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 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
if (buffer[0U] != 0xF0U && buffer[0U] != 0xF1U) {
// Rewrite the LCF and the destination TG
@ -274,7 +275,7 @@ void CP25Gateway::run()
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
@ -283,7 +284,7 @@ void CP25Gateway::run()
}
// 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 (buffer[0U] == 0x65U) {
dstId = (buffer[1U] << 16) & 0xFF0000U;
@ -309,9 +310,9 @@ void CP25Gateway::run()
if (currentId != 9999U) {
LogMessage("Unlinked from reflector %u by %s", currentId, callsign.c_str());
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
inactivityTimer.stop();
pollTimer.stop();
@ -330,16 +331,16 @@ void CP25Gateway::run()
// Link to the new reflector
if (reflector != NULL) {
currentId = dstId;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
currentId = dstId;
currentAddr = reflector->m_addr;
currentAddrLen = reflector->m_addrLen;
std::string callsign = lookup->find(srcId);
LogMessage("Linked to reflector %u by %s", currentId, callsign.c_str());
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start();
pollTimer.start();
@ -362,7 +363,7 @@ void CP25Gateway::run()
buffer[3U] = (currentId >> 0) & 0xFFU;
}
remoteNetwork.writeData(buffer, len, currentAddr, currentPort);
remoteNetwork.writeData(buffer, len, currentAddr, currentAddrLen);
inactivityTimer.start();
}
}
@ -370,7 +371,7 @@ void CP25Gateway::run()
if (voice != NULL) {
unsigned int length = voice->read(buffer);
if (length > 0U)
localNetwork.writeData(buffer, length, rptAddr, rptPort);
localNetwork.writeData(buffer, length, rptAddr, rptAddrLen);
}
unsigned int ms = stopWatch.elapsed();
@ -379,7 +380,7 @@ void CP25Gateway::run()
reflectors.clock(ms);
if (remoteSocket != NULL) {
int res = remoteSocket->read(buffer, 200U, address, port);
int res = remoteSocket->read(buffer, 200U, addr, addrlen);
if (res > 0) {
buffer[res] = '\0';
if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) {
@ -395,9 +396,9 @@ void CP25Gateway::run()
if (voice != NULL)
voice->unlinked();
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
inactivityTimer.stop();
pollTimer.stop();
@ -405,18 +406,18 @@ void CP25Gateway::run()
currentId = 9999U;
} else if (reflector != NULL && currentId == 9999U) {
currentId = tg;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
currentId = tg;
currentAddr = reflector->m_addr;
currentAddrLen = reflector->m_addrLen;
LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start();
pollTimer.start();
@ -424,22 +425,22 @@ void CP25Gateway::run()
} else if (reflector != NULL && currentId != 9999U) {
LogMessage("Unlinked from reflector %u by remote command", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
currentId = tg;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
currentId = tg;
currentAddr = reflector->m_addr;
currentAddrLen = reflector->m_addrLen;
LogMessage("Linked to reflector %u by remote command", currentId);
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
inactivityTimer.start();
pollTimer.start();
@ -459,9 +460,9 @@ void CP25Gateway::run()
if (currentId != 9999U && startupId == 9999U) {
LogMessage("Unlinking from %u due to inactivity", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
if (voice != NULL)
voice->unlinked();
@ -472,16 +473,16 @@ void CP25Gateway::run()
inactivityTimer.stop();
} else if (currentId != startupId) {
if (currentId != 9999U) {
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentPort);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen);
}
CP25Reflector* reflector = reflectors.find(startupId);
if (reflector != NULL) {
currentId = startupId;
currentAddr = reflector->m_address;
currentPort = reflector->m_port;
currentId = startupId;
currentAddr = reflector->m_addr;
currentAddrLen = reflector->m_addrLen;
inactivityTimer.start();
pollTimer.start();
@ -492,9 +493,9 @@ void CP25Gateway::run()
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
} else {
startupId = 9999U;
inactivityTimer.stop();
@ -507,7 +508,7 @@ void CP25Gateway::run()
pollTimer.clock(ms);
if (pollTimer.isRunning() && pollTimer.hasExpired()) {
if (currentId != 9999U)
remoteNetwork.writePoll(currentAddr, currentPort);
remoteNetwork.writePoll(currentAddr, currentAddrLen);
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
* it under the terms of the GNU General Public License as published by
@ -31,6 +31,8 @@ m_hostsFile1(hostsFile1),
m_hostsFile2(hostsFile2),
m_parrotAddress(),
m_parrotPort(0U),
m_p252dmrAddress(),
m_p252dmrPort(0U),
m_reflectors(),
m_timer(1000U, reloadTime * 60U)
{
@ -78,17 +80,13 @@ bool CReflectors::load()
char* p3 = ::strtok(NULL, " \t\r\n");
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;
refl->m_id = (unsigned int)::atoi(p1);
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl);
}
CP25Reflector* refl = new CP25Reflector;
refl->m_id = (unsigned int)::atoi(p1);
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
}
}
@ -110,17 +108,13 @@ bool CReflectors::load()
// Don't allow duplicate reflector ids from the secondary hosts file.
unsigned int id = (unsigned int)::atoi(p1);
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;
refl->m_id = id;
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl);
}
CP25Reflector* refl = new CP25Reflector;
refl->m_id = id;
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
}
}
}
@ -134,9 +128,8 @@ bool CReflectors::load()
// Add the Parrot entry
if (m_parrotPort > 0U) {
CP25Reflector* refl = new CP25Reflector;
refl->m_id = 10U;
refl->m_address = CUDPSocket::lookup(m_parrotAddress);
refl->m_port = m_parrotPort;
refl->m_id = 10U;
CUDPSocket::lookup(m_parrotAddress, m_parrotPort, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
LogInfo("Loaded P25 parrot (TG%u)", refl->m_id);
}
@ -144,9 +137,8 @@ bool CReflectors::load()
// Add the P252DMR entry
if (m_p252dmrPort > 0U) {
CP25Reflector* refl = new CP25Reflector;
refl->m_id = 20U;
refl->m_address = CUDPSocket::lookup(m_p252dmrAddress);
refl->m_port = m_p252dmrPort;
refl->m_id = 20U;
CUDPSocket::lookup(m_p252dmrAddress, m_p252dmrPort, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
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
* it under the terms of the GNU General Public License as published by
@ -29,14 +29,14 @@ class CP25Reflector {
public:
CP25Reflector() :
m_id(),
m_address(),
m_port(0U)
m_addr(),
m_addrLen(0U)
{
}
unsigned int m_id;
in_addr m_address;
unsigned int m_port;
unsigned int m_id;
sockaddr_storage m_addr;
unsigned int m_addrLen;
};
class CReflectors {
@ -58,8 +58,8 @@ private:
std::string m_hostsFile2;
std::string m_parrotAddress;
unsigned int m_parrotPort;
std::string m_p252dmrAddress;
unsigned int m_p252dmrPort;
std::string m_p252dmrAddress;
unsigned int m_p252dmrPort;
std::vector<CP25Reflector*> m_reflectors;
CTimer m_timer;
};

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
* it under the terms of the GNU General Public License as published by
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
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());
return err;
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res);
return 0;
}
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2;
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2;
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
default:
return false;
}
}
LogError("Cannot find address for host %s", hostname.c_str());
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
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());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
}
LogError("Cannot find address for host %s", hostname.c_str());
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
}
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
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 defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
@ -115,24 +157,6 @@ bool CUDPSocket::open()
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -143,7 +167,7 @@ bool CUDPSocket::open()
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else
@ -151,12 +175,14 @@ bool CUDPSocket::open()
#endif
return false;
}
LogInfo("Opening UDP port on %u", m_port);
}
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -188,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -209,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

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
* it under the terms of the GNU General Public License as published by
@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket();
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2);
static bool isnone(const sockaddr_storage& addr);
private:
std::string m_address;

View File

@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20200824";
const char* VERSION = "20200903";
#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
* 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) :
m_socket(port),
m_address(),
m_port(0U)
m_addr(),
m_addrLen(0U)
{
}
@ -44,24 +44,24 @@ bool CNetwork::open()
bool CNetwork::write(const unsigned char* data, unsigned int length)
{
if (m_port == 0U)
if (m_addrLen == 0U)
return true;
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)
{
in_addr address;
unsigned int port;
int length = m_socket.read(data, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrlen;
int length = m_socket.read(data, BUFFER_LENGTH, addr, addrlen);
if (length <= 0)
return 0U;
m_address.s_addr = address.s_addr;
m_port = port;
m_addr = addr;
m_addrLen = addrlen;
if (data[0U] == 0xF0U) { // A poll
write(data, length);
@ -76,7 +76,7 @@ unsigned int CNetwork::read(unsigned char* data)
void CNetwork::end()
{
m_port = 0U;
m_addrLen = 0U;
}
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
* it under the terms of the GNU General Public License as published by
@ -40,9 +40,9 @@ public:
void close();
private:
CUDPSocket m_socket;
in_addr m_address;
unsigned int m_port;
CUDPSocket m_socket;
sockaddr_storage m_addr;
unsigned int m_addrLen;
};
#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
* it under the terms of the GNU General Public License as published by
@ -61,49 +61,95 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints)
{
int err;
std::string portstr = std::to_string(port);
struct addrinfo *res;
/* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV;
err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err) {
sockaddr_in *paddr = (sockaddr_in *)&addr;
::memset(paddr, 0, address_length = sizeof(sockaddr_in));
paddr->sin_family = AF_INET;
paddr->sin_port = htons(port);
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
return err;
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res);
return 0;
}
bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in *)&addr1;
in_2 = (struct sockaddr_in *)&addr2;
return ( (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) &&
(in_1->sin_port == in_2->sin_port) );
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6 *)&addr1;
in6_2 = (struct sockaddr_in6 *)&addr2;
return ( IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) &&
(in6_1->sin6_port == in6_2->sin6_port) );
default:
return false;
}
}
::fprintf(stderr, "Cannot find address for host %s\n", 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;
#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());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
}
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ( (addr.ss_family == AF_INET) &&
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
}
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
int err;
sockaddr_storage addr;
unsigned int addrlen;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = af;
/* to determine protocol family, call lookup() first. */
err = lookup(m_address, m_port, addr, addrlen, hints);
if (err) {
::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 defined(_WIN32) || defined(_WIN64)
::fprintf(stderr, "Cannot create the UDP socket, err: %lu\n", ::GetLastError());
@ -114,24 +160,6 @@ bool CUDPSocket::open()
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
::fprintf(stderr, "The local address is invalid - %s\n", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -142,7 +170,7 @@ bool CUDPSocket::open()
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
::fprintf(stderr, "Cannot bind the UDP address, err: %lu\n", ::GetLastError());
#else
@ -157,7 +185,7 @@ bool CUDPSocket::open()
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -189,17 +217,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -210,28 +237,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

View File

@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket();
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints);
static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2);
static bool isnone(const sockaddr_storage &addr);
private:
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
* it under the terms of the GNU General Public License as published by
@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20161021";
const char* VERSION = "20200903";
#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
* it under the terms of the GNU General Public License as published by
@ -41,24 +41,23 @@ bool CNetwork::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(length > 0U);
assert(port > 0U);
if (m_debug)
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(length > 0U);
int len = m_socket.read(data, length, address, port);
int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0)
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
* it under the terms of the GNU General Public License as published by
@ -31,9 +31,9 @@ public:
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();

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
* it under the terms of the GNU General Public License as published by
@ -196,34 +196,34 @@ void CP25Reflector::run()
for (;;) {
unsigned char buffer[200U];
in_addr address;
unsigned int port;
sockaddr_storage addr;
unsigned int addrLen;
unsigned int len = network.readData(buffer, 200U, address, port);
unsigned int len = network.readData(buffer, 200U, addr, addrLen);
if (len > 0U) {
CP25Repeater* rpt = findRepeater(address, port);
CP25Repeater* rpt = findRepeater(addr);
if (buffer[0U] == 0xF0U) {
if (rpt == NULL) {
rpt = new CP25Repeater;
rpt->m_timer.start();
rpt->m_address = address;
rpt->m_port = port;
rpt->m_addr = addr;
rpt->m_addrLen = addrLen;
rpt->m_callsign = std::string((char*)(buffer + 1U), 10U);
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 {
rpt->m_timer.start();
}
// Return the poll
network.writeData(buffer, len, address, port);
network.writeData(buffer, len, addr, addrLen);
} 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) {
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);
delete itRpt;
break;
@ -237,7 +237,7 @@ void CP25Reflector::run()
displayed = false;
seen64 = 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) {
@ -266,10 +266,8 @@ void CP25Reflector::run()
}
for (std::vector<CP25Repeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
in_addr addr = (*it)->m_address;
unsigned int prt = (*it)->m_port;
if (addr.s_addr != address.s_addr || prt != port)
network.writeData(buffer, len, addr, prt);
if (!CUDPSocket::match(addr, (*it)->m_addr))
network.writeData(buffer, len, (*it)->m_addr, (*it)->m_addrLen);
}
if (buffer[0U] == 0x80U) {
@ -279,7 +277,7 @@ void CP25Reflector::run()
}
}
} 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);
}
}
@ -294,10 +292,7 @@ void CP25Reflector::run()
for (std::vector<CP25Repeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CP25Repeater* itRpt = *it;
if (itRpt->m_timer.hasExpired()) {
in_addr address = itRpt->m_address;
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);
LogMessage("Removing %s disappeared", itRpt->m_callsign.c_str());
m_repeaters.erase(it);
delete itRpt;
break;
@ -328,10 +323,10 @@ void CP25Reflector::run()
::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) {
if (address.s_addr == (*it)->m_address.s_addr && (*it)->m_port == port)
if (CUDPSocket::match(addr, (*it)->m_addr))
return *it;
}
@ -348,11 +343,9 @@ void CP25Reflector::dumpRepeaters() const
LogMessage("Currently linked repeaters:");
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;
unsigned int timer = (*it)->m_timer.getTimer();
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
* it under the terms of the GNU General Public License as published by
@ -41,17 +41,17 @@
class CP25Repeater {
public:
CP25Repeater() :
m_address(),
m_port(0U),
m_addr(),
m_addrLen(0U),
m_callsign(),
m_timer(1000U, 120U)
{
}
in_addr m_address;
unsigned int m_port;
std::string m_callsign;
CTimer m_timer;
sockaddr_storage m_addr;
unsigned int m_addrLen;
std::string m_callsign;
CTimer m_timer;
};
class CP25Reflector
@ -66,7 +66,7 @@ private:
CConf m_conf;
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;
};

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
* it under the terms of the GNU General Public License as published by
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
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());
return err;
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res);
return 0;
}
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2;
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2;
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
default:
return false;
}
}
LogError("Cannot find address for host %s", hostname.c_str());
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
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());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
}
LogError("Cannot find address for host %s", hostname.c_str());
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
}
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
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 defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
@ -115,24 +157,6 @@ bool CUDPSocket::open()
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -143,7 +167,7 @@ bool CUDPSocket::open()
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else
@ -151,12 +175,14 @@ bool CUDPSocket::open()
#endif
return false;
}
LogInfo("Opening UDP port on %u", m_port);
}
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -188,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -209,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

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
* it under the terms of the GNU General Public License as published by
@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket();
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2);
static bool isnone(const sockaddr_storage& addr);
private:
std::string m_address;

View File

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