Skip to content

Commit 5c1774a

Browse files
committed
p2p, refactor: return std::vector<CNetAddr> in LookupIntern
1 parent 8b59231 commit 5c1774a

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/netbase.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,9 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable)
132132
return names;
133133
}
134134

135-
static bool LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
135+
static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
136136
{
137-
vIP.clear();
138-
139-
if (!ContainsNoNUL(name)) {
140-
return false;
141-
}
142-
137+
if (!ContainsNoNUL(name)) return {};
143138
{
144139
CNetAddr addr;
145140
// 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
148143
// getaddrinfo to decode them and it wouldn't make sense to resolve
149144
// them, we return a network address representing it instead. See
150145
// CNetAddr::SetSpecial(const std::string&) for more details.
151-
if (addr.SetSpecial(name)) {
152-
vIP.push_back(addr);
153-
return true;
154-
}
146+
if (addr.SetSpecial(name)) return {addr};
155147
}
156148

149+
std::vector<CNetAddr> addresses;
150+
157151
for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
158-
if (nMaxSolutions > 0 && vIP.size() >= nMaxSolutions) {
152+
if (nMaxSolutions > 0 && addresses.size() >= nMaxSolutions) {
159153
break;
160154
}
161155
/* Never allow resolving to an internal address. Consider any such result invalid */
162156
if (!resolved.IsInternal()) {
163-
vIP.push_back(resolved);
157+
addresses.push_back(resolved);
164158
}
165159
}
166160

167-
return (vIP.size() > 0);
161+
return addresses;
168162
}
169163

170-
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
164+
bool LookupHost(const std::string& name, std::vector<CNetAddr>& addresses, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
171165
{
172166
if (!ContainsNoNUL(name)) {
173167
return false;
@@ -179,7 +173,8 @@ bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned in
179173
strHost = strHost.substr(1, strHost.size() - 2);
180174
}
181175

182-
return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
176+
addresses = LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
177+
return addresses.size() > 0;
183178
}
184179

185180
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
204199
std::string hostname;
205200
SplitHostPort(name, port, hostname);
206201

207-
std::vector<CNetAddr> vIP;
208-
bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
209-
if (!fRet)
202+
const std::vector<CNetAddr> addresses{LookupIntern(hostname, nMaxSolutions, fAllowLookup, dns_lookup_function)};
203+
if (addresses.empty())
210204
return false;
211-
vAddr.resize(vIP.size());
212-
for (unsigned int i = 0; i < vIP.size(); i++)
213-
vAddr[i] = CService(vIP[i], port);
205+
vAddr.resize(addresses.size());
206+
for (unsigned int i = 0; i < addresses.size(); i++)
207+
vAddr[i] = CService(addresses[i], port);
214208
return true;
215209
}
216210

0 commit comments

Comments
 (0)