mirror of
https://github.com/ShaYmez/P25Clients.git
synced 2024-11-21 19:55:22 -05:00
IPv6 cleanups.
This commit is contained in:
parent
59dd67a952
commit
27501ccdb3
@ -1,6 +1,6 @@
|
|||||||
CC = gcc
|
CC = cc
|
||||||
CXX = g++
|
CXX = c++
|
||||||
CFLAGS = -g -O3 -Wall -std=c++0x -pthread
|
CFLAGS = -g -O3 -Wall -DHAVE_LOG_H -std=c++0x -pthread
|
||||||
LIBS = -lpthread
|
LIBS = -lpthread
|
||||||
LDFLAGS = -g
|
LDFLAGS = -g
|
||||||
|
|
||||||
@ -19,4 +19,4 @@ install:
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) P25Gateway *.o *.d *.bak *~
|
$(RM) P25Gateway *.o *.d *.bak *~
|
||||||
|
|
||||||
|
@ -42,9 +42,9 @@ public:
|
|||||||
void close();
|
void close();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_callsign;
|
std::string m_callsign;
|
||||||
CUDPSocket m_socket;
|
CUDPSocket m_socket;
|
||||||
bool m_debug;
|
bool m_debug;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -169,7 +169,10 @@ void CP25Gateway::run()
|
|||||||
|
|
||||||
sockaddr_storage rptAddr;
|
sockaddr_storage rptAddr;
|
||||||
unsigned int rptAddrLen;
|
unsigned int rptAddrLen;
|
||||||
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
|
if (CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen) != 0) {
|
||||||
|
LogError("Unable to resolve the address of the host");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CUDPSocket* remoteSocket = NULL;
|
CUDPSocket* remoteSocket = NULL;
|
||||||
if (m_conf.getRemoteCommandsEnabled()) {
|
if (m_conf.getRemoteCommandsEnabled()) {
|
||||||
|
@ -83,10 +83,17 @@ bool CReflectors::load()
|
|||||||
std::string host = std::string(p2);
|
std::string host = std::string(p2);
|
||||||
unsigned int port = (unsigned int)::atoi(p3);
|
unsigned int port = (unsigned int)::atoi(p3);
|
||||||
|
|
||||||
CP25Reflector* refl = new CP25Reflector;
|
sockaddr_storage addr;
|
||||||
refl->m_id = (unsigned int)::atoi(p1);
|
unsigned int addrLen;
|
||||||
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
|
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
|
||||||
m_reflectors.push_back(refl);
|
CP25Reflector* refl = new CP25Reflector;
|
||||||
|
refl->m_id = (unsigned int)::atoi(p1);
|
||||||
|
refl->m_addr = addr;
|
||||||
|
refl->m_addrLen = addrLen;
|
||||||
|
m_reflectors.push_back(refl);
|
||||||
|
} else {
|
||||||
|
LogWarning("Unable to resolve the address of %s", host.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,10 +118,17 @@ bool CReflectors::load()
|
|||||||
std::string host = std::string(p2);
|
std::string host = std::string(p2);
|
||||||
unsigned int port = (unsigned int)::atoi(p3);
|
unsigned int port = (unsigned int)::atoi(p3);
|
||||||
|
|
||||||
CP25Reflector* refl = new CP25Reflector;
|
sockaddr_storage addr;
|
||||||
refl->m_id = id;
|
unsigned int addrLen;
|
||||||
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
|
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
|
||||||
m_reflectors.push_back(refl);
|
CP25Reflector* refl = new CP25Reflector;
|
||||||
|
refl->m_id = id;
|
||||||
|
refl->m_addr = addr;
|
||||||
|
refl->m_addrLen = addrLen;
|
||||||
|
m_reflectors.push_back(refl);
|
||||||
|
} else {
|
||||||
|
LogWarning("Unable to resolve the address of %s", host.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,20 +141,34 @@ bool CReflectors::load()
|
|||||||
|
|
||||||
// Add the Parrot entry
|
// Add the Parrot entry
|
||||||
if (m_parrotPort > 0U) {
|
if (m_parrotPort > 0U) {
|
||||||
CP25Reflector* refl = new CP25Reflector;
|
sockaddr_storage addr;
|
||||||
refl->m_id = 10U;
|
unsigned int addrLen;
|
||||||
CUDPSocket::lookup(m_parrotAddress, m_parrotPort, refl->m_addr, refl->m_addrLen);
|
if (CUDPSocket::lookup(m_parrotAddress, m_parrotPort, addr, addrLen) == 0) {
|
||||||
m_reflectors.push_back(refl);
|
CP25Reflector* refl = new CP25Reflector;
|
||||||
LogInfo("Loaded P25 parrot (TG%u)", refl->m_id);
|
refl->m_id = 10U;
|
||||||
|
refl->m_addr = addr;
|
||||||
|
refl->m_addrLen = addrLen;
|
||||||
|
m_reflectors.push_back(refl);
|
||||||
|
LogInfo("Loaded P25 parrot (TG%u)", refl->m_id);
|
||||||
|
} else {
|
||||||
|
LogWarning("Unable to resolve the address of the Parrot");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the P252DMR entry
|
// Add the P252DMR entry
|
||||||
if (m_p252dmrPort > 0U) {
|
if (m_p252dmrPort > 0U) {
|
||||||
CP25Reflector* refl = new CP25Reflector;
|
sockaddr_storage addr;
|
||||||
refl->m_id = 20U;
|
unsigned int addrLen;
|
||||||
CUDPSocket::lookup(m_p252dmrAddress, m_p252dmrPort, refl->m_addr, refl->m_addrLen);
|
if (CUDPSocket::lookup(m_p252dmrAddress, m_p252dmrPort, addr, addrLen) == 0) {
|
||||||
m_reflectors.push_back(refl);
|
CP25Reflector* refl = new CP25Reflector;
|
||||||
LogInfo("Loaded P252DMR (TG%u)", refl->m_id);
|
refl->m_id = 20U;
|
||||||
|
refl->m_addr = addr;
|
||||||
|
refl->m_addrLen = addrLen;
|
||||||
|
m_reflectors.push_back(refl);
|
||||||
|
LogInfo("Loaded P252DMR (TG%u)", refl->m_id);
|
||||||
|
} else {
|
||||||
|
LogWarning("Unable to resolve the address of P252DMR");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size = m_reflectors.size();
|
size = m_reflectors.size();
|
||||||
|
@ -54,14 +54,14 @@ public:
|
|||||||
void clock(unsigned int ms);
|
void clock(unsigned int ms);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_hostsFile1;
|
std::string m_hostsFile1;
|
||||||
std::string m_hostsFile2;
|
std::string m_hostsFile2;
|
||||||
std::string m_parrotAddress;
|
std::string m_parrotAddress;
|
||||||
unsigned int m_parrotPort;
|
unsigned int m_parrotPort;
|
||||||
std::string m_p252dmrAddress;
|
std::string m_p252dmrAddress;
|
||||||
unsigned int m_p252dmrPort;
|
unsigned int m_p252dmrPort;
|
||||||
std::vector<CP25Reflector*> m_reflectors;
|
std::vector<CP25Reflector*> m_reflectors;
|
||||||
CTimer m_timer;
|
CTimer m_timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "UDPSocket.h"
|
#include "UDPSocket.h"
|
||||||
#include "Log.h"
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@ -26,26 +25,36 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_LOG_H)
|
||||||
|
#include "Log.h"
|
||||||
|
#else
|
||||||
|
#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 int port) :
|
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
||||||
m_address(address),
|
m_address_save(address),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
assert(!address.empty());
|
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
LogError("Error from WSAStartup");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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 int port) :
|
CUDPSocket::CUDPSocket(unsigned int port) :
|
||||||
m_address(),
|
m_address_save(),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
@ -53,6 +62,12 @@ m_fd(-1)
|
|||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
LogError("Error from WSAStartup");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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()
|
||||||
@ -96,58 +111,82 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
|
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
|
||||||
{
|
{
|
||||||
if (addr1.ss_family != addr2.ss_family)
|
if (addr1.ss_family != addr2.ss_family)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (addr1.ss_family) {
|
if (type == IMT_ADDRESS_AND_PORT) {
|
||||||
case AF_INET:
|
switch (addr1.ss_family) {
|
||||||
struct sockaddr_in *in_1, *in_2;
|
case AF_INET:
|
||||||
in_1 = (struct sockaddr_in*)&addr1;
|
struct sockaddr_in *in_1, *in_2;
|
||||||
in_2 = (struct sockaddr_in*)&addr2;
|
in_1 = (struct sockaddr_in*)&addr1;
|
||||||
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
|
in_2 = (struct sockaddr_in*)&addr2;
|
||||||
case AF_INET6:
|
return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
|
||||||
struct sockaddr_in6 *in6_1, *in6_2;
|
case AF_INET6:
|
||||||
in6_1 = (struct sockaddr_in6*)&addr1;
|
struct sockaddr_in6 *in6_1, *in6_2;
|
||||||
in6_2 = (struct sockaddr_in6*)&addr2;
|
in6_1 = (struct sockaddr_in6*)&addr1;
|
||||||
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
|
in6_2 = (struct sockaddr_in6*)&addr2;
|
||||||
default:
|
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;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::isnone(const sockaddr_storage& addr)
|
bool CUDPSocket::isNone(const sockaddr_storage& addr)
|
||||||
{
|
{
|
||||||
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
||||||
|
|
||||||
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open()
|
bool CUDPSocket::open(const sockaddr_storage& address)
|
||||||
{
|
{
|
||||||
return open(AF_UNSPEC);
|
return open(address.ss_family);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open(const unsigned int af)
|
bool CUDPSocket::open(unsigned int af)
|
||||||
|
{
|
||||||
|
return open(0, af, m_address_save, m_port_save);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int 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 = af;
|
||||||
|
|
||||||
/* to determine protocol family, call lookup() first. */
|
/* to determine protocol family, call lookup() first. */
|
||||||
int err = lookup(m_address, m_port, addr, addrlen, hints);
|
int err = lookup(address, port, addr, addrlen, hints);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
LogError("The local address is invalid - %s", m_address.c_str());
|
LogError("The local address is invalid - %s", address.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
||||||
if (m_fd < 0) {
|
if (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
|
||||||
@ -156,9 +195,14 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_port > 0U) {
|
m_address[index] = address;
|
||||||
|
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(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
if (::setsockopt(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
|
||||||
@ -167,7 +211,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
if (::bind(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
|
||||||
@ -176,7 +220,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogInfo("Opening UDP port on %u", m_port);
|
LogInfo("Opening UDP port on %u", port);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -188,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
// Check that the readfrom() won't block
|
// Check that the readfrom() won't block
|
||||||
fd_set readFds;
|
int i, n;
|
||||||
FD_ZERO(&readFds);
|
struct pollfd pfd[UDP_SOCKET_MAX];
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
|
||||||
FD_SET((unsigned int)m_fd, &readFds);
|
if (m_fd[i] >= 0) {
|
||||||
#else
|
pfd[n].fd = m_fd[i];
|
||||||
FD_SET(m_fd, &readFds);
|
pfd[n].events = POLLIN;
|
||||||
#endif
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no socket descriptor to receive
|
||||||
|
if (n == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Return immediately
|
// Return immediately
|
||||||
timeval tv;
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
tv.tv_sec = 0L;
|
int ret = WSAPoll(pfd, n, 0);
|
||||||
tv.tv_usec = 0L;
|
#else
|
||||||
|
int ret = ::poll(pfd, n, 0);
|
||||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
#endif
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
LogError("Error returned from UDP select, err: %lu", ::GetLastError());
|
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
LogError("Error returned from UDP select, err: %d", errno);
|
LogError("Error returned from UDP poll, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0)
|
int index;
|
||||||
|
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)
|
||||||
@ -221,9 +278,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(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||||
#else
|
#else
|
||||||
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
ssize_t len = ::recvfrom(pfd[index].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)
|
||||||
@ -234,6 +291,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_counter++;
|
||||||
address_length = size;
|
address_length = size;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
@ -243,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
|
|||||||
assert(buffer != NULL);
|
assert(buffer != NULL);
|
||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
|
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, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#else
|
#else
|
||||||
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#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
|
||||||
return false;
|
} else {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
if (ret == int(length))
|
||||||
|
result = true;
|
||||||
|
#else
|
||||||
|
if (ret == ssize_t(length))
|
||||||
|
result = true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
return result;
|
||||||
if (ret != int(length))
|
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
if (ret != ssize_t(length))
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CUDPSocket::close()
|
void CUDPSocket::close()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (int i = 0; i < UDP_SOCKET_MAX; i++)
|
||||||
::closesocket(m_fd);
|
close(m_fd[i]);
|
||||||
#else
|
}
|
||||||
::close(m_fd);
|
|
||||||
#endif
|
void CUDPSocket::close(const unsigned int index)
|
||||||
|
{
|
||||||
|
if (m_fd[index] >= 0) {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
::closesocket(m_fd[index]);
|
||||||
|
#else
|
||||||
|
::close(m_fd[index]);
|
||||||
|
#endif
|
||||||
|
m_fd[index] = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@ -35,29 +36,45 @@
|
|||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(UDP_SOCKET_MAX)
|
||||||
|
#define UDP_SOCKET_MAX 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum IPMATCHTYPE {
|
||||||
|
IMT_ADDRESS_AND_PORT,
|
||||||
|
IMT_ADDRESS_ONLY
|
||||||
|
};
|
||||||
|
|
||||||
class CUDPSocket {
|
class CUDPSocket {
|
||||||
public:
|
public:
|
||||||
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
||||||
CUDPSocket(unsigned int port = 0U);
|
CUDPSocket(unsigned int port = 0U);
|
||||||
~CUDPSocket();
|
~CUDPSocket();
|
||||||
|
|
||||||
bool open();
|
bool open(unsigned int af = AF_UNSPEC);
|
||||||
bool open(const unsigned int af);
|
bool open(const sockaddr_storage& address);
|
||||||
|
bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int 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 &address_length);
|
||||||
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 address_length);
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
void close(const unsigned int index);
|
||||||
|
|
||||||
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);
|
||||||
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
|
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 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;
|
std::string m_address_save;
|
||||||
unsigned short m_port;
|
unsigned short m_port_save;
|
||||||
int m_fd;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -19,6 +19,6 @@
|
|||||||
#if !defined(VERSION_H)
|
#if !defined(VERSION_H)
|
||||||
#define VERSION_H
|
#define VERSION_H
|
||||||
|
|
||||||
const char* VERSION = "20200903";
|
const char* VERSION = "20200906";
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
CC = gcc
|
CC = cc
|
||||||
CXX = g++
|
CXX = c++
|
||||||
CFLAGS = -g -O3 -Wall -std=c++0x -pthread
|
CFLAGS = -g -O3 -Wall -std=c++0x -pthread
|
||||||
LIBS = -lpthread
|
LIBS = -lpthread
|
||||||
LDFLAGS = -g
|
LDFLAGS = -g
|
||||||
|
@ -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
|
* 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
|
||||||
@ -25,33 +25,49 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_LOG_H)
|
||||||
|
#include "Log.h"
|
||||||
|
#else
|
||||||
|
#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 int port) :
|
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
||||||
m_address(address),
|
m_address_save(address),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
assert(!address.empty());
|
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
::fprintf(stderr, "Error from WSAStartup\n");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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 int port) :
|
CUDPSocket::CUDPSocket(unsigned int port) :
|
||||||
m_address(),
|
m_address_save(),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
::fprintf(stderr, "Error from WSAStartup\n");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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()
|
||||||
@ -61,7 +77,7 @@ CUDPSocket::~CUDPSocket()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length)
|
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
::memset(&hints, 0, sizeof(hints));
|
::memset(&hints, 0, sizeof(hints));
|
||||||
@ -69,117 +85,142 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
|
|||||||
return lookup(hostname, port, addr, address_length, hints);
|
return lookup(hostname, port, addr, address_length, hints);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints)
|
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
|
||||||
{
|
{
|
||||||
int err;
|
|
||||||
std::string portstr = std::to_string(port);
|
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;
|
||||||
|
|
||||||
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) {
|
if (err != 0) {
|
||||||
sockaddr_in *paddr = (sockaddr_in *)&addr;
|
sockaddr_in* paddr = (sockaddr_in*)&addr;
|
||||||
::memset(paddr, 0, address_length = sizeof(sockaddr_in));
|
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in));
|
||||||
paddr->sin_family = AF_INET;
|
paddr->sin_family = AF_INET;
|
||||||
paddr->sin_port = htons(port);
|
paddr->sin_port = htons(port);
|
||||||
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
|
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
|
||||||
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
|
LogError("Cannot find address for host %s", hostname.c_str());
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2)
|
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
|
||||||
{
|
{
|
||||||
if (addr1.ss_family != addr2.ss_family)
|
if (addr1.ss_family != addr2.ss_family)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (addr1.ss_family) {
|
if (type == IMT_ADDRESS_AND_PORT) {
|
||||||
case AF_INET:
|
switch (addr1.ss_family) {
|
||||||
struct sockaddr_in *in_1, *in_2;
|
case AF_INET:
|
||||||
in_1 = (struct sockaddr_in *)&addr1;
|
struct sockaddr_in *in_1, *in_2;
|
||||||
in_2 = (struct sockaddr_in *)&addr2;
|
in_1 = (struct sockaddr_in*)&addr1;
|
||||||
return ( (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) &&
|
in_2 = (struct sockaddr_in*)&addr2;
|
||||||
(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) &&
|
return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port);
|
||||||
(in6_1->sin6_port == in6_2->sin6_port) );
|
default:
|
||||||
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;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::isnone(const sockaddr_storage &addr)
|
bool CUDPSocket::isNone(const sockaddr_storage& addr)
|
||||||
{
|
{
|
||||||
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
||||||
|
|
||||||
return ( (addr.ss_family == AF_INET) &&
|
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
||||||
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open()
|
bool CUDPSocket::open(const sockaddr_storage& address)
|
||||||
{
|
{
|
||||||
return open(AF_UNSPEC);
|
return open(address.ss_family);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open(const unsigned int af)
|
bool CUDPSocket::open(unsigned int af)
|
||||||
|
{
|
||||||
|
return open(0, af, m_address_save, m_port_save);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int port)
|
||||||
{
|
{
|
||||||
int err;
|
|
||||||
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 = af;
|
||||||
|
|
||||||
/* to determine protocol family, call lookup() first. */
|
/* to determine protocol family, call lookup() first. */
|
||||||
err = lookup(m_address, m_port, addr, addrlen, hints);
|
int err = lookup(address, port, addr, addrlen, hints);
|
||||||
if (err) {
|
if (err != 0) {
|
||||||
::fprintf(stderr, "The local address is invalid - %s\n", m_address.c_str());
|
LogError("The local address is invalid - %s", address.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
||||||
if (m_fd < 0) {
|
if (fd < 0) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
::fprintf(stderr, "Cannot create the UDP socket, err: %lu\n", ::GetLastError());
|
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Cannot create the UDP socket, err: %d\n", errno);
|
LogError("Cannot create the UDP socket, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_port > 0U) {
|
m_address[index] = address;
|
||||||
|
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(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
::fprintf(stderr, "Cannot set the UDP socket option, err: %lu\n", ::GetLastError());
|
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Cannot set the UDP socket option, err: %d\n", errno);
|
LogError("Cannot set the UDP socket option, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
::fprintf(stderr, "Cannot bind the UDP address, err: %lu\n", ::GetLastError());
|
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Cannot bind the UDP address, err: %d\n", errno);
|
LogError("Cannot bind the UDP address, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
::fprintf(stdout, "Opening UDP port on %u\n", m_port);
|
LogInfo("Opening UDP port on %u", port);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -191,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
// Check that the readfrom() won't block
|
// Check that the readfrom() won't block
|
||||||
fd_set readFds;
|
int i, n;
|
||||||
FD_ZERO(&readFds);
|
struct pollfd pfd[UDP_SOCKET_MAX];
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
|
||||||
FD_SET((unsigned int)m_fd, &readFds);
|
if (m_fd[i] >= 0) {
|
||||||
#else
|
pfd[n].fd = m_fd[i];
|
||||||
FD_SET(m_fd, &readFds);
|
pfd[n].events = POLLIN;
|
||||||
#endif
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no socket descriptor to receive
|
||||||
|
if (n == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Return immediately
|
// Return immediately
|
||||||
timeval tv;
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
tv.tv_sec = 0L;
|
int ret = WSAPoll(pfd, n, 0);
|
||||||
tv.tv_usec = 0L;
|
#else
|
||||||
|
int ret = ::poll(pfd, n, 0);
|
||||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
#endif
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
::fprintf(stderr, "Error returned from UDP select, err: %lu\n", ::GetLastError());
|
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Error returned from UDP select, err: %d\n", errno);
|
LogError("Error returned from UDP poll, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0)
|
int index;
|
||||||
|
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)
|
||||||
@ -224,19 +278,20 @@ 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(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||||
#else
|
#else
|
||||||
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
ssize_t len = ::recvfrom(pfd[index].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)
|
||||||
::fprintf(stderr, "Error returned from recvfrom, err: %lu\n", ::GetLastError());
|
LogError("Error returned from recvfrom, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Error returned from recvfrom, err: %d\n", errno);
|
LogError("Error returned from recvfrom, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_counter++;
|
||||||
address_length = size;
|
address_length = size;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
@ -246,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
|
|||||||
assert(buffer != NULL);
|
assert(buffer != NULL);
|
||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
|
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, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#else
|
#else
|
||||||
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#endif
|
#endif
|
||||||
if (ret < 0) {
|
|
||||||
|
if (ret < 0) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
::fprintf(stderr, "Error returned from sendto, err: %lu\n", ::GetLastError());
|
LogError("Error returned from sendto, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
::fprintf(stderr, "Error returned from sendto, err: %d\n", errno);
|
LogError("Error returned from sendto, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
} else {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
if (ret == int(length))
|
||||||
|
result = true;
|
||||||
|
#else
|
||||||
|
if (ret == ssize_t(length))
|
||||||
|
result = true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
return result;
|
||||||
if (ret != int(length))
|
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
if (ret != ssize_t(length))
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CUDPSocket::close()
|
void CUDPSocket::close()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (int i = 0; i < UDP_SOCKET_MAX; i++)
|
||||||
::closesocket(m_fd);
|
close(m_fd[i]);
|
||||||
#else
|
}
|
||||||
::close(m_fd);
|
|
||||||
#endif
|
void CUDPSocket::close(const unsigned int index)
|
||||||
|
{
|
||||||
|
if (m_fd[index] >= 0) {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
::closesocket(m_fd[index]);
|
||||||
|
#else
|
||||||
|
::close(m_fd[index]);
|
||||||
|
#endif
|
||||||
|
m_fd[index] = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
* 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,6 +26,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@ -35,29 +36,45 @@
|
|||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(UDP_SOCKET_MAX)
|
||||||
|
#define UDP_SOCKET_MAX 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum IPMATCHTYPE {
|
||||||
|
IMT_ADDRESS_AND_PORT,
|
||||||
|
IMT_ADDRESS_ONLY
|
||||||
|
};
|
||||||
|
|
||||||
class CUDPSocket {
|
class CUDPSocket {
|
||||||
public:
|
public:
|
||||||
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
||||||
CUDPSocket(unsigned int port = 0U);
|
CUDPSocket(unsigned int port = 0U);
|
||||||
~CUDPSocket();
|
~CUDPSocket();
|
||||||
|
|
||||||
bool open();
|
bool open(unsigned int af = AF_UNSPEC);
|
||||||
bool open(const unsigned int af);
|
bool open(const sockaddr_storage& address);
|
||||||
|
bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int 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 &address_length);
|
||||||
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 address_length);
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
void close(const unsigned int index);
|
||||||
|
|
||||||
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);
|
||||||
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints);
|
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 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;
|
std::string m_address_save;
|
||||||
unsigned short m_port;
|
unsigned short m_port_save;
|
||||||
int m_fd;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -19,6 +19,6 @@
|
|||||||
#if !defined(VERSION_H)
|
#if !defined(VERSION_H)
|
||||||
#define VERSION_H
|
#define VERSION_H
|
||||||
|
|
||||||
const char* VERSION = "20200903";
|
const char* VERSION = "20200906";
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
CC = gcc
|
CC = cc
|
||||||
CXX = g++
|
CXX = c++
|
||||||
CFLAGS = -g -O3 -Wall -std=c++0x -pthread
|
CFLAGS = -g -O3 -Wall -DHAVE_LOG_H -std=c++0x -pthread
|
||||||
LIBS = -lpthread
|
LIBS = -lpthread
|
||||||
LDFLAGS = -g
|
LDFLAGS = -g
|
||||||
|
|
||||||
@ -19,4 +19,4 @@ install:
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) P25Reflector *.o *.d *.bak *~
|
$(RM) P25Reflector *.o *.d *.bak *~
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ public:
|
|||||||
void close();
|
void close();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CUDPSocket m_socket;
|
CUDPSocket m_socket;
|
||||||
bool m_debug;
|
bool m_debug;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "UDPSocket.h"
|
#include "UDPSocket.h"
|
||||||
#include "Log.h"
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@ -26,26 +25,36 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_LOG_H)
|
||||||
|
#include "Log.h"
|
||||||
|
#else
|
||||||
|
#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 int port) :
|
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
||||||
m_address(address),
|
m_address_save(address),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
assert(!address.empty());
|
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
LogError("Error from WSAStartup");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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 int port) :
|
CUDPSocket::CUDPSocket(unsigned int port) :
|
||||||
m_address(),
|
m_address_save(),
|
||||||
m_port(port),
|
m_port_save(port),
|
||||||
m_fd(-1)
|
m_counter(0U)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
WSAData data;
|
WSAData data;
|
||||||
@ -53,6 +62,12 @@ m_fd(-1)
|
|||||||
if (wsaRet != 0)
|
if (wsaRet != 0)
|
||||||
LogError("Error from WSAStartup");
|
LogError("Error from WSAStartup");
|
||||||
#endif
|
#endif
|
||||||
|
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()
|
||||||
@ -96,58 +111,82 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
|
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
|
||||||
{
|
{
|
||||||
if (addr1.ss_family != addr2.ss_family)
|
if (addr1.ss_family != addr2.ss_family)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (addr1.ss_family) {
|
if (type == IMT_ADDRESS_AND_PORT) {
|
||||||
case AF_INET:
|
switch (addr1.ss_family) {
|
||||||
struct sockaddr_in *in_1, *in_2;
|
case AF_INET:
|
||||||
in_1 = (struct sockaddr_in*)&addr1;
|
struct sockaddr_in *in_1, *in_2;
|
||||||
in_2 = (struct sockaddr_in*)&addr2;
|
in_1 = (struct sockaddr_in*)&addr1;
|
||||||
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
|
in_2 = (struct sockaddr_in*)&addr2;
|
||||||
case AF_INET6:
|
return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
|
||||||
struct sockaddr_in6 *in6_1, *in6_2;
|
case AF_INET6:
|
||||||
in6_1 = (struct sockaddr_in6*)&addr1;
|
struct sockaddr_in6 *in6_1, *in6_2;
|
||||||
in6_2 = (struct sockaddr_in6*)&addr2;
|
in6_1 = (struct sockaddr_in6*)&addr1;
|
||||||
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
|
in6_2 = (struct sockaddr_in6*)&addr2;
|
||||||
default:
|
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;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::isnone(const sockaddr_storage& addr)
|
bool CUDPSocket::isNone(const sockaddr_storage& addr)
|
||||||
{
|
{
|
||||||
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
||||||
|
|
||||||
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open()
|
bool CUDPSocket::open(const sockaddr_storage& address)
|
||||||
{
|
{
|
||||||
return open(AF_UNSPEC);
|
return open(address.ss_family);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUDPSocket::open(const unsigned int af)
|
bool CUDPSocket::open(unsigned int af)
|
||||||
|
{
|
||||||
|
return open(0, af, m_address_save, m_port_save);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int 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 = af;
|
||||||
|
|
||||||
/* to determine protocol family, call lookup() first. */
|
/* to determine protocol family, call lookup() first. */
|
||||||
int err = lookup(m_address, m_port, addr, addrlen, hints);
|
int err = lookup(address, port, addr, addrlen, hints);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
LogError("The local address is invalid - %s", m_address.c_str());
|
LogError("The local address is invalid - %s", address.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
||||||
if (m_fd < 0) {
|
if (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
|
||||||
@ -156,9 +195,14 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_port > 0U) {
|
m_address[index] = address;
|
||||||
|
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(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
if (::setsockopt(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
|
||||||
@ -167,7 +211,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
if (::bind(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
|
||||||
@ -176,7 +220,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogInfo("Opening UDP port on %u", m_port);
|
LogInfo("Opening UDP port on %u", port);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -188,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
// Check that the readfrom() won't block
|
// Check that the readfrom() won't block
|
||||||
fd_set readFds;
|
int i, n;
|
||||||
FD_ZERO(&readFds);
|
struct pollfd pfd[UDP_SOCKET_MAX];
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
|
||||||
FD_SET((unsigned int)m_fd, &readFds);
|
if (m_fd[i] >= 0) {
|
||||||
#else
|
pfd[n].fd = m_fd[i];
|
||||||
FD_SET(m_fd, &readFds);
|
pfd[n].events = POLLIN;
|
||||||
#endif
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no socket descriptor to receive
|
||||||
|
if (n == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Return immediately
|
// Return immediately
|
||||||
timeval tv;
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
tv.tv_sec = 0L;
|
int ret = WSAPoll(pfd, n, 0);
|
||||||
tv.tv_usec = 0L;
|
#else
|
||||||
|
int ret = ::poll(pfd, n, 0);
|
||||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
#endif
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
LogError("Error returned from UDP select, err: %lu", ::GetLastError());
|
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
|
||||||
#else
|
#else
|
||||||
LogError("Error returned from UDP select, err: %d", errno);
|
LogError("Error returned from UDP poll, err: %d", errno);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0)
|
int index;
|
||||||
|
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)
|
||||||
@ -221,9 +278,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(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||||
#else
|
#else
|
||||||
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
ssize_t len = ::recvfrom(pfd[index].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)
|
||||||
@ -234,6 +291,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_counter++;
|
||||||
address_length = size;
|
address_length = size;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
@ -243,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
|
|||||||
assert(buffer != NULL);
|
assert(buffer != NULL);
|
||||||
assert(length > 0U);
|
assert(length > 0U);
|
||||||
|
|
||||||
|
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, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#else
|
#else
|
||||||
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||||
#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
|
||||||
return false;
|
} else {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
if (ret == int(length))
|
||||||
|
result = true;
|
||||||
|
#else
|
||||||
|
if (ret == ssize_t(length))
|
||||||
|
result = true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
return result;
|
||||||
if (ret != int(length))
|
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
if (ret != ssize_t(length))
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CUDPSocket::close()
|
void CUDPSocket::close()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
for (int i = 0; i < UDP_SOCKET_MAX; i++)
|
||||||
::closesocket(m_fd);
|
close(m_fd[i]);
|
||||||
#else
|
}
|
||||||
::close(m_fd);
|
|
||||||
#endif
|
void CUDPSocket::close(const unsigned int index)
|
||||||
|
{
|
||||||
|
if (m_fd[index] >= 0) {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
::closesocket(m_fd[index]);
|
||||||
|
#else
|
||||||
|
::close(m_fd[index]);
|
||||||
|
#endif
|
||||||
|
m_fd[index] = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@ -35,29 +36,45 @@
|
|||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(UDP_SOCKET_MAX)
|
||||||
|
#define UDP_SOCKET_MAX 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum IPMATCHTYPE {
|
||||||
|
IMT_ADDRESS_AND_PORT,
|
||||||
|
IMT_ADDRESS_ONLY
|
||||||
|
};
|
||||||
|
|
||||||
class CUDPSocket {
|
class CUDPSocket {
|
||||||
public:
|
public:
|
||||||
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
||||||
CUDPSocket(unsigned int port = 0U);
|
CUDPSocket(unsigned int port = 0U);
|
||||||
~CUDPSocket();
|
~CUDPSocket();
|
||||||
|
|
||||||
bool open();
|
bool open(unsigned int af = AF_UNSPEC);
|
||||||
bool open(const unsigned int af);
|
bool open(const sockaddr_storage& address);
|
||||||
|
bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int 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 &address_length);
|
||||||
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 address_length);
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
void close(const unsigned int index);
|
||||||
|
|
||||||
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);
|
||||||
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
|
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 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;
|
std::string m_address_save;
|
||||||
unsigned short m_port;
|
unsigned short m_port_save;
|
||||||
int m_fd;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user