Skip to content

Commit facf878

Browse files
theuniFuzzbawls
authored andcommitted
net: move ban and addrman functions into CConnman
1 parent 091aaf2 commit facf878

File tree

10 files changed

+192
-135
lines changed

10 files changed

+192
-135
lines changed

src/addrman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class CAddrMan
478478
}
479479

480480
//! Return the number of (unique) addresses in all tables.
481-
int size()
481+
size_t size() const
482482
{
483483
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
484484
return vRandom.size();

src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5232,7 +5232,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
52325232
vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
52335233
pfrom->nServices = ServiceFlags(nServiceInt);
52345234
if (!pfrom->fInbound) {
5235-
addrman.SetServices(pfrom->addr, pfrom->nServices);
5235+
connman.SetServices(pfrom->addr, pfrom->nServices);
52365236
}
52375237
if (pfrom->nServicesExpected & ~pfrom->nServices) {
52385238
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, pfrom->nServices, pfrom->nServicesExpected);
@@ -5318,11 +5318,11 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
53185318
}
53195319

53205320
// Get recent addresses
5321-
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000) {
5321+
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000) {
53225322
pfrom->PushMessage(NetMsgType::GETADDR);
53235323
pfrom->fGetAddr = true;
53245324
}
5325-
addrman.Good(pfrom->addr);
5325+
connman.MarkAddressGood(pfrom->addr);
53265326
}
53275327

53285328
std::string remoteAddr;
@@ -5373,7 +5373,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
53735373
vRecv >> vAddr;
53745374

53755375
// Don't want addr from older versions unless seeding
5376-
if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
5376+
if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
53775377
return true;
53785378
if (vAddr.size() > 1000) {
53795379
LOCK(cs_main);
@@ -5422,7 +5422,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
54225422
if (fReachable)
54235423
vAddrOk.push_back(addr);
54245424
}
5425-
addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
5425+
connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
54265426
if (vAddr.size() < 1000)
54275427
pfrom->fGetAddr = false;
54285428
if (pfrom->fOneShot)
@@ -5819,7 +5819,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
58195819
// getaddr message mitigates the attack.
58205820
else if ((strCommand == NetMsgType::GETADDR) && (pfrom->fInbound)) {
58215821
pfrom->vAddrToSend.clear();
5822-
std::vector<CAddress> vAddr = addrman.GetAddr();
5822+
std::vector<CAddress> vAddr = connman.GetAddresses();
58235823
FastRandomContext insecure_rand;
58245824
for (const CAddress& addr : vAddr)
58255825
pfrom->PushAddress(addr, insecure_rand);
@@ -6229,7 +6229,7 @@ bool SendMessages(CNode* pto, CConnman& connman)
62296229
if (pto->addr.IsLocal())
62306230
LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
62316231
else {
6232-
CNode::Ban(pto->addr, BanReasonNodeMisbehaving);
6232+
connman.Ban(pto->addr, BanReasonNodeMisbehaving);
62336233
}
62346234
}
62356235
state.fShouldBan = false;

src/masternodeman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
721721
// - this is checked later by .check() in many places and by ThreadCheckObfuScationPool()
722722
if (mnb.CheckInputsAndAdd(nDoS)) {
723723
// use this as a peer
724-
addrman.Add(CAddress(mnb.addr, NODE_NETWORK), pfrom->addr, 2 * 60 * 60);
724+
g_connman->AddNewAddress(CAddress(mnb.addr, NODE_NETWORK), pfrom->addr, 2 * 60 * 60);
725725
masternodeSync.AddedMasternodeList(mnb.GetHash());
726726
} else {
727727
LogPrint(BCLog::MASTERNODE,"mnb - Rejected Masternode entry %s\n", mnb.vin.prevout.hash.ToString());

src/net.cpp

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
8383
static bool vfLimited[NET_MAX] = {};
8484
static CNode* pnodeLocalHost = NULL;
8585
uint64_t nLocalHostNonce = 0;
86-
CAddrMan addrman;
8786
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
88-
bool fAddressesInitialized = false;
8987
std::string strSubVersion;
9088

9189
std::vector<CNode*> vNodes;
@@ -456,21 +454,21 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char* pszDest, bool fCo
456454
return NULL;
457455
}
458456

459-
static void DumpBanlist()
457+
void CConnman::DumpBanlist()
460458
{
461-
CNode::SweepBanned(); // clean unused entries (if bantime has expired)
459+
SweepBanned(); // clean unused entries (if bantime has expired)
462460

463-
if (!CNode::BannedSetIsDirty())
461+
if (!BannedSetIsDirty())
464462
return;
465463

466464
int64_t nStart = GetTimeMillis();
467465

468466
CBanDB bandb;
469467
banmap_t banmap;
470-
CNode::SetBannedSetDirty(false);
471-
CNode::GetBanned(banmap);
468+
SetBannedSetDirty(false);
469+
GetBanned(banmap);
472470
if (!bandb.Write(banmap))
473-
CNode::SetBannedSetDirty(true);
471+
SetBannedSetDirty(true);
474472

475473
LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
476474
banmap.size(), GetTimeMillis() - nStart);
@@ -520,11 +518,7 @@ void CNode::PushVersion()
520518
}
521519

