diff --git a/src/netbase.cpp b/src/netbase.cpp index f5f0997ba65..473ada7f09c 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -50,6 +50,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. // @@ -61,7 +62,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.