|
|
@ -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,86 +62,101 @@ 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;
|
|
|
|
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
freeaddrinfo(res);
|
|
|
|
in_addr_t address = ::inet_addr(hostname.c_str());
|
|
|
|
|
|
|
|
if (address != in_addr_t(-1)) {
|
|
|
|
return 0;
|
|
|
|
addr.s_addr = address;
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct hostent* hp = ::gethostbyname(hostname.c_str());
|
|
|
|
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
|
|
|
|
if (hp != NULL) {
|
|
|
|
{
|
|
|
|
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
|
|
|
|
if (addr1.ss_family != addr2.ss_family)
|
|
|
|
return addr;
|
|
|
|
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(const std::string& bindaddr)
|
|
|
|
bool CUDPSocket::open()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
return open(AF_UNSPEC);
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_port > 0U) {
|
|
|
|
bool CUDPSocket::open(const unsigned int af)
|
|
|
|
sockaddr_in addr;
|
|
|
|
{
|
|
|
|
::memset(&addr, 0x00, sizeof(sockaddr_in));
|
|
|
|
sockaddr_storage addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
unsigned int addrlen;
|
|
|
|
addr.sin_port = htons(m_port);
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
|
|
|
|
if ( bindaddr.length() > 0){
|
|
|
|
::memset(&hints, 0, sizeof(hints));
|
|
|
|
int validaddr = inet_pton(AF_INET, bindaddr.c_str(), &(addr.sin_addr));
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
if (validaddr != 1){
|
|
|
|
hints.ai_family = af;
|
|
|
|
LogError("The BindAddress in the .ini is invalid - %s", bindaddr.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_address.empty()) {
|
|
|
|
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if (m_fd < 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
|
|
|
|
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
|
|
|
|
LogError("Cannot create the UDP socket, err: %d", errno);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (addr.sin_addr.s_addr == INADDR_NONE) {
|
|
|
|
|
|
|
|
LogError("The local address is invalid - %s", m_address.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_port > 0U) {
|
|
|
|
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)
|
|
|
@ -152,7 +167,7 @@ bool CUDPSocket::open(const std::string& bindaddr)
|
|
|
|
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
|
|
|
@ -160,12 +175,14 @@ bool CUDPSocket::open(const std::string& bindaddr)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogInfo("Opening UDP port on %u", m_port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
@ -197,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)
|
|
|
@ -218,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)
|
|
|
|