522520

523-
banmap_t CNode::setBanned;
524-
RecursiveMutex CNode::cs_setBanned;
525-
bool CNode::setBannedIsDirty;
526-
527-
void CNode::ClearBanned()
521+
void CConnman::ClearBanned()
528522
{
529523
{
530524
LOCK(cs_setBanned);
@@ -535,7 +529,7 @@ void CNode::ClearBanned()
535529
uiInterface.BannedListChanged();
536530
}
537531

538-
bool CNode::IsBanned(CNetAddr ip)
532+
bool CConnman::IsBanned(CNetAddr ip)
539533
{
540534
bool fResult = false;
541535
{
@@ -552,7 +546,7 @@ bool CNode::IsBanned(CNetAddr ip)
552546
return fResult;
553547
}
554548

555-
bool CNode::IsBanned(CSubNet subnet)
549+
bool CConnman::IsBanned(CSubNet subnet)
556550
{
557551
bool fResult = false;
558552
{
@@ -567,13 +561,13 @@ bool CNode::IsBanned(CSubNet subnet)
567561
return fResult;
568562
}
569563

570-
void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch)
564+
void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch)
571565
{
572566
CSubNet subNet(addr);
573567
Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
574568
}
575569

576-
void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch)
570+
void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch)
577571
{
578572
CBanEntry banEntry(GetTime());
579573
banEntry.banReason = banReason;
@@ -605,13 +599,13 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti
605599
DumpBanlist(); //store banlist to disk immediately if user requested ban
606600
}
607601

608-
bool CNode::Unban(const CNetAddr &addr)
602+
bool CConnman::Unban(const CNetAddr &addr)
609603
{
610604
CSubNet subNet(addr);
611605
return Unban(subNet);
612606
}
613607

614-
bool CNode::Unban(const CSubNet &subNet)
608+
bool CConnman::Unban(const CSubNet &subNet)
615609
{
616610
{
617611
LOCK(cs_setBanned);
@@ -624,20 +618,20 @@ bool CNode::Unban(const CSubNet &subNet)
624618
return true;
625619
}
626620

627-
void CNode::GetBanned(banmap_t &banMap)
621+
void CConnman::GetBanned(banmap_t &banMap)
628622
{
629623
LOCK(cs_setBanned);
630624
banMap = setBanned; //create a thread safe copy
631625
}
632626

633-
void CNode::SetBanned(const banmap_t &banMap)
627+
void CConnman::SetBanned(const banmap_t &banMap)
634628
{
635629
LOCK(cs_setBanned);
636630
setBanned = banMap;
637631
setBannedIsDirty = true;
638632
}
639633

640-
void CNode::SweepBanned()
634+
void CConnman::SweepBanned()
641635
{
642636
int64_t now = GetTime();
643637

@@ -666,13 +660,13 @@ void CNode::SweepBanned()
666660
}
667661
}
668662

669-
bool CNode::BannedSetIsDirty()
663+
bool CConnman::BannedSetIsDirty()
670664
{
671665
LOCK(cs_setBanned);
672666
return setBannedIsDirty;
673667
}
674668

675-
void CNode::SetBannedSetDirty(bool dirty)
669+
void CConnman::SetBannedSetDirty(bool dirty)
676670
{
677671
LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
678672
setBannedIsDirty = dirty;
@@ -1046,7 +1040,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10461040
return;
10471041
}
10481042

1049-
if (CNode::IsBanned(addr) && !whitelisted) {
1043+
if (IsBanned(addr) && !whitelisted) {
10501044
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
10511045
CloseSocket(hSocket);
10521046
return;
@@ -1488,7 +1482,17 @@ void CConnman::ThreadDNSAddressSeed()
14881482
}
14891483

14901484

1491-
void DumpAddresses()
1485+
1486+
1487+
1488+
1489+
1490+
1491+
1492+
1493+
1494+
1495+
void CConnman::DumpAddresses()
14921496
{
14931497
int64_t nStart = GetTimeMillis();
14941498

@@ -1499,7 +1503,7 @@ void DumpAddresses()
14991503
addrman.size(), GetTimeMillis() - nStart);
15001504
}
15011505

