Skip to content

Commit a0f3d3c

Browse files
committed
net: move ban and addrman functions into CConnman
1 parent aaf018e commit a0f3d3c

File tree

7 files changed

+174
-132
lines changed

7 files changed

+174
-132
lines changed

src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4944,7 +4944,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
49444944
pfrom->nServices = ServiceFlags(nServiceInt);
49454945
if (!pfrom->fInbound)
49464946
{
4947-
addrman.SetServices(pfrom->addr, pfrom->nServices);
4947+
connman.SetServices(pfrom->addr, pfrom->nServices);
49484948
}
49494949
if (pfrom->nServicesExpected & ~pfrom->nServices)
49504950
{
@@ -5038,12 +5038,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
50385038
}
50395039

50405040
// Get recent addresses
5041-
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
5041+
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000)
50425042
{
50435043
pfrom->PushMessage(NetMsgType::GETADDR);
50445044
pfrom->fGetAddr = true;
50455045
}
5046-
addrman.Good(pfrom->addr);
5046+
connman.MarkAddressGood(pfrom->addr);
50475047
}
50485048

50495049
pfrom->fSuccessfullyConnected = true;
@@ -5108,7 +5108,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
51085108
vRecv >> vAddr;
51095109

51105110
// Don't want addr from older versions unless seeding
5111-
if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
5111+
if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
51125112
return true;
51135113
if (vAddr.size() > 1000)
51145114
{
@@ -5160,7 +5160,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
51605160
if (fReachable)
51615161
vAddrOk.push_back(addr);
51625162
}
5163-
addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
5163+
connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
51645164
if (vAddr.size() < 1000)
51655165
pfrom->fGetAddr = false;
51665166
if (pfrom->fOneShot)
@@ -5950,7 +5950,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
59505950
pfrom->fSentAddr = true;
59515951

59525952
pfrom->vAddrToSend.clear();
5953-
vector<CAddress> vAddr = addrman.GetAddr();
5953+
vector<CAddress> vAddr = connman.GetAddresses();
59545954
BOOST_FOREACH(const CAddress &addr, vAddr)
59555955
pfrom->PushAddress(addr);
59565956
}
@@ -6393,7 +6393,7 @@ bool SendMessages(CNode* pto, CConnman& connman)
63936393
LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
63946394
else
63956395
{
6396-
CNode::Ban(pto->addr, BanReasonNodeMisbehaving);
6396+
connman.Ban(pto->addr, BanReasonNodeMisbehaving);
63976397
}
63986398
}
63996399
state.fShouldBan = false;

src/net.cpp

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
8484
static bool vfLimited[NET_MAX] = {};
8585
static CNode* pnodeLocalHost = NULL;
8686
uint64_t nLocalHostNonce = 0;
87-
CAddrMan addrman;
8887
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
89-
bool fAddressesInitialized = false;
9088
std::string strSubVersion;
9189

9290
std::vector<CNode*> vNodes;
@@ -446,21 +444,21 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
446444
return NULL;
447445
}
448446

