From 7799eb125b7a1146f8251be5d26df574236212a9 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Fri, 7 Oct 2022 11:10:35 -0300 Subject: [PATCH] p2p, refactor: return `std::vector` in `LookupHost` --- src/net.cpp | 17 +++++++---------- src/netbase.cpp | 19 +++++++------------ src/netbase.h | 13 +++++++------ src/test/fuzz/netbase_dns_lookup.cpp | 8 +++----- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 337cf60680d..2eba59b9e5e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1487,7 +1487,6 @@ void CConnman::ThreadDNSAddressSeed() if (HaveNameProxy()) { AddAddrFetch(seed); } else { - std::vector vIPs; std::vector vAdd; ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE); std::string host = strprintf("x%x.%s", requiredServiceBits, seed); @@ -1496,8 +1495,9 @@ void CConnman::ThreadDNSAddressSeed() continue; } unsigned int nMaxIPs = 256; // Limits number of IPs learned from a DNS seed - if (LookupHost(host, vIPs, nMaxIPs, true)) { - for (const CNetAddr& ip : vIPs) { + const auto addresses{LookupHost(host, nMaxIPs, true)}; + if (!addresses.empty()) { + for (const CNetAddr& ip : addresses) { CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits); addr.nTime = rng.rand_uniform_delay(Now() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old vAdd.push_back(addr); @@ -2201,14 +2201,11 @@ void Discover() char pszHostName[256] = ""; if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) { - std::vector vaddr; - if (LookupHost(pszHostName, vaddr, 0, true)) + const std::vector addresses{LookupHost(pszHostName, 0, true)}; + for (const CNetAddr& addr : addresses) { - for (const CNetAddr &addr : vaddr) - { - if (AddLocal(addr, LOCAL_IF)) - LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr()); - } + if (AddLocal(addr, LOCAL_IF)) + LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr()); } } #elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS) diff --git a/src/netbase.cpp b/src/netbase.cpp index 2c69f2f16df..8513974af66 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -161,20 +161,16 @@ static std::vector LookupIntern(const std::string& name, unsigned int return addresses; } -bool LookupHost(const std::string& name, std::vector& addresses, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function) +std::vector LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function) { - if (!ContainsNoNUL(name)) { - return false; - } + if (!ContainsNoNUL(name)) return {}; std::string strHost = name; - if (strHost.empty()) - return false; + if (strHost.empty()) return {}; if (strHost.front() == '[' && strHost.back() == ']') { strHost = strHost.substr(1, strHost.size() - 2); } - addresses = LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function); - return addresses.size() > 0; + return LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function); } bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function) @@ -182,11 +178,10 @@ bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSL if (!ContainsNoNUL(name)) { return false; } - std::vector vIP; - LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function); - if(vIP.empty()) + const std::vector addresses{LookupHost(name, 1, fAllowLookup, dns_lookup_function)}; + if(addresses.empty()) return false; - addr = vIP.front(); + addr = addresses.front(); return true; } diff --git a/src/netbase.h b/src/netbase.h index a43f22f2407..dde5e5b43cf 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -105,21 +105,22 @@ extern DNSLookupFn g_dns_lookup; * @param name The string representing a host. Could be a name or a numerical * IP address (IPv6 addresses in their bracketed form are * allowed). - * @param[out] vIP The resulting network addresses to which the specified host - * string resolved. * - * @returns Whether or not the specified host string successfully resolved to - * any resulting network addresses. + * @returns The resulting network addresses to which the specified host + * string resolved. * * @see Lookup(const std::string&, std::vector&, uint16_t, bool, unsigned int, DNSLookupFn) * for additional parameter descriptions. */ -bool LookupHost(const std::string& name, std::vector& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup); +std::vector LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup); /** * Resolve a host string to its first corresponding network address. * - * @see LookupHost(const std::string&, std::vector&, uint16_t, bool, DNSLookupFn) + * @returns The resulting network addresses to which the specified host + * string resolved. + * + * @see LookupHost(const std::string&, uint16_t, bool, DNSLookupFn) * for additional parameter descriptions. */ bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup); diff --git a/src/test/fuzz/netbase_dns_lookup.cpp b/src/test/fuzz/netbase_dns_lookup.cpp index 81e216b358d..a47c09b6836 100644 --- a/src/test/fuzz/netbase_dns_lookup.cpp +++ b/src/test/fuzz/netbase_dns_lookup.cpp @@ -29,11 +29,9 @@ FUZZ_TARGET(netbase_dns_lookup) }; { - std::vector resolved_addresses; - if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) { - for (const CNetAddr& resolved_address : resolved_addresses) { - assert(!resolved_address.IsInternal()); - } + const std::vector resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)}; + for (const CNetAddr& resolved_address : resolved_addresses) { + assert(!resolved_address.IsInternal()); } assert(resolved_addresses.size() <= max_results || max_results == 0); }