From b24e6e4d1b917210b9e22711fb2535f53e949547 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 16 Dec 2011 19:48:03 -0500 Subject: [PATCH 1/2] Add -keepnode which attempts to -addnode and keep a connection open --- src/init.cpp | 2 +- src/net.cpp | 101 +++++++++++++++++++++++++++++++++++++++--------- src/netbase.cpp | 18 +++++++-- src/netbase.h | 1 + 4 files changed, 100 insertions(+), 22 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index e01516eb3..890a7d03a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -192,7 +192,7 @@ bool AppInit2(int argc, char* argv[]) " -dns \t " + _("Allow DNS lookups for addnode and connect") + "\n" + " -port= \t\t " + _("Listen for connections on (default: 8333 or testnet: 18333)") + "\n" + " -maxconnections=\t " + _("Maintain at most connections to peers (default: 125)") + "\n" + - " -addnode= \t " + _("Add a node to connect to") + "\n" + + " -addnode= \t " + _("Add a node to connect to and attempt to keep the connection open") + "\n" + " -connect= \t\t " + _("Connect only to the specified node") + "\n" + " -noirc \t " + _("Don't find peers using internet relay chat") + "\n" + " -nolisten \t " + _("Don't accept connections from outside") + "\n" + diff --git a/src/net.cpp b/src/net.cpp index 72897687e..f3544326c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -29,6 +29,7 @@ static const int MAX_OUTBOUND_CONNECTIONS = 8; void ThreadMessageHandler2(void* parg); void ThreadSocketHandler2(void* parg); void ThreadOpenConnections2(void* parg); +void ThreadOpenAddedConnections2(void* parg); #ifdef USE_UPNP void ThreadMapPort2(void* parg); #endif @@ -61,6 +62,9 @@ CCriticalSection cs_mapRelay; map mapAlreadyAskedFor; +set setservAddNodeAddresses; +CCriticalSection cs_setservAddNodeAddresses; + @@ -750,7 +754,9 @@ void ThreadSocketHandler2(void* parg) } else if (nInbound >= GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS) { - closesocket(hSocket); + CRITICAL_BLOCK(cs_setservAddNodeAddresses) + if (!setservAddNodeAddresses.count(addr)) + closesocket(hSocket); } else if (CNode::IsBanned(addr)) { @@ -1214,22 +1220,6 @@ void ThreadOpenConnections2(void* parg) } } - // Connect to manually added nodes first - if (mapArgs.count("-addnode")) - { - BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"]) - { - CAddress addr(strAddr, fAllowDNS); - if (addr.IsValid()) - { - OpenNetworkConnection(addr); - Sleep(500); - if (fShutdown) - return; - } - } - } - // Initiate network connections int64 nStart = GetTime(); loop @@ -1358,6 +1348,76 @@ void ThreadOpenConnections2(void* parg) } } +void ThreadOpenAddedConnections(void* parg) +{ + IMPLEMENT_RANDOMIZE_STACK(ThreadOpenAddedConnections(parg)); + try + { + vnThreadsRunning[7]++; + ThreadOpenAddedConnections2(parg); + vnThreadsRunning[7]--; + } + catch (std::exception& e) { + vnThreadsRunning[7]--; + PrintException(&e, "ThreadOpenAddedConnections()"); + } catch (...) { + vnThreadsRunning[7]--; + PrintException(NULL, "ThreadOpenAddedConnections()"); + } + printf("ThreadOpenAddedConnections exiting\n"); +} + +void ThreadOpenAddedConnections2(void* parg) +{ + printf("ThreadOpenAddedConnections started\n"); + + if (mapArgs.count("-addnode") == 0) + return; + + vector > vservAddressesToAdd(0); + BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) + { + vector vservNode(0); + if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fAllowDNS, 0)) + { + vservAddressesToAdd.push_back(vservNode); + CRITICAL_BLOCK(cs_setservAddNodeAddresses) + BOOST_FOREACH(CService& serv, vservNode) + setservAddNodeAddresses.insert(serv); + } + } + loop + { + vector > vservConnectAddresses = vservAddressesToAdd; + // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry + // (keeping in mind that addnode entries can have many IPs if fAllowDNS) + CRITICAL_BLOCK(cs_vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) + for (vector >::iterator it = vservConnectAddresses.begin(); it != vservConnectAddresses.end(); it++) + BOOST_FOREACH(CService& addrNode, *(it)) + if (pnode->addr == addrNode) + { + it = vservConnectAddresses.erase(it); + it--; + break; + } + BOOST_FOREACH(vector& vserv, vservConnectAddresses) + { + OpenNetworkConnection(CAddress(*(vserv.begin()))); + Sleep(500); + if (fShutdown) + return; + } + if (fShutdown) + return; + vnThreadsRunning[7]--; + Sleep(120000); // Retry every 2 minutes + vnThreadsRunning[7]++; + if (fShutdown) + return; + } +} + bool OpenNetworkConnection(const CAddress& addrConnect) { // @@ -1634,6 +1694,10 @@ void StartNode(void* parg) if (!CreateThread(ThreadSocketHandler, NULL)) printf("Error: CreateThread(ThreadSocketHandler) failed\n"); + // Initiate outbound connections from -addnode + if (!CreateThread(ThreadOpenAddedConnections, NULL)) + printf("Error: CreateThread(ThreadOpenAddedConnections) failed\n"); + // Initiate outbound connections if (!CreateThread(ThreadOpenConnections, NULL)) printf("Error: CreateThread(ThreadOpenConnections) failed\n"); @@ -1652,7 +1716,7 @@ bool StopNode() fShutdown = true; nTransactionsUpdated++; int64 nStart = GetTime(); - while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 + while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 || vnThreadsRunning[6] > 0 || vnThreadsRunning[7] > 0 #ifdef USE_UPNP || vnThreadsRunning[5] > 0 #endif @@ -1669,6 +1733,7 @@ bool StopNode() if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n"); if (fHaveUPnP && vnThreadsRunning[5] > 0) printf("ThreadMapPort still running\n"); if (vnThreadsRunning[6] > 0) printf("ThreadDNSAddressSeed still running\n"); + if (vnThreadsRunning[7] > 0) printf("ThreadOpenAddedConnections still running\n"); while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0) Sleep(20); Sleep(50); diff --git a/src/netbase.cpp b/src/netbase.cpp index 2c4df7faf..34d58f90a 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -96,7 +96,7 @@ bool LookupHostNumeric(const char *pszName, std::vector& vIP, int nMax return LookupHost(pszName, vIP, nMaxSolutions, false); } -bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup) +bool Lookup(const char *pszName, std::vector& vAddr, int portDefault, bool fAllowLookup, int nMaxSolutions) { if (pszName[0] == 0) return false; @@ -130,10 +130,22 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo } std::vector vIP; - bool fRet = LookupIntern(pszHost, vIP, 1, fAllowLookup); + bool fRet = LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup); + if (!fRet) + return false; + vAddr.resize(vIP.size()); + for (int i = 0; i < vIP.size(); i++) + vAddr[i] = CService(vIP[i], port); + return true; +} + +bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup) +{ + std::vector vService; + bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1); if (!fRet) return false; - addr = CService(vIP[0], port); + addr = vService[0]; return true; } diff --git a/src/netbase.h b/src/netbase.h index 6f06f8fe0..b12fb0045 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -128,6 +128,7 @@ class CService : public CNetAddr bool LookupHost(const char *pszName, std::vector& vIP, int nMaxSolutions = 0, bool fAllowLookup = true); bool LookupHostNumeric(const char *pszName, std::vector& vIP, int nMaxSolutions = 0); bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); +bool Lookup(const char *pszName, std::vector& vAddr, int portDefault = 0, bool fAllowLookup = true, int nMaxSolutions = 0); bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); From 10ba0a3efc350b82d1415736a545f17e739c0098 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 8 Dec 2011 20:34:11 -0500 Subject: [PATCH 2/2] Minor code cleanup to use fHaveUPnP instead of #ifdef --- src/net.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index f3544326c..74ff79d1d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1716,10 +1716,8 @@ bool StopNode() fShutdown = true; nTransactionsUpdated++; int64 nStart = GetTime(); - while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 || vnThreadsRunning[6] > 0 || vnThreadsRunning[7] > 0 -#ifdef USE_UPNP - || vnThreadsRunning[5] > 0 -#endif + while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 + || (fHaveUPnP && vnThreadsRunning[5] > 0) || vnThreadsRunning[6] > 0 || vnThreadsRunning[7] > 0 ) { if (GetTime() - nStart > 20)