Skip to content

Commit 7799eb1

Browse files
committed
p2p, refactor: return std::vector<CNetAddr> in LookupHost
1 parent 5c1774a commit 7799eb1

File tree

4 files changed

+24
-33
lines changed

4 files changed

+24
-33
lines changed

src/net.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,6 @@ void CConnman::ThreadDNSAddressSeed()
14871487
if (HaveNameProxy()) {
14881488
AddAddrFetch(seed);
14891489
} else {
1490-
std::vector<CNetAddr> vIPs;
14911490
std::vector<CAddress> vAdd;
14921491
ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
14931492
std::string host = strprintf("x%x.%s", requiredServiceBits, seed);
@@ -1496,8 +1495,9 @@ void CConnman::ThreadDNSAddressSeed()
14961495
continue;
14971496
}
14981497
unsigned int nMaxIPs = 256; // Limits number of IPs learned from a DNS seed
1499-
if (LookupHost(host, vIPs, nMaxIPs, true)) {
1500-
for (const CNetAddr& ip : vIPs) {
1498+
const auto addresses{LookupHost(host, nMaxIPs, true)};
1499+
if (!addresses.empty()) {
1500+
for (const CNetAddr& ip : addresses) {
15011501
CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
15021502
addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old
15031503
vAdd.push_back(addr);
@@ -2201,14 +2201,11 @@ void Discover()
22012201
char pszHostName[256] = "";
22022202
if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
22032203
{
2204-
std::vector<CNetAddr> vaddr;
2205-
if (LookupHost(pszHostName, vaddr, 0, true))
2204+
const std::vector<CNetAddr> addresses{LookupHost(pszHostName, 0, true)};
2205+
for (const CNetAddr& addr : addresses)
22062206
{
2207-
for (const CNetAddr &addr : vaddr)
2208-
{
2209-
if (AddLocal(addr, LOCAL_IF))
2210-
LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr());
2211-
}
2207+
if (AddLocal(addr, LOCAL_IF))
2208+
LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr());
22122209
}
22132210
}
22142211
#elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)

src/netbase.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,32 +161,27 @@ static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int
161161
return addresses;
162162
}
163163

164-
bool LookupHost(const std::string& name, std::vector<CNetAddr>& addresses, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
164+
std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
165165
{
166-
if (!ContainsNoNUL(name)) {
167-
return false;
168-
}
166+
if (!ContainsNoNUL(name)) return {};
169167
std::string strHost = name;
170-
if (strHost.empty())
171-
return false;
168+
if (strHost.empty()) return {};
172169
if (strHost.front() == '[' && strHost.back() == ']') {
173170
strHost = strHost.substr(1, strHost.size() - 2);
174171
}
175172

176-
addresses = LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
177-
return addresses.size() > 0;
173+
return LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
178174
}
179175

180176
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function)
181177
{
182178
if (!ContainsNoNUL(name)) {
183179
return false;
184180
}
185-
std::vector<CNetAddr> vIP;
186-
LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function);
187-
if(vIP.empty())
181+
const std::vector<CNetAddr> addresses{LookupHost(name, 1, fAllowLookup, dns_lookup_function)};
182+
if(addresses.empty())
188183
return false;
189-
addr = vIP.front();
184+
addr = addresses.front();
190185
return true;
191186
}
192187

src/netbase.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,22 @@ extern DNSLookupFn g_dns_lookup;
105105
* @param name The string representing a host. Could be a name or a numerical
106106
* IP address (IPv6 addresses in their bracketed form are
107107
* allowed).
108-
* @param[out] vIP The resulting network addresses to which the specified host
109-
* string resolved.
110108
*
111-
* @returns Whether or not the specified host string successfully resolved to
112-
* any resulting network addresses.
109+
* @returns The resulting network addresses to which the specified host
110+
* string resolved.
113111
*
114112
* @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
115113
* for additional parameter descriptions.
116114
*/
117-
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
115+
std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
118116

119117
/**
120118
* Resolve a host string to its first corresponding network address.
121119
*
122-
* @see LookupHost(const std::string&, std::vector<CNetAddr>&, uint16_t, bool, DNSLookupFn)
120+
* @returns The resulting network addresses to which the specified host
121+
* string resolved.
122+
*
123+
* @see LookupHost(const std::string&, uint16_t, bool, DNSLookupFn)
123124
* for additional parameter descriptions.
124125
*/
125126
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);

src/test/fuzz/netbase_dns_lookup.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ FUZZ_TARGET(netbase_dns_lookup)
2929
};
3030

3131
{
32-
std::vector<CNetAddr> resolved_addresses;
33-
if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) {
34-
for (const CNetAddr& resolved_address : resolved_addresses) {
35-
assert(!resolved_address.IsInternal());
36-
}
32+
const std::vector<CNetAddr> resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)};
33+
for (const CNetAddr& resolved_address : resolved_addresses) {
34+
assert(!resolved_address.IsInternal());
3735
}
3836
assert(resolved_addresses.size() <= max_results || max_results == 0);
3937
}

0 commit comments

Comments
 (0)