449-
static void DumpBanlist()
447+
void CConnman::DumpBanlist()
450448
{
451-
CNode::SweepBanned(); // clean unused entries (if bantime has expired)
449+
SweepBanned(); // clean unused entries (if bantime has expired)
452450

453-
if (!CNode::BannedSetIsDirty())
451+
if (!BannedSetIsDirty())
454452
return;
455453

456454
int64_t nStart = GetTimeMillis();
457455

458456
CBanDB bandb;
459457
banmap_t banmap;
460-
CNode::SetBannedSetDirty(false);
461-
CNode::GetBanned(banmap);
458+
SetBannedSetDirty(false);
459+
GetBanned(banmap);
462460
if (!bandb.Write(banmap))
463-
CNode::SetBannedSetDirty(true);
461+
SetBannedSetDirty(true);
464462

465463
LogPrint("net", "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
466464
banmap.size(), GetTimeMillis() - nStart);
@@ -501,11 +499,7 @@ void CNode::PushVersion()
501499

502500

503501

504-
banmap_t CNode::setBanned;
505-
CCriticalSection CNode::cs_setBanned;
506-
bool CNode::setBannedIsDirty;
507-
508-
void CNode::ClearBanned()
502+
void CConnman::ClearBanned()
509503
{
510504
{
511505
LOCK(cs_setBanned);
@@ -516,7 +510,7 @@ void CNode::ClearBanned()
516510
uiInterface.BannedListChanged();
517511
}
518512

519-
bool CNode::IsBanned(CNetAddr ip)
513+
bool CConnman::IsBanned(CNetAddr ip)
520514
{
521515
bool fResult = false;
522516
{
@@ -533,7 +527,7 @@ bool CNode::IsBanned(CNetAddr ip)
533527
return fResult;
534528
}
535529

536-
bool CNode::IsBanned(CSubNet subnet)
530+
bool CConnman::IsBanned(CSubNet subnet)
537531
{
538532
bool fResult = false;
539533
{
@@ -549,12 +543,12 @@ bool CNode::IsBanned(CSubNet subnet)
549543
return fResult;
550544
}
551545

552-
void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
546+
void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
553547
CSubNet subNet(addr);
554548
Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
555549
}
556550

557-
void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
551+
void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
558552
CBanEntry banEntry(GetTime());
559553
banEntry.banReason = banReason;
560554
if (bantimeoffset <= 0)
@@ -585,12 +579,12 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti
585579
DumpBanlist(); //store banlist to disk immediately if user requested ban
586580
}
587581

588-
bool CNode::Unban(const CNetAddr &addr) {
582+
bool CConnman::Unban(const CNetAddr &addr) {
589583
CSubNet subNet(addr);
590584
return Unban(subNet);
591585
}
592586

593-
bool CNode::Unban(const CSubNet &subNet) {
587+
bool CConnman::Unban(const CSubNet &subNet) {
594588
{
595589
LOCK(cs_setBanned);
596590
if (!setBanned.erase(subNet))
@@ -602,20 +596,20 @@ bool CNode::Unban(const CSubNet &subNet) {
602596
return true;
603597
}
604598

605-
void CNode::GetBanned(banmap_t &banMap)
599+
void CConnman::GetBanned(banmap_t &banMap)
606600
{
607601
LOCK(cs_setBanned);
608602
banMap = setBanned; //create a thread safe copy
609603
}
610604

611-
void CNode::SetBanned(const banmap_t &banMap)
605+
void CConnman::SetBanned(const banmap_t &banMap)
612606
{
613607
LOCK(cs_setBanned);
614608
setBanned = banMap;
615609
setBannedIsDirty = true;
616610
}
617611

618-
void CNode::SweepBanned()
612+
void CConnman::SweepBanned()
619613
{
620614
int64_t now = GetTime();
621615

@@ -636,13 +630,13 @@ void CNode::SweepBanned()
636630
}
637631
}
638632

639-
bool CNode::BannedSetIsDirty()
633+
bool CConnman::BannedSetIsDirty()
640634
{
641635
LOCK(cs_setBanned);
642636
return setBannedIsDirty;
643637
}
644638

645-
void CNode::SetBannedSetDirty(bool dirty)
639+
void CConnman::SetBannedSetDirty(bool dirty)
646640
{
647641
LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
648642
setBannedIsDirty = dirty;
@@ -1047,7 +1041,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10471041
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
10481042
#endif
10491043

1050-
if (CNode::IsBanned(addr) && !whitelisted)
1044+
if (IsBanned(addr) && !whitelisted)
10511045
{
10521046
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
10531047
CloseSocket(hSocket);
@@ -1548,7 +1542,7 @@ void CConnman::ThreadDNSAddressSeed()
15481542

15491543

15501544

1551-
void DumpAddresses()
1545+
void CConnman::DumpAddresses()
15521546
{
15531547
int64_t nStart = GetTimeMillis();
15541548

@@ -1559,7 +1553,7 @@ void DumpAddresses()
15591553
addrman.size(), GetTimeMillis() - nStart);
15601554
}
15611555

1562-
void DumpData()
1556+
void CConnman::DumpData()
15631557
{
15641558
DumpAddresses();
15651559
DumpBanlist();
@@ -1813,7 +1807,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
18131807
boost::this_thread::interruption_point();
18141808
if (!pszDest) {
18151809
if (IsLocal(addrConnect) ||
1816-
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
1810+
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
18171811
FindNode(addrConnect.ToStringIPPort()))
18181812
return false;
18191813
} else if (FindNode(std::string(pszDest)))
@@ -2054,10 +2048,22 @@ void static Discover(boost::thread_group& threadGroup)
20542048

20552049
CConnman::CConnman()
20562050
{
2051+
setBannedIsDirty = false;
2052+
fAddressesInitialized = false;
20572053
}
20582054

20592055
bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError)
20602056
{
2057+
Discover(threadGroup);
2058+
2059+
bool ret = connman.Start(threadGroup, scheduler, strNodeError);
2060+
2061+
return ret;
2062+
}
2063+
2064+
bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError)
2065+
{
2066+
20612067
uiInterface.InitMessage(_("Loading addresses..."));
20622068
// Load addresses from peers.dat
20632069
int64_t nStart = GetTimeMillis();
@@ -2078,33 +2084,22 @@ bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler&
20782084
CBanDB bandb;
20792085
banmap_t banmap;
20802086
if (bandb.Read(banmap)) {
2081-
CNode::SetBanned(banmap); // thread save setter
2082-
CNode::SetBannedSetDirty(false); // no need to write down, just read data
2083-
CNode::SweepBanned(); // sweep out unused entries
2087+
SetBanned(banmap); // thread save setter
2088+
SetBannedSetDirty(false); // no need to write down, just read data
2089+
SweepBanned(); // sweep out unused entries
20842090

20852091
LogPrint("net", "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
20862092
banmap.size(), GetTimeMillis() - nStart);
20872093
} else {
20882094
LogPrintf("Invalid or missing banlist.dat; recreating\n");
2089-
CNode::SetBannedSetDirty(true); // force write
2095+
SetBannedSetDirty(true); // force write
20902096
DumpBanlist();
20912097
}
20922098

20932099
uiInterface.InitMessage(_("Starting network threads..."));
20942100

20952101
fAddressesInitialized = true;
20962102

2097-
Discover(threadGroup);
2098-
2099-
bool ret = connman.Start(threadGroup, strNodeError);
2100-
2101-
// Dump network addresses
2102-
scheduler.scheduleEvery(DumpData, DUMP_ADDRESSES_INTERVAL);
2103-
return ret;
2104-
}
2105-
2106-
bool CConnman::Start(boost::thread_group& threadGroup, std::string& strNodeError)
2107-
{
21082103
if (semOutbound == NULL) {
21092104
// initialize semaphore
21102105
int nMaxOutbound = std::min((MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS), nMaxConnections);
@@ -2142,6 +2137,9 @@ bool CConnman::Start(boost::thread_group& threadGroup, std::string& strNodeError
21422137
// Process messages
21432138
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "msghand", boost::function<void()>(boost::bind(&CConnman::ThreadMessageHandler, this))));
21442139

2140+
// Dump network addresses
2141+
scheduler.scheduleEvery(boost::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL);
2142+
21452143
return true;
21462144
}
21472145

@@ -2150,12 +2148,6 @@ bool StopNode(CConnman& connman)
21502148
LogPrintf("StopNode()\n");
21512149
MapPort(false);
21522150

2153-
if (fAddressesInitialized)
2154-
{
2155-
DumpData();
2156-
fAddressesInitialized = false;
2157-
}
2158-
21592151
connman.Stop();
21602152
return true;
21612153
}
@@ -2181,6 +2173,12 @@ void CConnman::Stop()
21812173
for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++)
21822174
semOutbound->post();
21832175

2176+
if (fAddressesInitialized)
2177+
{
2178+
DumpData();
2179+
fAddressesInitialized = false;
2180+
}
2181+
21842182
// Close sockets
21852183
BOOST_FOREACH(CNode* pnode, vNodes)
21862184
if (pnode->hSocket != INVALID_SOCKET)
@@ -2221,6 +2219,36 @@ CConnman::~CConnman()
22212219
{
22222220
}
22232221

2222+
size_t CConnman::GetAddressCount() const
2223+
{
2224+
return addrman.size();
2225+
}
2226+
2227+
void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2228+
{
2229+
addrman.SetServices(addr, nServices);
2230+
}
2231+
2232+
void CConnman::MarkAddressGood(const CAddress& addr)
2233+
{
2234+
addrman.Good(addr);
2235+
}
2236+
2237+
void CConnman::AddNewAddress(const CAddress& addr, const CAddress& addrFrom, int64_t nTimePenalty)
2238+
{
2239+
addrman.Add(addr, addrFrom, nTimePenalty);
2240+
}
2241+
2242+
void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2243+
{
2244+
addrman.Add(vAddr, addrFrom, nTimePenalty);
2245+
}
2246+
2247+
std::vector<CAddress> CConnman::GetAddresses()
2248+
{
2249+
return addrman.GetAddr();
2250+
}
2251+
22242252
void RelayTransaction(const CTransaction& tx)
22252253
{
22262254
CInv inv(MSG_TX, tx.GetHash());

0 commit comments

Comments
 (0)