Simplify the UDP socket handling.

This commit is contained in:
Jonathan Naylor 2024-01-29 16:12:41 +00:00
parent 968e33740b
commit d82a7b9435
9 changed files with 212 additions and 307 deletions

4
.gitignore vendored
View File

@ -13,6 +13,8 @@ x64
*.user *.user
*.VC.db *.VC.db
.vs .vs
P25Reflector/P25Reflector
P25Gateway/P25Gateway P25Gateway/P25Gateway
P25Parrot/P25Parrot P25Parrot/P25Parrot
P25Gateway/GitVersion.h
P25Parrot/GitVersion.h

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016,2020 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,2016,2020,2024 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -26,8 +26,7 @@
CP25Network::CP25Network(unsigned short port, const std::string& callsign, bool debug) : CP25Network::CP25Network(unsigned short port, const std::string& callsign, bool debug) :
m_callsign(callsign), m_callsign(callsign),
m_socket(), m_socket(port),
m_port(port),
m_debug(debug) m_debug(debug)
{ {
assert(port > 0U); assert(port > 0U);
@ -43,16 +42,7 @@ bool CP25Network::open()
{ {
LogInfo("Opening P25 network connection"); LogInfo("Opening P25 network connection");
unsigned int index = 0U; return m_socket.open();
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;
} }
bool CP25Network::write(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen) bool CP25Network::write(const unsigned char* data, unsigned int length, const sockaddr_storage& addr, unsigned int addrLen)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2014,2016,2020 by Jonathan Naylor G4KLX * Copyright (C) 2009-2014,2016,2020,2024 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -42,10 +42,9 @@ public:
void close(); void close();
private: private:
std::string m_callsign; std::string m_callsign;
CUDPSocket m_socket; CUDPSocket m_socket;
unsigned short m_port; bool m_debug;
bool m_debug;
}; };
#endif #endif

View File

