aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbrunoerg <brunoely.gc@gmail.com>2022-10-07 11:10:35 -0300
committerbrunoerg <brunoely.gc@gmail.com>2023-05-26 13:38:22 -0300
commit7799eb125b7a1146f8251be5d26df574236212a9 (patch)
tree7ef87f84c198649e2e337f5cce00fc67bd84bd7b /src
parent5c1774a563dcc237a88df69569cd94fe81e908f7 (diff)
downloadbitcoin-7799eb125b7a1146f8251be5d26df574236212a9.tar.xz
p2p, refactor: return `std::vector<CNetAddr>` in `LookupHost`
Diffstat (limited to 'src')
-rw-r--r--src/net.cpp17
-rw-r--r--src/netbase.cpp19
-rw-r--r--src/netbase.h13
-rw-r--r--src/test/fuzz/netbase_dns_lookup.cpp8
4 files changed, 24 insertions, 33 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 337cf60680..2eba59b9e5 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1487,7 +1487,6 @@ void CConnman::ThreadDNSAddressSeed()
if (HaveNameProxy()) {
AddAddrFetch(seed);
} else {
- std::vector<CNetAddr> vIPs;
std::vector<CAddress> 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<NodeSeconds>() - 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<CNetAddr> vaddr;
- if (LookupHost(pszHostName, vaddr, 0, true))
+ const std::vector<CNetAddr> 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 2c69f2f16d..8513974af6 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -161,20 +161,16 @@ static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int
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)) {
- 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<CNetAddr> vIP;
- LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function);
- if(vIP.empty())
+ const std::vector<CNetAddr> 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 a43f22f240..dde5e5b43c 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<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
* 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.
*
- * @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.
*/
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 81e216b358..a47c09b683 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<CNetAddr> 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<CNetAddr> 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);
}