Skip to content

Commit 7c9a98a

Browse files
jonlsluke-jr
authored andcommitted
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.
1 parent 5d0219d commit 7c9a98a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/net.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,12 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
990990
return;
991991
}
992992

993+
if (!fNetworkActive) {
994+
LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
995+
CloseSocket(hSocket);
996+
return;
997+
}
998+
993999
if (!IsSelectableSocket(hSocket))
9941000
{
9951001
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
@@ -1783,6 +1789,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
17831789
// Initiate outbound network connection
17841790
//
17851791
boost::this_thread::interruption_point();
1792+
if (!fNetworkActive) {
1793+
return false;
1794+
}
17861795
if (!pszDest) {
17871796
if (IsLocal(addrConnect) ||
17881797
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
@@ -2024,8 +2033,28 @@ void Discover(boost::thread_group& threadGroup)
20242033
#endif
20252034
}
20262035

2036+
void CConnman::SetNetworkActive(bool active)
2037+
{
2038+
if (fDebug) {
2039+
LogPrint("net", "SetNetworkActive: %s\n", active);
2040+
}
2041+
2042+
if (!active) {
2043+
fNetworkActive = false;
2044+
2045+
LOCK(cs_vNodes);
2046+
// Close sockets to all nodes
2047+
BOOST_FOREACH(CNode* pnode, vNodes) {
2048+
pnode->CloseSocketDisconnect();
2049+
}
2050+
} else {
2051+
fNetworkActive = true;
2052+
}
2053+
}
2054+
20272055
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
20282056
{
2057+
fNetworkActive = true;
20292058
setBannedIsDirty = false;
20302059
fAddressesInitialized = false;
20312060
nLastNodeId = 0;

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class CConnman
131131
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
132132
void Stop();
133133
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
134+
void SetNetworkActive(bool active);
134135
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
135136
bool CheckIncomingNonce(uint64_t nonce);
136137

@@ -370,6 +371,7 @@ class CConnman
370371
unsigned int nReceiveFloodSize;
371372

372373
std::vector<ListenSocket> vhListenSocket;
374+
bool fNetworkActive;
373375
banmap_t setBanned;
374376
CCriticalSection cs_setBanned;
375377
bool setBannedIsDirty;

0 commit comments

Comments
 (0)