From 4cd7e992831d4db9201e0674ed782f47b2d78757 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 29 Jan 2024 16:00:25 +0000 Subject: [PATCH] Simplify the UDP socket handling. --- .gitignore | 3 +- NXDNGateway/NXDNNetwork.cpp | 14 +-- NXDNGateway/NXDNNetwork.h | 9 +- NXDNGateway/UDPSocket.cpp | 208 +++++++++++++++--------------------- NXDNGateway/UDPSocket.h | 33 +++--- NXDNGateway/Version.h | 4 +- NXDNParrot/UDPSocket.cpp | 208 +++++++++++++++--------------------- NXDNParrot/UDPSocket.h | 33 +++--- NXDNParrot/Version.h | 4 +- 9 files changed, 210 insertions(+), 306 deletions(-) diff --git a/.gitignore b/.gitignore index 0907130..f1fffe1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,6 @@ x64 .vs NXDNGateway/NXDNGateway NXDNParrot/NXDNParrot -NXDNReflector/NXDNReflector +NXDNGateway/GitVersion.h +NXDNParrot/GitVersion.h diff --git a/NXDNGateway/NXDNNetwork.cpp b/NXDNGateway/NXDNNetwork.cpp index c7644cc..7dc31c2 100644 --- a/NXDNGateway/NXDNNetwork.cpp +++ b/NXDNGateway/NXDNNetwork.cpp @@ -26,8 +26,7 @@ CNXDNNetwork::CNXDNNetwork(unsigned short port, const std::string& callsign, bool debug) : m_callsign(callsign), -m_socket(), -m_port(port), +m_socket(port), m_debug(debug) { assert(port > 0U); @@ -43,16 +42,7 @@ bool CNXDNNetwork::open() { LogInfo("Opening NXDN network connection"); - unsigned int index = 0U; - - bool ret1 = m_socket.open(index, PF_INET, "", m_port); - if (ret1) - index++; - - bool ret2 = m_socket.open(index, PF_INET6, "", m_port); - - // We're OK as long as we have either IPv4 or IPv6 or both. - return ret1 || ret2; + return m_socket.open(); } 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) diff --git a/NXDNGateway/NXDNNetwork.h b/NXDNGateway/NXDNNetwork.h index 1d4dd85..704dcff 100644 --- a/NXDNGateway/NXDNNetwork.h +++ b/NXDNGateway/NXDNNetwork.h @@ -1,5 +1,5 @@ /* -* Copyright (C) 2009-2014,2016,2018,2020 by Jonathan Naylor G4KLX +* Copyright (C) 2009-2014,2016,2018,2020,2024 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 @@ -42,10 +42,9 @@ public: void close(); private: - std::string m_callsign; - CUDPSocket m_socket; - unsigned short m_port; - bool m_debug; + std::string m_callsign; + CUDPSocket m_socket; + bool m_debug; }; #endif diff --git a/NXDNGateway/UDPSocket.cpp b/NXDNGateway/UDPSocket.cpp index 0792863..6981e29 100644 --- a/NXDNGateway/UDPSocket.cpp +++ b/NXDNGateway/UDPSocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2006-2016,2020,2024 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 @@ -34,29 +34,19 @@ #endif CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) : -m_address_save(address), -m_port_save(port), -m_counter(0U) +m_localAddress(address), +m_localPort(port), +m_fd(-1), +m_af(AF_UNSPEC) { - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - m_address[i] = ""; - m_port[i] = 0U; - m_af[i] = 0U; - m_fd[i] = -1; - } } CUDPSocket::CUDPSocket(unsigned short port) : -m_address_save(), -m_port_save(port), -m_counter(0U) +m_localAddress(), +m_localPort(port), +m_fd(-1), +m_af(AF_UNSPEC) { - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - m_address[i] = ""; - m_port[i] = 0U; - m_af[i] = 0U; - m_fd[i] = -1; - } } CUDPSocket::~CUDPSocket() @@ -93,10 +83,10 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd std::string portstr = std::to_string(port); struct addrinfo *res; - /* port is always digits, no needs to lookup service */ + /* 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); + 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)); @@ -109,7 +99,7 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen); - freeaddrinfo(res); + ::freeaddrinfo(res); return 0; } @@ -121,33 +111,33 @@ bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& ad if (type == IMT_ADDRESS_AND_PORT) { 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; + 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; } } else if (type == IMT_ADDRESS_ONLY) { 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; - 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); - default: - return false; + 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; + 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); + default: + return false; } } else { return false; @@ -163,35 +153,34 @@ bool CUDPSocket::isNone(const sockaddr_storage& addr) bool CUDPSocket::open(const sockaddr_storage& address) { - return open(address.ss_family); + m_af = address.ss_family; + + return open(); } -bool CUDPSocket::open(unsigned int af) +bool CUDPSocket::open() { - return open(0, af, m_address_save, m_port_save); -} + assert(m_fd == -1); -bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short port) -{ sockaddr_storage addr; unsigned int addrlen; struct addrinfo hints; ::memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; - hints.ai_family = af; + hints.ai_family = m_af; - /* to determine protocol family, call lookup() first. */ - int err = lookup(address, port, addr, addrlen, hints); + // To determine protocol family, call lookup() on the local address first. + int err = lookup(m_localAddress, m_localPort, addr, addrlen, hints); if (err != 0) { - LogError("The local address is invalid - %s", address.c_str()); + LogError("The local address is invalid - %s", m_localAddress.c_str()); return false; } - close(index); + m_af = addr.ss_family; - int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); - if (fd < 0) { + m_fd = ::socket(m_af, SOCK_DGRAM, 0); + if (m_fd < 0) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); #else @@ -200,62 +189,51 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std return false; } - m_address[index] = address; - m_port[index] = port; - m_af[index] = addr.ss_family; - m_fd[index] = fd; - - if (port > 0U) { + if (m_localPort > 0U) { int reuse = 1; - if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { + if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError()); #else LogError("Cannot set the UDP socket option, err: %d", errno); #endif + close(); return false; } - if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) { + if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); #else LogError("Cannot bind the UDP address, err: %d", errno); #endif + close(); return false; } - LogInfo("Opening UDP port on %hu", port); + LogInfo("Opening UDP port on %hu", m_localPort); } return true; } -int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length) +int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &addressLength) { assert(buffer != NULL); assert(length > 0U); + assert(m_fd >= 0); // Check that the readfrom() won't block - int i, n; - struct pollfd pfd[UDP_SOCKET_MAX]; - for (i = n = 0; i < UDP_SOCKET_MAX; i++) { - if (m_fd[i] >= 0) { - pfd[n].fd = m_fd[i]; - pfd[n].events = POLLIN; - n++; - } - } - - // no socket descriptor to receive - if (n == 0) - return 0; + struct pollfd pfd; + pfd.fd = m_fd; + pfd.events = POLLIN; + pfd.revents = 0; // Return immediately #if defined(_WIN32) || defined(_WIN64) - int ret = WSAPoll(pfd, n, 0); + int ret = WSAPoll(&pfd, 1, 0); #else - int ret = ::poll(pfd, n, 0); + int ret = ::poll(&pfd, 1, 0); #endif if (ret < 0) { #if defined(_WIN32) || defined(_WIN64) @@ -266,14 +244,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag return -1; } - int index; - for (i = 0; i < n; i++) { - // round robin - index = (i + m_counter) % n; - if (pfd[index].revents & POLLIN) - break; - } - if (i == n) + if ((pfd.revents & POLLIN) == 0) return 0; #if defined(_WIN32) || defined(_WIN64) @@ -283,9 +254,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag #endif #if defined(_WIN32) || defined(_WIN64) - int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); + int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #else - ssize_t len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); + ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #endif if (len <= 0) { #if defined(_WIN32) || defined(_WIN64) @@ -294,7 +265,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag LogError("Error returned from recvfrom, err: %d", errno); if (len == -1 && errno == ENOTSOCK) { - LogMessage("Re-opening UDP port on %hu", m_port[index]); + LogMessage("Re-opening UDP port on %hu", m_localPort); close(); open(); } @@ -302,43 +273,39 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag return -1; } - m_counter++; - address_length = size; + addressLength = size; + return len; } -bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length) +bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength) { assert(buffer != NULL); assert(length > 0U); + assert(m_fd >= 0); bool result = false; - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - if (m_fd[i] < 0 || m_af[i] != address.ss_family) - continue; - #if defined(_WIN32) || defined(_WIN64) - int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length); + int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength); #else - ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length); + ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength); #endif - if (ret < 0) { + if (ret < 0) { #if defined(_WIN32) || defined(_WIN64) - LogError("Error returned from sendto, err: %lu", ::GetLastError()); + LogError("Error returned from sendto, err: %lu", ::GetLastError()); #else - LogError("Error returned from sendto, err: %d", errno); + LogError("Error returned from sendto, err: %d", errno); #endif - } else { + } else { #if defined(_WIN32) || defined(_WIN64) - if (ret == int(length)) - result = true; + if (ret == int(length)) + result = true; #else - if (ret == ssize_t(length)) - result = true; + if (ret == ssize_t(length)) + result = true; #endif - } } return result; @@ -346,18 +313,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s void CUDPSocket::close() { - for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) - close(i); -} - -void CUDPSocket::close(const unsigned int index) -{ - if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) { + if (m_fd >= 0) { #if defined(_WIN32) || defined(_WIN64) - ::closesocket(m_fd[index]); + ::closesocket(m_fd); #else - ::close(m_fd[index]); + ::close(m_fd); #endif - m_fd[index] = -1; + m_fd = -1; } } + diff --git a/NXDNGateway/UDPSocket.h b/NXDNGateway/UDPSocket.h index 3e75554..28e350c 100644 --- a/NXDNGateway/UDPSocket.h +++ b/NXDNGateway/UDPSocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2011,2013,2015,2016,2020,2024 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 @@ -35,10 +35,6 @@ #include #endif -#if !defined(UDP_SOCKET_MAX) -#define UDP_SOCKET_MAX 1 -#endif - enum IPMATCHTYPE { IMT_ADDRESS_AND_PORT, IMT_ADDRESS_ONLY @@ -50,34 +46,33 @@ public: CUDPSocket(unsigned short port = 0U); ~CUDPSocket(); - bool open(unsigned int af = AF_UNSPEC); + bool open(); bool open(const sockaddr_storage& address); - bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short 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); + int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &addressLength); + bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength); void close(); - void close(const unsigned int index); static void startup(); static void shutdown(); - static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length); - static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints); + static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& addressLength); + static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& addressLength, struct addrinfo& hints); static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT); static bool isNone(const sockaddr_storage& addr); private: - std::string m_address_save; - unsigned short m_port_save; - std::string m_address[UDP_SOCKET_MAX]; - unsigned short m_port[UDP_SOCKET_MAX]; - unsigned int m_af[UDP_SOCKET_MAX]; - int m_fd[UDP_SOCKET_MAX]; - unsigned int m_counter; + std::string m_localAddress; + unsigned short m_localPort; +#if defined(_WIN32) || defined(_WIN64) + SOCKET m_fd; +#else + int m_fd; +#endif + sa_family_t m_af; }; #endif diff --git a/NXDNGateway/Version.h b/NXDNGateway/Version.h index e8d17bf..edafaf8 100644 --- a/NXDNGateway/Version.h +++ b/NXDNGateway/Version.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018,2020,2023 by Jonathan Naylor G4KLX + * Copyright (C) 2015-2018,2020,2023,2024 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 = "20231112"; +const char* VERSION = "20240129"; #endif diff --git a/NXDNParrot/UDPSocket.cpp b/NXDNParrot/UDPSocket.cpp index 0792863..6981e29 100644 --- a/NXDNParrot/UDPSocket.cpp +++ b/NXDNParrot/UDPSocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2006-2016,2020,2024 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 @@ -34,29 +34,19 @@ #endif CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) : -m_address_save(address), -m_port_save(port), -m_counter(0U) +m_localAddress(address), +m_localPort(port), +m_fd(-1), +m_af(AF_UNSPEC) { - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - m_address[i] = ""; - m_port[i] = 0U; - m_af[i] = 0U; - m_fd[i] = -1; - } } CUDPSocket::CUDPSocket(unsigned short port) : -m_address_save(), -m_port_save(port), -m_counter(0U) +m_localAddress(), +m_localPort(port), +m_fd(-1), +m_af(AF_UNSPEC) { - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - m_address[i] = ""; - m_port[i] = 0U; - m_af[i] = 0U; - m_fd[i] = -1; - } } CUDPSocket::~CUDPSocket() @@ -93,10 +83,10 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd std::string portstr = std::to_string(port); struct addrinfo *res; - /* port is always digits, no needs to lookup service */ + /* 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); + 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)); @@ -109,7 +99,7 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen); - freeaddrinfo(res); + ::freeaddrinfo(res); return 0; } @@ -121,33 +111,33 @@ bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& ad if (type == IMT_ADDRESS_AND_PORT) { 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; + 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; } } else if (type == IMT_ADDRESS_ONLY) { 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; - 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); - default: - return false; + 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; + 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); + default: + return false; } } else { return false; @@ -163,35 +153,34 @@ bool CUDPSocket::isNone(const sockaddr_storage& addr) bool CUDPSocket::open(const sockaddr_storage& address) { - return open(address.ss_family); + m_af = address.ss_family; + + return open(); } -bool CUDPSocket::open(unsigned int af) +bool CUDPSocket::open() { - return open(0, af, m_address_save, m_port_save); -} + assert(m_fd == -1); -bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short port) -{ sockaddr_storage addr; unsigned int addrlen; struct addrinfo hints; ::memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; - hints.ai_family = af; + hints.ai_family = m_af; - /* to determine protocol family, call lookup() first. */ - int err = lookup(address, port, addr, addrlen, hints); + // To determine protocol family, call lookup() on the local address first. + int err = lookup(m_localAddress, m_localPort, addr, addrlen, hints); if (err != 0) { - LogError("The local address is invalid - %s", address.c_str()); + LogError("The local address is invalid - %s", m_localAddress.c_str()); return false; } - close(index); + m_af = addr.ss_family; - int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); - if (fd < 0) { + m_fd = ::socket(m_af, SOCK_DGRAM, 0); + if (m_fd < 0) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); #else @@ -200,62 +189,51 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std return false; } - m_address[index] = address; - m_port[index] = port; - m_af[index] = addr.ss_family; - m_fd[index] = fd; - - if (port > 0U) { + if (m_localPort > 0U) { int reuse = 1; - if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { + if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError()); #else LogError("Cannot set the UDP socket option, err: %d", errno); #endif + close(); return false; } - if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) { + if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) { #if defined(_WIN32) || defined(_WIN64) LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); #else LogError("Cannot bind the UDP address, err: %d", errno); #endif + close(); return false; } - LogInfo("Opening UDP port on %hu", port); + LogInfo("Opening UDP port on %hu", m_localPort); } return true; } -int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length) +int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &addressLength) { assert(buffer != NULL); assert(length > 0U); + assert(m_fd >= 0); // Check that the readfrom() won't block - int i, n; - struct pollfd pfd[UDP_SOCKET_MAX]; - for (i = n = 0; i < UDP_SOCKET_MAX; i++) { - if (m_fd[i] >= 0) { - pfd[n].fd = m_fd[i]; - pfd[n].events = POLLIN; - n++; - } - } - - // no socket descriptor to receive - if (n == 0) - return 0; + struct pollfd pfd; + pfd.fd = m_fd; + pfd.events = POLLIN; + pfd.revents = 0; // Return immediately #if defined(_WIN32) || defined(_WIN64) - int ret = WSAPoll(pfd, n, 0); + int ret = WSAPoll(&pfd, 1, 0); #else - int ret = ::poll(pfd, n, 0); + int ret = ::poll(&pfd, 1, 0); #endif if (ret < 0) { #if defined(_WIN32) || defined(_WIN64) @@ -266,14 +244,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag return -1; } - int index; - for (i = 0; i < n; i++) { - // round robin - index = (i + m_counter) % n; - if (pfd[index].revents & POLLIN) - break; - } - if (i == n) + if ((pfd.revents & POLLIN) == 0) return 0; #if defined(_WIN32) || defined(_WIN64) @@ -283,9 +254,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag #endif #if defined(_WIN32) || defined(_WIN64) - int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); + int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #else - ssize_t len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); + ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size); #endif if (len <= 0) { #if defined(_WIN32) || defined(_WIN64) @@ -294,7 +265,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag LogError("Error returned from recvfrom, err: %d", errno); if (len == -1 && errno == ENOTSOCK) { - LogMessage("Re-opening UDP port on %hu", m_port[index]); + LogMessage("Re-opening UDP port on %hu", m_localPort); close(); open(); } @@ -302,43 +273,39 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag return -1; } - m_counter++; - address_length = size; + addressLength = size; + return len; } -bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length) +bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength) { assert(buffer != NULL); assert(length > 0U); + assert(m_fd >= 0); bool result = false; - for (int i = 0; i < UDP_SOCKET_MAX; i++) { - if (m_fd[i] < 0 || m_af[i] != address.ss_family) - continue; - #if defined(_WIN32) || defined(_WIN64) - int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length); + int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength); #else - ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length); + ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength); #endif - if (ret < 0) { + if (ret < 0) { #if defined(_WIN32) || defined(_WIN64) - LogError("Error returned from sendto, err: %lu", ::GetLastError()); + LogError("Error returned from sendto, err: %lu", ::GetLastError()); #else - LogError("Error returned from sendto, err: %d", errno); + LogError("Error returned from sendto, err: %d", errno); #endif - } else { + } else { #if defined(_WIN32) || defined(_WIN64) - if (ret == int(length)) - result = true; + if (ret == int(length)) + result = true; #else - if (ret == ssize_t(length)) - result = true; + if (ret == ssize_t(length)) + result = true; #endif - } } return result; @@ -346,18 +313,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s void CUDPSocket::close() { - for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) - close(i); -} - -void CUDPSocket::close(const unsigned int index) -{ - if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) { + if (m_fd >= 0) { #if defined(_WIN32) || defined(_WIN64) - ::closesocket(m_fd[index]); + ::closesocket(m_fd); #else - ::close(m_fd[index]); + ::close(m_fd); #endif - m_fd[index] = -1; + m_fd = -1; } } + diff --git a/NXDNParrot/UDPSocket.h b/NXDNParrot/UDPSocket.h index 3e75554..28e350c 100644 --- a/NXDNParrot/UDPSocket.h +++ b/NXDNParrot/UDPSocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2011,2013,2015,2016,2020,2024 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 @@ -35,10 +35,6 @@ #include #endif -#if !defined(UDP_SOCKET_MAX) -#define UDP_SOCKET_MAX 1 -#endif - enum IPMATCHTYPE { IMT_ADDRESS_AND_PORT, IMT_ADDRESS_ONLY @@ -50,34 +46,33 @@ public: CUDPSocket(unsigned short port = 0U); ~CUDPSocket(); - bool open(unsigned int af = AF_UNSPEC); + bool open(); bool open(const sockaddr_storage& address); - bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short 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); + int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &addressLength); + bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength); void close(); - void close(const unsigned int index); static void startup(); static void shutdown(); - static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length); - static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints); + static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& addressLength); + static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& addressLength, struct addrinfo& hints); static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT); static bool isNone(const sockaddr_storage& addr); private: - std::string m_address_save; - unsigned short m_port_save; - std::string m_address[UDP_SOCKET_MAX]; - unsigned short m_port[UDP_SOCKET_MAX]; - unsigned int m_af[UDP_SOCKET_MAX]; - int m_fd[UDP_SOCKET_MAX]; - unsigned int m_counter; + std::string m_localAddress; + unsigned short m_localPort; +#if defined(_WIN32) || defined(_WIN64) + SOCKET m_fd; +#else + int m_fd; +#endif + sa_family_t m_af; }; #endif diff --git a/NXDNParrot/Version.h b/NXDNParrot/Version.h index 6e33237..e24a117 100644 --- a/NXDNParrot/Version.h +++ b/NXDNParrot/Version.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2018,2024 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 = "20201101"; +const char* VERSION = "20240129"; #endif