mirror of
https://github.com/g4klx/DMRGateway
synced 2025-12-20 21:25:37 +08:00
sync to MMDVMHost-ipv6 code
This commit is contained in:
@@ -63,15 +63,24 @@ CUDPSocket::~CUDPSocket()
|
||||
}
|
||||
|
||||
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
|
||||
::memset(&hints, 0, sizeof(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 err;
|
||||
std::string portstr = std::to_string(port);
|
||||
struct addrinfo hints, *res;
|
||||
struct addrinfo *res;
|
||||
|
||||
::memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_flags = AI_NUMERICSERV;
|
||||
/* port is always digits, no needs to lookup service */
|
||||
hints.ai_flags |= AI_NUMERICSERV;
|
||||
|
||||
err = getaddrinfo(hostname.c_str(), portstr.c_str(), &hints, &res);
|
||||
err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
|
||||
if (err) {
|
||||
sockaddr_in *paddr = (sockaddr_in *)&addr;
|
||||
::memset(paddr, 0, address_length = sizeof(sockaddr_in));
|
||||
@@ -88,14 +97,55 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool CUDPSocket::isnone(const sockaddr_storage &addr)
|
||||
{
|
||||
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
||||
|
||||
return ( (addr.ss_family == AF_INET) &&
|
||||
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
|
||||
}
|
||||
|
||||
bool CUDPSocket::open()
|
||||
{
|
||||
return open(AF_UNSPEC);
|
||||
}
|
||||
|
||||
bool CUDPSocket::open(const unsigned int af)
|
||||
{
|
||||
int err;
|
||||
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. */
|
||||
err = lookup(m_address.empty() ? "0.0.0.0" : m_address.c_str(), m_port, addr, addrlen);
|
||||
err = lookup(m_address, m_port, addr, addrlen, hints);
|
||||
if (err) {
|
||||
LogError("The local address is invalid - %s", m_address.c_str());
|
||||
return false;
|
||||
|
||||
@@ -42,6 +42,7 @@ public:
|
||||
~CUDPSocket();
|
||||
|
||||
bool open();
|
||||
bool open(const unsigned int af);
|
||||
|
||||
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);
|
||||
@@ -49,6 +50,9 @@ public:
|
||||
void close();
|
||||
|
||||
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 bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2);
|
||||
static bool isnone(const sockaddr_storage &addr);
|
||||
|
||||
private:
|
||||
std::string m_address;
|
||||
|
||||
Reference in New Issue
Block a user