Skip to content

Commit 2280057

Browse files
jellefoksJimmy-ge
andauthored
starboard: Cherry pick PR #8053: Fix DNS lookups for IPv6 addresses. (#8367)
Refer to the original PR: #8053 The Starboard layer wrapper for getaddrinfo() made incorrect copies of the sockaddr structure for IPv6 addresses. This issue prevented proper resolution and use of IPv6 addresses for DNS lookups. This change modifies the wrapper to ensure that the entire sockaddr structure, including the address data, is correctly copied, thus resolving the IPv6 address lookup failures. Additionally, extensive tests have been added and updated to thoroughly verify both IPv4 and IPv6 address resolution for known external hosts and localhost. Bug: 460175900 Bug: 438858995 Bug: 409716671 Bug: 460594994 Bug: 439002238 Bug: 405988446 Co-authored-by: jimmy.ge <[email protected]>
1 parent 77af2bd commit 2280057

2 files changed

Lines changed: 90 additions & 43 deletions

File tree

‎starboard/nplb/posix_compliance/posix_socket_resolve_test.cc‎

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,50 @@
1717
#include <sys/socket.h>
1818
#include <sys/types.h>
1919

20+
#include <cstdint>
2021
#include <tuple>
2122
#include <utility>
2223

24+
#include "starboard/common/log.h"
2325
#include "starboard/configuration.h"
2426
#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
2527
#include "testing/gmock/include/gmock/gmock.h"
2628

