Allow network activity to be temporarily suspended.

Added the function SetNetworkActive() which when called with argument set to false disconnects all nodes and sets the flag fNetworkActive to false. As long as this flag is false no new connections are attempted and no incoming connections are accepted. Network activity is reenabled by calling the function with argument true.
pull/8996/head
Jon Lund Steffensen 12 years ago committed by Luke Dashjr
parent 5d0219d983
commit 7c9a98aac8

@ -990,6 +990,12 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
return; return;
} }
if (!fNetworkActive) {
LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
CloseSocket(hSocket);
return;
}
if (!IsSelectableSocket(hSocket)) if (!IsSelectableSocket(hSocket))
{ {
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString()); LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
@ -1783,6 +1789,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
// Initiate outbound network connection // Initiate outbound network connection
// //
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
if (!fNetworkActive) {
return false;
}
if (!pszDest) { if (!pszDest) {
if (IsLocal(addrConnect) || if (IsLocal(addrConnect) ||
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) || FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
@ -2024,8 +2033,28 @@ void Discover(boost::thread_group& threadGroup)
#endif #endif
} }
void CConnman::SetNetworkActive(bool active)
{
if (fDebug) {
LogPrint("net", "SetNetworkActive: %s\n", active);
}
if (!active) {
fNetworkActive = false;
LOCK(cs_vNodes);
// Close sockets to all nodes
BOOST_FOREACH(CNode* pnode, vNodes) {
pnode->CloseSocketDisconnect();
}
} else {
fNetworkActive = true;
}
}
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In) CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
{ {
fNetworkActive = true;
setBannedIsDirty = false; setBannedIsDirty = false;
fAddressesInitialized = false; fAddressesInitialized = false;
nLastNodeId = 0; nLastNodeId = 0;

@ -131,6 +131,7 @@ public:
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options); bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
void Stop(); void Stop();
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
void SetNetworkActive(bool active);
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false); bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
bool CheckIncomingNonce(uint64_t nonce); bool CheckIncomingNonce(uint64_t nonce);
@ -370,6 +371,7 @@ private:
unsigned int nReceiveFloodSize; unsigned int nReceiveFloodSize;
std::vector<ListenSocket> vhListenSocket; std::vector<ListenSocket> vhListenSocket;
bool fNetworkActive;
banmap_t setBanned; banmap_t setBanned;
CCriticalSection cs_setBanned; CCriticalSection cs_setBanned;
bool setBannedIsDirty; bool setBannedIsDirty;

Loading…
Cancel
Save