Skip to content

Commit 4452205

Browse files
committed
Merge pull request #5288
e3cae52 Added -whiteconnections=<n> option (Josh Lehan)
2 parents 708037f + e3cae52 commit 4452205

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

src/init.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ std::string HelpMessage(HelpMessageMode mode)
330330
strUsage += HelpMessageOpt("-whitebind=<addr>", _("Bind to given address and whitelist peers connecting to it. Use [host]:port notation for IPv6"));
331331
strUsage += HelpMessageOpt("-whitelist=<netmask>", _("Whitelist peers connecting from the given netmask or IP address. Can be specified multiple times.") +
332332
" " + _("Whitelisted peers cannot be DoS banned and their transactions are always relayed, even if they are already in the mempool, useful e.g. for a gateway"));
333+
strUsage += HelpMessageOpt("-whiteconnections=<n>", strprintf(_("Reserve this many inbound connections for whitelisted peers (default: %d)"), 0));
333334

334335
#ifdef ENABLE_WALLET
335336
strUsage += HelpMessageGroup(_("Wallet options:"));
@@ -725,16 +726,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
725726
LogPrintf("%s: parameter interaction: -zapwallettxes=<mode> -> setting -rescan=1\n", __func__);
726727
}
727728

728-
// Make sure enough file descriptors are available
729-
int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1);
730-
nMaxConnections = GetArg("-maxconnections", 125);
731-
nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
732-
int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS);
733-
if (nFD < MIN_CORE_FILEDESCRIPTORS)
734-
return InitError(_("Not enough file descriptors available."));
735-
if (nFD - MIN_CORE_FILEDESCRIPTORS < nMaxConnections)
736-
nMaxConnections = nFD - MIN_CORE_FILEDESCRIPTORS;
737-
738729
// if using block pruning, then disable txindex
739730
if (GetArg("-prune", 0)) {
740731
if (GetBoolArg("-txindex", false))
@@ -745,6 +736,47 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
745736
}
746737
#endif
747738
}
739+
740+
// Make sure enough file descriptors are available
741+
int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1);
742+
int nUserMaxConnections = GetArg("-maxconnections", 125);
743+
nMaxConnections = std::max(nUserMaxConnections, 0);
744+
int nUserWhiteConnections = GetArg("-whiteconnections", 0);
745+
nWhiteConnections = std::max(nUserWhiteConnections, 0);
746+
747+
if ((mapArgs.count("-whitelist")) || (mapArgs.count("-whitebind"))) {
748+
if (!(mapArgs.count("-maxconnections"))) {
749+
// User is using whitelist feature,
750+
// but did not specify -maxconnections parameter.
751+
// Silently increase the default to compensate,
752+
// so that the whitelist connection reservation feature
753+
// does not inadvertently reduce the default
754+
// inbound connection capacity of the network.
755+
nMaxConnections += nWhiteConnections;
756+
}
757+
} else {
758+
// User not using whitelist feature.
759+
// Silently disable connection reservation,
760+
// for the same reason as above.
761+
nWhiteConnections = 0;
762+
}
763+
764+
// Trim requested connection counts, to fit into system limitations
765+
nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
766+
int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS);
767+
if (nFD < MIN_CORE_FILEDESCRIPTORS)
768+
return InitError(_("Not enough file descriptors available."));
769+
nMaxConnections = std::min(nFD - MIN_CORE_FILEDESCRIPTORS, nMaxConnections);
770+
771+
if (nMaxConnections < nUserMaxConnections)
772+
InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));
773+
774+
// Connection capacity is prioritized in this order:
775+
// outbound connections (hardcoded to 8),
776+
// then whitelisted connections,
777+
// then non-whitelisted connections get whatever's left (if any).
778+
if ((nWhiteConnections > 0) && (nWhiteConnections >= (nMaxConnections - 8)))
779+
InitWarning(strprintf(_("All non-whitelisted incoming connections will be dropped, because -whiteconnections is %d and -maxconnections is only %d."), nWhiteConnections, nMaxConnections));
748780

