Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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 : 2009 : BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
18 [ + - + - ]: 4018 : : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
19 : : {
20 [ + - ]: 2009 : LoadBanlist();
21 [ + - ]: 2009 : DumpBanlist();
22 : 2009 : }
23 : :
24 : 2009 : BanMan::~BanMan()
25 : : {
26 : 2009 : DumpBanlist();
27 : 2009 : }
28 : :
29 : 2009 : void BanMan::LoadBanlist()
30 : : {
31 : 2009 : LOCK(m_banned_mutex);
32 : :
33 [ - + - - : 2009 : if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…"));
- - ]
34 : :
35 : 2009 : const auto start{SteadyClock::now()};
36 [ + - + + ]: 2009 : if (m_ban_db.Read(m_banned)) {
37 [ + - ]: 1115 : SweepBanned(); // sweep out unused entries
38 : :
39 [ + - - + : 1115 : LogDebug(BCLog::NET, "Loaded %d banned node addresses/subnets %dms", m_banned.size(),
- - ]
40 : : Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
41 : : } else {
42 [ + - ]: 894 : LogInfo("Recreating the banlist database");
43 [ + - ]: 894 : m_banned = {};
44 : 894 : m_is_dirty = true;
45 : : }
46 : 2009 : }
47 : :
48 : 22327 : void BanMan::DumpBanlist()
49 : : {
50 [ + + + - ]: 22327 : static Mutex dump_mutex;
51 : 22327 : LOCK(dump_mutex);
52 : :
53 [ + - ]: 22327 : banmap_t banmap;
54 : 22327 : {
55 [ + - ]: 22327 : LOCK(m_banned_mutex);
56 [ + - ]: 22327 : SweepBanned();
57 [ + + + - ]: 22327 : if (!m_is_dirty) return;
58 [ + - ]: 18179 : banmap = m_banned;
59 [ + - ]: 18179 : m_is_dirty = false;
60 : 4148 : }
61 : :
62 : 18179 : const auto start{SteadyClock::now()};
63 [ + - + + ]: 18179 : if (!m_ban_db.Write(banmap)) {
64 [ + - ]: 4277 : LOCK(m_banned_mutex);
65 [ + - ]: 4277 : m_is_dirty = true;
66 : 4277 : }
67 : :
68 [ + - + + : 18179 : LogDebug(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms", banmap.size(),
+ - ]
69 : : Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
70 [ + - + - ]: 44654 : }
71 : :
72 : 2633 : void BanMan::ClearBanned()
73 : : {
74 : 2633 : {
75 : 2633 : LOCK(m_banned_mutex);
76 : 2633 : m_banned.clear();
77 [ + - ]: 2633 : m_is_dirty = true;
78 : 2633 : }
79 : 2633 : DumpBanlist(); //store banlist to disk
80 [ - + ]: 2633 : if (m_client_interface) m_client_interface->BannedListChanged();
81 : 2633 : }
82 : :
83 : 2583 : bool BanMan::IsDiscouraged(const CNetAddr& net_addr)
84 : : {
85 : 2583 : LOCK(m_banned_mutex);
86 [ + - + - : 5166 : return m_discouraged.contains(net_addr.GetAddrBytes());
+ - ]
87 : 2583 : }
88 : :
89 : 2187 : bool BanMan::IsBanned(const CNetAddr& net_addr)
90 : : {
91 : 2187 : auto current_time = GetTime();
92 : 2187 : LOCK(m_banned_mutex);
93 [ + + ]: 35009 : for (const auto& it : m_banned) {
94 : 33430 : CSubNet sub_net = it.first;
95 : 33430 : CBanEntry ban_entry = it.second;
96 : :
97 [ + + + - : 33430 : if (current_time < ban_entry.nBanUntil && sub_net.Match(net_addr)) {
+ + ]
98 : 608 : return true;
99 : : }
100 : 33430 : }
101 : : return false;
102 : 2187 : }
103 : :
104 : 1750 : bool BanMan::IsBanned(const CSubNet& sub_net)
105 : : {
106 : 1750 : auto current_time = GetTime();
107 : 1750 : LOCK(m_banned_mutex);
108 [ + - ]: 1750 : banmap_t::iterator i = m_banned.find(sub_net);
109 [ + + ]: 1750 : if (i != m_banned.end()) {
110 [ + + ]: 643 : CBanEntry ban_entry = (*i).second;
111 [ + + ]: 643 : if (current_time < ban_entry.nBanUntil) {
112 : 400 : return true;
113 : : }
114 : : }
115 : : return false;
116 : 1750 : }
117 : :
118 : 15102 : void BanMan::Ban(const CNetAddr& net_addr, int64_t ban_time_offset, bool since_unix_epoch)
119 : : {
120 : 15102 : CSubNet sub_net(net_addr);
121 [ + - ]: 15102 : Ban(sub_net, ban_time_offset, since_unix_epoch);
122 : 15102 : }
123 : :
124 : 1522 : void BanMan::Discourage(const CNetAddr& net_addr)
125 : : {
126 : 1522 : LOCK(m_banned_mutex);
127 [ + - + - : 4566 : m_discouraged.insert(net_addr.GetAddrBytes());
+ - ]
128 : 1522 : }
129 : :
130 : 17653 : void BanMan::Ban(const CSubNet& sub_net, int64_t ban_time_offset, bool since_unix_epoch)
131 : : {
132 [ + + ]: 17653 : CBanEntry ban_entry(GetTime());
133 : :
134 : 17653 : int64_t normalized_ban_time_offset = ban_time_offset;
135 : 17653 : bool normalized_since_unix_epoch = since_unix_epoch;
136 [ + + ]: 17653 : if (ban_time_offset <= 0) {
137 : 17317 : normalized_ban_time_offset = m_default_ban_time;
138 : 17317 : normalized_since_unix_epoch = false;
139 : : }
140 [ + + ]: 17653 : ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset;
141 : :
142 : 17653 : {
143 : 17653 : LOCK(m_banned_mutex);
144 [ + - + + ]: 17653 : if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) {
145 [ + - ]: 11485 : m_banned[sub_net] = ban_entry;
146 [ + - ]: 11485 : m_is_dirty = true;
147 : : } else
148 [ + - ]: 6168 : return;
149 : 6168 : }
150 [ - + ]: 11485 : if (m_client_interface) m_client_interface->BannedListChanged();
151 : :
152 : : //store banlist to disk immediately
153 : 11485 : DumpBanlist();
154 : : }
155 : :
156 : 1871 : bool BanMan::Unban(const CNetAddr& net_addr)
157 : : {
158 : 1871 : CSubNet sub_net(net_addr);
159 [ + - ]: 1871 : return Unban(sub_net);
160 : 1871 : }
161 : :
162 : 2638 : bool BanMan::Unban(const CSubNet& sub_net)
163 : : {
164 : 2638 : {
165 : 2638 : LOCK(m_banned_mutex);
166 [ + - + + : 2638 : if (m_banned.erase(sub_net) == 0) return false;
+ - ]
167 [ + - ]: 545 : m_is_dirty = true;
168 : 2093 : }
169 [ - + ]: 545 : if (m_client_interface) m_client_interface->BannedListChanged();
170 : 545 : DumpBanlist(); //store banlist to disk immediately
171 : 545 : return true;
172 : : }
173 : :
174 : 2760 : void BanMan::GetBanned(banmap_t& banmap)
175 : : {
176 : 2760 : LOCK(m_banned_mutex);
177 : : // Sweep the banlist so expired bans are not returned
178 [ + - ]: 2760 : SweepBanned();
179 [ + - + - ]: 2760 : banmap = m_banned; //create a thread safe copy
180 : 2760 : }
181 : :
182 : 26202 : void BanMan::SweepBanned()
183 : : {
184 : 26202 : AssertLockHeld(m_banned_mutex);
185 : :
186 : 26202 : int64_t now = GetTime();
187 : 26202 : bool notify_ui = false;
188 : 26202 : banmap_t::iterator it = m_banned.begin();
189 [ + + ]: 760213 : while (it != m_banned.end()) {
190 : 734011 : CSubNet sub_net = (*it).first;
191 [ + - ]: 734011 : CBanEntry ban_entry = (*it).second;
192 [ + - + + : 734011 : if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
+ + ]
193 : 3400 : m_banned.erase(it++);
194 : 3400 : m_is_dirty = true;
195 : 3400 : notify_ui = true;
196 [ + - - + : 3400 : LogDebug(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
- - - - ]
197 : : } else {
198 : 730611 : ++it;
199 : : }
200 : 734011 : }
201 : :
202 : : // update UI
203 [ + + - + ]: 26202 : if (notify_ui && m_client_interface) {
204 : 0 : m_client_interface->BannedListChanged();
205 : : }
206 : 26202 : }
|