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 <cstdint>
9 #include <memory>
10 
11 #include <addrdb.h>
12 #include <fs.h>
13 #include <net_types.h> // For banmap_t
14 #include <sync.h>
15 
16 // NOTE: When adjusting this, update rpcnet:setban's help ("24h")
17 static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
18 
19 class CClientUIInterface;
20 class CNetAddr;
21 class CSubNet;
22 
23 // Denial-of-service detection/prevention
24 // The idea is to detect peers that are behaving
25 // badly and disconnect/ban them, but do it in a
26 // one-coding-mistake-won't-shatter-the-entire-network
27 // way.
28 // IMPORTANT: There should be nothing I can give a
29 // node that it will forward on that will make that
30 // node's peers drop it. If there is, an attacker
31 // can isolate a node and/or try to split the network.
32 // Dropping a node for sending stuff that is invalid
33 // now but might be valid in a later version is also
34 // dangerous, because it can cause a network split
35 // between nodes running old code and nodes running
36 // new code.
37 
38 class BanMan
39 {
40 public:
41  ~BanMan();
42  BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time);
43  void Ban(const CNetAddr& net_addr, const BanReason& ban_reason, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
44  void Ban(const CSubNet& sub_net, const BanReason& ban_reason, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
45  void ClearBanned();
46  int IsBannedLevel(CNetAddr net_addr);
47  bool IsBanned(CNetAddr net_addr);
48  bool IsBanned(CSubNet sub_net);
49  bool Unban(const CNetAddr& net_addr);
50  bool Unban(const CSubNet& sub_net);
51  void GetBanned(banmap_t& banmap);
52  void DumpBanlist();
53 
54 private:
55  void SetBanned(const banmap_t& banmap);
56  bool BannedSetIsDirty();
58  void SetBannedSetDirty(bool dirty = true);
60  void SweepBanned();
61 
63  banmap_t m_banned GUARDED_BY(m_cs_banned);
64  bool m_is_dirty GUARDED_BY(m_cs_banned);
67  const int64_t m_default_ban_time;
68 };
69 
70 #endif
Definition: banman.h:38
BanReason
Definition: addrdb.h:20
void SetBanned(const banmap_t &banmap)
Definition: banman.cpp:179
CClientUIInterface * m_client_interface
Definition: banman.h:65
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
bool BannedSetIsDirty()
Definition: banman.cpp:211
RecursiveMutex m_cs_banned
Definition: banman.h:62
CBanDB m_ban_db
Definition: banman.h:66
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:67
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:17
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