Convert to IPv6

This commit is contained in:
Jonathan Naylor 2020-09-03 15:12:03 +01:00
parent 93cd9d1863
commit 62d05606d3
30 changed files with 572 additions and 529 deletions

View File

@ -34,8 +34,8 @@ m_latitude(0.0F),
m_longitude(0.0F),
m_height(0),
m_desc(),
m_aprsAddress(),
m_aprsPort(port),
m_aprsAddr(),
m_aprsAddrLen(),
m_aprsSocket()
#if defined(USE_GPSD)
,m_gpsdEnabled(false),
@ -53,7 +53,7 @@ m_gpsdData()
m_callsign.append(suffix.substr(0U, 1U));
}
m_aprsAddress = CUDPSocket::lookup(address);
CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen);
}
CAPRSWriter::~CAPRSWriter()
@ -120,7 +120,7 @@ void CAPRSWriter::write(const char* data)
if (m_debug)
LogDebug("APRS ==> %s", data);
m_aprsSocket.write((unsigned char*)data, (unsigned int)::strlen(data), m_aprsAddress, m_aprsPort);
m_aprsSocket.write((unsigned char*)data, (unsigned int)::strlen(data), m_aprsAddr, m_aprsAddrLen);
}
void CAPRSWriter::clock(unsigned int ms)

View File

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

View File

@ -28,15 +28,15 @@ const unsigned int BUFFER_LENGTH = 200U;
CIcomNetwork::CIcomNetwork(unsigned int localPort, const std::string& rptAddress, unsigned int rptPort, bool debug) :
m_socket(localPort),
m_address(),
m_port(rptPort),
m_addr(),
m_addrLen(),
m_debug(debug)
{
assert(localPort > 0U);
assert(!rptAddress.empty());
assert(rptPort > 0U);
m_address = CUDPSocket::lookup(rptAddress);
CUDPSocket::lookup(rptAddress, rptPort, m_addr, m_addrLen);
}
CIcomNetwork::~CIcomNetwork()
@ -47,9 +47,6 @@ bool CIcomNetwork::open()
{
LogMessage("Opening Icom connection");
if (m_address.s_addr == INADDR_NONE)
return false;
return m_socket.open();
}
@ -84,7 +81,7 @@ bool CIcomNetwork::write(const unsigned char* data, unsigned int length)
if (m_debug)
CUtils::dump(1U, "Icom Data Sent", buffer, 102U);
return m_socket.write(buffer, 102U, m_address, m_port);
return m_socket.write(buffer, 102U, m_addr, m_addrLen);
}
unsigned int CIcomNetwork::read(unsigned char* data)
@ -92,15 +89,15 @@ unsigned int CIcomNetwork::read(unsigned char* data)
assert(data != NULL);
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
sockaddr_storage addr;
unsigned int addrlen;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrlen);
if (length <= 0)
return 0U;
if (m_address.s_addr != address.s_addr || m_port != port) {
LogWarning("Icom Data received from an unknown address or port - %08X:%u", ntohl(address.s_addr), port);
if (!CUDPSocket::match(m_addr, addr)) {
LogWarning("Icom Data received from an unknown address or port");
return 0U;
}
@ -114,7 +111,7 @@ unsigned int CIcomNetwork::read(unsigned char* data)
buffer[37U] = 0x02U;
buffer[38U] = 0x4FU;
buffer[39U] = 0x4BU;
m_socket.write(buffer, length, address, port);
m_socket.write(buffer, length, addr, addrlen);
return 0U;
}

View File

@ -41,10 +41,10 @@ public:
virtual void clock(unsigned int ms);
private:
CUDPSocket m_socket;
in_addr m_address;
unsigned int m_port;
bool m_debug;
CUDPSocket m_socket;
sockaddr_storage m_addr;
unsigned int m_addrLen;
bool m_debug;
};
#endif

View File

