P25Clients/P25Gateway/UDPSocket.cpp

326 lines
8.2 KiB
C++
Raw Normal View History

/*
2024-01-29 11:12:41 -05:00
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "UDPSocket.h"
#include <cassert>
#if !defined(_WIN32) && !defined(_WIN64)
#include <cerrno>
#include <cstring>
#endif
2020-09-06 08:59:52 -04:00
#if defined(HAVE_LOG_H)
#include "Log.h"
#else
#define LogMessage(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
2020-09-06 08:59:52 -04:00
#define LogError(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
#define LogInfo(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
#endif
CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) :
2024-01-29 11:12:41 -05:00
m_localAddress(address),
m_localPort(port),
m_fd(-1),
m_af(AF_UNSPEC)
{
}
CUDPSocket::CUDPSocket(unsigned short port) :
2024-01-29 11:12:41 -05:00
m_localAddress(),
m_localPort(port),
m_fd(-1),
m_af(AF_UNSPEC)
{
}
CUDPSocket::~CUDPSocket()
{
2020-09-20 16:49:25 -04:00
}
void CUDPSocket::startup()
{
#if defined(_WIN32) || defined(_WIN64)
WSAData data;
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
if (wsaRet != 0)
LogError("Error from WSAStartup");
#endif
}
void CUDPSocket::shutdown()
{
#if defined(_WIN32) || defined(_WIN64)
::WSACleanup();
#endif
}
int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockaddr_storage& addr, unsigned int& address_length)
{
2020-09-03 09:00:48 -04:00
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
2020-09-03 09:00:48 -04:00
{
std::string portstr = std::to_string(port);
struct addrinfo *res;
2024-01-29 11:12:41 -05:00
/* Port is always digits, no needs to lookup service */
2020-09-03 09:00:48 -04:00
hints.ai_flags |= AI_NUMERICSERV;
2024-01-29 11:12:41 -05:00
int err = ::getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
2020-09-03 09:00:48 -04:00
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;
}
2020-09-03 09:00:48 -04:00
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
2024-01-29 11:12:41 -05:00
::freeaddrinfo(res);
2020-09-03 09:00:48 -04:00
return 0;
}
2020-09-06 08:59:52 -04:00
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
2020-09-03 09:00:48 -04:00
{
if (addr1.ss_family != addr2.ss_family)
return false;
2020-09-06 08:59:52 -04:00
if (type == IMT_ADDRESS_AND_PORT) {
switch (addr1.ss_family) {
2024-01-29 11:12:41 -05:00
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;
2020-09-06 08:59:52 -04:00
}
} else if (type == IMT_ADDRESS_ONLY) {
switch (addr1.ss_family) {
2024-01-29 11:12:41 -05:00
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;
2020-09-06 08:59:52 -04:00
}
} else {
2020-09-03 09:00:48 -04:00
return false;
}
2020-09-03 09:00:48 -04:00
}
2020-09-06 08:59:52 -04:00
bool CUDPSocket::isNone(const sockaddr_storage& addr)
2020-09-03 09:00:48 -04:00
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
2020-09-03 09:00:48 -04:00
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
}
2020-09-06 08:59:52 -04:00
bool CUDPSocket::open(const sockaddr_storage& address)
{
2024-01-29 11:12:41 -05:00
m_af = address.ss_family;
2020-09-03 09:00:48 -04:00
2024-01-29 11:12:41 -05:00
return open();
2020-09-06 08:59:52 -04:00
}
2024-01-29 11:12:41 -05:00
bool CUDPSocket::open()
2020-09-03 09:00:48 -04:00
{
2024-01-29 11:12:41 -05:00
assert(m_fd == -1);
2020-09-03 09:00:48 -04:00
sockaddr_storage addr;
unsigned int addrlen;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
2020-09-06 08:59:52 -04:00
hints.ai_flags = AI_PASSIVE;
2024-01-29 11:12:41 -05:00
hints.ai_family = m_af;
2020-09-03 09:00:48 -04:00
2024-01-29 11:12:41 -05:00
// To determine protocol family, call lookup() on the local address first.
int err = lookup(m_localAddress, m_localPort, addr, addrlen, hints);
2020-09-03 09:00:48 -04:00
if (err != 0) {
2024-01-29 11:12:41 -05:00
LogError("The local address is invalid - %s", m_localAddress.c_str());
2020-09-03 09:00:48 -04:00
return false;
}
2024-01-29 11:12:41 -05:00
m_af = addr.ss_family;
2021-03-31 10:13:53 -04:00
2024-01-29 11:12:41 -05:00
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
LogError("Cannot create the UDP socket, err: %d", errno);
#endif
return false;
}
2024-01-29 11:12:41 -05:00
if (m_localPort > 0U) {
int reuse = 1;
2024-01-29 11:12:41 -05:00
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
2024-01-29 11:12:41 -05:00
close();
return false;
}
2024-01-29 11:12:41 -05:00
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
2024-01-29 11:12:41 -05:00
close();
return false;
}
2020-09-03 09:00:48 -04:00
2024-01-29 11:12:41 -05:00
LogInfo("Opening UDP port on %hu", m_localPort);
}
return true;
}
2024-01-29 11:12:41 -05:00
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &addressLength)
{
assert(buffer != NULL);
assert(length > 0U);
2024-01-29 11:12:41 -05:00
assert(m_fd >= 0);
// Check that the readfrom() won't block
2024-01-29 11:12:41 -05:00
struct pollfd pfd;
pfd.fd = m_fd;
pfd.events = POLLIN;
pfd.revents = 0;
2020-09-06 08:59:52 -04:00
// Return immediately
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
int ret = WSAPoll(&pfd, 1, 0);
#else
2024-01-29 11:12:41 -05:00
int ret = ::poll(&pfd, 1, 0);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)
2020-09-06 08:59:52 -04:00
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
#else
2020-09-06 08:59:52 -04:00
LogError("Error returned from UDP poll, err: %d", errno);
#endif
return -1;
}
2024-01-29 11:12:41 -05:00
if ((pfd.revents & POLLIN) == 0)
return 0;
#if defined(_WIN32) || defined(_WIN64)
2020-09-03 09:00:48 -04:00
int size = sizeof(sockaddr_storage);
#else
2020-09-03 09:00:48 -04:00
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
2024-01-29 11:12:41 -05:00
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from recvfrom, err: %lu", ::GetLastError());
#else
LogError("Error returned from recvfrom, err: %d", errno);
if (len == -1 && errno == ENOTSOCK) {
2024-01-29 11:12:41 -05:00
LogMessage("Re-opening UDP port on %hu", m_localPort);
close();
open();
}
#endif
return -1;
}
2024-01-29 11:12:41 -05:00
addressLength = size;
return len;
}
2024-01-29 11:12:41 -05:00
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int addressLength)
{
assert(buffer != NULL);
assert(length > 0U);
2024-01-29 11:12:41 -05:00
assert(m_fd >= 0);
2020-09-06 08:59:52 -04:00
bool result = false;
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength);
#else
2024-01-29 11:12:41 -05:00
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, addressLength);
#endif
2020-09-06 08:59:52 -04:00
2024-01-29 11:12:41 -05:00
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
LogError("Error returned from sendto, err: %lu", ::GetLastError());
#else
2024-01-29 11:12:41 -05:00
LogError("Error returned from sendto, err: %d", errno);
#endif
2024-01-29 11:12:41 -05:00
} else {
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
if (ret == int(length))
result = true;
#else
2024-01-29 11:12:41 -05:00
if (ret == ssize_t(length))
result = true;
#endif
2020-09-06 08:59:52 -04:00
}
2020-09-06 08:59:52 -04:00
return result;
}
void CUDPSocket::close()
{
2024-01-29 11:12:41 -05:00
if (m_fd >= 0) {
#if defined(_WIN32) || defined(_WIN64)
2024-01-29 11:12:41 -05:00
::closesocket(m_fd);
#else
2024-01-29 11:12:41 -05:00
::close(m_fd);
#endif
2024-01-29 11:12:41 -05:00
m_fd = -1;
2020-09-06 08:59:52 -04:00
}
}
2024-01-29 11:12:41 -05:00