Skip to content

Commit

Permalink
net: nodes with ForceInbound permission force eviction
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#27600
Rebased-From: 2e416e0
  • Loading branch information
pinheadmz authored and luke-jr committed Aug 28, 2023
1 parent a88314b commit df56306
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ size_t 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<NodeEvictionCandidate> vEvictionCandidates;
{
Expand Down Expand Up @@ -948,7 +948,7 @@ bool CConnman::AttemptToEvictConnection()
vEvictionCandidates.push_back(candidate);
}
}
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates), force);
if (!node_id_to_evict) {
return false;
}
Expand Down Expand Up @@ -1046,7 +1046,9 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,

if (nInbound >= nMaxInbound)
{
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;
Expand Down
9 changes: 8 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,14 @@ class CConnman
*/
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, ServiceFlags& out_node_services) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
void AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr, const std::vector<NetWhitelistPermissions>& ranges) const;

Expand Down

0 comments on commit df56306

Please sign in to comment.