|
|
@ -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
|
|
|
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
in_addr CUDPSocket::lookup(const std::string& hostname)
|
|
|
|
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
in_addr addr;
|
|
|
|
struct addrinfo hints;
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
::memset(&hints, 0, sizeof(hints));
|
|
|
|
unsigned long address = ::inet_addr(hostname.c_str());
|
|
|
|
|
|
|
|
if (address != INADDR_NONE && address != INADDR_ANY) {
|
|
|
|
|
|
|
|
addr.s_addr = address;
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct hostent* hp = ::gethostbyname(hostname.c_str());
|
|
|
|
return lookup(hostname, port, addr, address_length, hints);
|
|
|
|
if (hp != NULL) {
|
|
|
|
}
|
|
|
|
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string portstr = std::to_string(port);
|
|
|
|
|
|
|
|
struct addrinfo *res;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* port is always digits, no needs to lookup service */
|
|
|
|
|
|
|
|
hints.ai_flags |= AI_NUMERICSERV;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
|
|
|
sockaddr_in* paddr = (sockaddr_in*)&addr;
|
|
|
|
|
|
|
|
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in));
|
|
|
|
|
|
|
|
paddr->sin_family = AF_INET;
|
|
|
|
|
|
|
|
paddr->sin_port = htons(port);
|
|
|
|
|
|
|
|
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
|
|
|
|
LogError("Cannot find address for host %s", hostname.c_str());
|
|
|
|
LogError("Cannot find address for host %s", hostname.c_str());
|
|
|
|
|
|
|
|
return err;
|
|
|
|
addr.s_addr = INADDR_NONE;
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
in_addr_t address = ::inet_addr(hostname.c_str());
|
|
|
|
|
|
|
|
if (address != in_addr_t(-1)) {
|
|
|
|
|
|
|
|
addr.s_addr = address;
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct hostent* hp = ::gethostbyname(hostname.c_str());
|
|
|
|
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
|
|
|
|
if (hp != NULL) {
|
|
|
|
|
|
|
|
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (addr1.ss_family != addr2.ss_family)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (addr1.ss_family) {
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
|
|
|
|
struct sockaddr_in *in_1, *in_2;
|
|
|
|
|
|
|
|
in_1 = (struct sockaddr_in*)&addr1;
|
|
|
|
|
|
|
|
in_2 = (struct sockaddr_in*)&addr2;
|
|
|
|
|
|
|
|
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
|
|
|
struct sockaddr_in6 *in6_1, *in6_2;
|
|
|
|
|
|
|
|
in6_1 = (struct sockaddr_in6*)&addr1;
|
|
|
|
|
|
|
|
in6_2 = (struct sockaddr_in6*)&addr2;
|
|
|
|
|
|
|
|
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LogError("Cannot find address for host %s", hostname.c_str());
|
|
|
|
bool CUDPSocket::isnone(const sockaddr_storage& addr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
|
|
|
|
|
|
|
|
|
|
|
addr.s_addr = INADDR_NONE;
|
|
|
|
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::open()
|
|
|
|
bool CUDPSocket::open()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
return open(AF_UNSPEC);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::open(const unsigned int af)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sockaddr_storage addr;
|
|
|
|
|
|
|
|
unsigned int addrlen;
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
::memset(&hints, 0, sizeof(hints));
|
|
|
|
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
|
|
|
hints.ai_family = af;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* to determine protocol family, call lookup() first. */
|
|
|
|
|
|
|
|
int err = lookup(m_address, m_port, addr, addrlen, hints);
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
|
|
|
LogError("The local address is invalid - %s", m_address.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
|
|
|
if (m_fd < 0) {
|
|
|
|
if (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());
|
|
|
@ -115,24 +157,6 @@ bool CUDPSocket::open()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_port > 0U) {
|
|
|
|
if (m_port > 0U) {
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
|
|
|
::memset(&addr, 0x00, sizeof(sockaddr_in));
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
addr.sin_port = htons(m_port);
|
|
|
|
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_address.empty()) {
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
|
|
|
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (addr.sin_addr.s_addr == INADDR_NONE) {
|
|
|
|
|
|
|
|
LogError("The local address is invalid - %s", m_address.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int reuse = 1;
|
|
|
|
int reuse = 1;
|
|
|
|
if (::setsockopt(m_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)
|
|
|
@ -143,7 +167,7 @@ bool CUDPSocket::open()
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
|
|
|
|
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#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
|
|
|
@ -158,7 +182,7 @@ bool CUDPSocket::open()
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
|
|
|
|
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(length > 0U);
|
|
|
|
assert(length > 0U);
|
|
|
@ -190,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
|
|
|
|
if (ret == 0)
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
int size = sizeof(sockaddr_in);
|
|
|
|
int size = sizeof(sockaddr_storage);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
socklen_t size = sizeof(sockaddr_in);
|
|
|
|
socklen_t size = sizeof(sockaddr_storage);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
|
|
|
|
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
|
|
|
|
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (len <= 0) {
|
|
|
|
if (len <= 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
@ -211,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
address = addr.sin_addr;
|
|
|
|
address_length = size;
|
|
|
|
port = ntohs(addr.sin_port);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
|
|
|
|
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(length > 0U);
|
|
|
|
assert(length > 0U);
|
|
|
|
|
|
|
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
|
|
|
::memset(&addr, 0x00, sizeof(sockaddr_in));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
addr.sin_addr = address;
|
|
|
|
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
|
|
|
|
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
|
|
|
|
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret < 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|