Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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 : :
6 : : #include <banman.h>
7 : :
8 : : #include <common/system.h>
9 : : #include <logging.h>
10 : : #include <netaddress.h>
11 : : #include <node/interface_ui.h>
12 : : #include <sync.h>
13 : : #include <util/time.h>
14 : : #include <util/translation.h>
15 : :
16 : :
17 : 3065 : BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
18 [ + - + - ]: 6130 : : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
19 : : {
20 [ + - ]: 3065 : LoadBanlist();
21 [ + - ]: 3065 : DumpBanlist();
22 : 3065 : }
23 : :
24 : 3065 : BanMan::~BanMan()
25 : : {
26 : 3065 : DumpBanlist();
27 : 3065 : }
28 : :
29 : 3065 : void BanMan::LoadBanlist()
30 : : {
31 : 3065 : LOCK(m_banned_mutex);
32 : :
33 [ - + - - : 3065 : if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…"));
- - ]
34 : :
35 : 3065 : const auto start{SteadyClock::now()};
36 [ + - + + ]: 3065 : if (m_ban_db.Read(m_banned)) {
37 [ + - ]: 1699 : SweepBanned(); // sweep out unused entries
38 : :
39 [ + - - + : 1699 : LogDebug(BCLog::NET, "Loaded %d banned node addresses/subnets %dms\n", m_banned.size(),
- - ]
40 : : Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
41 : : } else {
42 [ + - ]: 1366 : LogInfo("Recreating the banlist database");
43 [ + - ]: 1366 : m_banned = {};
44 : 1366 : m_is_dirty = true;
45 : : }
46 : 3065 : }
47 : :
48 : 15910 : void BanMan::DumpBanlist()
49 : : {
50 [ + + + - ]: 15910 : static Mutex dump_mutex;
51 : 15910 : LOCK(dump_mutex);
52 : :
53 [ + - ]: 15910 : banmap_t banmap;
54 : 15910 : {
55 [ + - ]: 15910 : LOCK(m_banned_mutex);
56 [ + - ]: 15910 : SweepBanned();
57 [ + + + - ]: 15910 : if (!m_is_dirty) return;
58 [ + - ]: 9237 : banmap = m_banned;
59 [ + - ]: 9237 : m_is_dirty = false;
60 : 6673 : }
61 : :
62 : 9237 : const auto start{SteadyClock::now()};
63 [ + - + + ]: 9237 : if (!m_ban_db.Write(banmap)) {
64 [ + - ]: 646 : LOCK(m_banned_mutex);
65 [ + - ]: 646 : m_is_dirty = true;
66 : 646 : }
67 : :
68 [ + - + + : 9237 : LogDebug(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms\n", banmap.size(),
+ - ]
69 : : Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
70 [ + - + - ]: 31820 : }
71 : :
72 : 3136 : void BanMan::ClearBanned()
73 : : {
74 : 3136 : {
75 : 3136 : LOCK(m_banned_mutex);
76 : 3136 : m_banned.clear();
77 [ + - ]: 3136 : m_is_dirty = true;
78 : 3136 : }
79 : 3136 : DumpBanlist(); //store banlist to disk
80 [ - + ]: 3136 : if (m_client_interface) m_client_interface->BannedListChanged();
81 : 3136 : }
82 : :
83 : 445 : bool BanMan::IsDiscouraged(const CNetAddr& net_addr)
84 : : {
85 : 445 : LOCK(m_banned_mutex);
86 [ + - + - : 445 : return m_discouraged.contains(net_addr.GetAddrBytes());
+ - ]
87 : 445 : }
88 : :
89 : 474 : bool BanMan::IsBanned(const CNetAddr& net_addr)
90 : : {
91 : 474 : auto current_time = GetTime();
92 : 474 : LOCK(m_banned_mutex);
93 [ + + ]: 1801 : for (const auto& it : m_banned) {
94 : 1334 : CSubNet sub_net = it.first;
95 : 1334 : CBanEntry ban_entry = it.second;
96 : :
97 [ + + + - : 1334 : if (current_time < ban_entry.nBanUntil && sub_net.Match(net_addr)) {
+ + ]
98 : 7 : return true;
99 : : }
100 : 1334 : }
101 : : return false;
102 : 474 : }
103 : :
104 : 261 : bool BanMan::IsBanned(const CSubNet& sub_net)
105 : : {
106 : 261 : auto current_time = GetTime();
107 : 261 : LOCK(m_banned_mutex);
108 [ + - ]: 261 : banmap_t::iterator i = m_banned.find(sub_net);
109 [ + + ]: 261 : if (i != m_banned.end()) {
110 [ - + ]: 3 : CBanEntry ban_entry = (*i).second;
111 [ - + ]: 3 : if (current_time < ban_entry.nBanUntil) {
112 : 0 : return true;
113 : : }
114 : : }
115 : : return false;
116 : 261 : }
117 : :
118 : 6318 : void BanMan::Ban(const CNetAddr& net_addr, int64_t ban_time_offset, bool since_unix_epoch)
119 : : {
120 : 6318 : CSubNet sub_net(net_addr);
121 [ + - ]: 6318 : Ban(sub_net, ban_time_offset, since_unix_epoch);
122 : 6318 : }
123 : :
124 : 324 : void BanMan::Discourage(const CNetAddr& net_addr)
125 : : {
126 : 324 : LOCK(m_banned_mutex);
127 [ + - + - : 648 : m_discouraged.insert(net_addr.GetAddrBytes());
+ - ]
128 : 324 : }
129 : :
130 : 6688 : void BanMan::Ban(const CSubNet& sub_net, int64_t ban_time_offset, bool since_unix_epoch)
131 : : {
132 [ + + ]: 6688 : CBanEntry ban_entry(GetTime());
133 : :
134 : 6688 : int64_t normalized_ban_time_offset = ban_time_offset;
135 : 6688 : bool normalized_since_unix_epoch = since_unix_epoch;
136 [ + + ]: 6688 : if (ban_time_offset <= 0) {
137 : 6616 : normalized_ban_time_offset = m_default_ban_time;
138 : 6616 : normalized_since_unix_epoch = false;
139 : : }
140 [ + + ]: 6688 : ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset;
141 : :
142 : 6688 : {
143 : 6688 : LOCK(m_banned_mutex);
144 [ + - + + ]: 6688 : if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) {
145 [ + - ]: 4203 : m_banned[sub_net] = ban_entry;
146 [ + - ]: 4203 : m_is_dirty = true;
147 : : } else
148 [ + - ]: 2485 : return;
149 : 2485 : }
150 [ - + ]: 4203 : if (m_client_interface) m_client_interface->BannedListChanged();
151 : :
152 : : //store banlist to disk immediately
153 : 4203 : DumpBanlist();
154 : : }
155 : :
156 : 1274 : bool BanMan::Unban(const CNetAddr& net_addr)
157 : : {
158 : 1274 : CSubNet sub_net(net_addr);
159 [ + - ]: 1274 : return Unban(sub_net);
160 : 1274 : }
161 : :
162 : 2398 : bool BanMan::Unban(const CSubNet& sub_net)
163 : : {
164 : 2398 : {
165 : 2398 : LOCK(m_banned_mutex);
166 [ + - + + : 2398 : if (m_banned.erase(sub_net) == 0) return false;
+ - ]
167 [ + - ]: 25 : m_is_dirty = true;
168 : 2373 : }
169 [ - + ]: 25 : if (m_client_interface) m_client_interface->BannedListChanged();
170 : 25 : DumpBanlist(); //store banlist to disk immediately
171 : 25 : return true;
172 : : }
173 : :
174 : 3532 : void BanMan::GetBanned(banmap_t& banmap)
175 : : {
176 : 3532 : LOCK(m_banned_mutex);
177 : : // Sweep the banlist so expired bans are not returned
178 [ + - ]: 3532 : SweepBanned();
179 [ + - + - ]: 3532 : banmap = m_banned; //create a thread safe copy
180 : 3532 : }
181 : :
182 : 21141 : void BanMan::SweepBanned()
183 : : {
184 : 21141 : AssertLockHeld(m_banned_mutex);
185 : :
186 : 21141 : int64_t now = GetTime();
187 : 21141 : bool notify_ui = false;
188 : 21141 : banmap_t::iterator it = m_banned.begin();
189 [ + + ]: 237879 : while (it != m_banned.end()) {
190 : 216738 : CSubNet sub_net = (*it).first;
191 [ + - ]: 216738 : CBanEntry ban_entry = (*it).second;
192 [ + - + + : 216738 : if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
+ + ]
193 : 1432 : m_banned.erase(it++);
194 : 1432 : m_is_dirty = true;
195 : 1432 : notify_ui = true;
196 [ + - - + : 1432 : LogDebug(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
- - - - ]
197 : : } else {
198 : 215306 : ++it;
199 : : }
200 : 216738 : }
201 : :
202 : : // update UI
203 [ + + - + ]: 21141 : if (notify_ui && m_client_interface) {
204 : 0 : m_client_interface->BannedListChanged();
205 : : }
206 : 21141 : }
|