44
44
// Dump addresses to peers.dat every 15 minutes (900s)
45
45
static constexpr int DUMP_PEERS_INTERVAL = 15 * 60 ;
46
46
47
- // Dump addresses to banlist.dat every 15 minutes (900s)
48
- static constexpr int DUMP_BANS_INTERVAL = 60 * 15 ;
49
-
50
47
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
51
48
#define FEELER_SLEEP_WINDOW 1
52
49
@@ -460,7 +457,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
460
457
return pnode;
461
458
}
462
459
463
- void CConnman ::DumpBanlist ()
460
+ void BanMan ::DumpBanlist ()
464
461
{
465
462
SweepBanned (); // clean unused entries (if bantime has expired)
466
463
@@ -491,7 +488,7 @@ void CNode::CloseSocketDisconnect()
491
488
}
492
489
}
493
490
494
- void CConnman ::ClearBanned ()
491
+ void BanMan ::ClearBanned ()
495
492
{
496
493
{
497
494
LOCK (cs_setBanned);
@@ -503,7 +500,7 @@ void CConnman::ClearBanned()
503
500
clientInterface->BannedListChanged ();
504
501
}
505
502
506
- bool CConnman ::IsBanned (CNetAddr ip)
503
+ bool BanMan ::IsBanned (CNetAddr ip)
507
504
{
508
505
LOCK (cs_setBanned);
509
506
for (const auto & it : setBanned) {
@@ -517,7 +514,7 @@ bool CConnman::IsBanned(CNetAddr ip)
517
514
return false ;
518
515
}
519
516
520
- bool CConnman ::IsBanned (CSubNet subnet)
517
+ bool BanMan ::IsBanned (CSubNet subnet)
521
518
{
522
519
LOCK (cs_setBanned);
523
520
banmap_t ::iterator i = setBanned.find (subnet);
@@ -531,12 +528,12 @@ bool CConnman::IsBanned(CSubNet subnet)
531
528
return false ;
532
529
}
533
530
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) {
535
532
CSubNet subNet (addr);
536
533
Ban (subNet, banReason, bantimeoffset, sinceUnixEpoch);
537
534
}
538
535
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) {
540
537
CBanEntry banEntry (GetTime ());
541
538
banEntry.banReason = banReason;
542
539
if (bantimeoffset <= 0 )
@@ -561,12 +558,12 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
561
558
DumpBanlist (); // store banlist to disk immediately if user requested ban
562
559
}
563
560
564
- bool CConnman ::Unban (const CNetAddr &addr) {
561
+ bool BanMan ::Unban (const CNetAddr &addr) {
565
562
CSubNet subNet (addr);
566
563
return Unban (subNet);
567
564
}
568
565
569
- bool CConnman ::Unban (const CSubNet &subNet) {
566
+ bool BanMan ::Unban (const CSubNet &subNet) {
570
567
{
571
568
LOCK (cs_setBanned);
572
569
if (!setBanned.erase (subNet))
@@ -579,22 +576,22 @@ bool CConnman::Unban(const CSubNet &subNet) {
579
576
return true ;
580
577
}
581
578
582
- void CConnman ::GetBanned (banmap_t &banMap)
579
+ void BanMan ::GetBanned (banmap_t &banMap)
583
580
{
584
581
LOCK (cs_setBanned);
585
582
// Sweep the banlist so expired bans are not returned
586
583
SweepBanned ();
587
584
banMap = setBanned; // create a thread safe copy
588
585
}
589
586
590
- void CConnman ::SetBanned (const banmap_t &banMap)
587
+ void BanMan ::SetBanned (const banmap_t &banMap)
591
588
{
592
589
LOCK (cs_setBanned);
593
590
setBanned = banMap;
594
591
setBannedIsDirty = true ;
595
592
}
596
593
597
- void CConnman ::SweepBanned ()
594
+ void BanMan ::SweepBanned ()
598
595
{
599
596
int64_t now = GetTime ();
600
597
bool notifyUI = false ;
@@ -622,13 +619,13 @@ void CConnman::SweepBanned()
622
619
}
623
620
}
624
621
625
- bool CConnman ::BannedSetIsDirty ()
622
+ bool BanMan ::BannedSetIsDirty ()
626
623
{
627
624
LOCK (cs_setBanned);
628
625
return setBannedIsDirty;
629
626
}
630
627
631
- void CConnman ::SetBannedSetDirty (bool dirty)
628
+ void BanMan ::SetBannedSetDirty (bool dirty)
632
629
{
633
630
LOCK (cs_setBanned); // reuse setBanned lock for the isDirty flag
634
631
setBannedIsDirty = dirty;
@@ -1103,7 +1100,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1103
1100
// on all platforms. Set it again here just to be sure.
1104
1101
SetSocketNoDelay (hSocket);
1105
1102
1106
- if (IsBanned (addr) && !whitelisted)
1103
+ if (m_banman && m_banman-> IsBanned (addr) && !whitelisted)
1107
1104
{
1108
1105
LogPrint (BCLog::NET, " connection from %s dropped (banned)\n " , addr.ToString ());
1109
1106
CloseSocket (hSocket);
@@ -2075,7 +2072,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
2075
2072
}
2076
2073
if (!pszDest) {
2077
2074
if (IsLocal (addrConnect) ||
2078
- FindNode (static_cast <CNetAddr>(addrConnect)) || IsBanned (addrConnect) ||
2075
+ FindNode (static_cast <CNetAddr>(addrConnect)) || (m_banman && m_banman-> IsBanned (addrConnect) ) ||
2079
2076
FindNode (addrConnect.ToStringIPPort ()))
2080
2077
return ;
2081
2078
} else if (FindNode (std::string (pszDest)))
@@ -2376,24 +2373,6 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2376
2373
DumpAddresses ();
2377
2374
}
2378
2375
}
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
- }
2397
2376
2398
2377
uiInterface.InitMessage (_ (" Starting network threads..." ));
2399
2378
@@ -2448,11 +2427,38 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2448
2427
2449
2428
// Dump network addresses
2450
2429
scheduler.scheduleEvery (std::bind (&CConnman::DumpAddresses, this ), DUMP_PEERS_INTERVAL * 1000 );
2451
- scheduler.scheduleEvery (std::bind (&CConnman::DumpBanlist, this ), DUMP_BANS_INTERVAL * 1000 );
2452
2430
2453
2431
return true ;
2454
2432
}
2455
2433
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
+
2456
2462
class CNetCleanup
2457
2463
{
2458
2464
public:
@@ -2508,7 +2514,6 @@ void CConnman::Stop()
2508
2514
if (fAddressesInitialized )
2509
2515
{
2510
2516
DumpAddresses ();
2511
- DumpBanlist ();
2512
2517
fAddressesInitialized = false ;
2513
2518
}
2514
2519
0 commit comments