Extend capabilities of Address class

This commit is contained in:
sfan5 2024-01-01 15:35:46 +01:00
parent 171f911237
commit dc7fb26921
4 changed files with 111 additions and 49 deletions

@ -1621,7 +1621,8 @@ bool Game::connectToServer(const GameStartData &start_data,
try {
connect_address.Resolve(start_data.address.c_str());
if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
if (connect_address.isAny()) {
// replace with localhost IP
if (connect_address.isIPv6()) {
IPv6AddressBytes addr_bytes;
addr_bytes.bytes[15] = 1;

@ -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 &copy_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;
}
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;
};

@ -32,14 +32,36 @@ public:
void runTests(IGameDef *gamedef);
void testBasic();
void testIsLocalhost();
void testResolve();
};
static TestAddress g_test_instance;
void TestAddress::runTests(IGameDef *gamedef)
{
TEST(testBasic);
TEST(testIsLocalhost);
TEST(testResolve);
}
void TestAddress::testBasic()
{
Address tmp;
UASSERT(!tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), 0);
tmp = Address(static_cast<u32>(0), 0);
UASSERT(tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), AF_INET);
UASSERT(tmp.isAny());
tmp = Address(static_cast<IPv6AddressBytes*>(nullptr), 0);
UASSERT(tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), AF_INET6);
UASSERT(tmp.isAny());
}
void TestAddress::testIsLocalhost()
@ -65,3 +87,33 @@ void TestAddress::testIsLocalhost()
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost())
}
void TestAddress::testResolve()
{
// Empty test
{
Address tmp(1, 2, 3, 4, 5);
tmp.Resolve("");
UASSERT(tmp.isValid());
UASSERT(tmp.isAny());
}
// Localhost test
Address result, fallback;
result.Resolve("localhost", &fallback);
UASSERT(result.isValid());
UASSERT(result.isLocalhost());
if (fallback.isValid()) {
UASSERT(fallback.isLocalhost());
// the result should be ::1 and 127.0.0.1 so the fallback addr should be
// of a different family
UASSERTCMP(int, !=, result.getFamily(), fallback.getFamily());
} else if (g_settings->getBool("enable_ipv6")) {
warningstream << "Couldn't verify Address::Resolve fallback (no IPv6?)"
<< std::endl;
}
}