Bitcoin Core  0.19.99
P2P Digital Currency
banman.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <fs.h>
10 #include <net_types.h> // For banmap_t
11 #include <sync.h>
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <memory>
16 
17 // NOTE: When adjusting this, update rpcnet:setban's help ("24h")
18 static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
19 // How often to dump addresses to banlist.dat
20 static constexpr std::chrono::minutes DUMP_BANS_INTERVAL{15};
21 
22 class CClientUIInterface;
23 class CNetAddr;
24 class CSubNet;
25 
26 // Denial-of-service detection/prevention
27 // The idea is to detect peers that are behaving
28 // badly and disconnect/ban them, but do it in a
29 // one-coding-mistake-won't-shatter-the-entire-network
30 // way.
31 // IMPORTANT: There should be nothing I can give a
32 // node that it will forward on that will make that
33 // node's peers drop it. If there is, an attacker
34 // can isolate a node and/or try to split the network.
35 // Dropping a node for sending stuff that is invalid
36 // now but might be valid in a later version is also
37 // dangerous, because it can cause a network split
38 // between nodes running old code and nodes running
39 // new code.
40 
41 class BanMan
42 {
43 public:
44  ~BanMan();
45  BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time);
46  void Ban(const CNetAddr& net_addr, const BanReason& ban_reason, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
47  void Ban(const CSubNet& sub_net, const BanReason& ban_reason, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
48  void ClearBanned();
49  int IsBannedLevel(CNetAddr net_addr);
50  bool IsBanned(CNetAddr net_addr);
51  bool IsBanned(CSubNet sub_net);
52  bool Unban(const CNetAddr& net_addr);
53  bool Unban(const CSubNet& sub_net);
54  void GetBanned(banmap_t& banmap);
55  void DumpBanlist();
56 
57 private:
58  void SetBanned(const banmap_t& banmap);
59  bool BannedSetIsDirty();
61  void SetBannedSetDirty(bool dirty = true);
63  void SweepBanned();
64 
66  banmap_t m_banned GUARDED_BY(m_cs_banned);
67  bool m_is_dirty GUARDED_BY(m_cs_banned);
70  const int64_t m_default_ban_time;
71 };
72 
73 #endif
Definition: banman.h:41
BanReason
Definition: addrdb.h:20
void SetBanned(const banmap_t &banmap)
Definition: banman.cpp:179
CClientUIInterface * m_client_interface
Definition: banman.h:68
void SetBannedSetDirty(bool dirty=true)
set the "dirty" flag for the banlist
Definition: banman.cpp:217
Signals for UI communication.
Definition: ui_interface.h:29
~BanMan()
Definition: banman.cpp:37
void ClearBanned()
Definition: banman.cpp:60
Access to the banlist database (banlist.dat)
Definition: addrdb.h:88
bool IsBanned(CNetAddr net_addr)
Definition: banman.cpp:92
banmap_t m_banned GUARDED_BY(m_cs_banned)
void SweepBanned()
clean unused entries (if bantime has expired)
Definition: banman.cpp:186
static constexpr std::chrono::minutes DUMP_BANS_INTERVAL
Definition: banman.h:20
bool BannedSetIsDirty()
Definition: banman.cpp:211
RecursiveMutex m_cs_banned
Definition: banman.h:65
CBanDB m_ban_db
Definition: banman.h:69
void DumpBanlist()
Definition: banman.cpp:42
void GetBanned(banmap_t &banmap)
Definition: banman.cpp:171
void Ban(const CNetAddr &net_addr, const BanReason &ban_reason, int64_t ban_time_offset=0, bool since_unix_epoch=false)
Definition: banman.cpp:121
const int64_t m_default_ban_time
Definition: banman.h:70
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME
Definition: banman.h:18
bool Unban(const CNetAddr &net_addr)
Definition: banman.cpp:153
int IsBannedLevel(CNetAddr net_addr)
Definition: banman.cpp:71
BanMan(fs::path ban_file, CClientUIInterface *client_interface, int64_t default_ban_time)
Definition: banman.cpp:15
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13