749781
// ********************************************************* Step 3: parameter-to-internal-flags
750782

@@ -921,6 +953,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
921953
LogPrintf("Using data directory %s\n", strDataDir);
922954
LogPrintf("Using config file %s\n", GetConfigFile().string());
923955
LogPrintf("Using at most %i connections (%i file descriptors available)\n", nMaxConnections, nFD);
956+
if (nWhiteConnections > 0)
957+
LogPrintf("Reserving %i of these connections for whitelisted inbound peers\n", nWhiteConnections);
924958
std::ostringstream strErrors;
925959

926960
LogPrintf("Using %u threads for script verification\n", nScriptCheckThreads);

src/net.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ uint64_t nLocalHostNonce = 0;
7979
static std::vector<ListenSocket> vhListenSocket;
8080
CAddrMan addrman;
8181
int nMaxConnections = 125;
82+
int nWhiteConnections = 0;
8283
bool fAddressesInitialized = false;
8384

8485
vector<CNode*> vNodes;
@@ -928,6 +929,7 @@ void ThreadSocketHandler()
928929
SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
929930
CAddress addr;
930931
int nInbound = 0;
932+
int nMaxInbound = nMaxConnections - MAX_OUTBOUND_CONNECTIONS;
931933

932934
if (hSocket != INVALID_SOCKET)
933935
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
@@ -947,8 +949,14 @@ void ThreadSocketHandler()
947949
if (nErr != WSAEWOULDBLOCK)
948950
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
949951
}
950-
else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
952+
else if (nInbound >= nMaxInbound)
951953
{
954+
LogPrint("net", "connection from %s dropped (full)\n", addr.ToString());
955+
CloseSocket(hSocket);
956+
}
957+
else if (!whitelisted && (nInbound >= (nMaxInbound - nWhiteConnections)))
958+
{
959+
LogPrint("net", "connection from %s dropped (non-whitelisted)\n", addr.ToString());
952960
CloseSocket(hSocket);
953961
}
954962
else if (CNode::IsBanned(addr) && !whitelisted)
@@ -962,6 +970,8 @@ void ThreadSocketHandler()
962970
pnode->AddRef();
963971
pnode->fWhitelisted = whitelisted;
964972

973+
LogPrint("net", "connection from %s accepted\n", addr.ToString());
974+
965975
{
966976
LOCK(cs_vNodes);
967977
vNodes.push_back(pnode);
@@ -2295,4 +2305,4 @@ void DumpBanlist()
22952305

22962306
LogPrint("net", "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
22972307
banmap.size(), GetTimeMillis() - nStart);
2298-
}
2308+
}

src/net.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,20 @@ extern bool fListen;
141141
extern uint64_t nLocalServices;
142142
extern uint64_t nLocalHostNonce;
143143
extern CAddrMan addrman;
144+
145+
// The allocation of connections against the maximum allowed (nMaxConnections)
146+
// is prioritized as follows:
147+
// 1st: Outbound connections (MAX_OUTBOUND_CONNECTIONS)
148+
// 2nd: Inbound connections from whitelisted peers (nWhiteConnections)
149+
// 3rd: Inbound connections from non-whitelisted peers
150+
// Thus, the number of connection slots for the general public to use is:
151+
// nMaxConnections - (MAX_OUTBOUND_CONNECTIONS + nWhiteConnections)
152+
// Any additional inbound connections beyond limits will be immediately closed
153+
154+
/** Maximum number of connections to simultaneously allow (aka connection slots) */
144155
extern int nMaxConnections;
156+
/** Number of connection slots to reserve for inbound from whitelisted peers */
157+
extern int nWhiteConnections;
145158

146159
extern std::vector<CNode*> vNodes;
147160
extern CCriticalSection cs_vNodes;

0 commit comments

Comments
 (0)