Skip to content

Commit

Permalink
net: Split resolving out of CService
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni committed Jul 31, 2016
1 parent 31d6b1d commit f96c7c4
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ CService HTTPRequest::GetPeer()
const char* address = "";
uint16_t port = 0;
evhttp_connection_get_peer(con, (char**)&address, &port);
peer = CService(address, port);
LookupNumeric(address, peer, port);
}
return peer;
}
Expand Down
8 changes: 6 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
std::string proxyArg = GetArg("-proxy", "");
SetLimited(NET_TOR);
if (proxyArg != "" && proxyArg != "0") {
proxyType addrProxy = proxyType(CService(proxyArg, 9050), proxyRandomize);
CService resolved;
LookupNumeric(proxyArg.c_str(), resolved, 9050);
proxyType addrProxy = proxyType(resolved, proxyRandomize);
if (!addrProxy.IsValid())
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg));

Expand All @@ -1115,7 +1117,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
if (onionArg == "0") { // Handle -noonion/-onion=0
SetLimited(NET_TOR); // set onions as unreachable
} else {
proxyType addrOnion = proxyType(CService(onionArg, 9050), proxyRandomize);
CService resolved;
LookupNumeric(onionArg.c_str(), resolved, 9050);
proxyType addrOnion = proxyType(resolved, proxyRandomize);
if (!addrOnion.IsValid())
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));
SetProxy(NET_TOR, addrOnion);
Expand Down
17 changes: 11 additions & 6 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
// one by discovery.
CAddress GetLocalAddress(const CNetAddr *paddrPeer)
{
CAddress ret(CService("0.0.0.0",GetListenPort()), NODE_NONE);
CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
CService addr;
if (GetLocal(addr, paddrPeer))
{
Expand Down Expand Up @@ -494,7 +494,7 @@ void CNode::PushVersion()
int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);

int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices));
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
CAddress addrMe = GetLocalAddress(&addr);
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
if (fLogIPs)
Expand Down Expand Up @@ -1727,7 +1727,8 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
}

BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
CService service(strAddNode, Params().GetDefaultPort());
CService service;
LookupNumeric(strAddNode.c_str(), service, Params().GetDefaultPort());
if (service.IsValid()) {
// strAddNode is an IP:port
auto it = mapConnected.find(service);
Expand Down Expand Up @@ -1765,7 +1766,8 @@ 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(info.strAddedNode, Params().GetDefaultPort());
CService service;
LookupNumeric(info.strAddedNode.c_str(), service, Params().GetDefaultPort());
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
MilliSleep(500);
}
Expand Down Expand Up @@ -2063,8 +2065,11 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
semOutbound = new CSemaphore(nMaxOutbound);
}

if (pnodeLocalHost == NULL)
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
if (pnodeLocalHost == NULL) {
CNetAddr local;
LookupHost("127.0.0.1", local, false);
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
}

Discover(threadGroup);

Expand Down
34 changes: 1 addition & 33 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
}
}

addr = CService("0.0.0.0:0");
addr = CService();

if (!HaveNameProxy())
return false;
Expand Down Expand Up @@ -1124,38 +1124,6 @@ bool CService::SetSockAddr(const struct sockaddr *paddr)
}
}

CService::CService(const char *pszIpPort)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, 0, false))
*this = ip;
}

CService::CService(const char *pszIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, portDefault, false))
*this = ip;
}

CService::CService(const std::string &strIpPort)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, 0, false))
*this = ip;
}

CService::CService(const std::string &strIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, portDefault, false))
*this = ip;
}

unsigned short CService::GetPort() const
{
return port;
Expand Down
4 changes: 0 additions & 4 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ class CService : public CNetAddr
CService(const CNetAddr& ip, unsigned short port);
CService(const struct in_addr& ipv4Addr, unsigned short port);
CService(const struct sockaddr_in& addr);
explicit CService(const char *pszIpPort, int portDefault);
explicit CService(const char *pszIpPort);
explicit CService(const std::string& strIpPort, int portDefault);
explicit CService(const std::string& strIpPort);
void Init();
void SetPort(unsigned short portIn);
unsigned short GetPort() const;
Expand Down
4 changes: 3 additions & 1 deletion src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons
{
Q_UNUSED(pos);
// Validate the proxy
proxyType addrProxy = proxyType(CService(input.toStdString(), 9050), true);
CService serv;
LookupNumeric(input.toStdString().c_str(), serv, 9050);
proxyType addrProxy = proxyType(serv, true);
if (addrProxy.IsValid())
return QValidator::Acceptable;

Expand Down
Loading

0 comments on commit f96c7c4

Please sign in to comment.