Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_BANMAN_H6#define BITCOIN_BANMAN_H8#include <addrdb.h>9#include <common/bloom.h>10#include <net_types.h>11#include <sync.h>12#include <util/fs.h>14#include <chrono>15#include <cstdint>16#include <memory>18// NOTE: When adjusting this, update rpcnet:setban's help ("24h")19inline constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban21/// How often to dump banned addresses/subnets to disk.22inline constexpr std::chrono::minutes DUMP_BANS_INTERVAL{15};24class CClientUIInterface;25class CNetAddr;26class CSubNet;28// Banman manages two related but distinct concepts:29//30// 1. Banning. This is configured manually by the user, through the setban RPC.31// If an address or subnet is banned, we never accept incoming connections from32// it and never create outgoing connections to it. We won't gossip its address33// to other peers in addr messages. Banned addresses and subnets are stored to34// disk on shutdown and reloaded on startup. Banning can be used to35// prevent connections with spy nodes or other griefers.36//37// 2. Discouragement. If a peer misbehaves (see Misbehaving() in38// net_processing.cpp), we'll mark that address as discouraged. We still allow39// incoming connections from them, but they're preferred for eviction when40// we receive new incoming connections. We never make outgoing connections to41// them, and do not gossip their address to other peers. This is implemented as42// a bloom filter. We can (probabilistically) test for membership, but can't43// list all discouraged addresses or unmark them as discouraged. Discouragement44// can prevent our limited connection slots being used up by incompatible45// or broken peers.46//47// Neither banning nor discouragement are protections against denial-of-service48// attacks, since if an attacker has a way to waste our resources and we49// disconnect from them and ban that address, it's trivial for them to50// reconnect from another IP address.51//52// Attempting to automatically disconnect or ban any class of peer carries the53// risk of splitting the network. For example, if we banned/disconnected for a54// transaction that fails a policy check and a future version changes the55// policy check so the transaction is accepted, then that transaction could56// cause the network to split between old nodes and new nodes.57//58// NOTE: previously a misbehaving peer would get banned instead of discouraged.59// This meant a peer could unboundedly grow our in-memory map of banned ips. When60// receiving an ADDR message we would also compare every address received to every61// item in the map. See https://bitcoincore.org/en/2024/07/03/disclose-unbounded-banlist.63class BanMan64{65public:66~BanMan();67BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time);68void Ban(const CNetAddr& net_addr, int64_t ban_time_offset = 0, bool since_unix_epoch = false) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);69void Ban(const CSubNet& sub_net, int64_t ban_time_offset = 0, bool since_unix_epoch = false) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);70void Discourage(const CNetAddr& net_addr) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);71void ClearBanned() EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);73//! Return whether net_addr is banned74bool IsBanned(const CNetAddr& net_addr) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);76//! Return whether sub_net is exactly banned77bool IsBanned(const CSubNet& sub_net) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);79//! Return whether net_addr is discouraged.80bool IsDiscouraged(const CNetAddr& net_addr) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);82bool Unban(const CNetAddr& net_addr) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);83bool Unban(const CSubNet& sub_net) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);84void GetBanned(banmap_t& banmap) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);85void DumpBanlist() EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);87private:88void LoadBanlist() EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex);89//!clean unused entries (if bantime has expired)90void SweepBanned() EXCLUSIVE_LOCKS_REQUIRED(m_banned_mutex);92Mutex m_banned_mutex;93banmap_t m_banned GUARDED_BY(m_banned_mutex);94bool m_is_dirty GUARDED_BY(m_banned_mutex){false};95CClientUIInterface* m_client_interface = nullptr;96CBanDB m_ban_db;97const int64_t m_default_ban_time;98CRollingBloomFilter m_discouraged GUARDED_BY(m_banned_mutex) {50000, 0.000001};99};101#endif // BITCOIN_BANMAN_H