@ -36,9 +36,10 @@ const unsigned int BUFFER_LENGTH = 200U;
CKenwoodNetwork::CKenwoodNetwork(unsigned int localPort, const std::string& rptAddress, unsigned int rptPort, bool debug) :
m_rtpSocket(localPort + 0U),
m_rtcpSocket(localPort + 1U),
m_address(),
m_rtcpPort(rptPort + 1U),
m_rtpPort(rptPort + 0U),
m_rtcpAddr(),
m_rtcpAddrLen(),
m_rtpAddr(),
m_rtpAddrLen(),
m_headerSeen(false),
m_seen1(false),
m_seen2(false),
@ -64,7 +65,8 @@ m_random()
m_sacch = new unsigned char[10U];
m_address = CUDPSocket::lookup(rptAddress);
CUDPSocket::lookup(rptAddress, rptPort + 0U, m_rtpAddr, m_rtpAddrLen);
CUDPSocket::lookup(rptAddress, rptPort + 1U, m_rtcpAddr, m_rtcpAddrLen);
std::random_device rd;
std::mt19937 mt(rd());
@ -80,9 +82,6 @@ bool CKenwoodNetwork::open()
{
LogMessage("Opening Kenwood connection");
if (m_address.s_addr == INADDR_NONE)
return false;
if (!m_rtcpSocket.open())
return false;
@ -281,7 +280,7 @@ bool CKenwoodNetwork::writeRTPVoiceHeader(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U);
return m_rtpSocket.write(buffer, 47U, m_address, m_rtpPort);
return m_rtpSocket.write(buffer, 47U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data)
@ -327,7 +326,7 @@ bool CKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U);
return m_rtpSocket.write(buffer, 47U, m_address, m_rtpPort);
return m_rtpSocket.write(buffer, 47U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTPVoiceData(const unsigned char* data)
@ -373,7 +372,7 @@ bool CKenwoodNetwork::writeRTPVoiceData(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 59U);
return m_rtpSocket.write(buffer, 59U, m_address, m_rtpPort);
return m_rtpSocket.write(buffer, 59U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTCPStart()
@ -433,7 +432,7 @@ bool CKenwoodNetwork::writeRTCPStart()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U);
return m_rtcpSocket.write(buffer, 28U, m_address, m_rtcpPort);
return m_rtcpSocket.write(buffer, 28U, m_rtcpAddr, m_rtcpAddrLen);
}
bool CKenwoodNetwork::writeRTCPPing()
@ -475,7 +474,7 @@ bool CKenwoodNetwork::writeRTCPPing()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U);
return m_rtcpSocket.write(buffer, 28U, m_address, m_rtcpPort);
return m_rtcpSocket.write(buffer, 28U, m_rtcpAddr, m_rtcpAddrLen);
}
bool CKenwoodNetwork::writeRTCPHang(unsigned char type, unsigned short src, unsigned short dst)
@ -518,7 +517,7 @@ bool CKenwoodNetwork::writeRTCPHang()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 20U);
return m_rtcpSocket.write(buffer, 20U, m_address, m_rtcpPort);
return m_rtcpSocket.write(buffer, 20U, m_rtcpAddr, m_rtcpAddrLen);
}
unsigned int CKenwoodNetwork::read(unsigned char* data)
@ -553,15 +552,15 @@ unsigned int CKenwoodNetwork::readRTP(unsigned char* data)
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Check if the data is for us
if (m_address.s_addr != address.s_addr) {
LogMessage("Kenwood RTP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr);
if (!CUDPSocket::match(m_rtpAddr, addr)) {
LogMessage("Kenwood RTP packet received from an invalid source");
return 0U;
}
@ -579,15 +578,15 @@ unsigned int CKenwoodNetwork::readRTCP(unsigned char* data)
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Check if the data is for us
if (m_address.s_addr != address.s_addr) {
LogMessage("Kenwood RTCP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr);
if (!CUDPSocket::match(m_rtcpAddr, addr)) {
LogMessage("Kenwood RTCP packet received from an invalid source");
return 0U;
}

View File

@ -43,29 +43,30 @@ public:
virtual void clock(unsigned int ms);
private:
CUDPSocket m_rtpSocket;
CUDPSocket m_rtcpSocket;
in_addr m_address;
unsigned int m_rtcpPort;
unsigned int m_rtpPort;
bool m_headerSeen;
bool m_seen1;
bool m_seen2;
bool m_seen3;
bool m_seen4;
unsigned char* m_sacch;
uint8_t m_sessionId;
uint16_t m_seqNo;
unsigned int m_ssrc;
bool m_debug;
uint32_t m_startSecs;
uint32_t m_startUSecs;
CTimer m_rtcpTimer;
CTimer m_hangTimer;
unsigned char m_hangType;
unsigned short m_hangSrc;
unsigned short m_hangDst;
std::mt19937 m_random;
CUDPSocket m_rtpSocket;
CUDPSocket m_rtcpSocket;
sockaddr_storage m_rtcpAddr;
unsigned int m_rtcpAddrLen;
sockaddr_storage m_rtpAddr;
unsigned int m_rtpAddrLen;
bool m_headerSeen;
bool m_seen1;
bool m_seen2;
bool m_seen3;
bool m_seen4;
unsigned char* m_sacch;
uint8_t m_sessionId;
uint16_t m_seqNo;
unsigned int m_ssrc;
bool m_debug;
uint32_t m_startSecs;
uint32_t m_startUSecs;
CTimer m_rtcpTimer;
CTimer m_hangTimer;
unsigned char m_hangType;
unsigned short m_hangSrc;
unsigned short m_hangDst;
std::mt19937 m_random;
bool processIcomVoiceHeader(const unsigned char* data);
bool processIcomVoiceData(const unsigned char* data);

View File

@ -247,24 +247,24 @@ void CNXDNGateway::run()
bool grp = false;
unsigned short currentId = 9999U;
in_addr currentAddr;
unsigned int currentPort = 0U;
sockaddr_storage currentAddr;
unsigned int currentAddrLen = 0U;
unsigned short startupId = m_conf.getNetworkStartup();
if (startupId != 9999U) {
CNXDNReflector* 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, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
LogMessage("Linked at startup to reflector %u", currentId);
} else {
@ -274,14 +274,14 @@ void CNXDNGateway::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 (::memcmp(buffer, "NXDND", 5U) == 0) {
unsigned short dstId = 0U;
@ -326,9 +326,9 @@ void CNXDNGateway::run()
if (voice != NULL && dstId == 9999U)
voice->unlinked();
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
inactivityTimer.stop();
pollTimer.stop();
@ -340,9 +340,9 @@ void CNXDNGateway::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());
@ -350,9 +350,9 @@ void CNXDNGateway::run()
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
inactivityTimer.start();
pollTimer.start();
@ -392,7 +392,7 @@ void CNXDNGateway::run()
// If we're linked and we have a network, send it on
if (currentId != 9999U) {
remoteNetwork.writeData(buffer, len, srcId, dstId, grp, currentAddr, currentPort);
remoteNetwork.writeData(buffer, len, srcId, dstId, grp, currentAddr, currentAddrLen);
inactivityTimer.start();
}
}
@ -404,7 +404,7 @@ void CNXDNGateway::run()
}
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) {
@ -420,9 +420,9 @@ void CNXDNGateway::run()
if (voice != NULL)
voice->unlinked();
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
inactivityTimer.stop();
pollTimer.stop();
@ -430,18 +430,18 @@ void CNXDNGateway::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, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
inactivityTimer.start();
pollTimer.start();
@ -449,22 +449,22 @@ void CNXDNGateway::run()
} else if (reflector != NULL && currentId != 9999U) {
LogMessage("Unlinked from reflector %u by remote command", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
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, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
inactivityTimer.start();
pollTimer.start();
@ -491,9 +491,9 @@ void CNXDNGateway::run()
if (currentId != 9999U && startupId == 9999U) {
LogMessage("Unlinking from %u due to inactivity", currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
if (voice != NULL)
voice->unlinked();
@ -504,16 +504,16 @@ void CNXDNGateway::run()
lostTimer.stop();
} else if (currentId != startupId) {
if (currentId != 9999U) {
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentPort, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
remoteNetwork.writeUnlink(currentAddr, currentAddrLen, currentId);
}
CNXDNReflector* 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();
@ -524,9 +524,9 @@ void CNXDNGateway::run()
if (voice != NULL)
voice->linkedTo(currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
} else {
startupId = 9999U;
inactivityTimer.stop();
@ -539,7 +539,7 @@ void CNXDNGateway::run()
pollTimer.clock(ms);
if (pollTimer.isRunning() && pollTimer.hasExpired()) {
if (currentId != 9999U)
remoteNetwork.writePoll(currentAddr, currentPort, currentId);
remoteNetwork.writePoll(currentAddr, currentAddrLen, currentId);
pollTimer.start();
}

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
@ -43,11 +43,10 @@ bool CNXDNNetwork::open()
return m_socket.open();
}
bool CNXDNNetwork::writeData(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const in_addr& address, unsigned int port)
bool CNXDNNetwork::writeData(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const sockaddr_storage& addr, unsigned int addrLen)
{
assert(data != NULL);
assert(length > 0U);
assert(port > 0U);
unsigned char buffer[50U];
@ -85,13 +84,11 @@ bool CNXDNNetwork::writeData(const unsigned char* data, unsigned int length, uns
if (m_debug)
CUtils::dump(1U, "NXDN Network Data Sent", buffer, 43U);
return m_socket.write(buffer, 43U, address, port);
return m_socket.write(buffer, 43U, addr, addrLen);
}
bool CNXDNNetwork::writePoll(const in_addr& address, unsigned int port, unsigned short tg)
bool CNXDNNetwork::writePoll(const sockaddr_storage& addr, unsigned int addrLen, unsigned short tg)
{
assert(port > 0U);
unsigned char data[20U];
data[0U] = 'N';
@ -109,13 +106,11 @@ bool CNXDNNetwork::writePoll(const in_addr& address, unsigned int port, unsigned
if (m_debug)
CUtils::dump(1U, "NXDN Network Poll Sent", data, 17U);
return m_socket.write(data, 17U, address, port);
return m_socket.write(data, 17U, addr, addrLen);
}
bool CNXDNNetwork::writeUnlink(const in_addr& address, unsigned int port, unsigned short tg)
bool CNXDNNetwork::writeUnlink(const sockaddr_storage& addr, unsigned int addrLen, unsigned short tg)
{
assert(port > 0U);
unsigned char data[20U];
data[0U] = 'N';
@ -133,15 +128,15 @@ bool CNXDNNetwork::writeUnlink(const in_addr& address, unsigned int port, unsign
if (m_debug)
CUtils::dump(1U, "NXDN Network Unlink Sent", data, 17U);
return m_socket.write(data, 17U, address, port);
return m_socket.write(data, 17U, addr, addrLen);
}
unsigned int CNXDNNetwork::readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port)
unsigned int CNXDNNetwork::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,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
@ -31,13 +31,13 @@ public:
bool open();
bool writeData(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const in_addr& address, unsigned int port);
bool writeData(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const sockaddr_storage& addr, unsigned int addrLen);
bool writePoll(const in_addr& address, unsigned int port, unsigned short tg);
bool writePoll(const sockaddr_storage& addr, unsigned int addrLen, unsigned short tg);
bool writeUnlink(const in_addr& address, unsigned int port, unsigned short tg);
bool writeUnlink(const sockaddr_storage& addr, unsigned int addrLen, unsigned short tg);
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
@ -78,17 +78,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) {
CNXDNReflector* refl = new CNXDNReflector;
refl->m_id = (unsigned short)::atoi(p1);
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl);
}
CNXDNReflector* refl = new CNXDNReflector;
refl->m_id = (unsigned short)::atoi(p1);
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
}
}
@ -110,17 +106,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) {
CNXDNReflector* refl = new CNXDNReflector;
refl->m_id = id;
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl);
}
CNXDNReflector* refl = new CNXDNReflector;
refl->m_id = id;
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
}
}
}
@ -134,9 +126,8 @@ bool CReflectors::load()
// Add the Parrot entry
if (m_parrotPort > 0U) {
CNXDNReflector* refl = new CNXDNReflector;
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 NXDN parrot (TG%u)", refl->m_id);
}
@ -144,9 +135,8 @@ bool CReflectors::load()
// Add the NXDN2DMR entry
if (m_nxdn2dmrPort > 0U) {
CNXDNReflector* refl = new CNXDNReflector;
refl->m_id = 20U;
refl->m_address = CUDPSocket::lookup(m_nxdn2dmrAddress);
refl->m_port = m_nxdn2dmrPort;
refl->m_id = 20U;
CUDPSocket::lookup(m_nxdn2dmrAddress, m_nxdn2dmrPort, refl->m_addr, refl->m_addrLen);
m_reflectors.push_back(refl);
LogInfo("Loaded NXDN2DMR reflector (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 CNXDNReflector {
public:
CNXDNReflector() :
m_id(0U),
m_address(),
m_port(0U)
m_addr(),
m_addrLen(0U)
{
}
unsigned short m_id;
in_addr m_address;
unsigned int m_port;
unsigned short m_id;
sockaddr_storage m_addr;
unsigned int m_addrLen;
};
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
* it under the terms of the GNU General Public License as published by
@ -24,7 +24,6 @@
#if !defined(_WIN32) && !defined(_WIN64)
#include <cerrno>
#include <cstring>
#include <ifaddrs.h>
#endif
@ -63,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());
@ -116,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)
@ -144,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
@ -152,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);
@ -189,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)
@ -210,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
@ -24,8 +24,8 @@
CNXDNNetwork::CNXDNNetwork(unsigned int port) :
m_socket(port),
m_address(),
m_port(0U)
m_addr(),
m_addrLen(0U)
{
}
@ -42,24 +42,24 @@ bool CNXDNNetwork::open()
bool CNXDNNetwork::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 CNXDNNetwork::read(unsigned char* data, unsigned int len)
{
in_addr address;
unsigned int port;
int length = m_socket.read(data, len, address, port);
sockaddr_storage addr;
unsigned int addrlen;
int length = m_socket.read(data, len, addr, addrlen);
if (length <= 0)
return 0U;
m_address.s_addr = address.s_addr;
m_port = port;
m_addr = addr;
m_addrLen = addrlen;
if (::memcmp(data, "NXDNP", 5U) == 0 && length == 17) { // A poll
write(data, length);
@ -73,7 +73,7 @@ unsigned int CNXDNNetwork::read(unsigned char* data, unsigned int len)
void CNXDNNetwork::end()
{
m_port = 0U;
m_addrLen = 0U;
}
void CNXDNNetwork::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

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

View File

@ -30,12 +30,13 @@ const unsigned int ICOM_PORT = 41300U;
CIcomNetwork::CIcomNetwork(const std::string& address, bool debug) :
m_socket(ICOM_PORT),
m_address(),
m_addr(),
m_addrLen(),
m_debug(debug)
{
assert(!address.empty());
m_address = CUDPSocket::lookup(address);
CUDPSocket::lookup(address, ICOM_PORT, m_addr, m_addrLen);
}
CIcomNetwork::~CIcomNetwork()
@ -46,9 +47,6 @@ bool CIcomNetwork::open()
{
LogMessage("Opening Icom network connection");
if (m_address.s_addr == INADDR_NONE)
return false;
return m_socket.open();
}
@ -83,22 +81,22 @@ bool CIcomNetwork::write(const unsigned char* data, unsigned int len)
if (m_debug)
CUtils::dump(1U, "Icom Network Data Sent", buffer, 102U);
return m_socket.write(buffer, 102U, m_address, ICOM_PORT);
return m_socket.write(buffer, 102U, m_addr, m_addrLen);
}
unsigned int CIcomNetwork::read(unsigned char* data)
{
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Check if the data is for us
if (m_address.s_addr != address.s_addr || port != ICOM_PORT) {
LogMessage("Icom packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, ICOM_PORT, port);
if (!CUDPSocket::match(m_addr, addr)) {
LogMessage("Icom packet received from an invalid source");
return 0U;
}

View File

@ -41,9 +41,10 @@ public:
void clock(unsigned int ms);
private:
CUDPSocket m_socket;
in_addr m_address;
bool m_debug;
CUDPSocket m_socket;
sockaddr_storage m_addr;
unsigned int m_addrLen;
bool m_debug;
};
#endif

View File

@ -39,7 +39,10 @@ const unsigned int RTCP_PORT = 64001U;
CKenwoodNetwork::CKenwoodNetwork(const std::string& address, bool debug) :
m_rtpSocket(RTP_PORT),
m_rtcpSocket(RTCP_PORT),
m_address(),
m_rtpAddr(),
m_rtpAddrLen(),
m_rtcpAddr(),
m_rtcpAddrLen(),
m_headerSeen(false),
m_seen1(false),
m_seen2(false),
@ -63,7 +66,8 @@ m_random()
m_sacch = new unsigned char[10U];
m_address = CUDPSocket::lookup(address);
CUDPSocket::lookup(address, RTP_PORT, m_rtpAddr, m_rtpAddrLen);
CUDPSocket::lookup(address, RTCP_PORT, m_rtcpAddr, m_rtcpAddrLen);
std::random_device rd;
std::mt19937 mt(rd());
@ -79,9 +83,6 @@ bool CKenwoodNetwork::open()
{
LogMessage("Opening Kenwood connection");
if (m_address.s_addr == INADDR_NONE)
return false;
if (!m_rtcpSocket.open())
return false;
@ -280,7 +281,7 @@ bool CKenwoodNetwork::writeRTPVoiceHeader(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U);
return m_rtpSocket.write(buffer, 47U, m_address, RTP_PORT);
return m_rtpSocket.write(buffer, 47U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data)
@ -326,7 +327,7 @@ bool CKenwoodNetwork::writeRTPVoiceTrailer(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 47U);
return m_rtpSocket.write(buffer, 47U, m_address, RTP_PORT);
return m_rtpSocket.write(buffer, 47U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTPVoiceData(const unsigned char* data)
@ -372,7 +373,7 @@ bool CKenwoodNetwork::writeRTPVoiceData(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTP Data Sent", buffer, 59U);
return m_rtpSocket.write(buffer, 59U, m_address, RTP_PORT);
return m_rtpSocket.write(buffer, 59U, m_rtpAddr, m_rtpAddrLen);
}
bool CKenwoodNetwork::writeRTCPStart()
@ -432,7 +433,7 @@ bool CKenwoodNetwork::writeRTCPStart()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U);
return m_rtcpSocket.write(buffer, 28U, m_address, RTCP_PORT);
return m_rtcpSocket.write(buffer, 28U, m_rtcpAddr, m_rtcpAddrLen);
}
bool CKenwoodNetwork::writeRTCPPing()
@ -474,7 +475,7 @@ bool CKenwoodNetwork::writeRTCPPing()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 28U);
return m_rtcpSocket.write(buffer, 28U, m_address, RTCP_PORT);
return m_rtcpSocket.write(buffer, 28U, m_rtcpAddr, m_rtcpAddrLen);
}
bool CKenwoodNetwork::writeRTCPHang(unsigned char type, unsigned short src, unsigned short dst)
@ -517,7 +518,7 @@ bool CKenwoodNetwork::writeRTCPHang()
if (m_debug)
CUtils::dump(1U, "Kenwood Network RTCP Data Sent", buffer, 20U);
return m_rtcpSocket.write(buffer, 20U, m_address, RTCP_PORT);
return m_rtcpSocket.write(buffer, 20U, m_rtcpAddr, m_rtcpAddrLen);
}
unsigned int CKenwoodNetwork::read(unsigned char* data)
@ -552,15 +553,15 @@ unsigned int CKenwoodNetwork::readRTP(unsigned char* data)
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_rtpSocket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Check if the data is for us
if (m_address.s_addr != address.s_addr) {
LogMessage("Kenwood RTP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr);
if (!CUDPSocket::match(m_rtpAddr, addr)) {
LogMessage("Kenwood RTP packet received from an invalid source");
return 0U;
}
@ -578,15 +579,15 @@ unsigned int CKenwoodNetwork::readRTCP(unsigned char* data)
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_rtcpSocket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Check if the data is for us
if (m_address.s_addr != address.s_addr) {
LogMessage("Kenwood RTCP packet received from an invalid source, %08X != %08X", m_address.s_addr, address.s_addr);
if (!CUDPSocket::match(m_rtcpAddr, addr)) {
LogMessage("Kenwood RTCP packet received from an invalid source");
return 0U;
}

View File

@ -42,27 +42,30 @@ public:
void clock(unsigned int ms);
private:
CUDPSocket m_rtpSocket;
CUDPSocket m_rtcpSocket;
in_addr m_address;
bool m_headerSeen;
bool m_seen1;
bool m_seen2;
bool m_seen3;
bool m_seen4;
unsigned char* m_sacch;
uint8_t m_sessionId;
uint16_t m_seqNo;
unsigned int m_ssrc;
bool m_debug;
uint32_t m_startSecs;
uint32_t m_startUSecs;
CTimer m_rtcpTimer;
CTimer m_hangTimer;
unsigned char m_hangType;
unsigned short m_hangSrc;
unsigned short m_hangDst;
std::mt19937 m_random;
CUDPSocket m_rtpSocket;
CUDPSocket m_rtcpSocket;
sockaddr_storage m_rtpAddr;
unsigned int m_rtpAddrLen;
sockaddr_storage m_rtcpAddr;
unsigned int m_rtcpAddrLen;
bool m_headerSeen;
bool m_seen1;
bool m_seen2;
bool m_seen3;
bool m_seen4;
unsigned char* m_sacch;
uint8_t m_sessionId;
uint16_t m_seqNo;
unsigned int m_ssrc;
bool m_debug;
uint32_t m_startSecs;
uint32_t m_startUSecs;
CTimer m_rtcpTimer;
CTimer m_hangTimer;
unsigned char m_hangType;
unsigned short m_hangSrc;
unsigned short m_hangDst;
std::mt19937 m_random;
bool processIcomVoiceHeader(const unsigned char* data);
bool processIcomVoiceData(const unsigned char* data);

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
@ -41,23 +41,21 @@ bool CNXDNNetwork::open()
return m_socket.open();
}
bool CNXDNNetwork::write(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port)
bool CNXDNNetwork::write(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, "NXDN Network Data Sent", data, length);
return m_socket.write(data, length, address, port);
return m_socket.write(data, length, addr, addrLen);
}
bool CNXDNNetwork::write(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const in_addr& address, unsigned int port)
bool CNXDNNetwork::write(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const sockaddr_storage& addr, unsigned int addrLen)
{
assert(data != NULL);
assert(length > 0U);
assert(port > 0U);
unsigned char buffer[50U];
@ -95,15 +93,15 @@ bool CNXDNNetwork::write(const unsigned char* data, unsigned int length, unsigne
if (m_debug)
CUtils::dump(1U, "NXDN Network Data Sent", buffer, 43U);
return m_socket.write(buffer, 43U, address, port);
return m_socket.write(buffer, 43U, addr, addrLen);
}
unsigned int CNXDNNetwork::read(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port)
unsigned int CNXDNNetwork::read(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,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
@ -31,16 +31,16 @@ public:
bool open();
bool write(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port);
bool write(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const in_addr& address, unsigned int port);
bool write(const unsigned char* data, unsigned int length, const sockaddr_storage& address, unsigned int addrLen);
bool write(const unsigned char* data, unsigned int length, unsigned short srcId, unsigned short dstId, bool grp, const sockaddr_storage& addr, unsigned int addrLen);
unsigned int read(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port);
unsigned int read(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen);
void close();
private:
CUDPSocket m_socket;
bool m_debug;
CUDPSocket m_socket;
bool m_debug;
};
#endif

View File

@ -233,12 +233,12 @@ void CNXDNReflector::run()
for (;;) {
unsigned char buffer[200U];
in_addr address;
unsigned int port;
sockaddr_storage address;
unsigned int addressLen;
unsigned int len = nxdnNetwork.read(buffer, 200U, address, port);
unsigned int len = nxdnNetwork.read(buffer, 200U, address, addressLen);
if (len > 0U) {
CNXDNRepeater* rpt = findRepeater(address, port);
CNXDNRepeater* rpt = findRepeater(address);
if (::memcmp(buffer, "NXDNP", 5U) == 0 && len == 17U) {
unsigned short id = (buffer[15U] << 8) | buffer[16U];
@ -246,25 +246,25 @@ void CNXDNReflector::run()
if (rpt == NULL) {
rpt = new CNXDNRepeater;
rpt->m_timer.start();
rpt->m_address = address;
rpt->m_port = port;
rpt->m_addr = address;
rpt->m_addrLen = addressLen;
rpt->m_callsign = std::string((char*)(buffer + 5U), 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
nxdnNetwork.write(buffer, len, address, port);
nxdnNetwork.write(buffer, len, address, addressLen);
}
} else if (::memcmp(buffer, "NXDNU", 5U) == 0 && len == 17U) {
unsigned short id = (buffer[15U] << 8) | buffer[16U];
if (id == tg) {
if (rpt != NULL) {
std::string callsign = std::string((char*)(buffer + 5U), 10U);
LogMessage("Removing %s (%s:%u)", callsign.c_str(), ::inet_ntoa(address), port);
LogMessage("Removing %s", callsign.c_str());
for (std::vector<CNXDNRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if (*it == rpt) {
@ -334,10 +334,10 @@ void CNXDNReflector::run()
watchdogTimer.start();
for (std::vector<CNXDNRepeater*>::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)
nxdnNetwork.write(buffer, len, addr, prt);
sockaddr_storage addr = (*it)->m_addr;
unsigned int addrLen = (*it)->m_addrLen;
if (!CUDPSocket::match(addr, address))
nxdnNetwork.write(buffer, len, addr, addrLen);
}
if (m_icomNetwork != NULL)
@ -355,7 +355,7 @@ void CNXDNReflector::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);
}
}
@ -406,9 +406,9 @@ void CNXDNReflector::run()
watchdogTimer.start();
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
in_addr addr = (*it)->m_address;
unsigned int prt = (*it)->m_port;
nxdnNetwork.write(buffer, len, srcId, dstId, grp, addr, prt);
sockaddr_storage addr = (*it)->m_addr;
unsigned int addrLen = (*it)->m_addrLen;
nxdnNetwork.write(buffer, len, srcId, dstId, grp, addr, addrLen);
}
if (m_kenwoodNetwork != NULL)
@ -474,9 +474,9 @@ void CNXDNReflector::run()
watchdogTimer.start();
for (std::vector<CNXDNRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
in_addr addr = (*it)->m_address;
unsigned int prt = (*it)->m_port;
nxdnNetwork.write(buffer, len, srcId, dstId, grp, addr, prt);
sockaddr_storage addr = (*it)->m_addr;
unsigned int addrLen = (*it)->m_addrLen;
nxdnNetwork.write(buffer, len, srcId, dstId, grp, addr, addrLen);
}
if (m_icomNetwork != NULL)
@ -507,10 +507,8 @@ void CNXDNReflector::run()
for (std::vector<CNXDNRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CNXDNRepeater* 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", callsign.c_str());
m_repeaters.erase(it);
delete itRpt;
break;
@ -552,10 +550,10 @@ void CNXDNReflector::run()
::LogFinalise();
}
CNXDNRepeater* CNXDNReflector::findRepeater(const in_addr& address, unsigned int port) const
CNXDNRepeater* CNXDNReflector::findRepeater(const sockaddr_storage& addr) const
{
for (std::vector<CNXDNRepeater*>::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;
}
@ -572,12 +570,10 @@ void CNXDNReflector::dumpRepeaters() const
LogMessage("Currently linked repeaters:");
for (std::vector<CNXDNRepeater*>::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

@ -43,17 +43,17 @@
class CNXDNRepeater {
public:
CNXDNRepeater() :
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 CNXDNReflector
@ -70,7 +70,7 @@ private:
CKenwoodNetwork* m_kenwoodNetwork;
std::vector<CNXDNRepeater*> m_repeaters;
CNXDNRepeater* findRepeater(const in_addr& address, unsigned int port) const;
CNXDNRepeater* findRepeater(const sockaddr_storage& addr) const;
void dumpRepeaters() const;
bool openIcomNetwork();

View File

@ -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 = "20200823";
const char* VERSION = "20200903";
#endif