0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -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; 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 {};
if (!ContainsNoNUL(name)) {
return false;
}
{ {
CNetAddr addr; CNetAddr addr;
// From our perspective, onion addresses are not hostnames but rather // 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 // getaddrinfo to decode them and it wouldn't make sense to resolve
// them, we return a network address representing it instead. See // them, we return a network address representing it instead. See
// CNetAddr::SetSpecial(const std::string&) for more details. // CNetAddr::SetSpecial(const std::string&) for more details.
if (addr.SetSpecial(name)) { if (addr.SetSpecial(name)) return {addr};
vIP.push_back(addr);
return true;
}
} }
std::vector<CNetAddr> addresses;
for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) { for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
if (nMaxSolutions > 0 && vIP.size() >= nMaxSolutions) { if (nMaxSolutions > 0 && addresses.size() >= nMaxSolutions) {
break; break;
} }
/* Never allow resolving to an internal address. Consider any such result invalid */ /* Never allow resolving to an internal address. Consider any such result invalid */
if (!resolved.IsInternal()) { 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)) { if (!ContainsNoNUL(name)) {
return false; 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); 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) 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; std::string hostname;
SplitHostPort(name, port, hostname); SplitHostPort(name, port, hostname);
std::vector<CNetAddr> vIP; const std::vector<CNetAddr> addresses{LookupIntern(hostname, nMaxSolutions, fAllowLookup, dns_lookup_function)};
bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function); if (addresses.empty())
if (!fRet)
return false; return false;
vAddr.resize(vIP.size()); vAddr.resize(addresses.size());
for (unsigned int i = 0; i < vIP.size(); i++) for (unsigned int i = 0; i < addresses.size(); i++)
vAddr[i] = CService(vIP[i], port); vAddr[i] = CService(addresses[i], port);
return true; return true;
} }