Bitcoin ABC 0.32.5
P2P Digital Currency
banman.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2017 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5#ifndef BITCOIN_BANMAN_H
6#define BITCOIN_BANMAN_H
7
8#include <addrdb.h>
9#include <common/bloom.h>
10#include <net_types.h> // For banmap_t
11#include <sync.h>
12#include <util/fs.h>
13
14#include <chrono>
15#include <cstdint>
16#include <memory>
17
18// Default 24-hour ban.
19// NOTE: When adjusting this, update rpcnet:setban's help ("24h")
20static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24;
21
23static constexpr std::chrono::minutes DUMP_BANS_INTERVAL{15};
24
26class CNetAddr;
27class CSubNet;
28
29// Banman manages two related but distinct concepts:
30//
31// 1. Banning. This is configured manually by the user, through the setban RPC.
32// If an address or subnet is banned, we never accept incoming connections from
33// it and never create outgoing connections to it. We won't gossip its address
34// to other peers in addr messages. Banned addresses and subnets are stored to
35// disk on shutdown and reloaded on startup. Banning can be used to
36// prevent connections with spy nodes or other griefers.
37//
38// 2. Discouragement. If a peer misbehaves (see Misbehaving() in
39// net_processing.cpp), we'll mark that address as discouraged. We still allow
40// incoming connections from them, but they're preferred for eviction when
41// we receive new incoming connections. We never make outgoing connections to
42// them, and do not gossip their address to other peers. This is implemented as
43// a bloom filter. We can (probabilistically) test for membership, but can't
44// list all discouraged addresses or unmark them as discouraged. Discouragement
45// can prevent our limited connection slots being used up by incompatible
46// or broken peers.
47//
48// Neither banning nor discouragement are protections against denial-of-service
49// attacks, since if an attacker has a way to waste our resources and we
50// disconnect from them and ban that address, it's trivial for them to
51// reconnect from another IP address.
52//
53// Attempting to automatically disconnect or ban any class of peer carries the
54// risk of splitting the network. For example, if we banned/disconnected for a
55// transaction that fails a policy check and a future version changes the
56// policy check so the transaction is accepted, then that transaction could
57// cause the network to split between old nodes and new nodes.
58
59class BanMan {
60public:
61 ~BanMan();
62 BanMan(fs::path ban_file, const CChainParams &chainparams,
63 CClientUIInterface *client_interface, int64_t default_ban_time);
64 void Ban(const CNetAddr &net_addr, int64_t ban_time_offset = 0,
65 bool since_unix_epoch = false);
66 void Ban(const CSubNet &sub_net, int64_t ban_time_offset = 0,
67 bool since_unix_epoch = false);
68 void Discourage(const CNetAddr &net_addr);
69 void ClearBanned();
70
72 bool IsBanned(const CNetAddr &net_addr);
73
75 bool IsBanned(const CSubNet &sub_net);
76
78 bool IsDiscouraged(const CNetAddr &net_addr);
79
80 bool Unban(const CNetAddr &net_addr);
81 bool Unban(const CSubNet &sub_net);
82 void GetBanned(banmap_t &banmap);
83 void DumpBanlist();
84
85private:
86 bool BannedSetIsDirty();
88 void SetBannedSetDirty(bool dirty = true);
90 void SweepBanned();
91
94 bool m_is_dirty GUARDED_BY(m_cs_banned);
97 const int64_t m_default_ban_time;
98 CRollingBloomFilter m_discouraged GUARDED_BY(m_cs_banned){50000, 0.000001};
99};
100
101#endif // BITCOIN_BANMAN_H
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME
Definition: banman.h:20
static constexpr std::chrono::minutes DUMP_BANS_INTERVAL
How often to dump banned addresses/subnets to disk.
Definition: banman.h:23
Definition: banman.h:59
void Discourage(const CNetAddr &net_addr)
Definition: banman.cpp:116
void Ban(const CNetAddr &net_addr, int64_t ban_time_offset=0, bool since_unix_epoch=false)
Definition: banman.cpp:110
banmap_t m_banned GUARDED_BY(m_cs_banned)
const int64_t m_default_ban_time
Definition: banman.h:97
void GetBanned(banmap_t &banmap)
Definition: banman.cpp:173
void ClearBanned()
Definition: banman.cpp:64
bool IsBanned(const CNetAddr &net_addr)
Return whether net_addr is banned.
Definition: banman.cpp:83
RecursiveMutex m_cs_banned
Definition: banman.h:92
bool BannedSetIsDirty()
Definition: banman.cpp:207
CClientUIInterface * m_client_interface
Definition: banman.h:95
bool Unban(const CNetAddr &net_addr)
Definition: banman.cpp:151
BanMan(fs::path ban_file, const CChainParams &chainparams, CClientUIInterface *client_interface, int64_t default_ban_time)
Definition: banman.cpp:14
CBanDB m_ban_db
Definition: banman.h:96
CRollingBloomFilter m_discouraged GUARDED_BY(m_cs_banned)
Definition: banman.h:98
bool m_is_dirty GUARDED_BY(m_cs_banned)
void SetBannedSetDirty(bool dirty=true)
set the "dirty" flag for the banlist
Definition: banman.cpp:212
~BanMan()
Definition: banman.cpp:39
void SweepBanned()
clean unused entries (if bantime has expired)
Definition: banman.cpp:181
void DumpBanlist()
Definition: banman.cpp:43
bool IsDiscouraged(const CNetAddr &net_addr)
Return whether net_addr is discouraged.
Definition: banman.cpp:78
Access to the banlist databases (banlist.json and banlist.dat)
Definition: addrdb.h:74
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
Signals for UI communication.
Definition: ui_interface.h:25
Network address.
Definition: netaddress.h:121
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:115
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13