0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

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

This commit is contained in:
brunoerg 2022-10-04 17:42:35 -03:00
parent 8b59231641
commit 5c1774a563

View file

@ -132,14 +132,9 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable)
return names;
}
static bool LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
vIP.clear();
if (!ContainsNoNUL(name)) {
return false;
}
if (!ContainsNoNUL(name)) return {};
{
CNetAddr addr;
// From our perspective, onion addresses are not hostnames but rather
@ -148,26 +143,25 @@ static bool LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, un
// getaddrinfo to decode them and it wouldn't make sense to resolve
// them, we return a network address representing it instead. See
// CNetAddr::SetSpecial(const std::string&) for more details.
if (addr.SetSpecial(name)) {
vIP.push_back(addr);
return true;
}
if (addr.SetSpecial(name)) return {addr};
}
std::vector<CNetAddr> addresses;
for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
if (nMaxSolutions > 0 && vIP.size() >= nMaxSolutions) {
if (nMaxSolutions > 0 && addresses.size() >= nMaxSolutions) {
break;
}
/* Never allow resolving to an internal address. Consider any such result invalid */
if (!resolved.IsInternal()) {
vIP.push_back(resolved);
addresses.push_back(resolved);
}
}
return (vIP.size() > 0);
return addresses;
}
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
bool LookupHost(const std::string& name, std::vector<CNetAddr>& addresses, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
if (!ContainsNoNUL(name)) {
return false;
@ -179,7 +173,8 @@ bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned in
strHost = strHost.substr(1, strHost.size() - 2);
}
return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
addresses = 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)
@ -204,13 +199,12 @@ bool Lookup(const std::string& name, std::vector<CService>& vAddr, uint16_t port
std::string hostname;
SplitHostPort(name, port, hostname);
std::vector<CNetAddr> vIP;
bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
if (!fRet)
const std::vector<CNetAddr> addresses{LookupIntern(hostname, nMaxSolutions, fAllowLookup, dns_lookup_function)};
if (addresses.empty())
return false;
vAddr.resize(vIP.size());
for (unsigned int i = 0; i < vIP.size(); i++)
vAddr[i] = CService(vIP[i], port);
vAddr.resize(addresses.size());
for (unsigned int i = 0; i < addresses.size(); i++)
vAddr[i] = CService(addresses[i], port);
return true;
}