diff options
author | Cory Fields <cory-nospam-@coryfields.com> | 2016-08-04 16:37:49 -0400 |
---|---|---|
committer | Cory Fields <cory-nospam-@coryfields.com> | 2016-08-04 16:41:39 -0400 |
commit | 8945384bca00f74ba85c98a52925c254c49025a5 (patch) | |
tree | c5479d77e5feb28ed75b53715073bcf3b46c1ffe /src | |
parent | 21ba407a7369a0229b8a8554dee0da63a64e6639 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/httpserver.cpp | 2 | ||||
-rw-r--r-- | src/init.cpp | 6 | ||||
-rw-r--r-- | src/net.cpp | 6 | ||||
-rw-r--r-- | src/netbase.cpp | 9 | ||||
-rw-r--r-- | src/netbase.h | 2 | ||||
-rw-r--r-- | src/qt/optionsdialog.cpp | 3 | ||||
-rw-r--r-- | src/test/netbase_tests.cpp | 6 | ||||
-rw-r--r-- | src/torcontrol.cpp | 5 |
8 files changed, 18 insertions, 21 deletions
diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 2c5bc2c792..2bb4be564e 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -619,7 +619,7 @@ CService HTTPRequest::GetPeer() const char* address = ""; uint16_t port = 0; evhttp_connection_get_peer(con, (char**)&address, &port); - LookupNumeric(address, peer, port); + peer = LookupNumeric(address, port); } return peer; } diff --git a/src/init.cpp b/src/init.cpp index 4f5eeeffe6..04d7ed0ea3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1098,8 +1098,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::string proxyArg = GetArg("-proxy", ""); SetLimited(NET_TOR); if (proxyArg != "" && proxyArg != "0") { - CService resolved; - LookupNumeric(proxyArg.c_str(), resolved, 9050); + CService resolved(LookupNumeric(proxyArg.c_str(), 9050)); proxyType addrProxy = proxyType(resolved, proxyRandomize); if (!addrProxy.IsValid()) return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg)); @@ -1119,8 +1118,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (onionArg == "0") { // Handle -noonion/-onion=0 SetLimited(NET_TOR); // set onions as unreachable } else { - CService resolved; - LookupNumeric(onionArg.c_str(), resolved, 9050); + CService resolved(LookupNumeric(onionArg.c_str(), 9050)); proxyType addrOnion = proxyType(resolved, proxyRandomize); if (!addrOnion.IsValid()) return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg)); diff --git a/src/net.cpp b/src/net.cpp index e9bb406e97..fc44a0f17c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1728,8 +1728,7 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo() } BOOST_FOREACH(const std::string& strAddNode, lAddresses) { - CService service; - LookupNumeric(strAddNode.c_str(), service, Params().GetDefaultPort()); + CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort())); if (service.IsValid()) { // strAddNode is an IP:port auto it = mapConnected.find(service); @@ -1767,8 +1766,7 @@ void ThreadOpenAddedConnections() CSemaphoreGrant grant(*semOutbound); // If strAddedNode is an IP/port, decode it immediately, so // OpenNetworkConnection can detect existing connections to that IP/port. - CService service; - LookupNumeric(info.strAddedNode.c_str(), service, Params().GetDefaultPort()); + CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort())); OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false); MilliSleep(500); } diff --git a/src/netbase.cpp b/src/netbase.cpp index c2294a078d..4f243ec6f5 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -231,9 +231,14 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo return true; } -bool LookupNumeric(const char *pszName, CService& addr, int portDefault) +CService LookupNumeric(const char *pszName, int portDefault) { - return Lookup(pszName, addr, portDefault, false); + CService addr; + // "1.2:345" will fail to resolve the ip, but will still set the port. + // If the ip fails to resolve, re-init the result. + if(!Lookup(pszName, addr, portDefault, false)) + addr = CService(); + return addr; } struct timeval MillisToTimeval(int64_t nTimeout) diff --git a/src/netbase.h b/src/netbase.h index d0abd5e954..bb12019a82 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -49,7 +49,7 @@ bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nM bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup); bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup); bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions); -bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); +CService LookupNumeric(const char *pszName, int portDefault = 0); bool LookupSubNet(const char *pszName, CSubNet& subnet); bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0); bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0); diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index e419e4d9ee..f73bb87064 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -327,8 +327,7 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons { Q_UNUSED(pos); // Validate the proxy - CService serv; - LookupNumeric(input.toStdString().c_str(), serv, 9050); + CService serv(LookupNumeric(input.toStdString().c_str(), 9050)); proxyType addrProxy = proxyType(serv, true); if (addrProxy.IsValid()) return QValidator::Acceptable; diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp index 7a8584f0f4..18ad5dc90b 100644 --- a/src/test/netbase_tests.cpp +++ b/src/test/netbase_tests.cpp @@ -93,9 +93,7 @@ BOOST_AUTO_TEST_CASE(netbase_splithost) bool static TestParse(string src, string canon) { - CService addr; - if (!LookupNumeric(src.c_str(), addr, 65535)) - return canon == ""; + CService addr(LookupNumeric(src.c_str(), 65535)); return canon == addr.ToString(); } @@ -107,7 +105,7 @@ BOOST_AUTO_TEST_CASE(netbase_lookupnumeric) BOOST_CHECK(TestParse("::", "[::]:65535")); BOOST_CHECK(TestParse("[::]:8333", "[::]:8333")); BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535")); - BOOST_CHECK(TestParse(":::", "")); + BOOST_CHECK(TestParse(":::", "[::]:0")); } BOOST_AUTO_TEST_CASE(onioncat_test) diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index c7f9627005..99c45d489c 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -438,7 +438,7 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep if ((i = m.find("PrivateKey")) != m.end()) private_key = i->second; } - LookupNumeric(std::string(service_id+".onion").c_str(), service, GetListenPort()); + service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString()); if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile()); @@ -462,8 +462,7 @@ void TorController::auth_cb(TorControlConnection& conn, const TorControlReply& r // Now that we know Tor is running setup the proxy for onion addresses // if -onion isn't set to something else. if (GetArg("-onion", "") == "") { - CService resolved; - assert(LookupNumeric("127.0.0.1", resolved, 9050)); + CService resolved(LookupNumeric("127.0.0.1", 9050)); proxyType addrOnion = proxyType(resolved, true); SetProxy(NET_TOR, addrOnion); SetLimited(NET_TOR, false); |