Skip to content

Commit

Permalink
net: split socket creation out of connection
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni committed Sep 20, 2017
1 parent b887676 commit ced8190
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
15 changes: 13 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,27 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
if (addrConnect.IsValid()) {
bool proxyConnectionFailed = false;

if (GetProxy(addrConnect.GetNetwork(), proxy))
if (GetProxy(addrConnect.GetNetwork(), proxy)) {
if (!CreateSocket(proxy.proxy, hSocket)) {
return nullptr;
}
connected = ConnectThroughProxy(proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket, nConnectTimeout, &proxyConnectionFailed);
else // no proxy needed (none set for target network)
} else {
// no proxy needed (none set for target network)
if (!CreateSocket(addrConnect, hSocket)) {
return nullptr;
}
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
}
if (!proxyConnectionFailed) {
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
// the proxy, mark this as an attempt.
addrman.Attempt(addrConnect, fCountFailure);
}
} else if (pszDest && GetNameProxy(proxy)) {
if (!CreateSocket(proxy.proxy, hSocket)) {
return nullptr;
}
std::string host;
int port = default_port;
SplitHostPort(std::string(pszDest), port, host);
Expand Down
33 changes: 24 additions & 9 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
return true;
}

bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout)
bool CreateSocket(const CService &addrConnect, SOCKET& hSocketRet)
{
hSocketRet = INVALID_SOCKET;

Expand Down Expand Up @@ -428,7 +428,23 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int
CloseSocket(hSocket);
return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
}
hSocketRet = hSocket;
return true;
}

bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocket, int nTimeout)
{
struct sockaddr_storage sockaddr;
socklen_t len = sizeof(sockaddr);
if (hSocket == INVALID_SOCKET) {
LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToString());
return false;
}
if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToString());
CloseSocket(hSocket);
return false;
}
if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
{
int nErr = WSAGetLastError();
Expand Down Expand Up @@ -481,8 +497,6 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int
return false;
}
}

hSocketRet = hSocket;
return true;
}

Expand Down Expand Up @@ -534,9 +548,8 @@ bool IsProxy(const CNetAddr &addr) {
return false;
}

bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocket, int nTimeout, bool *outProxyConnectionFailed)
{
SOCKET hSocket = INVALID_SOCKET;
// first connect to proxy server
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) {
if (outProxyConnectionFailed)
Expand All @@ -548,14 +561,16 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
ProxyCredentials random_auth;
static std::atomic_int counter(0);
random_auth.username = random_auth.password = strprintf("%i", counter++);
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket))
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket)) {
CloseSocket(hSocket);
return false;
}
} else {
if (!Socks5(strDest, (unsigned short)port, 0, hSocket))
if (!Socks5(strDest, (unsigned short)port, 0, hSocket)) {
CloseSocket(hSocket);
return false;
}
}

hSocketRet = hSocket;
return true;
}
bool LookupSubNet(const char* pszName, CSubNet& ret)
Expand Down
1 change: 1 addition & 0 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
CService LookupNumeric(const char *pszName, int portDefault = 0);
bool LookupSubNet(const char *pszName, CSubNet& subnet);
bool CreateSocket(const CService &addrConnect, SOCKET& hSocketRet);
bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout);
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
/** Return readable error string for a network error code */
Expand Down

0 comments on commit ced8190

Please sign in to comment.