@ -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 * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,29 +34,19 @@
#endif #endif
CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) : CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) :
m_address_save(address), m_localAddress(address),
m_port_save(port), m_localPort(port),
m_counter(0U) 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) : CUDPSocket::CUDPSocket(unsigned short port) :
m_address_save(), m_localAddress(),
m_port_save(port), m_localPort(port),
m_counter(0U) 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() CUDPSocket::~CUDPSocket()
@ -93,10 +83,10 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd
std::string portstr = std::to_string(port); std::string portstr = std::to_string(port);
struct addrinfo *res; 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; 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) { if (err != 0) {
sockaddr_in* paddr = (sockaddr_in*)&addr; sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in)); ::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); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res); ::freeaddrinfo(res);
return 0; return 0;
} }
@ -121,33 +111,33 @@ bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& ad
if (type == IMT_ADDRESS_AND_PORT) { if (type == IMT_ADDRESS_AND_PORT) {
switch (addr1.ss_family) { switch (addr1.ss_family) {
case AF_INET: case AF_INET:
struct sockaddr_in *in_1, *in_2; struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1; in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2; 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); return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
case AF_INET6: case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2; struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1; in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2; 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); return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port);
default: default:
return false; return false;
} }
} else if (type == IMT_ADDRESS_ONLY) { } else if (type == IMT_ADDRESS_ONLY) {
switch (addr1.ss_family) { switch (addr1.ss_family) {
case AF_INET: case AF_INET:
struct sockaddr_in *in_1, *in_2; struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1; in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2; in_2 = (struct sockaddr_in*)&addr2;
return in_1->sin_addr.s_addr == in_2->sin_addr.s_addr; return in_1->sin_addr.s_addr == in_2->sin_addr.s_addr;
case AF_INET6: case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2; struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1; in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2; in6_2 = (struct sockaddr_in6*)&addr2;
return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr); return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr);
default: default:
return false; return false;
} }
} else { } else {
return false; return false;
@ -163,35 +153,34 @@ bool CUDPSocket::isNone(const sockaddr_storage& addr)
bool CUDPSocket::open(const sockaddr_storage& address) 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; sockaddr_storage addr;
unsigned int addrlen; unsigned int addrlen;
struct addrinfo hints; struct addrinfo hints;
::memset(&hints, 0, sizeof(hints)); ::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
hints.ai_family = af; hints.ai_family = m_af;
/* to determine protocol family, call lookup() first. */ // To determine protocol family, call lookup() on the local address first.
int err = lookup(address, port, addr, addrlen, hints); int err = lookup(m_localAddress, m_localPort, addr, addrlen, hints);
if (err != 0) { 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; return false;
} }
close(index); m_af = addr.ss_family;
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); m_fd = ::socket(m_af, SOCK_DGRAM, 0);
if (fd < 0) { if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
#else #else
@ -200,62 +189,51 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std
return false; return false;
} }
m_address[index] = address; if (m_localPort > 0U) {
m_port[index] = port;
m_af[index] = addr.ss_family;
m_fd[index] = fd;
if (port > 0U) {
int reuse = 1; 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) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError()); LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
#else #else
LogError("Cannot set the UDP socket option, err: %d", errno); LogError("Cannot set the UDP socket option, err: %d", errno);
#endif #endif
close();
return false; return false;
} }
if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) { if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else #else
LogError("Cannot bind the UDP address, err: %d", errno); LogError("Cannot bind the UDP address, err: %d", errno);
#endif #endif
close();
return false; return false;
} }
LogInfo("Opening UDP port on %hu", port); LogInfo("Opening UDP port on %hu", m_localPort);
} }
return true; 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(buffer != NULL);
assert(length > 0U); assert(length > 0U);
assert(m_fd >= 0);
// Check that the readfrom() won't block // Check that the readfrom() won't block
int i, n; struct pollfd pfd;
struct pollfd pfd[UDP_SOCKET_MAX]; pfd.fd = m_fd;
for (i = n = 0; i < UDP_SOCKET_MAX; i++) { pfd.events = POLLIN;
if (m_fd[i] >= 0) { pfd.revents = 0;
pfd[n].fd = m_fd[i];
pfd[n].events = POLLIN;
n++;
}
}
// no socket descriptor to receive
if (n == 0)
return 0;
// Return immediately // Return immediately
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int ret = WSAPoll(pfd, n, 0); int ret = WSAPoll(&pfd, 1, 0);
#else #else
int ret = ::poll(pfd, n, 0); int ret = ::poll(&pfd, 1, 0);
#endif #endif
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -266,14 +244,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
return -1; return -1;
} }
int index; if ((pfd.revents & POLLIN) == 0)
for (i = 0; i < n; i++) {
// round robin
index = (i + m_counter) % n;
if (pfd[index].revents & POLLIN)
break;
}
if (i == n)
return 0; return 0;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -283,9 +254,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #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 #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 #endif
if (len <= 0) { if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64) #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); LogError("Error returned from recvfrom, err: %d", errno);
if (len == -1 && errno == ENOTSOCK) { 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(); close();
open(); open();
} }
@ -302,43 +273,39 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
return -1; return -1;
} }
m_counter++; addressLength = size;
address_length = size;
return len; 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(buffer != NULL);
assert(length > 0U); assert(length > 0U);
assert(m_fd >= 0);
bool result = false; 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) #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 #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 #endif
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from sendto, err: %lu", ::GetLastError()); LogError("Error returned from sendto, err: %lu", ::GetLastError());
#else #else
LogError("Error returned from sendto, err: %d", errno); LogError("Error returned from sendto, err: %d", errno);
#endif #endif
} else { } else {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
if (ret == int(length)) if (ret == int(length))
result = true; result = true;
#else #else
if (ret == ssize_t(length)) if (ret == ssize_t(length))
result = true; result = true;
#endif #endif
}
} }
return result; return result;
@ -346,18 +313,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
void CUDPSocket::close() void CUDPSocket::close()
{ {
for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) if (m_fd >= 0) {
close(i);
}
void CUDPSocket::close(const unsigned int index)
{
if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
::closesocket(m_fd[index]); ::closesocket(m_fd);
#else #else
::close(m_fd[index]); ::close(m_fd);
#endif #endif
m_fd[index] = -1; m_fd = -1;
} }
} }

View File

