Browse Source

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
0.14
Cory Fields 8 years ago
parent
commit
8945384bca
  1. 2
      src/httpserver.cpp
  2. 6
      src/init.cpp
  3. 6
      src/net.cpp
  4. 9
      src/netbase.cpp
  5. 2
      src/netbase.h
  6. 3
      src/qt/optionsdialog.cpp
  7. 6
      src/test/netbase_tests.cpp
  8. 5
      src/torcontrol.cpp

2
src/httpserver.cpp

@ -619,7 +619,7 @@ CService HTTPRequest::GetPeer()
const char* address = ""; const char* address = "";
uint16_t port = 0; uint16_t port = 0;
evhttp_connection_get_peer(con, (char**)&address, &port); evhttp_connection_get_peer(con, (char**)&address, &port);
LookupNumeric(address, peer, port); peer = LookupNumeric(address, port);
} }
return peer; return peer;
} }

6
src/init.cpp

@ -1098,8 +1098,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
std::string proxyArg = GetArg("-proxy", ""); std::string proxyArg = GetArg("-proxy", "");
SetLimited(NET_TOR); SetLimited(NET_TOR);
if (proxyArg != "" && proxyArg != "0") { if (proxyArg != "" && proxyArg != "0") {
CService resolved; CService resolved(LookupNumeric(proxyArg.c_str(), 9050));
LookupNumeric(proxyArg.c_str(), resolved, 9050);
proxyType addrProxy = proxyType(resolved, proxyRandomize); proxyType addrProxy = proxyType(resolved, proxyRandomize);
if (!addrProxy.IsValid()) if (!addrProxy.IsValid())
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg)); 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 if (onionArg == "0") { // Handle -noonion/-onion=0
SetLimited(NET_TOR); // set onions as unreachable SetLimited(NET_TOR); // set onions as unreachable
} else { } else {
CService resolved; CService resolved(LookupNumeric(onionArg.c_str(), 9050));
LookupNumeric(onionArg.c_str(), resolved, 9050);
proxyType addrOnion = proxyType(resolved, proxyRandomize); proxyType addrOnion = proxyType(resolved, proxyRandomize);
if (!addrOnion.IsValid()) if (!addrOnion.IsValid())
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg)); return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));

6
src/net.cpp

@ -1728,8 +1728,7 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
} }
BOOST_FOREACH(const std::string& strAddNode, lAddresses) { BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
CService service; CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
LookupNumeric(strAddNode.c_str(), service, Params().GetDefaultPort());
if (service.IsValid()) { if (service.IsValid()) {
// strAddNode is an IP:port // strAddNode is an IP:port
auto it = mapConnected.find(service); auto it = mapConnected.find(service);
@ -1767,8 +1766,7 @@ void ThreadOpenAddedConnections()
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
// If strAddedNode is an IP/port, decode it immediately, so // If strAddedNode is an IP/port, decode it immediately, so
// OpenNetworkConnection can detect existing connections to that IP/port. // OpenNetworkConnection can detect existing connections to that IP/port.
CService service; CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
LookupNumeric(info.strAddedNode.c_str(), service, Params().GetDefaultPort());
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false); OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
MilliSleep(500); MilliSleep(500);
} }

9
src/netbase.cpp

@ -231,9 +231,14 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo
return true; 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) struct timeval MillisToTimeval(int64_t nTimeout)

2
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 LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
bool Lookup(const char *pszName, CService& addr, int portDefault, 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 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 LookupSubNet(const char *pszName, CSubNet& subnet);
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0); 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); bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);

3
src/qt/optionsdialog.cpp

@ -327,8 +327,7 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons
{ {
Q_UNUSED(pos); Q_UNUSED(pos);
// Validate the proxy // Validate the proxy
CService serv; CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
LookupNumeric(input.toStdString().c_str(), serv, 9050);
proxyType addrProxy = proxyType(serv, true); proxyType addrProxy = proxyType(serv, true);
if (addrProxy.IsValid()) if (addrProxy.IsValid())
return QValidator::Acceptable; return QValidator::Acceptable;

6
src/test/netbase_tests.cpp

@ -93,9 +93,7 @@ BOOST_AUTO_TEST_CASE(netbase_splithost)
bool static TestParse(string src, string canon) bool static TestParse(string src, string canon)
{ {
CService addr; CService addr(LookupNumeric(src.c_str(), 65535));
if (!LookupNumeric(src.c_str(), addr, 65535))
return canon == "";
return canon == addr.ToString(); return canon == addr.ToString();
} }
@ -107,7 +105,7 @@ BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
BOOST_CHECK(TestParse("::", "[::]:65535")); BOOST_CHECK(TestParse("::", "[::]:65535"));
BOOST_CHECK(TestParse("[::]:8333", "[::]:8333")); BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535")); 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) BOOST_AUTO_TEST_CASE(onioncat_test)

5
src/torcontrol.cpp

@ -438,7 +438,7 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep
if ((i = m.find("PrivateKey")) != m.end()) if ((i = m.find("PrivateKey")) != m.end())
private_key = i->second; 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()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile()); 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 // Now that we know Tor is running setup the proxy for onion addresses
// if -onion isn't set to something else. // if -onion isn't set to something else.
if (GetArg("-onion", "") == "") { if (GetArg("-onion", "") == "") {
CService resolved; CService resolved(LookupNumeric("127.0.0.1", 9050));
assert(LookupNumeric("127.0.0.1", resolved, 9050));
proxyType addrOnion = proxyType(resolved, true); proxyType addrOnion = proxyType(resolved, true);
SetProxy(NET_TOR, addrOnion); SetProxy(NET_TOR, addrOnion);
SetLimited(NET_TOR, false); SetLimited(NET_TOR, false);

Loading…
Cancel
Save