mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Extend capabilities of Address class
This commit is contained in:
parent
171f911237
commit
dc7fb26921
4 changed files with 111 additions and 49 deletions
|
@ -84,7 +84,7 @@ Address::Address(const IPv6AddressBytes *ipv6_bytes, u16 port)
|
|||
}
|
||||
|
||||
// Equality (address family, IP and port must be equal)
|
||||
bool Address::operator==(const Address &other)
|
||||
bool Address::operator==(const Address &other) const
|
||||
{
|
||||
if (other.m_addr_family != m_addr_family || other.m_port != m_port)
|
||||
return false;
|
||||
|
@ -101,44 +101,60 @@ bool Address::operator==(const Address &other)
|
|||
return false;
|
||||
}
|
||||
|
||||
void Address::Resolve(const char *name)
|
||||
void Address::Resolve(const char *name, Address *fallback)
|
||||
{
|
||||
if (!name || name[0] == 0) {
|
||||
if (m_addr_family == AF_INET)
|
||||
setAddress(static_cast<u32>(0));
|
||||
else if (m_addr_family == AF_INET6)
|
||||
setAddress(static_cast<IPv6AddressBytes*>(nullptr));
|
||||
if (fallback)
|
||||
*fallback = Address();
|
||||
return;
|
||||
}
|
||||
|
||||
struct addrinfo *resolved, hints;
|
||||
const auto ©_from_ai = [] (const struct addrinfo *ai, Address *to) {
|
||||
if (ai->ai_family == AF_INET) {
|
||||
struct sockaddr_in *t = (struct sockaddr_in *)ai->ai_addr;
|
||||
to->m_addr_family = AF_INET;
|
||||
to->m_address.ipv4 = t->sin_addr;
|
||||
} else if (ai->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *t = (struct sockaddr_in6 *)ai->ai_addr;
|
||||
to->m_addr_family = AF_INET6;
|
||||
to->m_address.ipv6 = t->sin6_addr;
|
||||
} else {
|
||||
to->m_addr_family = 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
|
||||
// Setup hints
|
||||
// set a type, so every unique address is only returned once
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
if (g_settings->getBool("enable_ipv6")) {
|
||||
// AF_UNSPEC allows both IPv6 and IPv4 addresses to be returned
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
} else {
|
||||
hints.ai_family = AF_INET;
|
||||
}
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
|
||||
// Do getaddrinfo()
|
||||
int e = getaddrinfo(name, NULL, &hints, &resolved);
|
||||
struct addrinfo *resolved = nullptr;
|
||||
int e = getaddrinfo(name, nullptr, &hints, &resolved);
|
||||
if (e != 0)
|
||||
throw ResolveError(gai_strerror(e));
|
||||
assert(resolved);
|
||||
|
||||
// Copy data
|
||||
if (resolved->ai_family == AF_INET) {
|
||||
struct sockaddr_in *t = (struct sockaddr_in *)resolved->ai_addr;
|
||||
m_addr_family = AF_INET;
|
||||
m_address.ipv4 = t->sin_addr;
|
||||
} else if (resolved->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *t = (struct sockaddr_in6 *)resolved->ai_addr;
|
||||
m_addr_family = AF_INET6;
|
||||
m_address.ipv6 = t->sin6_addr;
|
||||
} else {
|
||||
m_addr_family = 0;
|
||||
copy_from_ai(resolved, this);
|
||||
if (fallback) {
|
||||
*fallback = Address();
|
||||
if (resolved->ai_next)
|
||||
copy_from_ai(resolved->ai_next, fallback);
|
||||
}
|
||||
|
||||
freeaddrinfo(resolved);
|
||||
}
|
||||
|
||||
|
@ -151,28 +167,11 @@ std::string Address::serializeString() const
|
|||
return str;
|
||||
}
|
||||
|
||||
struct in_addr Address::getAddress() const
|
||||
{
|
||||
return m_address.ipv4;
|
||||
}
|
||||
|
||||
struct in6_addr Address::getAddress6() const
|
||||
{
|
||||
return m_address.ipv6;
|
||||
}
|
||||
|
||||
u16 Address::getPort() const
|
||||
{
|
||||
return m_port;
|
||||
}
|
||||
|
||||
bool Address::isZero() const
|
||||
bool Address::isAny() const
|
||||
{
|
||||
if (m_addr_family == AF_INET) {
|
||||
return m_address.ipv4.s_addr == 0;
|
||||
}
|
||||
|
||||
if (m_addr_family == AF_INET6) {
|
||||
} else if (m_addr_family == AF_INET6) {
|
||||
static const char zero[16] = {0};
|
||||
return memcmp(m_address.ipv6.s6_addr, zero, 16) == 0;
|
||||
}
|
||||
|
@ -218,18 +217,19 @@ void Address::print(std::ostream& s) const
|
|||
|
||||
bool Address::isLocalhost() const
|
||||
{
|
||||
if (isIPv6()) {
|
||||
if (m_addr_family == AF_INET6) {
|
||||
static const u8 localhost_bytes[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
|
||||
static const u8 mapped_ipv4_localhost[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 0};
|
||||
|
||||
auto addr = m_address.ipv6.s6_addr;
|
||||
auto *addr = m_address.ipv6.s6_addr;
|
||||
|
||||
return memcmp(addr, localhost_bytes, 16) == 0 ||
|
||||
memcmp(addr, mapped_ipv4_localhost, 13) == 0;
|
||||
} else if (m_addr_family == AF_INET) {
|
||||
auto addr = ntohl(m_address.ipv4.s_addr);
|
||||
return (addr >> 24) == 0x7f;
|
||||
}
|
||||
|
||||
auto addr = ntohl(m_address.ipv4.s_addr);
|
||||
return (addr >> 24) == 0x7f;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -47,21 +47,29 @@ public:
|
|||
Address(u8 a, u8 b, u8 c, u8 d, u16 port);
|
||||
Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
|
||||
|
||||
bool operator==(const Address &address);
|
||||
bool operator!=(const Address &address) { return !(*this == address); }
|
||||
bool operator==(const Address &address) const;
|
||||
bool operator!=(const Address &address) const { return !(*this == address); }
|
||||
|
||||
struct in_addr getAddress() const;
|
||||
struct in6_addr getAddress6() const;
|
||||
u16 getPort() const;
|
||||
int getFamily() const { return m_addr_family; }
|
||||
bool isValid() const { return m_addr_family != 0; }
|
||||
bool isIPv6() const { return m_addr_family == AF_INET6; }
|
||||
bool isZero() const;
|
||||
struct in_addr getAddress() const { return m_address.ipv4; }
|
||||
struct in6_addr getAddress6() const { return m_address.ipv6; }
|
||||
u16 getPort() const { return m_port; }
|
||||
|
||||
void print(std::ostream &s) const;
|
||||
std::string serializeString() const;
|
||||
|
||||
// Is this an address that binds to all interfaces (like INADDR_ANY)?
|
||||
bool isAny() const;
|
||||
// Is this an address referring to the local host?
|
||||
bool isLocalhost() const;
|
||||
|
||||
// Resolve() may throw ResolveError (address is unchanged in this case)
|
||||
void Resolve(const char *name);
|
||||
// `name`: hostname or numeric IP
|
||||
// `fallback`: fallback IP to try gets written here
|
||||
// any empty name resets the IP to the "any address"
|
||||
// may throw ResolveError (address is unchanged in this case)
|
||||
void Resolve(const char *name, Address *fallback = nullptr);
|
||||
|
||||
void setAddress(u32 address);
|
||||
void setAddress(u8 a, u8 b, u8 c, u8 d);
|
||||
|
@ -75,5 +83,6 @@ private:
|
|||
struct in_addr ipv4;
|
||||
struct in6_addr ipv6;
|
||||
} m_address;
|
||||
u16 m_port = 0; // Port is separate from sockaddr structures
|
||||
// port is separate from in_addr structures
|
||||
u16 m_port = 0;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue