Skip to content

Commit 23333b7

Browse files
committed
net: Allow DNS lookups on nodes with IPV6 lo only
AI_ADDRCONFIG prevents ::1 from being considered a valid address on hosts that have a IPV6 loopback IP address but no other IPV6 interfaces.
1 parent 4a020ca commit 23333b7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/netbase.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_loo
4848
ai_hint.ai_protocol = IPPROTO_TCP;
4949
// We don't care which address family (IPv4 or IPv6) is returned
5050
ai_hint.ai_family = AF_UNSPEC;
51+
5152
// If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
5253
// return addresses whose family we have an address configured for.
5354
//
@@ -59,7 +60,17 @@ std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_loo
5960
addrinfo* ai_res{nullptr};
6061
const int n_err{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)};
6162
if (n_err != 0) {
62-
return {};
63+
if ((ai_hint.ai_flags & AI_ADDRCONFIG) == AI_ADDRCONFIG) {
64+
// AI_ADDRCONFIG on some systems may exclude loopback-only addresses
65+
// If first lookup failed we perform a second lookup without AI_ADDRCONFIG
66+
ai_hint.ai_flags = (ai_hint.ai_flags & ~AI_ADDRCONFIG);
67+
const int n_err_retry{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)};
68+
if (n_err_retry != 0) {
69+
return {};
70+
}
71+
} else {
72+
return {};
73+
}
6374
}
6475

6576
// Traverse the linked list starting with ai_trav.

0 commit comments

Comments
 (0)