p2p, refactor: return std::vector<CNetAddr> in LookupHost

This commit is contained in:
brunoerg 2022-10-07 11:10:35 -03:00
parent 5c1774a563
commit 7799eb125b
4 changed files with 24 additions and 33 deletions

View file

@ -1487,7 +1487,6 @@ void CConnman::ThreadDNSAddressSeed()
if (HaveNameProxy()) { if (HaveNameProxy()) {
AddAddrFetch(seed); AddAddrFetch(seed);
} else { } else {
std::vector<CNetAddr> vIPs;
std::vector<CAddress> vAdd; std::vector<CAddress> vAdd;
ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE); ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
std::string host = strprintf("x%x.%s", requiredServiceBits, seed); std::string host = strprintf("x%x.%s", requiredServiceBits, seed);
@ -1496,8 +1495,9 @@ void CConnman::ThreadDNSAddressSeed()
continue; continue;
} }
unsigned int nMaxIPs = 256; // Limits number of IPs learned from a DNS seed unsigned int nMaxIPs = 256; // Limits number of IPs learned from a DNS seed
if (LookupHost(host, vIPs, nMaxIPs, true)) { const auto addresses{LookupHost(host, nMaxIPs, true)};
for (const CNetAddr& ip : vIPs) { if (!addresses.empty()) {
for (const CNetAddr& ip : addresses) {
CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits); CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old
vAdd.push_back(addr); vAdd.push_back(addr);
@ -2201,14 +2201,11 @@ void Discover()
char pszHostName[256] = ""; char pszHostName[256] = "";
if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
{ {
std::vector<CNetAddr> vaddr; const std::vector<CNetAddr> addresses{LookupHost(pszHostName, 0, true)};
if (LookupHost(pszHostName, vaddr, 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) #elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)

View file

@ -161,20 +161,16 @@ static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int
return addresses; return addresses;
} }
bool LookupHost(const std::string& name, std::vector<CNetAddr>& addresses, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function) std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{ {
if (!ContainsNoNUL(name)) { if (!ContainsNoNUL(name)) return {};
return false;
}
std::string strHost = name; std::string strHost = name;
if (strHost.empty()) if (strHost.empty()) return {};
return false;
if (strHost.front() == '[' && strHost.back() == ']') { if (strHost.front() == '[' && strHost.back() == ']') {
strHost = strHost.substr(1, strHost.size() - 2); strHost = strHost.substr(1, strHost.size() - 2);
} }
addresses = LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function); return LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
return addresses.size() > 0;
} }
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn 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)) { if (!ContainsNoNUL(name)) {
return false; return false;
} }
std::vector<CNetAddr> vIP; const std::vector<CNetAddr> addresses{LookupHost(name, 1, fAllowLookup, dns_lookup_function)};
LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function); if(addresses.empty())
if(vIP.empty())
return false; return false;
addr = vIP.front(); addr = addresses.front();
return true; return true;
} }

View file

@ -105,21 +105,22 @@ extern DNSLookupFn g_dns_lookup;
* @param name The string representing a host. Could be a name or a numerical * @param name The string representing a host. Could be a name or a numerical
* IP address (IPv6 addresses in their bracketed form are * IP address (IPv6 addresses in their bracketed form are
* allowed). * 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 * @returns The resulting network addresses to which the specified host
* any resulting network addresses. * string resolved.
* *
* @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn) * @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
* for additional parameter descriptions. * for additional parameter descriptions.
*/ */
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup); std::vector<CNetAddr> 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. * Resolve a host string to its first corresponding network address.
* *
* @see LookupHost(const std::string&, std::vector<CNetAddr>&, 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. * for additional parameter descriptions.
*/ */
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup); bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);

View file

@ -29,11 +29,9 @@ FUZZ_TARGET(netbase_dns_lookup)
}; };
{ {
std::vector<CNetAddr> resolved_addresses; const std::vector<CNetAddr> resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)};
if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) { for (const CNetAddr& resolved_address : resolved_addresses) {
for (const CNetAddr& resolved_address : resolved_addresses) { assert(!resolved_address.IsInternal());
assert(!resolved_address.IsInternal());
}
} }
assert(resolved_addresses.size() <= max_results || max_results == 0); assert(resolved_addresses.size() <= max_results || max_results == 0);
} }