|
| 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 | + : clientInterface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time) |
| 16 | +{ |
| 17 | + if (clientInterface) clientInterface->InitMessage(_("Loading banlist...")); |
| 18 | + |
| 19 | + int64_t nStart = GetTimeMillis(); |
| 20 | + setBannedIsDirty = 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() - nStart); |
| 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 nStart = 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() - nStart); |
| 57 | +} |
| 58 | + |
| 59 | +void BanMan::ClearBanned() |
| 60 | +{ |
| 61 | + { |
| 62 | + LOCK(cs_setBanned); |
| 63 | + setBanned.clear(); |
| 64 | + setBannedIsDirty = true; |
| 65 | + } |
| 66 | + DumpBanlist(); //store banlist to disk |
| 67 | + if (clientInterface) clientInterface->BannedListChanged(); |
| 68 | +} |
| 69 | + |
| 70 | +bool BanMan::IsBanned(CNetAddr netAddr) |
| 71 | +{ |
| 72 | + LOCK(cs_setBanned); |
| 73 | + for (const auto& it : setBanned) { |
| 74 | + CSubNet subNet = it.first; |
| 75 | + CBanEntry banEntry = it.second; |
| 76 | + |
| 77 | + if (subNet.Match(netAddr) && GetTime() < banEntry.nBanUntil) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + return false; |
| 82 | +} |
| 83 | + |
| 84 | +bool BanMan::IsBanned(CSubNet subNet) |
| 85 | +{ |
| 86 | + LOCK(cs_setBanned); |
| 87 | + banmap_t::iterator i = setBanned.find(subNet); |
| 88 | + if (i != setBanned.end()) { |
| 89 | + CBanEntry banEntry = (*i).second; |
| 90 | + if (GetTime() < banEntry.nBanUntil) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | +} |
| 96 | + |
| 97 | +void BanMan::Ban(const CNetAddr& netAddr, const BanReason& banReason, int64_t bantimeoffset, bool sinceUnixEpoch) |
| 98 | +{ |
| 99 | + CSubNet subNet(netAddr); |
| 100 | + Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch); |
| 101 | +} |
| 102 | + |
| 103 | +void BanMan::Ban(const CSubNet& subNet, const BanReason& banReason, int64_t bantimeoffset, bool sinceUnixEpoch) |
| 104 | +{ |
| 105 | + CBanEntry banEntry(GetTime()); |
| 106 | + banEntry.banReason = banReason; |
| 107 | + if (bantimeoffset <= 0) { |
| 108 | + bantimeoffset = m_default_ban_time; |
| 109 | + sinceUnixEpoch = false; |
| 110 | + } |
| 111 | + banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime()) + bantimeoffset; |
| 112 | + |
| 113 | + { |
| 114 | + LOCK(cs_setBanned); |
| 115 | + if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) { |
| 116 | + setBanned[subNet] = banEntry; |
| 117 | + setBannedIsDirty = true; |
| 118 | + } else |
| 119 | + return; |
| 120 | + } |
| 121 | + if (clientInterface) clientInterface->BannedListChanged(); |
| 122 | + |
| 123 | + //store banlist to disk immediately if user requested ban |
| 124 | + if (banReason == BanReasonManuallyAdded) DumpBanlist(); |
| 125 | +} |
| 126 | + |
| 127 | +bool BanMan::Unban(const CNetAddr& netAddr) |
| 128 | +{ |
| 129 | + CSubNet subNet(netAddr); |
| 130 | + return Unban(subNet); |
| 131 | +} |
| 132 | + |
| 133 | +bool BanMan::Unban(const CSubNet& subNet) |
| 134 | +{ |
| 135 | + { |
| 136 | + LOCK(cs_setBanned); |
| 137 | + if (setBanned.erase(subNet) == 0) return false; |
| 138 | + setBannedIsDirty = true; |
| 139 | + } |
| 140 | + if (clientInterface) clientInterface->BannedListChanged(); |
| 141 | + DumpBanlist(); //store banlist to disk immediately |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +void BanMan::GetBanned(banmap_t& banMap) |
| 146 | +{ |
| 147 | + LOCK(cs_setBanned); |
| 148 | + // Sweep the banlist so expired bans are not returned |
| 149 | + SweepBanned(); |
| 150 | + banMap = setBanned; //create a thread safe copy |
| 151 | +} |
| 152 | + |
| 153 | +void BanMan::SetBanned(const banmap_t& banMap) |
| 154 | +{ |
| 155 | + LOCK(cs_setBanned); |
| 156 | + setBanned = banMap; |
| 157 | + setBannedIsDirty = true; |
| 158 | +} |
| 159 | + |
| 160 | +void BanMan::SweepBanned() |
| 161 | +{ |
| 162 | + int64_t now = GetTime(); |
| 163 | + bool notifyUI = false; |
| 164 | + { |
| 165 | + LOCK(cs_setBanned); |
| 166 | + banmap_t::iterator it = setBanned.begin(); |
| 167 | + while (it != setBanned.end()) { |
| 168 | + CSubNet subNet = (*it).first; |
| 169 | + CBanEntry banEntry = (*it).second; |
| 170 | + if (now > banEntry.nBanUntil) { |
| 171 | + setBanned.erase(it++); |
| 172 | + setBannedIsDirty = true; |
| 173 | + notifyUI = true; |
| 174 | + LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString()); |
| 175 | + } else |
| 176 | + ++it; |
| 177 | + } |
| 178 | + } |
| 179 | + // update UI |
| 180 | + if (notifyUI && clientInterface) { |
| 181 | + clientInterface->BannedListChanged(); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +bool BanMan::BannedSetIsDirty() |
| 186 | +{ |
| 187 | + LOCK(cs_setBanned); |
| 188 | + return setBannedIsDirty; |
| 189 | +} |
| 190 | + |
| 191 | +void BanMan::SetBannedSetDirty(bool dirty) |
| 192 | +{ |
| 193 | + LOCK(cs_setBanned); //reuse setBanned lock for the setBannedIsDirty flag |
| 194 | + setBannedIsDirty = dirty; |
| 195 | +} |
0 commit comments