Skip to content

Commit 8945384

Browse files
committed
net: Have LookupNumeric return a CService directly
Also fix up a few small issues: - Lookup with "badip:port" now sets the port to 0 - Don't allow assert to have side-effects
1 parent 21ba407 commit 8945384

File tree

8 files changed

+18
-21
lines changed

8 files changed

+18
-21
lines changed

src/httpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ CService HTTPRequest::GetPeer()
619619
const char* address = "";
620620
uint16_t port = 0;
621621
evhttp_connection_get_peer(con, (char**)&address, &port);
622-
LookupNumeric(address, peer, port);
622+
peer = LookupNumeric(address, port);
623623
}
624624
return peer;
625625
}

src/init.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
10981098
std::string proxyArg = GetArg("-proxy", "");
10991099
SetLimited(NET_TOR);
11001100
if (proxyArg != "" && proxyArg != "0") {
1101-
CService resolved;
1102-
LookupNumeric(proxyArg.c_str(), resolved, 9050);
1101+
CService resolved(LookupNumeric(proxyArg.c_str(), 9050));
11031102
proxyType addrProxy = proxyType(resolved, proxyRandomize);
11041103
if (!addrProxy.IsValid())
11051104
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg));
@@ -1119,8 +1118,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11191118
if (onionArg == "0") { // Handle -noonion/-onion=0
11201119
SetLimited(NET_TOR); // set onions as unreachable
11211120
} else {
1122-
CService resolved;
1123-
LookupNumeric(onionArg.c_str(), resolved, 9050);
1121+
CService resolved(LookupNumeric(onionArg.c_str(), 9050));
11241122
proxyType addrOnion = proxyType(resolved, proxyRandomize);
11251123
if (!addrOnion.IsValid())
11261124
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));

src/net.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,8 +1728,7 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
17281728
}
17291729

17301730
BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
1731-
CService service;
1732-
LookupNumeric(strAddNode.c_str(), service, Params().GetDefaultPort());
1731+
CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
17331732
if (service.IsValid()) {
17341733
// strAddNode is an IP:port
17351734
auto it = mapConnected.find(service);
@@ -1767,8 +1766,7 @@ void ThreadOpenAddedConnections()
17671766
CSemaphoreGrant grant(*semOutbound);
17681767
// If strAddedNode is an IP/port, decode it immediately, so
17691768
// OpenNetworkConnection can detect existing connections to that IP/port.
1770-
CService service;
1771-
LookupNumeric(info.strAddedNode.c_str(), service, Params().GetDefaultPort());
1769+
CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
17721770
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
17731771
MilliSleep(500);
17741772
}

src/netbase.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,14 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo
231231
return true;
232232
}
233233

234-
bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
234+
CService LookupNumeric(const char *pszName, int portDefault)
235235
{
236-
return Lookup(pszName, addr, portDefault, false);
236+
CService addr;
237+
// "1.2:345" will fail to resolve the ip, but will still set the port.
238+
// If the ip fails to resolve, re-init the result.
239+
if(!Lookup(pszName, addr, portDefault, false))
240+
addr = CService();
241+
return addr;
237242
}
238243

239244
struct timeval MillisToTimeval(int64_t nTimeout)

src/netbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nM
4949
bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
5050
bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
5151
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
52-
bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
52+
CService LookupNumeric(const char *pszName, int portDefault = 0);
5353
bool LookupSubNet(const char *pszName, CSubNet& subnet);
5454
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
5555
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);

src/qt/optionsdialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons
327327
{
328328
Q_UNUSED(pos);
329329
// Validate the proxy
330-
CService serv;
331-
LookupNumeric(input.toStdString().c_str(), serv, 9050);
330+
CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
332331
proxyType addrProxy = proxyType(serv, true);
333332
if (addrProxy.IsValid())
334333
return QValidator::Acceptable;

src/test/netbase_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ BOOST_AUTO_TEST_CASE(netbase_splithost)
9393

9494
bool static TestParse(string src, string canon)
9595
{
96-
CService addr;
97-
if (!LookupNumeric(src.c_str(), addr, 65535))
98-
return canon == "";
96+
CService addr(LookupNumeric(src.c_str(), 65535));
9997
return canon == addr.ToString();
10098
}
10199

@@ -107,7 +105,7 @@ BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
107105
BOOST_CHECK(TestParse("::", "[::]:65535"));
108106
BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
109107
BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
110-
BOOST_CHECK(TestParse(":::", ""));
108+
BOOST_CHECK(TestParse(":::", "[::]:0"));
111109
}
112110

113111
BOOST_AUTO_TEST_CASE(onioncat_test)

src/torcontrol.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep
438438
if ((i = m.find("PrivateKey")) != m.end())
439439
private_key = i->second;
440440
}
441-
LookupNumeric(std::string(service_id+".onion").c_str(), service, GetListenPort());
441+
service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort());
442442
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
443443
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
444444
LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile());
@@ -462,8 +462,7 @@ void TorController::auth_cb(TorControlConnection& conn, const TorControlReply& r
462462
// Now that we know Tor is running setup the proxy for onion addresses
463463
// if -onion isn't set to something else.
464464
if (GetArg("-onion", "") == "") {
465-
CService resolved;
466-
assert(LookupNumeric("127.0.0.1", resolved, 9050));
465+
CService resolved(LookupNumeric("127.0.0.1", 9050));
467466
proxyType addrOnion = proxyType(resolved, true);
468467
SetProxy(NET_TOR, addrOnion);
469468
SetLimited(NET_TOR, false);

0 commit comments

Comments
 (0)