Skip to content

Commit 4c0d961

Browse files
theunidongcarl
authored andcommitted
banman: create and split out banman
Some say he has always been.
1 parent 83c1ea2 commit 4c0d961

File tree

10 files changed

+172
-125
lines changed

10 files changed

+172
-125
lines changed

src/init.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ static const bool DEFAULT_PROXYRANDOMIZE = true;
7373
static const bool DEFAULT_REST_ENABLE = false;
7474
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
7575

76+
// Dump addresses to banlist.dat every 15 minutes (900s)
77+
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;
78+
7679
std::unique_ptr<CConnman> g_connman;
7780
std::unique_ptr<PeerLogicValidation> peerLogic;
81+
std::unique_ptr<BanMan> g_banman;
7882

7983
#ifdef WIN32
8084
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
@@ -199,6 +203,7 @@ void Shutdown(InitInterfaces& interfaces)
199203
// destruct and reset all to nullptr.
200204
peerLogic.reset();
201205
g_connman.reset();
206+
g_banman.reset();
202207
g_txindex.reset();
203208

204209
if (g_is_mempool_loaded && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
@@ -1290,11 +1295,12 @@ bool AppInitMain(InitInterfaces& interfaces)
12901295
// is not yet setup and may end up being set up twice if we
12911296
// need to reindex later.
12921297

1298+
assert(!g_banman);
1299+
g_banman = MakeUnique<BanMan>(&uiInterface);
12931300
assert(!g_connman);
12941301
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
1295-
CConnman& connman = *g_connman;
12961302

1297-
peerLogic.reset(new PeerLogicValidation(&connman, scheduler, gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)));
1303+
peerLogic.reset(new PeerLogicValidation(g_connman.get(), g_banman.get(), scheduler, gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)));
12981304
RegisterValidationInterface(peerLogic.get());
12991305

13001306
// sanitize comments per BIP-0014, format user agent and check total size
@@ -1704,6 +1710,7 @@ bool AppInitMain(InitInterfaces& interfaces)
17041710
connOptions.nMaxFeeler = 1;
17051711
connOptions.nBestHeight = chain_active_height;
17061712
connOptions.uiInterface = &uiInterface;
1713+
connOptions.m_banman = g_banman.get();
17071714
connOptions.m_msgproc = peerLogic.get();
17081715
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
17091716
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
@@ -1749,7 +1756,7 @@ bool AppInitMain(InitInterfaces& interfaces)
17491756
connOptions.m_specified_outgoing = connect;
17501757
}
17511758
}
1752-
if (!connman.Start(scheduler, connOptions)) {
1759+
if (!g_connman->Start(scheduler, connOptions)) {
17531760
return false;
17541761
}
17551762

@@ -1762,5 +1769,9 @@ bool AppInitMain(InitInterfaces& interfaces)
17621769
client->start(scheduler);
17631770
}
17641771

1772+
scheduler.scheduleEvery([]{
1773+
g_banman->DumpBanlist();
1774+
}, DUMP_BANS_INTERVAL * 1000);
1775+
17651776
return true;
17661777
}

