Skip to content

Commit 35e408f

Browse files
committed
Regard connection failures as attempt for addrman
This avoids connecting to them again too soon in ThreadOpenConnections. Make an exception for connection failures to the proxy as these shouldn't affect the status of specific nodes.
1 parent 90f7aa7 commit 35e408f

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/net.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
399399

400400
// Connect
401401
SOCKET hSocket;
402-
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort()) : ConnectSocket(addrConnect, hSocket))
402+
bool proxyConnectionFailed = false;
403+
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
404+
ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
403405
{
404406
addrman.Attempt(addrConnect);
405407

@@ -415,6 +417,10 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
415417
pnode->nTimeConnected = GetTime();
416418

417419
return pnode;
420+
} else if (!proxyConnectionFailed) {
421+
// If connecting to the node failed, and failure is not caused by a problem connecting to
422+
// the proxy, mark this as an attempt.
423+
addrman.Attempt(addrConnect);
418424
}
419425

420426
return NULL;

src/netbase.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,23 @@ bool IsProxy(const CNetAddr &addr) {
519519
return false;
520520
}
521521

522-
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
522+
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
523523
{
524524
proxyType proxy;
525+
if (outProxyConnectionFailed)
526+
*outProxyConnectionFailed = false;
525527
// no proxy needed (none set for target network)
526528
if (!GetProxy(addrDest.GetNetwork(), proxy))
527529
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
528530

529531
SOCKET hSocket = INVALID_SOCKET;
530532

531533
// first connect to proxy server
532-
if (!ConnectSocketDirectly(proxy, hSocket, nTimeout))
534+
if (!ConnectSocketDirectly(proxy, hSocket, nTimeout)) {
535+
if (outProxyConnectionFailed)
536+
*outProxyConnectionFailed = true;
533537
return false;
538+
}
534539
// do socks negotiation
535540
if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
536541
return false;
@@ -539,10 +544,14 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
539544
return true;
540545
}
541546

542-
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout)
547+
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed)
543548
{
544549
string strDest;
545550
int port = portDefault;
551+
552+
if (outProxyConnectionFailed)
553+
*outProxyConnectionFailed = false;
554+
546555
SplitHostPort(string(pszDest), port, strDest);
547556

548557
SOCKET hSocket = INVALID_SOCKET;
@@ -561,8 +570,11 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
561570
if (!HaveNameProxy())
562571
return false;
563572
// first connect to name proxy server
564-
if (!ConnectSocketDirectly(nameProxy, hSocket, nTimeout))
573+
if (!ConnectSocketDirectly(nameProxy, hSocket, nTimeout)) {
574+
if (outProxyConnectionFailed)
575+
*outProxyConnectionFailed = true;
565576
return false;
577+
}
566578
// do socks negotiation
567579
if (!Socks5(strDest, (unsigned short)port, hSocket))
568580
return false;

src/netbase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nM
182182
bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
183183
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
184184
bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
185-
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
186-
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
185+
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
186+
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
187187
/** Return readable error string for a network error code */
188188
std::string NetworkErrorString(int err);
189189
/** Close socket and set hSocket to INVALID_SOCKET */

0 commit comments

Comments
 (0)