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