mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
76279c1a37
ab7538832043a6c15e45a178fb6bb6298a00108f refactor: Remove redundant scope in `BanMan::SweepBanned()` (Hennadii Stepanov)
52c0b3e859089c0ac98e7261ded6391b4f8eeeaf refactor: Add thread safety annotation to `BanMan::SweepBanned()` (Hennadii Stepanov)
3919059deb60d6ead9defc9d213a3f0c2ab72e90 refactor: Move code from ctor into private `BanMan::LoadBanlist()` (Hennadii Stepanov)
Pull request description:
This PR adds a proper thread safety annotation to `BanMan::SweepBanned()`.
Also a simple refactoring applied.
ACKs for top commit:
ajtowns:
ACK ab7538832043a6c15e45a178fb6bb6298a00108f
w0xlt:
ACK ab75388320
theStack:
Code-review ACK ab7538832043a6c15e45a178fb6bb6298a00108f
Tree-SHA512: 8699079c308454f3ffe72be2e77f0935214156bd509f9338b1104f8d128bfdd02ee06ee8c1c99b2eefdf317a00edd555d52990caaeb1ed4540eedc1e3c9d1faf
101 lines
4.0 KiB
C++
101 lines
4.0 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
#ifndef BITCOIN_BANMAN_H
|
|
#define BITCOIN_BANMAN_H
|
|
|
|
#include <addrdb.h>
|
|
#include <bloom.h>
|
|
#include <fs.h>
|
|
#include <net_types.h> // For banmap_t
|
|
#include <sync.h>
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
|
|
// NOTE: When adjusting this, update rpcnet:setban's help ("24h")
|
|
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
|
|
|
|
/// How often to dump banned addresses/subnets to disk.
|
|
static constexpr std::chrono::minutes DUMP_BANS_INTERVAL{15};
|
|
|
|
class CClientUIInterface;
|
|
class CNetAddr;
|
|
class CSubNet;
|
|
|
|
// Banman manages two related but distinct concepts:
|
|
//
|
|
// 1. Banning. This is configured manually by the user, through the setban RPC.
|
|
// If an address or subnet is banned, we never accept incoming connections from
|
|
// it and never create outgoing connections to it. We won't gossip its address
|
|
// to other peers in addr messages. Banned addresses and subnets are stored to
|
|
// disk on shutdown and reloaded on startup. Banning can be used to
|
|
// prevent connections with spy nodes or other griefers.
|
|
//
|
|
// 2. Discouragement. If a peer misbehaves enough (see Misbehaving() in
|
|
// net_processing.cpp), we'll mark that address as discouraged. We still allow
|
|
// incoming connections from them, but they're preferred for eviction when
|
|
// we receive new incoming connections. We never make outgoing connections to
|
|
// them, and do not gossip their address to other peers. This is implemented as
|
|
// a bloom filter. We can (probabilistically) test for membership, but can't
|
|
// list all discouraged addresses or unmark them as discouraged. Discouragement
|
|
// can prevent our limited connection slots being used up by incompatible
|
|
// or broken peers.
|
|
//
|
|
// Neither banning nor discouragement are protections against denial-of-service
|
|
// attacks, since if an attacker has a way to waste our resources and we
|
|
// disconnect from them and ban that address, it's trivial for them to
|
|
// reconnect from another IP address.
|
|
//
|
|
// Attempting to automatically disconnect or ban any class of peer carries the
|
|
// risk of splitting the network. For example, if we banned/disconnected for a
|
|
// transaction that fails a policy check and a future version changes the
|
|
// policy check so the transaction is accepted, then that transaction could
|
|
// cause the network to split between old nodes and new nodes.
|
|
|
|
class BanMan
|
|
{
|
|
public:
|
|
~BanMan();
|
|
BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time);
|
|
void Ban(const CNetAddr& net_addr, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
|
|
void Ban(const CSubNet& sub_net, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
|
|
void Discourage(const CNetAddr& net_addr);
|
|
void ClearBanned();
|
|
void ClearDiscouraged();
|
|
|
|
//! Return whether net_addr is banned
|
|
bool IsBanned(const CNetAddr& net_addr);
|
|
|
|
//! Return whether sub_net is exactly banned
|
|
bool IsBanned(const CSubNet& sub_net);
|
|
|
|
//! Return whether net_addr is discouraged.
|
|
bool IsDiscouraged(const CNetAddr& net_addr);
|
|
|
|
bool Unban(const CNetAddr& net_addr);
|
|
bool Unban(const CSubNet& sub_net);
|
|
void GetBanned(banmap_t& banmap);
|
|
void DumpBanlist();
|
|
|
|
private:
|
|
void LoadBanlist() EXCLUSIVE_LOCKS_REQUIRED(!m_cs_banned);
|
|
bool BannedSetIsDirty();
|
|
//!set the "dirty" flag for the banlist
|
|
void SetBannedSetDirty(bool dirty = true);
|
|
//!clean unused entries (if bantime has expired)
|
|
void SweepBanned() EXCLUSIVE_LOCKS_REQUIRED(m_cs_banned);
|
|
|
|
RecursiveMutex m_cs_banned;
|
|
banmap_t m_banned GUARDED_BY(m_cs_banned);
|
|
bool m_is_dirty GUARDED_BY(m_cs_banned){false};
|
|
CClientUIInterface* m_client_interface = nullptr;
|
|
CBanDB m_ban_db;
|
|
const int64_t m_default_ban_time;
|
|
CRollingBloomFilter m_discouraged GUARDED_BY(m_cs_banned) {50000, 0.000001};
|
|
};
|
|
|
|
#endif
|