@ -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 * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -35,10 +35,6 @@
#include <ws2tcpip.h> #include <ws2tcpip.h>
#endif #endif
#if !defined(UDP_SOCKET_MAX)
#define UDP_SOCKET_MAX 1
#endif
enum IPMATCHTYPE { enum IPMATCHTYPE {
IMT_ADDRESS_AND_PORT, IMT_ADDRESS_AND_PORT,
IMT_ADDRESS_ONLY IMT_ADDRESS_ONLY
@ -50,34 +46,33 @@ public:
CUDPSocket(unsigned short port = 0U); CUDPSocket(unsigned short port = 0U);
~CUDPSocket(); ~CUDPSocket();
bool open(unsigned int af = AF_UNSPEC); bool open();
bool open(const sockaddr_storage& address); 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); 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 address_length); bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength);
void close(); void close();
void close(const unsigned int index);
static void startup(); static void startup();
static void shutdown(); 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& addressLength);
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, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT); static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT);
static bool isNone(const sockaddr_storage& addr); static bool isNone(const sockaddr_storage& addr);
private: private:
std::string m_address_save; std::string m_localAddress;
unsigned short m_port_save; unsigned short m_localPort;
std::string m_address[UDP_SOCKET_MAX]; #if defined(_WIN32) || defined(_WIN64)
unsigned short m_port[UDP_SOCKET_MAX]; SOCKET m_fd;
unsigned int m_af[UDP_SOCKET_MAX]; #else
int m_fd[UDP_SOCKET_MAX]; int m_fd;
unsigned int m_counter; #endif
sa_family_t m_af;
}; };
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015-2020 by Jonathan Naylor G4KLX * Copyright (C) 2015-2020,2024 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20201105"; const char* VERSION = "20240129";
#endif #endif

View File

@ -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 * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,29 +34,19 @@
#endif #endif
CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) : CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) :
m_address_save(address), m_localAddress(address),
m_port_save(port), m_localPort(port),
m_counter(0U) 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) : CUDPSocket::CUDPSocket(unsigned short port) :
m_address_save(), m_localAddress(),
m_port_save(port), m_localPort(port),
m_counter(0U) 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() CUDPSocket::~CUDPSocket()
@ -93,10 +83,10 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockadd
std::string portstr = std::to_string(port); std::string portstr = std::to_string(port);
struct addrinfo *res; 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; 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) { if (err != 0) {
sockaddr_in* paddr = (sockaddr_in*)&addr; sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in)); ::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); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res); ::freeaddrinfo(res);
return 0; return 0;
} }
@ -121,33 +111,33 @@ bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& ad
if (type == IMT_ADDRESS_AND_PORT) { if (type == IMT_ADDRESS_AND_PORT) {
switch (addr1.ss_family) { switch (addr1.ss_family) {
case AF_INET: case AF_INET:
struct sockaddr_in *in_1, *in_2; struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1; in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2; 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); return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
case AF_INET6: case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2; struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1; in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2; 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); return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port);
default: default:
return false; return false;
} }
} else if (type == IMT_ADDRESS_ONLY) { } else if (type == IMT_ADDRESS_ONLY) {
switch (addr1.ss_family) { switch (addr1.ss_family) {
case AF_INET: case AF_INET:
struct sockaddr_in *in_1, *in_2; struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1; in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2; in_2 = (struct sockaddr_in*)&addr2;
return in_1->sin_addr.s_addr == in_2->sin_addr.s_addr; return in_1->sin_addr.s_addr == in_2->sin_addr.s_addr;
case AF_INET6: case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2; struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1; in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2; in6_2 = (struct sockaddr_in6*)&addr2;
return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr); return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr);
default: default:
return false; return false;
} }
} else { } else {
return false; return false;
@ -163,35 +153,34 @@ bool CUDPSocket::isNone(const sockaddr_storage& addr)
bool CUDPSocket::open(const sockaddr_storage& address) 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; sockaddr_storage addr;
unsigned int addrlen; unsigned int addrlen;
struct addrinfo hints; struct addrinfo hints;
::memset(&hints, 0, sizeof(hints)); ::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
hints.ai_family = af; hints.ai_family = m_af;
/* to determine protocol family, call lookup() first. */ // To determine protocol family, call lookup() on the local address first.
int err = lookup(address, port, addr, addrlen, hints); int err = lookup(m_localAddress, m_localPort, addr, addrlen, hints);
if (err != 0) { 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; return false;
} }
close(index); m_af = addr.ss_family;
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); m_fd = ::socket(m_af, SOCK_DGRAM, 0);
if (fd < 0) { if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError()); LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
#else #else
@ -200,62 +189,51 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std
return false; return false;
} }
m_address[index] = address; if (m_localPort > 0U) {
m_port[index] = port;
m_af[index] = addr.ss_family;
m_fd[index] = fd;
if (port > 0U) {
int reuse = 1; 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) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError()); LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
#else #else
LogError("Cannot set the UDP socket option, err: %d", errno); LogError("Cannot set the UDP socket option, err: %d", errno);
#endif #endif
close();
return false; return false;
} }
if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) { if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError()); LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else #else
LogError("Cannot bind the UDP address, err: %d", errno); LogError("Cannot bind the UDP address, err: %d", errno);
#endif #endif
close();
return false; return false;
} }
LogInfo("Opening UDP port on %hu", port); LogInfo("Opening UDP port on %hu", m_localPort);
} }
return true; 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(buffer != NULL);
assert(length > 0U); assert(length > 0U);
assert(m_fd >= 0);
// Check that the readfrom() won't block // Check that the readfrom() won't block
int i, n; struct pollfd pfd;
struct pollfd pfd[UDP_SOCKET_MAX]; pfd.fd = m_fd;
for (i = n = 0; i < UDP_SOCKET_MAX; i++) { pfd.events = POLLIN;
if (m_fd[i] >= 0) { pfd.revents = 0;
pfd[n].fd = m_fd[i];
pfd[n].events = POLLIN;
n++;
}
}
// no socket descriptor to receive
if (n == 0)
return 0;
// Return immediately // Return immediately
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int ret = WSAPoll(pfd, n, 0); int ret = WSAPoll(&pfd, 1, 0);
#else #else
int ret = ::poll(pfd, n, 0); int ret = ::poll(&pfd, 1, 0);
#endif #endif
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -266,14 +244,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
return -1; return -1;
} }
int index; if ((pfd.revents & POLLIN) == 0)
for (i = 0; i < n; i++) {
// round robin
index = (i + m_counter) % n;
if (pfd[index].revents & POLLIN)
break;
}
if (i == n)
return 0; return 0;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@ -283,9 +254,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #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 #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 #endif
if (len <= 0) { if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64) #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); LogError("Error returned from recvfrom, err: %d", errno);
if (len == -1 && errno == ENOTSOCK) { 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(); close();
open(); open();
} }
@ -302,43 +273,39 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
return -1; return -1;
} }
m_counter++; addressLength = size;
address_length = size;
return len; 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(buffer != NULL);
assert(length > 0U); assert(length > 0U);
assert(m_fd >= 0);
bool result = false; 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) #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 #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 #endif
if (ret < 0) { if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from sendto, err: %lu", ::GetLastError()); LogError("Error returned from sendto, err: %lu", ::GetLastError());
#else #else
LogError("Error returned from sendto, err: %d", errno); LogError("Error returned from sendto, err: %d", errno);
#endif #endif
} else { } else {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
if (ret == int(length)) if (ret == int(length))
result = true; result = true;
#else #else
if (ret == ssize_t(length)) if (ret == ssize_t(length))
result = true; result = true;
#endif #endif
}
} }
return result; return result;
@ -346,18 +313,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
void CUDPSocket::close() void CUDPSocket::close()
{ {
for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) if (m_fd >= 0) {
close(i);
}
void CUDPSocket::close(const unsigned int index)
{
if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
::closesocket(m_fd[index]); ::closesocket(m_fd);
#else #else
::close(m_fd[index]); ::close(m_fd);
#endif #endif
m_fd[index] = -1; m_fd = -1;
} }
} }

