From 23333b7ed243071c9b4e4f04c727556d8065acbb Mon Sep 17 00:00:00 2001 From: Max Edwards Date: Fri, 7 Jun 2024 13:24:04 +0100 Subject: [PATCH] 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. --- src/netbase.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/netbase.cpp b/src/netbase.cpp index e231766487f85..22326b0e985a5 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -48,6 +48,7 @@ std::vector WrappedGetAddrInfo(const std::string& name, bool allow_loo ai_hint.ai_protocol = IPPROTO_TCP; // We don't care which address family (IPv4 or IPv6) is returned ai_hint.ai_family = AF_UNSPEC; + // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only // return addresses whose family we have an address configured for. // @@ -59,7 +60,17 @@ std::vector WrappedGetAddrInfo(const std::string& name, bool allow_loo addrinfo* ai_res{nullptr}; const int n_err{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)}; if (n_err != 0) { - return {}; + if ((ai_hint.ai_flags & AI_ADDRCONFIG) == AI_ADDRCONFIG) { + // AI_ADDRCONFIG on some systems may exclude loopback-only addresses + // If first lookup failed we perform a second lookup without AI_ADDRCONFIG + ai_hint.ai_flags = (ai_hint.ai_flags & ~AI_ADDRCONFIG); + const int n_err_retry{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)}; + if (n_err_retry != 0) { + return {}; + } + } else { + return {}; + } } // Traverse the linked list starting with ai_trav.