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 : 1970 : BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
18 [ + - + - ]: 3940 : : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
19 : : {
20 [ + - ]: 1970 : LoadBanlist();
21 [ + - ]: 1970 : DumpBanlist();
22 : 1970 : }
23 : :
24 : 1970 : BanMan::~BanMan()
25 : : {
26 : 1970 : DumpBanlist();
27 : 1970 : }
28 : :
29 : 1970 : void BanMan::LoadBanlist()
30 : : {
31 : 1970 : LOCK(m_banned_mutex);
32 : :
33 [ - + - - : 1970 : if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…"));
- - ]
34 : :
35 : 1970 : const auto start{SteadyClock::now()};
36 [ + - + + ]: 1970 : if (m_ban_db.Read(m_banned)) {
37 [ + - ]: 1103 : SweepBanned(); // sweep out unused entries
38 : :
39 [ + - - + : 1103 : 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 [ + - ]: 867 : LogInfo("Recreating the banlist database");
43 [ + - ]: 867 : m_banned = {};
44 : 867 : m_is_dirty = true;
45 : : }
46 : 1970 : }
47 : :
48 : 19943 : void BanMan::DumpBanlist()
49 : : {
50 [ + + + - ]: 19943 : static Mutex dump_mutex;
51 : 19943 : LOCK(dump_mutex);
52 : :
53 [ + - ]: 19943 : banmap_t banmap;
54 : 19943 : {
55 [ + - ]: 19943 : LOCK(m_banned_mutex);
56 [ + - ]: 19943 : SweepBanned();
57 [ + + + - ]: 19943 : if (!m_is_dirty) return;
58 [ + - ]: 15630 : banmap = m_banned;
59 [ + - ]: 15630 : m_is_dirty = false;
60 : 4313 : }
61 : :
62 : 15630 : const auto start{SteadyClock::now()};
63 [ + - + + ]: 15630 : if (!m_ban_db.Write(banmap)) {
64 [ + - ]: 2405 : LOCK(m_banned_mutex);
65 [ + - ]: 2405 : m_is_dirty = true;
66 : 2405 : }
67 : :
68 [ + - + + : 15630 : LogDebug(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms\n", banmap.size(),
+ - ]
69 : : Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
70 [ + - + - ]: 39886 : }
71 : :
72 : 2761 : void BanMan::ClearBanned()
73 : : {
74 : 2761 : {
75 : 2761 : LOCK(m_banned_mutex);
76 : 2761 : m_banned.clear();
77 [ + - ]: 2761 : m_is_dirty = true;
78 : 2761 : }
79 : 2761 : DumpBanlist(); //store banlist to disk
80 [ - + ]: 2761 : if (m_client_interface) m_client_interface->BannedListChanged();
81 : 2761 : }
82 : :
83 : 2356 : bool BanMan::IsDiscouraged(const CNetAddr& net_addr)
84 : : {
85 : 2356 : LOCK(m_banned_mutex);
86 [ + - + - : 4712 : return m_discouraged.contains(net_addr.GetAddrBytes());
+ - ]
87 : 2356 : }
88 : :
89 : 1794 : bool BanMan::IsBanned(const CNetAddr& net_addr)
90 : : {
91 : 1794 : auto current_time = GetTime();
92 : 1794 : LOCK(m_banned_mutex);
93 [ + + ]: 21819 : for (const auto& it : m_banned) {
94 : 20637 : CSubNet sub_net = it.first;
95 : 20637 : CBanEntry ban_entry = it.second;
96 : :
97 [ + + + - : 20637 : if (current_time < ban_entry.nBanUntil && sub_net.Match(net_addr)) {
+ + ]
98 : 612 : return true;
99 : : }
100 : 20637 : }
101 : : return false;
102 : 1794 : }
103 : :
104 : 1908 : bool BanMan::IsBanned(const CSubNet& sub_net)
105 : : {
106 : 1908 : auto current_time = GetTime();
107 : 1908 : LOCK(m_banned_mutex);
108 [ + - ]: 1908 : banmap_t::iterator i = m_banned.find(sub_net);
109 [ + + ]: 1908 : if (i != m_banned.end()) {
110 [ + + ]: 698 : CBanEntry ban_entry = (*i).second;
111 [ + + ]: 698 : if (current_time < ban_entry.nBanUntil) {
112 : 420 : return true;
113 : : }
114 : : }
115 : : return false;
116 : 1908 : }
117 : :
118 : 16354 : void BanMan::Ban(const CNetAddr& net_addr, int64_t ban_time_offset, bool since_unix_epoch)
119 : : {
120 : 16354 : CSubNet sub_net(net_addr);
121 [ + - ]: 16354 : Ban(sub_net, ban_time_offset, since_unix_epoch);
122 : 16354 : }
123 : :
124 : 1439 : void BanMan::Discourage(const CNetAddr& net_addr)
125 : : {
126 : 1439 : LOCK(m_banned_mutex);
127 [ + - + - : 4317 : m_discouraged.insert(net_addr.GetAddrBytes());
+ - ]
128 : 1439 : }
129 : :
130 : 18775 : void BanMan::Ban(const CSubNet& sub_net, int64_t ban_time_offset, bool since_unix_epoch)
131 : : {
132 [ + + ]: 18775 : CBanEntry ban_entry(GetTime());
133 : :
134 : 18775 : int64_t normalized_ban_time_offset = ban_time_offset;
135 : 18775 : bool normalized_since_unix_epoch = since_unix_epoch;
136 [ + + ]: 18775 : if (ban_time_offset <= 0) {
137 : 18568 : normalized_ban_time_offset = m_default_ban_time;
138 : 18568 : normalized_since_unix_epoch = false;
139 : : }
140 [ + + ]: 18775 : ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset;
141 : :
142 : 18775 : {
143 : 18775 : LOCK(m_banned_mutex);
144 [ + - + + ]: 18775 : if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) {
145 [ + - ]: 10570 : m_banned[sub_net] = ban_entry;
146 [ + - ]: 10570 : m_is_dirty = true;
147 : : } else
148 [ + - ]: 8205 : return;
149 : 8205 : }
150 [ - + ]: 10570 : if (m_client_interface) m_client_interface->BannedListChanged();
151 : :
152 : : //store banlist to disk immediately
153 : 10570 : DumpBanlist();
154 : : }
155 : :
156 : 2121 : bool BanMan::Unban(const CNetAddr& net_addr)
157 : : {
158 : 2121 : CSubNet sub_net(net_addr);
159 [ + - ]: 2121 : return Unban(sub_net);
160 : 2121 : }
161 : :
162 : 3111 : bool BanMan::Unban(const CSubNet& sub_net)
163 : : {
164 : 3111 : {
165 : 3111 : LOCK(m_banned_mutex);
166 [ + - + + : 3111 : if (m_banned.erase(sub_net) == 0) return false;
+ - ]
167 [ + - ]: 375 : m_is_dirty = true;
168 : 2736 : }
169 [ - + ]: 375 : if (m_client_interface) m_client_interface->BannedListChanged();
170 : 375 : DumpBanlist(); //store banlist to disk immediately
171 : 375 : return true;
172 : : }
173 : :
174 : 2622 : void BanMan::GetBanned(banmap_t& banmap)
175 : : {
176 : 2622 : LOCK(m_banned_mutex);
177 : : // Sweep the banlist so expired bans are not returned
178 [ + - ]: 2622 : SweepBanned();
179 [ + - + - ]: 2622 : banmap = m_banned; //create a thread safe copy
180 : 2622 : }
181 : :
182 : 23668 : void BanMan::SweepBanned()
183 : : {
184 : 23668 : AssertLockHeld(m_banned_mutex);
185 : :
186 : 23668 : int64_t now = GetTime();
187 : 23668 : bool notify_ui = false;
188 : 23668 : banmap_t::iterator it = m_banned.begin();
189 [ + + ]: 660010 : while (it != m_banned.end()) {
190 : 636342 : CSubNet sub_net = (*it).first;
191 [ + - ]: 636342 : CBanEntry ban_entry = (*it).second;
192 [ + - + + : 636342 : if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
+ + ]
193 : 3880 : m_banned.erase(it++);
194 : 3880 : m_is_dirty = true;
195 : 3880 : notify_ui = true;
196 [ + - - + : 3880 : LogDebug(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
- - - - ]
197 : : } else {
198 : 632462 : ++it;
199 : : }
200 : 636342 : }
201 : :
202 : : // update UI
203 [ + + - + ]: 23668 : if (notify_ui && m_client_interface) {
204 : 0 : m_client_interface->BannedListChanged();
205 : : }
206 : 23668 : }
|