View File

@ -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 * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -35,10 +35,6 @@
#include <ws2tcpip.h> #include <ws2tcpip.h>
#endif #endif
#if !defined(UDP_SOCKET_MAX)
#define UDP_SOCKET_MAX 1
#endif
enum IPMATCHTYPE { enum IPMATCHTYPE {
IMT_ADDRESS_AND_PORT, IMT_ADDRESS_AND_PORT,
IMT_ADDRESS_ONLY IMT_ADDRESS_ONLY
@ -50,34 +46,33 @@ public:
CUDPSocket(unsigned short port = 0U); CUDPSocket(unsigned short port = 0U);
~CUDPSocket(); ~CUDPSocket();
bool open(unsigned int af = AF_UNSPEC); bool open();
bool open(const sockaddr_storage& address); 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); 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 address_length); bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength);
void close(); void close();
void close(const unsigned int index);
static void startup(); static void startup();
static void shutdown(); 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& addressLength);
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, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT); static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT);
static bool isNone(const sockaddr_storage& addr); static bool isNone(const sockaddr_storage& addr);
private: private:
std::string m_address_save; std::string m_localAddress;
unsigned short m_port_save; unsigned short m_localPort;
std::string m_address[UDP_SOCKET_MAX]; #if defined(_WIN32) || defined(_WIN64)
unsigned short m_port[UDP_SOCKET_MAX]; SOCKET m_fd;
unsigned int m_af[UDP_SOCKET_MAX]; #else
int m_fd[UDP_SOCKET_MAX]; int m_fd;
unsigned int m_counter; #endif
sa_family_t m_af;
}; };
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX * Copyright (C) 2015,2016,2020,2024 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20201101"; const char* VERSION = "20240129";
#endif #endif