diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -353,7 +353,7 @@ bool AttemptToEvictConnection(); CNode *ConnectNode(CAddress addrConnect, const char *pszDest, - bool fCountFailure); + bool fCountFailure, bool manual_connection); bool IsWhitelistedRange(const CNetAddr &addr); void DeleteNode(CNode *pnode); diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -361,7 +361,7 @@ } CNode *CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, - bool fCountFailure) { + bool fCountFailure, bool manual_connection) { if (pszDest == nullptr) { if (IsLocal(addrConnect)) { return nullptr; @@ -433,8 +433,8 @@ if (hSocket == INVALID_SOCKET) { return nullptr; } - connected = - ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout); + connected = ConnectSocketDirectly( + addrConnect, hSocket, nConnectTimeout, manual_connection); } if (!proxyConnectionFailed) { // If a connection to the node was attempted, and failure (if any) @@ -2120,7 +2120,8 @@ return; } - CNode *pnode = ConnectNode(addrConnect, pszDest, fCountFailure); + CNode *pnode = + ConnectNode(addrConnect, pszDest, fCountFailure, manual_connection); if (!pnode) { return; diff --git a/src/netbase.h b/src/netbase.h --- a/src/netbase.h +++ b/src/netbase.h @@ -57,7 +57,8 @@ bool LookupSubNet(const char *pszName, CSubNet &subnet); SOCKET CreateSocket(const CService &addrConnect); bool ConnectSocketDirectly(const CService &addrConnect, - const SOCKET &hSocketRet, int nTimeout); + const SOCKET &hSocketRet, int nTimeout, + bool manual_connection); bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, int port, const SOCKET &hSocketRet, int nTimeout, bool *outProxyConnectionFailed); diff --git a/src/netbase.cpp b/src/netbase.cpp --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include #ifndef WIN32 @@ -522,8 +524,19 @@ return hSocket; } +template +static void LogConnectFailure(bool manual_connection, const char *fmt, + const Args &... args) { + std::string error_message = tfm::format(fmt, args...); + if (manual_connection) { + LogPrintf("%s\n", error_message); + } else { + LogPrint(BCLog::NET, "%s\n", error_message); + } +} + bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET &hSocket, - int nTimeout) { + int nTimeout, bool manual_connection) { struct sockaddr_storage sockaddr; socklen_t len = sizeof(sockaddr); if (hSocket == INVALID_SOCKET) { @@ -567,8 +580,10 @@ return false; } if (nRet != 0) { - LogPrintf("connect() to %s failed after select(): %s\n", - addrConnect.ToString(), NetworkErrorString(nRet)); + LogConnectFailure(manual_connection, + "connect() to %s failed after select(): %s", + addrConnect.ToString(), + NetworkErrorString(nRet)); return false; } } @@ -578,8 +593,9 @@ else #endif { - LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), - NetworkErrorString(WSAGetLastError())); + LogConnectFailure(manual_connection, "connect() to %s failed: %s", + addrConnect.ToString(), + NetworkErrorString(WSAGetLastError())); return false; } } @@ -643,7 +659,7 @@ int port, const SOCKET &hSocket, int nTimeout, bool *outProxyConnectionFailed) { // first connect to proxy server - if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) { + if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout, true)) { if (outProxyConnectionFailed) { *outProxyConnectionFailed = true; } @@ -663,6 +679,7 @@ } return true; } + bool LookupSubNet(const char *pszName, CSubNet &ret) { std::string strSubnet(pszName); size_t slash = strSubnet.find_last_of('/'); diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -279,7 +279,8 @@ return false; } // no proxy needed (none set for target network) - connected = ConnectSocketDirectly(you, sock, nConnectTimeout); + connected = + ConnectSocketDirectly(you, sock, nConnectTimeout, true); } } diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py --- a/test/lint/lint-format-strings.py +++ b/test/lint/lint-format-strings.py @@ -16,7 +16,7 @@ FALSE_POSITIVES = [ ("src/dbwrapper.cpp", "vsnprintf(p, limit - p, format, backup_ap)"), ("src/index/base.cpp", "FatalError(const char *fmt, const Args &... args)"), - ("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args)"), + ("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char *fmt, const Args &... args)"), ("src/util.cpp", "strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION))"), ("src/util.cpp", "strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION)"), ("src/seeder/main.cpp", "fprintf(stderr, help, argv[0])"),