src/interfaces/node.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,24 @@ class NodeImpl : public Node
122122
}
123123
bool getBanned(banmap_t& banmap) override
124124
{
125-
if (g_connman) {
126-
g_connman->GetBanned(banmap);
125+
if (g_banman) {
126+
g_banman->GetBanned(banmap);
127127
return true;
128128
}
129129
return false;
130130
}
131131
bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) override
132132
{
133-
if (g_connman) {
134-
g_connman->Ban(net_addr, reason, ban_time_offset);
133+
if (g_banman) {
134+
g_banman->Ban(net_addr, reason, ban_time_offset);
135135
return true;
136136
}
137137
return false;
138138
}
139139
bool unban(const CSubNet& ip) override
140140
{
141-
if (g_connman) {
142-
g_connman->Unban(ip);
141+
if (g_banman) {
142+
g_banman->Unban(ip);
143143
return true;
144144
}
145145
return false;

src/net.cpp

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
// Dump addresses to peers.dat every 15 minutes (900s)
4545
static constexpr int DUMP_PEERS_INTERVAL = 15 * 60;
4646

47-
// Dump addresses to banlist.dat every 15 minutes (900s)
48-
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;
49-
5047
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
5148
#define FEELER_SLEEP_WINDOW 1
5249

@@ -460,7 +457,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
460457
return pnode;
461458
}
462459

463-
void CConnman::DumpBanlist()
460+
void BanMan::DumpBanlist()
464461
{
465462
SweepBanned(); // clean unused entries (if bantime has expired)
466463

@@ -491,7 +488,7 @@ void CNode::CloseSocketDisconnect()
491488
}
492489
}
493490

494-
void CConnman::ClearBanned()
491+
void BanMan::ClearBanned()
495492
{
496493
{
497494
LOCK(cs_setBanned);
@@ -503,7 +500,7 @@ void CConnman::ClearBanned()
503500
clientInterface->BannedListChanged();
504501
}
505502

506-
bool CConnman::IsBanned(CNetAddr ip)
503+
bool BanMan::IsBanned(CNetAddr ip)
507504
{
508505
LOCK(cs_setBanned);
509506
for (const auto& it : setBanned) {
@@ -517,7 +514,7 @@ bool CConnman::IsBanned(CNetAddr ip)
517514
return false;
518515
}
519516

520-
bool CConnman::IsBanned(CSubNet subnet)
517+
bool BanMan::IsBanned(CSubNet subnet)
521518
{
522519
LOCK(cs_setBanned);
523520
banmap_t::iterator i = setBanned.find(subnet);
@@ -531,12 +528,12 @@ bool CConnman::IsBanned(CSubNet subnet)
531528
return false;
532529
}
533530

534-
void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
531+
void BanMan::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
535532
CSubNet subNet(addr);
536533
Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
537534
}
538535

539-
void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
536+
void BanMan::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
540537
CBanEntry banEntry(GetTime());
541538
banEntry.banReason = banReason;
542539
if (bantimeoffset <= 0)
@@ -561,12 +558,12 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
561558
DumpBanlist(); //store banlist to disk immediately if user requested ban
562559
}
563560

564-
bool CConnman::Unban(const CNetAddr &addr) {
561+
bool BanMan::Unban(const CNetAddr &addr) {
565562
CSubNet subNet(addr);
566563
return Unban(subNet);
567564
}
568565

569-
bool CConnman::Unban(const CSubNet &subNet) {
566+
bool BanMan::Unban(const CSubNet &subNet) {
570567
{
571568
LOCK(cs_setBanned);
572569
if (!setBanned.erase(subNet))
@@ -579,22 +576,22 @@ bool CConnman::Unban(const CSubNet &subNet) {
579576
return true;
580577
}
581578

582-
void CConnman::GetBanned(banmap_t &banMap)
579+
void BanMan::GetBanned(banmap_t &banMap)
583580
{
584581
LOCK(cs_setBanned);
585582
// Sweep the banlist so expired bans are not returned
586583
SweepBanned();
587584
banMap = setBanned; //create a thread safe copy
588585
}
589586

590-
void CConnman::SetBanned(const banmap_t &banMap)
587+
void BanMan::SetBanned(const banmap_t &banMap)
591588
{
592589
LOCK(cs_setBanned);
593590
setBanned = banMap;
594591
setBannedIsDirty = true;
595592
}
596593

597-
void CConnman::SweepBanned()
594+
void BanMan::SweepBanned()
598595
{
599596
int64_t now = GetTime();
600597
bool notifyUI = false;
@@ -622,13 +619,13 @@ void CConnman::SweepBanned()
622619
}
623620
}
624621

625-
bool CConnman::BannedSetIsDirty()
622+
bool BanMan::BannedSetIsDirty()
626623
{
627624
LOCK(cs_setBanned);
628625
return setBannedIsDirty;
629626
}
630627

631-
void CConnman::SetBannedSetDirty(bool dirty)
628+
void BanMan::SetBannedSetDirty(bool dirty)
632629
{
633630
LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
634631
setBannedIsDirty = dirty;
@@ -1103,7 +1100,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
11031100
// on all platforms. Set it again here just to be sure.
11041101
SetSocketNoDelay(hSocket);
11051102

1106-
if (IsBanned(addr) && !whitelisted)
1103+
if (m_banman && m_banman->IsBanned(addr) && !whitelisted)
11071104
{
11081105
LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
11091106
CloseSocket(hSocket);
@@ -2075,7 +2072,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
20752072
}
20762073
if (!pszDest) {
20772074
if (IsLocal(addrConnect) ||
2078-
FindNode(static_cast<CNetAddr>(addrConnect)) || IsBanned(addrConnect) ||
2075+
FindNode(static_cast<CNetAddr>(addrConnect)) || (m_banman && m_banman->IsBanned(addrConnect)) ||
20792076
FindNode(addrConnect.ToStringIPPort()))
20802077
return;
20812078
} else if (FindNode(std::string(pszDest)))
@@ -2376,24 +2373,6 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23762373
DumpAddresses();
23772374
}
23782375
}
2379-
if (clientInterface)
2380-
clientInterface->InitMessage(_("Loading banlist..."));
2381-
// Load addresses from banlist.dat
2382-
nStart = GetTimeMillis();
2383-
CBanDB bandb;
2384-
banmap_t banmap;
2385-
if (bandb.Read(banmap)) {
2386-
SetBanned(banmap); // thread save setter
2387-
SetBannedSetDirty(false); // no need to write down, just read data
2388-
SweepBanned(); // sweep out unused entries
2389-
2390-
LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2391-
banmap.size(), GetTimeMillis() - nStart);
2392-
} else {
2393-
LogPrintf("Invalid or missing banlist.dat; recreating\n");
2394-
SetBannedSetDirty(true); // force write
2395-
DumpBanlist();
2396-
}
23972376

23982377
uiInterface.InitMessage(_("Starting network threads..."));
23992378

@@ -2448,11 +2427,38 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
24482427

24492428
// Dump network addresses
24502429
scheduler.scheduleEvery(std::bind(&CConnman::DumpAddresses, this), DUMP_PEERS_INTERVAL * 1000);
2451-
scheduler.scheduleEvery(std::bind(&CConnman::DumpBanlist, this), DUMP_BANS_INTERVAL * 1000);
24522430

24532431
return true;
24542432
}
24552433

2434+
BanMan::BanMan(CClientUIInterface* client_interface) : clientInterface(client_interface)
2435+
{
2436+
if (clientInterface) clientInterface->InitMessage(_("Loading banlist..."));
2437+
// Load addresses from banlist.dat
2438+
2439+
int64_t nStart = GetTimeMillis();
2440+
setBannedIsDirty = false;
2441+
CBanDB bandb;
2442+
banmap_t banmap;
2443+
if (bandb.Read(banmap)) {
2444+
SetBanned(banmap); // thread save setter
2445+
SetBannedSetDirty(false); // no need to write down, just read data
2446+
SweepBanned(); // sweep out unused entries
2447+
2448+
LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2449+
banmap.size(), GetTimeMillis() - nStart);
2450+
} else {
2451+
LogPrintf("Invalid or missing banlist.dat; recreating\n");
2452+
SetBannedSetDirty(true); // force write
2453+
DumpBanlist();
2454+
}
2455+
}
2456+
2457+
BanMan::~BanMan()
2458+
{
2459+
DumpBanlist();
2460+
}
2461+
24562462
class CNetCleanup
24572463
{
24582464
public:
@@ -2508,7 +2514,6 @@ void CConnman::Stop()
25082514
if (fAddressesInitialized)
25092515
{
25102516
DumpAddresses();
2511-
DumpBanlist();
25122517
fAddressesInitialized = false;
25132518
}
25142519

0 commit comments

Comments
 (0)