1502-
void DumpData()
1506+
void CConnman::DumpData()
15031507
{
15041508
DumpAddresses();
15051509
DumpBanlist();
@@ -1740,7 +1744,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
17401744
boost::this_thread::interruption_point();
17411745
if (!pszDest) {
17421746
if (IsLocal(addrConnect) ||
1743-
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
1747+
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
17441748
FindNode(addrConnect.ToStringIPPort()))
17451749
return false;
17461750
} else if (FindNode(pszDest))
@@ -1954,10 +1958,22 @@ void static Discover(boost::thread_group& threadGroup)
19541958

19551959
CConnman::CConnman()
19561960
{
1961+
setBannedIsDirty = false;
1962+
fAddressesInitialized = false;
19571963
}
19581964

19591965
bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError)
19601966
{
1967+
Discover(threadGroup);
1968+
1969+
bool ret = connman.Start(threadGroup, scheduler, strNodeError);
1970+
1971+
return ret;
1972+
}
1973+
1974+
bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError)
1975+
{
1976+
19611977
uiInterface.InitMessage(_("Loading addresses..."));
19621978
// Load addresses from peers.dat
19631979
int64_t nStart = GetTimeMillis();
@@ -1978,15 +1994,15 @@ bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler&
19781994
CBanDB bandb;
19791995
banmap_t banmap;
19801996
if (bandb.Read(banmap)) {
1981-
CNode::SetBanned(banmap); // thread save setter
1982-
CNode::SetBannedSetDirty(false); // no need to write down, just read data
1983-
CNode::SweepBanned(); // sweep out unused entries
1997+
SetBanned(banmap); // thread save setter
1998+
SetBannedSetDirty(false); // no need to write down, just read data
1999+
SweepBanned(); // sweep out unused entries
19842000

19852001
LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
19862002
banmap.size(), GetTimeMillis() - nStart);
19872003
} else {
19882004
LogPrintf("Invalid or missing banlist.dat; recreating\n");
1989-
CNode::SetBannedSetDirty(true); // force write
2005+
SetBannedSetDirty(true); // force write
19902006
DumpBanlist();
19912007
}
19922008

@@ -1996,17 +2012,6 @@ bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler&
19962012

19972013
fAddressesInitialized = true;
19982014

1999-
Discover(threadGroup);
2000-
2001-
bool ret = connman.Start(threadGroup, strNodeError);
2002-
2003-
// Dump network addresses
2004-
scheduler.scheduleEvery(DumpData, DUMP_ADDRESSES_INTERVAL);
2005-
return ret;
2006-
}
2007-
2008-
bool CConnman::Start(boost::thread_group& threadGroup, std::string& strNodeError)
2009-
{
20102015
if (semOutbound == NULL) {
20112016
// initialize semaphore
20122017
int nMaxOutbound = std::min((MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS), nMaxConnections);
@@ -2045,6 +2050,9 @@ bool CConnman::Start(boost::thread_group& threadGroup, std::string& strNodeError
20452050
// Process messages
20462051
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "msghand", boost::function<void()>(boost::bind(&CConnman::ThreadMessageHandler, this))));
20472052

2053+
// Dump network addresses
2054+
scheduler.scheduleEvery(boost::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL);
2055+
20482056
return true;
20492057
}
20502058

@@ -2053,11 +2061,6 @@ bool StopNode(CConnman& connman)
20532061
LogPrintf("StopNode()\n");
20542062
MapPort(false);
20552063

2056-
if (fAddressesInitialized) {
2057-
DumpData();
2058-
fAddressesInitialized = false;
2059-
}
2060-
20612064
connman.Stop();
20622065
return true;
20632066
}
@@ -2091,6 +2094,12 @@ void CConnman::Stop()
20912094
for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++)
20922095
semOutbound->post();
20932096

2097+
if (fAddressesInitialized)
2098+
{
2099+
DumpData();
2100+
fAddressesInitialized = false;
2101+
}
2102+
20942103
// Close sockets
20952104
for(CNode* pnode : vNodes)
20962105
if (pnode->hSocket != INVALID_SOCKET)
@@ -2131,6 +2140,36 @@ CConnman::~CConnman()
21312140
{
21322141
}
21332142

2143+
size_t CConnman::GetAddressCount() const
2144+
{
2145+
return addrman.size();
2146+
}
2147+
2148+
void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2149+
{
2150+
addrman.SetServices(addr, nServices);
2151+
}
2152+
2153+
void CConnman::MarkAddressGood(const CAddress& addr)
2154+
{
2155+
addrman.Good(addr);
2156+
}
2157+
2158+
void CConnman::AddNewAddress(const CAddress& addr, const CAddress& addrFrom, int64_t nTimePenalty)
2159+
{
2160+
addrman.Add(addr, addrFrom, nTimePenalty);
2161+
}
2162+
2163+
void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2164+
{
2165+
addrman.Add(vAddr, addrFrom, nTimePenalty);
2166+
}
2167+
2168+
std::vector<CAddress> CConnman::GetAddresses()
2169+
{
2170+
return addrman.GetAddr();
2171+
}
2172+
21342173
void RelayTransaction(const CTransaction& tx)
21352174
{
21362175
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);

0 commit comments

Comments
 (0)