|
| 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 | + |
| 6 | +#include <banman.h> |
| 7 | + |
| 8 | +#include <netaddress.h> |
| 9 | +#include <ui_interface.h> |
| 10 | +#include <util/system.h> |
| 11 | +#include <util/time.h> |
| 12 | + |
| 13 | + |
| 14 | +BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time) |
| 15 | + : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time) |
| 16 | +{ |
| 17 | + if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist...")); |
| 18 | + |
| 19 | + int64_t n_start = GetTimeMillis(); |
| 20 | + m_is_dirty = false; |
| 21 | + banmap_t banmap; |
| 22 | + if (m_ban_db.Read(banmap)) { |
| 23 | + SetBanned(banmap); // thread save setter |
| 24 | + SetBannedSetDirty(false); // no need to write down, just read data |
| 25 | + SweepBanned(); // sweep out unused entries |
| 26 | + |
| 27 | + LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n", |
| 28 | + banmap.size(), GetTimeMillis() - n_start); |
| 29 | + } else { |
| 30 | + LogPrintf("Invalid or missing banlist.dat; recreating\n"); |
| 31 | + SetBannedSetDirty(true); // force write |
| 32 | + DumpBanlist(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +BanMan::~BanMan() |
| 37 | +{ |
| 38 | + DumpBanlist(); |
| 39 | +} |
| 40 | + |
| 41 | +void BanMan::DumpBanlist() |
| 42 | +{ |
| 43 | + SweepBanned(); // clean unused entries (if bantime has expired) |
| 44 | + |
| 45 | + if (!BannedSetIsDirty()) return; |
| 46 | + |
| 47 | + int64_t n_start = GetTimeMillis(); |
| 48 | + |
| 49 | + banmap_t banmap; |
| 50 | + GetBanned(banmap); |
| 51 | + if (m_ban_db.Write(banmap)) { |
| 52 | + SetBannedSetDirty(false); |
| 53 | + } |
| 54 | + |
| 55 | + LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n", |
| 56 | + banmap.size(), GetTimeMillis() - n_start); |
| 57 | +} |
| 58 | + |
| 59 | +void BanMan::ClearBanned() |
| 60 | +{ |
| 61 | + { |
| 62 | + LOCK(m_cs_banned); |
| 63 | + m_banned.clear(); |
| 64 | + m_is_dirty = true; |
| 65 | + } |
| 66 | + DumpBanlist(); //store banlist to disk |
| 67 | + if (m_client_interface) m_client_interface->BannedListChanged(); |
| 68 | +} |
| 69 | + |
| 70 | +bool BanMan::IsBanned(CNetAddr net_addr) |
| 71 | +{ |
| 72 | + LOCK(m_cs_banned); |
| 73 | + for (const auto& it : m_banned) { |
| 74 | + CSubNet sub_net = it.first; |
| 75 | + CBanEntry ban_entry = it.second; |
| 76 | + |
| 77 | + if (sub_net.Match(net_addr) && GetTime() < ban_entry.nBanUntil) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + return false; |
| 82 | +} |
| 83 | + |
| 84 | +bool BanMan::IsBanned(CSubNet sub_net) |
| 85 | +{ |
| 86 | + LOCK(m_cs_banned); |
| 87 | + banmap_t::iterator i = m_banned.find(sub_net); |
| 88 | + if (i != m_banned.end()) { |
| 89 | + CBanEntry ban_entry = (*i).second; |
| 90 | + if (GetTime() < ban_entry.nBanUntil) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | +} |
| 96 | + |
| 97 | +void BanMan::Ban(const CNetAddr& net_addr, const BanReason& ban_reason, int64_t ban_time_offset, bool since_unix_epoch) |
| 98 | +{ |
| 99 | + CSubNet sub_net(net_addr); |
| 100 | + Ban(sub_net, ban_reason, ban_time_offset, since_unix_epoch); |
| 101 | +} |
| 102 | + |
| 103 | +void BanMan::Ban(const CSubNet& sub_net, const BanReason& ban_reason, int64_t ban_time_offset, bool since_unix_epoch) |
| 104 | +{ |
| 105 | + CBanEntry ban_entry(GetTime(), ban_reason); |
| 106 | + |
| 107 | + int64_t normalized_ban_time_offset = ban_time_offset; |
| 108 | + bool normalized_since_unix_epoch = since_unix_epoch; |
| 109 | + if (ban_time_offset <= 0) { |
| 110 | + normalized_ban_time_offset = m_default_ban_time; |
| 111 | + normalized_since_unix_epoch = false; |
| 112 | + } |
| 113 | + ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset; |
| 114 | + |
| 115 | + { |
| 116 | + LOCK(m_cs_banned); |
| 117 | + if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) { |
| 118 | + m_banned[sub_net] = ban_entry; |
| 119 | + m_is_dirty = true; |
| 120 | + } else |
| 121 | + return; |
| 122 | + } |
| 123 | + if (m_client_interface) m_client_interface->BannedListChanged(); |
| 124 | + |
| 125 | + //store banlist to disk immediately if user requested ban |
| 126 | + if (ban_reason == BanReasonManuallyAdded) DumpBanlist(); |
| 127 | +} |
| 128 | + |
| 129 | +bool BanMan::Unban(const CNetAddr& net_addr) |
| 130 | +{ |
| 131 | + CSubNet sub_net(net_addr); |
| 132 | + return Unban(sub_net); |
| 133 | +} |
| 134 | + |
| 135 | +bool BanMan::Unban(const CSubNet& sub_net) |
| 136 | +{ |
| 137 | + { |
| 138 | + LOCK(m_cs_banned); |
| 139 | + if (m_banned.erase(sub_net) == 0) return false; |
| 140 | + m_is_dirty = true; |
| 141 | + } |
| 142 | + if (m_client_interface) m_client_interface->BannedListChanged(); |
| 143 | + DumpBanlist(); //store banlist to disk immediately |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +void BanMan::GetBanned(banmap_t& banmap) |
| 148 | +{ |
| 149 | + LOCK(m_cs_banned); |
| 150 | + // Sweep the banlist so expired bans are not returned |
| 151 | + SweepBanned(); |
| 152 | + banmap = m_banned; //create a thread safe copy |
| 153 | +} |
| 154 | + |
| 155 | +void BanMan::SetBanned(const banmap_t& banmap) |
| 156 | +{ |
| 157 | + LOCK(m_cs_banned); |
| 158 | + m_banned = banmap; |
| 159 | + m_is_dirty = true; |
| 160 | +} |
| 161 | + |
| 162 | +void BanMan::SweepBanned() |
| 163 | +{ |
| 164 | + int64_t now = GetTime(); |
| 165 | + bool notify_ui = false; |
| 166 | + { |
| 167 | + LOCK(m_cs_banned); |
| 168 | + banmap_t::iterator it = m_banned.begin(); |
| 169 | + while (it != m_banned.end()) { |
| 170 | + CSubNet sub_net = (*it).first; |
| 171 | + CBanEntry ban_entry = (*it).second; |
| 172 | + if (now > ban_entry.nBanUntil) { |
| 173 | + m_banned.erase(it++); |
| 174 | + m_is_dirty = true; |
| 175 | + notify_ui = true; |
| 176 | + LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, sub_net.ToString()); |
| 177 | + } else |
| 178 | + ++it; |
| 179 | + } |
| 180 | + } |
| 181 | + // update UI |
| 182 | + if (notify_ui && m_client_interface) { |
| 183 | + m_client_interface->BannedListChanged(); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +bool BanMan::BannedSetIsDirty() |
| 188 | +{ |
| 189 | + LOCK(m_cs_banned); |
| 190 | + return m_is_dirty; |
| 191 | +} |
| 192 | + |
| 193 | +void BanMan::SetBannedSetDirty(bool dirty) |
| 194 | +{ |
| 195 | + LOCK(m_cs_banned); //reuse m_banned lock for the m_is_dirty flag |
| 196 | + m_is_dirty = dirty; |
| 197 | +} |
0 commit comments