diff --git a/src/net.cpp b/src/net.cpp index e1206745a4..8b0581d65b 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1653,7 +1653,7 @@ std::pair CConnman::SocketSendData(CNode& node) const * to forge. In order to partition a node the attacker must be * simultaneously better at all of them than honest peers. */ -bool CConnman::AttemptToEvictConnection() +bool CConnman::AttemptToEvictConnection(bool force) { std::vector vEvictionCandidates; { @@ -1681,7 +1681,7 @@ bool CConnman::AttemptToEvictConnection() vEvictionCandidates.push_back(candidate); } } - const std::optional node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates)); + const std::optional node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates), force); if (!node_id_to_evict) { return false; } @@ -1776,7 +1776,9 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr&& sock, if (nInbound >= m_max_inbound) { - if (!AttemptToEvictConnection()) { + // If the inbound connection attempt is granted ForceInbound permission, try a little harder + // to make room by evicting a peer we may not have otherwise evicted. + if (!AttemptToEvictConnection(NetPermissions::HasFlag(permission_flags, NetPermissionFlags::ForceInbound))) { // No connection to evict, disconnect the new connection LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n"); return; diff --git a/src/net.h b/src/net.h index beec58c389..786d6b7dc7 100644 --- a/src/net.h +++ b/src/net.h @@ -1338,7 +1338,14 @@ private: */ bool AlreadyConnectedToAddress(const CAddress& addr); - bool AttemptToEvictConnection(); + /** + * Attempt to disconnect a connected peer. + * Used to make room for new inbound connections, returns true if successful. + * @param[in] force Try to evict a random inbound ban-able peer if + * all connections are otherwise protected. + */ + bool AttemptToEvictConnection(bool force); + CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex); void AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr, const std::vector& ranges) const;