2729
namespace nplb {
2830
namespace {
2931

32+
// IPv4 Address 8.8.8.8
33+
34+
const in_addr_t kExpectedIpv4Addr1 =
35+
htonl((8 << 24) | (8 << 16) | (8 << 8) | 8);
36+
// IPv4 Address 8.8.4.4
37+
const in_addr_t kExpectedIpv4Addr2 =
38+
htonl((8 << 24) | (8 << 16) | (4 << 8) | 4);
39+
// IPv6 Address 2001:4860:4860::8888
40+
const unsigned char kExpectedIpv6Addr1[16] = {
41+
0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x88};
42+
// IPv6 Address 2001:4860:4860::8844
43+
const unsigned char kExpectedIpv6Addr2[16] = {
44+
0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x44};
45+
46+
void VerifyGoogleDnsAddress(const struct addrinfo* entry) {
47+
ASSERT_NE(nullptr, entry);
48+
if (entry->ai_family == AF_INET) {
49+
struct sockaddr_in* address =
50+
reinterpret_cast<struct sockaddr_in*>(entry->ai_addr);
51+
EXPECT_THAT(address->sin_addr.s_addr,
52+
testing::AnyOf(kExpectedIpv4Addr1, kExpectedIpv4Addr2));
53+
} else if (entry->ai_family == AF_INET6) {
54+
struct sockaddr_in6* address =
55+
reinterpret_cast<struct sockaddr_in6*>(entry->ai_addr);
56+
EXPECT_THAT(address->sin6_addr.s6_addr,
57+
testing::AnyOf(testing::ElementsAreArray(kExpectedIpv6Addr1),
58+
testing::ElementsAreArray(kExpectedIpv6Addr2)));
59+
} else {
60+
FAIL() << "Expected only IPv4 or IPv6 addresses";
61+
}
62+
}
63+
3064
class PosixSocketResolveTest
3165
: public ::testing::TestWithParam<std::tuple<int, std::pair<int, int>>> {
3266
public:
@@ -45,40 +79,48 @@ class PosixSocketResolveTest
4579
};
4680

4781
// A random host name to use to test DNS resolution.
48-
const char kTestHostName[] = "www.example.com";
82+
const char kTestHostName[] = "dns.google";
4983
const char kTestService[] = "443";
5084
const char kLocalhost[] = "localhost";
5185

52-
TEST_F(PosixSocketResolveTest, SunnyDay) {
86+
TEST_P(PosixSocketResolveTest, SunnyDayHints) {
87+
// Perform a DNS lookup for a host with known IPv4 and IPv6 addresses, and
88+
// verify the returned address.
5389
struct addrinfo hints = {0};
90+
hints.ai_family = GetAddressFamily();
91+
hints.ai_socktype = GetSocketType();
92+
hints.ai_protocol = GetProtocol();
5493
struct addrinfo* ai = nullptr;
5594

5695
int result = getaddrinfo(kTestHostName, 0, &hints, &ai);
96+
// When requesting a specific address familiy, some platforms will return
97+
// an error if the device does not have an interface for the given address
98+
// family.
99+
if (result != 0 && hints.ai_family != AF_UNSPEC) {
100+
GTEST_SKIP() << "Skipping test for unsupported address family";
101+
}
57102
EXPECT_EQ(result, 0);
58103
ASSERT_NE(nullptr, ai);
59104

60-
int address_count = 0;
61-
struct sockaddr_in* ai_addr = nullptr;
62-
63-
for (const struct addrinfo* i = ai; i != nullptr; i = i->ai_next) {
64-
++address_count;
65-
if (ai_addr == nullptr && i->ai_addr != nullptr) {
66-
ai_addr = reinterpret_cast<sockaddr_in*>(i->ai_addr);
67-
if (i->ai_family == AF_INET) {
68-
EXPECT_EQ(static_cast<size_t>(i->ai_addrlen),
69-
sizeof(struct sockaddr_in));
70-
} else if (i->ai_family == AF_INET6) {
71-
EXPECT_EQ(static_cast<size_t>(i->ai_addrlen),
72-
sizeof(struct sockaddr_in6));
73-
}
74-
break;
105+
int address_count_ipv4 = 0;
106+
int address_count_ipv6 = 0;
107+
108+
for (const struct addrinfo* entry = ai; entry != nullptr;
109+
entry = entry->ai_next) {
110+
EXPECT_NE(nullptr, entry->ai_addr);
111+
VerifyGoogleDnsAddress(entry);
112+
if (entry->ai_family == AF_INET) {
113+
++address_count_ipv4;
114+
} else if (entry->ai_family == AF_INET6) {
115+
++address_count_ipv6;
75116
}
76117
}
77-
EXPECT_LT(0, address_count);
78-
EXPECT_NE(nullptr, ai_addr);
79-
80-
EXPECT_TRUE(ai_addr->sin_family == AF_INET ||
81-
ai_addr->sin_family == AF_INET6);
118+
// IPv4 addresses are either filtered, or at least two are returned.
119+
EXPECT_THAT(address_count_ipv4, testing::AnyOf(0, testing::Ge(2)));
120+
// IPv6 addresses are either filtered, or at least two returned.
121+
EXPECT_THAT(address_count_ipv6, testing::AnyOf(0, testing::Ge(2)));
122+
// We expect at least two IP addresses for the test host.
123+
EXPECT_LT(1, address_count_ipv4 + address_count_ipv6);
82124

83125
freeaddrinfo(ai);
84126
}
@@ -119,19 +161,21 @@ TEST_P(PosixSocketResolveTest, SunnyDayFiltered) {
119161
}
120162
}
121163

122-
for (const struct addrinfo* i = ai; i != nullptr; i = i->ai_next) {
164+
for (const struct addrinfo* entry = ai; entry != nullptr;
165+
entry = entry->ai_next) {
123166
if (GetAddressFamily() != AF_UNSPEC) {
124-
EXPECT_EQ(i->ai_addr->sa_family, GetAddressFamily());
167+
EXPECT_EQ(entry->ai_addr->sa_family, GetAddressFamily());
125168
} else {
126-
EXPECT_TRUE(i->ai_addr->sa_family == AF_INET ||
127-
i->ai_addr->sa_family == AF_INET6);
169+
EXPECT_TRUE(entry->ai_addr->sa_family == AF_INET ||
170+
entry->ai_addr->sa_family == AF_INET6);
128171
}
129172
if (GetSocketType() != 0) {
130-
EXPECT_EQ(i->ai_socktype, GetSocketType());
173+
EXPECT_EQ(entry->ai_socktype, GetSocketType());
131174
}
132175
if (GetProtocol() != 0) {
133-
EXPECT_EQ(i->ai_protocol, GetProtocol());
176+
EXPECT_EQ(entry->ai_protocol, GetProtocol());
134177
}
178+
VerifyGoogleDnsAddress(entry);
135179
}
136180

137181
freeaddrinfo(ai);
@@ -218,6 +262,10 @@ TEST_P(PosixSocketResolveTest, Localhost) {
218262
struct addrinfo* ai = nullptr;
219263

220264
int result = getaddrinfo(kLocalhost, 0, &hints, &ai);
265+
if (result == -1) {
266+
SB_LOG(INFO) << "getaddrinfo() failed with errno=" << errno << " "
267+
<< strerror(errno) << " and result=" << result;
268+
}
221269
EXPECT_EQ(result, 0);
222270
ASSERT_NE(nullptr, ai);
223271

@@ -268,14 +316,7 @@ TEST_P(PosixSocketResolveTest, RainyDayNullHostname) {
268316
hints.ai_flags = AI_ADDRCONFIG;
269317
struct addrinfo* ai = nullptr;
270318

271-
int result = getaddrinfo(nullptr, nullptr, &hints, &ai);
272-
273-
EXPECT_FALSE(result == 0);
274-
275-
// Since we cannot guarantee the contents of what the string is,
276-
// (the strings are implementation-defined), we can only expect
277-
// that the pointer is not null.
278-
EXPECT_NE(nullptr, gai_strerror(result));
319+
EXPECT_FALSE(getaddrinfo(nullptr, nullptr, &hints, &ai) == 0);
279320
}
280321

281322
#if SB_HAS(IPV6)

‎starboard/shared/modular/starboard_layer_posix_socket_abi_wrappers.cc‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
#include <stdlib.h>
1818
#include <string.h>
1919

20+
#include <algorithm>
2021
#include <iterator>
2122
#include <memory>
2223

2324
#include "starboard/common/log.h"
2425

2526
namespace {
2627

28+
// Ensure that |struct musl_sockaddr| is large enough to hold either
29+
// |struct sockaddr_in| or |struct sockaddr_in6| on this platform.
30+
static_assert(sizeof(struct musl_sockaddr) >= sizeof(struct sockaddr_in));
31+
static_assert(sizeof(struct musl_sockaddr) >= sizeof(struct sockaddr_in6));
32+
2733
struct platform_iov_deleter {
2834
void operator()(struct iovec* ptr) const { free(ptr); }
2935
};
@@ -329,18 +335,18 @@ SB_EXPORT int __abi_wrap_getaddrinfo(const char* node,
329335
free(musl_ai);
330336
return -1;
331337
}
332-
musl_ai->ai_addrlen = ai_copy.ai_addrlen;
333-
musl_ai->ai_addr =
334-
(struct musl_sockaddr*)calloc(1, sizeof(struct musl_sockaddr));
338+
musl_ai->ai_addrlen =
339+
std::min(static_cast<uint32_t>(ai_copy.ai_addrlen),
340+
static_cast<uint32_t>(sizeof(struct musl_sockaddr)));
341+
musl_ai->ai_addr = (struct musl_sockaddr*)calloc(1, musl_ai->ai_addrlen);
342+
memcpy(musl_ai->ai_addr, ai_copy.ai_addr, musl_ai->ai_addrlen);
343+
// Ensure that the sa_family value is translated if the platform value
344+
// differs from the musl value.
335345
for (int i = 0; i < sizeof(PLATFORM_AF_ORDERED) / sizeof(int); i++) {
336346
if (ai_copy.ai_addr->sa_family == PLATFORM_AF_ORDERED[i]) {
337347
musl_ai->ai_addr->sa_family = MUSL_AF_ORDERED[i];
338348
}
339349
}
340-
if (ai_copy.ai_addr->sa_data != nullptr) {
341-
memcpy(musl_ai->ai_addr->sa_data, ai_copy.ai_addr->sa_data,
342-
sizeof(ai_copy.ai_addr->sa_data));
343-
}
344350
if (ai_copy.ai_canonname) {
345351
size_t canonname_len = strlen(ai_copy.ai_canonname);
346352
musl_ai->ai_canonname =

0 commit comments

Comments
 (0)