Skip to content

Commit d1391e3

Browse files
sipaFuzzbawls
authored andcommitted
Introduce enum ServiceFlags for service flags
1 parent b6a0455 commit d1391e3

File tree

11 files changed

+96
-88
lines changed

11 files changed

+96
-88
lines changed

src/addrman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
265265
pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty);
266266

267267
// add services
268-
pinfo->nServices |= addr.nServices;
268+
pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);
269269

270270
// do not update if no new information is present
271271
if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
@@ -503,7 +503,7 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
503503
info.nTime = nTime;
504504
}
505505

506-
void CAddrMan::SetServices_(const CService& addr, uint64_t nServices)
506+
void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
507507
{
508508
CAddrInfo* pinfo = Find(addr);
509509

src/addrman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class CAddrMan
265265
void Connected_(const CService& addr, int64_t nTime);
266266

267267
//! Update an entry's service bits.
268-
void SetServices_(const CService& addr, uint64_t nServices);
268+
void SetServices_(const CService& addr, ServiceFlags nServices);
269269

270270
public:
271271
/**
@@ -593,7 +593,7 @@ class CAddrMan
593593
}
594594
}
595595

596-
void SetServices(const CService& addr, uint64_t nServices)
596+
void SetServices(const CService& addr, ServiceFlags nServices)
597597
{
598598
LOCK(cs);
599599
Check();

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2014 The Bitcoin developers
2+
// Copyright (c) 2009-2015 The Bitcoin developers
33
// Copyright (c) 2014-2015 The Dash developers
44
// Copyright (c) 2011-2013 The PPCoin developers
55
// Copyright (c) 2013-2014 The NovaCoin Developers
@@ -1043,7 +1043,7 @@ bool AppInit2()
10431043
nMaxDatacarrierBytes = GetArg("-datacarriersize", nMaxDatacarrierBytes);
10441044

10451045
if (GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
1046-
nLocalServices |= NODE_BLOOM;
1046+
nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
10471047

10481048
nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
10491049

src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2014 The Bitcoin developers
2+
// Copyright (c) 2009-2015 The Bitcoin developers
33
// Copyright (c) 2014-2015 The Dash developers
44
// Copyright (c) 2011-2013 The PPCoin developers
55
// Copyright (c) 2013-2014 The NovaCoin Developers
@@ -5233,7 +5233,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
52335233
CAddress addrMe;
52345234
CAddress addrFrom;
52355235
uint64_t nNonce = 1;
5236-
vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
5236+
uint64_t nServiceInt;
5237+
vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
5238+
pfrom->nServices = ServiceFlags(nServiceInt);
52375239
if (!pfrom->fInbound) {
52385240
addrman.SetServices(pfrom->addr, pfrom->nServices);
52395241
}

src/net.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ struct ListenSocket {
6868
}
6969

7070
/** Services this node implementation cares about */
71-
static const uint64_t nRelevantServices = NODE_NETWORK;
71+
static const ServiceFlags nRelevantServices = NODE_NETWORK;
7272

7373
//
7474
// Global state variables
7575
//
7676
bool fDiscover = true;
7777
bool fListen = true;
78-
uint64_t nLocalServices = NODE_NETWORK;
78+
ServiceFlags nLocalServices = NODE_NETWORK;
7979
RecursiveMutex cs_mapLocalHost;
8080
std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
8181
static bool vfLimited[NET_MAX] = {};
@@ -173,7 +173,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6>& vSeedsIn
173173
// one by discovery.
174174
CAddress GetLocalAddress(const CNetAddr* paddrPeer)
175175
{
176-
CAddress ret(CService("0.0.0.0", GetListenPort()), 0);
176+
CAddress ret(CService("0.0.0.0", GetListenPort()), NODE_NONE);
177177
CService addr;
178178
if (GetLocal(addr, paddrPeer)) {
179179
ret = CAddress(addr, nLocalServices);
@@ -448,7 +448,7 @@ CNode* ConnectNode(CAddress addrConnect, const char* pszDest, bool fCountFailure
448448
vNodes.push_back(pnode);
449449
}
450450

451-
pnode->nServicesExpected = addrConnect.nServices & nRelevantServices;
451+
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
452452
pnode->nTimeConnected = GetTime();
453453

454454
return pnode;
@@ -500,7 +500,7 @@ void CNode::PushVersion()
500500
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
501501
else
502502
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
503-
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
503+
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalServices, nTime, addrYou, addrMe,
504504
nLocalHostNonce, strSubVersion, nBestHeight, true);
505505
}
506506

@@ -1301,7 +1301,7 @@ void ThreadDNSAddressSeed()
13011301
} else {
13021302
std::vector<CNetAddr> vIPs;
13031303
std::vector<CAddress> vAdd;
1304-
uint64_t requiredServiceBits = nRelevantServices;
1304+
ServiceFlags requiredServiceBits = nRelevantServices;
13051305
if (LookupHost(seed.getHost(requiredServiceBits).c_str(), vIPs, 0, true)) {
13061306
for (CNetAddr& ip : vIPs) {
13071307
int nOneDay = 24 * 3600;
@@ -1369,7 +1369,7 @@ void ThreadOpenConnections()
13691369
for (int64_t nLoop = 0;; nLoop++) {
13701370
ProcessOneShot();
13711371
for (std::string strAddr : mapMultiArgs["-connect"]) {
1372-
CAddress addr(CService(), 0);
1372+
CAddress addr(CService(), NODE_NONE);
13731373
OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
13741374
for (int i = 0; i < 10 && i < nLoop; i++) {
13751375
MilliSleep(500);
@@ -1520,8 +1520,8 @@ void ThreadOpenAddedConnections()
15201520
for (std::vector<CService>& vserv : lservAddressesToAdd) {
15211521
CSemaphoreGrant grant(*semOutbound);
15221522
/* We want -addnode to work even for nodes that don't provide all
1523-
* wanted services, so pass in nServices=0 to CAddress. */
1524-
OpenNetworkConnection(CAddress(vserv[i % vserv.size()], 0), false, &grant);
1523+
* wanted services, so pass in nServices=NODE_NONE to CAddress. */
1524+
OpenNetworkConnection(CAddress(vserv[i % vserv.size()], NODE_NONE), false, &grant);
15251525
MilliSleep(500);
15261526
}
15271527
MilliSleep(120000); // Retry every 2 minutes
@@ -2116,8 +2116,8 @@ unsigned int SendBufferSize() { return 1000 * GetArg("-maxsendbuffer", 1 * 1000)
21162116

21172117
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
21182118
{
2119-
nServices = 0;
2120-
nServicesExpected = 0;
2119+
nServices = NODE_NONE;
2120+
nServicesExpected = NODE_NONE;
21212121
hSocket = hSocketIn;
21222122
nRecvVersion = INIT_PROTO_VERSION;
21232123
nLastSend = 0;

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2014 The Bitcoin developers
2+
// Copyright (c) 2009-2015 The Bitcoin developers
33
// Copyright (c) 2015-2020 The PIVX developers
44
// Distributed under the MIT/X11 software license, see the accompanying
55
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -135,7 +135,7 @@ bool validateMasternodeIP(const std::string& addrStr); // valid, reacha
135135

136136
extern bool fDiscover;
137137
extern bool fListen;
138-
extern uint64_t nLocalServices;
138+
extern ServiceFlags nLocalServices;
139139
extern uint64_t nLocalHostNonce;
140140
extern CAddrMan addrman;
141141
extern int nMaxConnections;
@@ -168,7 +168,7 @@ class CNodeStats
168168
{
169169
public:
170170
NodeId nodeid;
171-
uint64_t nServices;
171+
ServiceFlags nServices;
172172
int64_t nLastSend;
173173
int64_t nLastRecv;
174174
int64_t nTimeConnected;
@@ -295,8 +295,8 @@ class CNode
295295
{
296296
public:
297297
// socket
298-
uint64_t nServices;
299-
uint64_t nServicesExpected;
298+
ServiceFlags nServices;
299+
ServiceFlags nServicesExpected;
300300
SOCKET hSocket;
301301
CDataStream ssSend;
302302
size_t nSendSize; // total size of all vSendMsg entries

src/protocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ CAddress::CAddress() : CService()
171171
Init();
172172
}
173173

174-
CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
174+
CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
175175
{
176176
Init();
177177
nServices = nServicesIn;
178178
}
179179

180180
void CAddress::Init()
181181
{
182-
nServices = 0;
182+
nServices = NODE_NONE;
183183
nTime = 100000000;
184184
}
185185

src/protocol.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2014 The Bitcoin developers
2+
// Copyright (c) 2009-2015 The Bitcoin developers
33
// Copyright (c) 2014-2015 The Dash developers
44
// Copyright (c) 2016-2020 The PIVX developers
55
// Distributed under the MIT/X11 software license, see the accompanying
@@ -283,17 +283,21 @@ extern const char* SYNCSTATUSCOUNT;
283283
const std::vector<std::string>& getAllNetMessageTypes();
284284

285285
/** nServices flags */
286-
enum {
286+
enum ServiceFlags : uint64_t {
287+
// Nothing
288+
NODE_NONE = 0,
289+
// NODE_NETWORK means that the node is capable of serving the block chain. It is currently
290+
// set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
291+
// network services but don't provide them.
287292
NODE_NETWORK = (1 << 0),
288293

289294
// NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
290295
// Bitcoin Core nodes used to support this by default, without advertising this bit,
291296
// but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
292297
NODE_BLOOM = (1 << 2),
293298

294-
// NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
295-
// that the node doens't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )
296-
299+
// NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
300+
// that the node doesn't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )
297301
NODE_BLOOM_WITHOUT_MN = (1 << 4),
298302

299303
// Bits 24-31 are reserved for temporary experiments. Just pick a bit that
@@ -310,7 +314,7 @@ class CAddress : public CService
310314
{
311315
public:
312316
CAddress();
313-
explicit CAddress(CService ipIn, uint64_t nServicesIn);
317+
explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
314318

315319
void Init();
316320

@@ -326,13 +330,15 @@ class CAddress : public CService
326330
if ((nType & SER_DISK) ||
327331
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
328332
READWRITE(nTime);
329-
READWRITE(nServices);
333+
uint64_t nServicesInt = nServices;
334+
READWRITE(nServicesInt);
335+
nServices = (ServiceFlags)nServicesInt;
330336
READWRITE(*(CService*)this);
331337
}
332338

333339
// TODO: make private (improves encapsulation)
334340
public:
335-
uint64_t nServices;
341+
ServiceFlags nServices;
336342

337343
// disk and network only
338344
unsigned int nTime;

src/test/DoS_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
4848
BOOST_AUTO_TEST_CASE(DoS_banning)
4949
{
5050
CNode::ClearBanned();
51-
CAddress addr1(ip(0xa0b0c001), 0);
51+
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
5252
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
5353
dummyNode1.nVersion = 1;
5454
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
5555
SendMessages(&dummyNode1, false);
5656
BOOST_CHECK(CNode::IsBanned(addr1));
5757
BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned
5858

59-
CAddress addr2(ip(0xa0b0c002), 0);
59+
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
6060
CNode dummyNode2(INVALID_SOCKET, addr2, "", true);
6161
dummyNode2.nVersion = 1;
6262
Misbehaving(dummyNode2.GetId(), 50);
@@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
7272
{
7373
CNode::ClearBanned();
7474
mapArgs["-banscore"] = "111"; // because 11 is my favorite number
75-
CAddress addr1(ip(0xa0b0c001), 0);
75+
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
7676
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
7777
dummyNode1.nVersion = 1;
7878
Misbehaving(dummyNode1.GetId(), 100);
@@ -93,7 +93,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
9393
int64_t nStartTime = GetTime();
9494
SetMockTime(nStartTime); // Overrides future calls to GetTime()
9595

96-
CAddress addr(ip(0xa0b0c001), 0);
96+
CAddress addr(ip(0xa0b0c001), NODE_NONE);
9797
CNode dummyNode(INVALID_SOCKET, addr, "", true);
9898
dummyNode.nVersion = 1;
9999

0 commit comments

Comments
 (0)