diff --git a/APRSWriter.cpp b/APRSWriter.cpp index cc9cfaf..a302952 100644 --- a/APRSWriter.cpp +++ b/APRSWriter.cpp @@ -36,7 +36,7 @@ m_longitude(0.0F), m_height(0), m_desc(), m_aprsAddress(), -m_aprsPort(port), +m_aprsLen(), m_aprsSocket() { assert(!callsign.empty()); @@ -48,7 +48,7 @@ m_aprsSocket() m_callsign.append(suffix.substr(0U, 1U)); } - m_aprsAddress = CUDPSocket::lookup(address); + CUDPSocket::lookup(address, port, m_aprsAddress, m_aprsLen); } CAPRSWriter::~CAPRSWriter() @@ -159,5 +159,5 @@ void CAPRSWriter::sendIdFrame() if (m_debug) LogDebug("APRS ==> %s", output); - m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort); + m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsLen); } diff --git a/APRSWriter.h b/APRSWriter.h index b1b32c6..f3ad1f7 100644 --- a/APRSWriter.h +++ b/APRSWriter.h @@ -62,8 +62,8 @@ private: float m_longitude; int m_height; std::string m_desc; - in_addr m_aprsAddress; - unsigned int m_aprsPort; + sockaddr_storage m_aprsAddress; + unsigned int m_aprsLen; CUDPSocket m_aprsSocket; void sendIdFrame(); diff --git a/DMRGateway.cpp b/DMRGateway.cpp index 3e94830..1924a07 100644 --- a/DMRGateway.cpp +++ b/DMRGateway.cpp @@ -2486,10 +2486,9 @@ void CDMRGateway::createAPRS() void CDMRGateway::processDynamicTGControl() { unsigned char buffer[100U]; - in_addr address; - unsigned int port; - - int len = m_socket->read(buffer, 100U, address, port); + sockaddr_storage address; + unsigned int addrlen; + int len = m_socket->read(buffer, 100U, address, addrlen); if (len <= 0) return; diff --git a/DMRNetwork.cpp b/DMRNetwork.cpp index eeb9142..cd379f2 100644 --- a/DMRNetwork.cpp +++ b/DMRNetwork.cpp @@ -33,7 +33,7 @@ const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U; CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, const std::string& name, bool location, bool debug) : m_address(), -m_port(port), +m_addrlen(), m_id(NULL), m_password(password), m_name(name), @@ -56,7 +56,7 @@ m_beacon(false) assert(id > 1000U); assert(!password.empty()); - m_address = CUDPSocket::lookup(address); + CUDPSocket::lookup(address, port, m_address, m_addrlen); m_buffer = new unsigned char[BUFFER_LENGTH]; m_salt = new unsigned char[sizeof(uint32_t)]; @@ -305,7 +305,7 @@ void CDMRNetwork::clock(unsigned int ms) if (m_status == WAITING_CONNECT) { m_retryTimer.clock(ms); if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) { - bool ret = m_socket.open(); + bool ret = m_socket.open(m_address.ss_family); if (ret) { ret = writeLogin(); if (!ret) @@ -321,9 +321,9 @@ void CDMRNetwork::clock(unsigned int ms) return; } - in_addr address; - unsigned int port; - int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, port); + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen); if (length < 0) { LogError("%s, Socket has failed, retrying connection to the master", m_name.c_str()); close(); @@ -331,7 +331,10 @@ void CDMRNetwork::clock(unsigned int ms) return; } - if (length > 0 && m_address.s_addr == address.s_addr && m_port == port) { + if (m_debug && length > 0) + CUtils::dump(1U, "Network Received", m_buffer, length); + + if (length > 0 && CUDPSocket::match(m_address, address)) { if (::memcmp(m_buffer, "DMRD", 4U) == 0) { if (m_debug) CUtils::dump(1U, "Network Received", m_buffer, length); @@ -523,7 +526,7 @@ bool CDMRNetwork::write(const unsigned char* data, unsigned int length) if (m_debug) CUtils::dump(1U, "Network Transmitted", data, length); - bool ret = m_socket.write(data, length, m_address, m_port); + bool ret = m_socket.write(data, length, m_address, m_addrlen); if (!ret) { LogError("%s, Socket has failed when writing data to the master, retrying connection", m_name.c_str()); m_socket.close(); diff --git a/DMRNetwork.h b/DMRNetwork.h index a5e97b1..91c78b6 100644 --- a/DMRNetwork.h +++ b/DMRNetwork.h @@ -57,15 +57,15 @@ public: void close(); -private: - in_addr m_address; - unsigned int m_port; - uint8_t* m_id; - std::string m_password; - std::string m_name; - bool m_location; - bool m_debug; - CUDPSocket m_socket; +private: + sockaddr_storage m_address; + unsigned int m_addrlen; + uint8_t* m_id; + std::string m_password; + std::string m_name; + bool m_location; + bool m_debug; + CUDPSocket m_socket; enum STATUS { WAITING_CONNECT, diff --git a/MMDVMNetwork.cpp b/MMDVMNetwork.cpp index 3c73172..d65254a 100644 --- a/MMDVMNetwork.cpp +++ b/MMDVMNetwork.cpp @@ -32,7 +32,7 @@ const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U; CMMDVMNetwork::CMMDVMNetwork(const std::string& rptAddress, unsigned int rptPort, const std::string& localAddress, unsigned int localPort, bool debug) : m_rptAddress(), -m_rptPort(rptPort), +m_rptAddrLen(), m_id(0U), m_netId(NULL), m_debug(debug), @@ -49,7 +49,7 @@ m_talkerAliasLen(0U) assert(!rptAddress.empty()); assert(rptPort > 0U); - m_rptAddress = CUDPSocket::lookup(rptAddress); + CUDPSocket::lookup(rptAddress, rptPort, m_rptAddress, m_rptAddrLen); m_buffer = new unsigned char[BUFFER_LENGTH]; m_netId = new unsigned char[4U]; @@ -206,7 +206,7 @@ bool CMMDVMNetwork::write(const CDMRData& data) if (m_debug) CUtils::dump(1U, "Network Transmitted", buffer, HOMEBREW_DATA_PACKET_LENGTH); - m_socket.write(buffer, HOMEBREW_DATA_PACKET_LENGTH, m_rptAddress, m_rptPort); + m_socket.write(buffer, HOMEBREW_DATA_PACKET_LENGTH, m_rptAddress, m_rptAddrLen); return true; } @@ -239,7 +239,7 @@ bool CMMDVMNetwork::readTalkerAlias(unsigned char* data, unsigned int& length) bool CMMDVMNetwork::writeBeacon() { - return m_socket.write((unsigned char*)"DMRB", 4U, m_rptAddress, m_rptPort); + return m_socket.write((unsigned char*)"DMRB", 4U, m_rptAddress, m_rptAddrLen); } void CMMDVMNetwork::close() @@ -251,14 +251,14 @@ void CMMDVMNetwork::close() void CMMDVMNetwork::clock(unsigned int ms) { - in_addr address; - unsigned int port; - int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, port); + sockaddr_storage address; + unsigned int addrlen; + int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen); if (length <= 0) return; - if (m_rptAddress.s_addr != address.s_addr || m_rptPort != port) { - LogMessage("Packet received from an invalid source, %08X != %08X and/or %u != %u", m_rptAddress.s_addr, address.s_addr, m_rptPort, port); + if (!CUDPSocket::match(m_rptAddress, address)) { + LogMessage("MMDVM packet received from an invalid source"); return; } @@ -284,7 +284,7 @@ void CMMDVMNetwork::clock(unsigned int ms) ::memcpy(m_configData, m_buffer + 8U, m_configLen); } - m_socket.write((unsigned char*)"DMRP", 4U, m_rptAddress, m_rptPort); + m_socket.write((unsigned char*)"DMRP", 4U, m_rptAddress, m_rptAddrLen); } else { CUtils::dump("Unknown packet from the MMDVM", m_buffer, length); } diff --git a/MMDVMNetwork.h b/MMDVMNetwork.h index e9b1f93..83baa5e 100644 --- a/MMDVMNetwork.h +++ b/MMDVMNetwork.h @@ -54,8 +54,8 @@ public: void close(); private: - in_addr m_rptAddress; - unsigned int m_rptPort; + sockaddr_storage m_rptAddress; + unsigned int m_rptAddrLen; unsigned int m_id; unsigned char* m_netId; bool m_debug; diff --git a/Makefile b/Makefile index a13f510..d670519 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ LIBS = -lpthread LDFLAGS = -g -OBJECTS = APRSWriter.o BPTC19696.o Conf.o CRC.o DMRCSBK.o DMRData.o DMRDataHeader.o DMREmbeddedData.o DMREMB.o DMRFullLC.o DMRGateway.o DMRLC.o DMRNetwork.o \ - DMRSlotType.o DynVoice.o Golay2087.o GPSD.o Hamming.o Log.o MMDVMNetwork.o PassAllPC.o PassAllTG.o QR1676.o Reflectors.o Rewrite.o RewriteDstId.o \ - RewriteDynTGNet.o RewriteDynTGRF.o RewritePC.o RewriteSrc.o RewriteSrcId.o RewriteTG.o RewriteType.o RS129.o SHA256.o StopWatch.o Sync.o Thread.o \ - Timer.o UDPSocket.o Utils.o XLXVoice.o +OBJECTS = APRSWriter.o BPTC19696.o Conf.o CRC.o DMRCSBK.o DMRData.o DMRDataHeader.o DMREmbeddedData.o DMREMB.o DMRFullLC.o DMRGateway.o \ + DMRLC.o DMRNetwork.o DMRSlotType.o DynVoice.o Golay2087.o GPSD.o Hamming.o Log.o MMDVMNetwork.o PassAllPC.o PassAllTG.o \ + QR1676.o Reflectors.o Rewrite.o RewriteDstId.o RewriteDynTGNet.o RewriteDynTGRF.o RewritePC.o RewriteSrc.o RewriteSrcId.o \ + RewriteTG.o RewriteType.o RS129.o SHA256.o StopWatch.o Sync.o Thread.o Timer.o UDPSocket.o Utils.o XLXVoice.o all: DMRGateway diff --git a/UDPSocket.cpp b/UDPSocket.cpp index 396f1f7..659d816 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -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) diff --git a/UDPSocket.h b/UDPSocket.h index e0af272..386f059 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -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 #include #else -#include +#include +#include #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; diff --git a/Version.h b/Version.h index 23ae81e..ff2325e 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200830"; +const char* VERSION = "20200903"; #endif