diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -868,7 +868,6 @@ bool fClient{false}; // after BIP159, set by version message bool m_limited_node{false}; - const bool fInbound; std::atomic_bool fSuccessfullyConnected{false}; // Setting fDisconnect to true will cause the node to be disconnected the // next time DisconnectNodes() runs @@ -889,6 +888,10 @@ return m_conn_type == ConnectionType::ADDR_FETCH; } + bool IsInboundConn() const { + return m_conn_type == ConnectionType::INBOUND; + } + protected: mapMsgCmdSize mapSendBytesPerMsgCmd; mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv); diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -338,7 +338,7 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce) { LOCK(cs_vNodes); for (const CNode *pnode : vNodes) { - if (!pnode->fSuccessfullyConnected && !pnode->fInbound && + if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() == nonce) { return false; } @@ -551,7 +551,7 @@ LOCK(cs_SubVer); stats.cleanSubVer = cleanSubVer; } - stats.fInbound = fInbound; + stats.fInbound = IsInboundConn(); stats.m_manual_connection = IsManualConn(); stats.nStartingHeight = nStartingHeight; { @@ -940,7 +940,7 @@ if (node->HasPermission(PF_NOBAN)) { continue; } - if (!node->fInbound) { + if (!node->IsInboundConn()) { continue; } if (node->fDisconnect) { @@ -1081,7 +1081,7 @@ { LOCK(cs_vNodes); for (const CNode *pnode : vNodes) { - if (pnode->fInbound) { + if (pnode->IsInboundConn()) { nInbound++; } } @@ -1792,7 +1792,7 @@ !pnode->IsFeelerConn() && !pnode->IsAddrFetchConn() && !pnode->IsManualConn() && - !pnode->fInbound; + !pnode->IsInboundConn(); } } if (nRelevant >= 2) { @@ -1916,7 +1916,7 @@ { LOCK(cs_vNodes); for (const CNode *pnode : vNodes) { - if (!pnode->fInbound && !pnode->IsManualConn() && + if (!pnode->IsInboundConn() && !pnode->IsManualConn() && !pnode->IsFeelerConn() && !pnode->fDisconnect && !pnode->IsAddrFetchConn() && pnode->fSuccessfullyConnected) { ++nOutbound; @@ -1996,7 +1996,7 @@ { LOCK(cs_vNodes); for (const CNode *pnode : vNodes) { - if (!pnode->fInbound && + if (!pnode->IsInboundConn() && (pnode->m_conn_type != ConnectionType::MANUAL)) { // Netgroups for inbound and addnode peers are not excluded // because our goal here is to not use multiple of our @@ -2161,12 +2161,12 @@ LOCK(cs_vNodes); for (const CNode *pnode : vNodes) { if (pnode->addr.IsValid()) { - mapConnected[pnode->addr] = pnode->fInbound; + mapConnected[pnode->addr] = pnode->IsInboundConn(); } std::string addrName = pnode->GetAddrName(); if (!addrName.empty()) { mapConnectedByName[std::move(addrName)] = - std::make_pair(pnode->fInbound, + std::make_pair(pnode->IsInboundConn(), static_cast(pnode->addr)); } } @@ -2812,7 +2812,8 @@ int nNum = 0; for (const auto &pnode : vNodes) { - if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) { + if (flags & + (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) { nNum++; } } @@ -2989,8 +2990,7 @@ const CAddress &addrBindIn, const std::string &addrNameIn, ConnectionType conn_type_in) : nTimeConnected(GetSystemTimeInSeconds()), addr(addrIn), - addrBind(addrBindIn), fInbound(conn_type_in == ConnectionType::INBOUND), - nKeyedNetGroup(nKeyedNetGroupIn), + addrBind(addrBindIn), nKeyedNetGroup(nKeyedNetGroupIn), // Don't relay addr messages to peers that we connect to as // block-relay-only peers (to prevent adversaries from inferring these // links from addr traffic). diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -487,7 +487,7 @@ // Whether this node should be marked as a preferred download node. state->fPreferredDownload = - (!node.fInbound || node.HasPermission(PF_NOBAN)) && + (!node.IsInboundConn() || node.HasPermission(PF_NOBAN)) && !node.IsAddrFetchConn() && !node.fClient; nPreferredDownload += state->fPreferredDownload; @@ -935,8 +935,8 @@ } static bool IsOutboundDisconnectionCandidate(const CNode &node) { - return !(node.fInbound || node.IsManualConn() || node.IsFeelerConn() || - node.IsAddrFetchConn()); + return !(node.IsInboundConn() || node.IsManualConn() || + node.IsFeelerConn() || node.IsAddrFetchConn()); } void PeerLogicValidation::InitializeNode(const Config &config, CNode *pnode) { @@ -945,13 +945,14 @@ NodeId nodeid = pnode->GetId(); { LOCK(cs_main); - mapNodeState.emplace_hint( - mapNodeState.end(), std::piecewise_construct, - std::forward_as_tuple(nodeid), - std::forward_as_tuple(addr, std::move(addrName), pnode->fInbound, - pnode->IsManualConn())); - } - if (!pnode->fInbound) { + mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, + std::forward_as_tuple(nodeid), + std::forward_as_tuple(addr, + std::move(addrName), + pnode->IsInboundConn(), + pnode->IsManualConn())); + } + if (!pnode->IsInboundConn()) { PushNodeVersion(config, *pnode, *connman, GetTime()); } } @@ -2534,11 +2535,11 @@ vRecv >> nVersion >> nServiceInt >> nTime >> addrMe; nSendVersion = std::min(nVersion, PROTOCOL_VERSION); nServices = ServiceFlags(nServiceInt); - if (!pfrom.fInbound) { + if (!pfrom.IsInboundConn()) { connman.SetServices(pfrom.addr, nServices); } - if (!pfrom.fInbound && !pfrom.IsFeelerConn() && !pfrom.IsManualConn() && - !HasAllDesirableServiceFlags(nServices)) { + if (!pfrom.IsInboundConn() && !pfrom.IsFeelerConn() && + !pfrom.IsManualConn() && !HasAllDesirableServiceFlags(nServices)) { LogPrint(BCLog::NET, "peer=%d does not offer the expected services " "(%08x offered, %08x expected); disconnecting\n", @@ -2572,19 +2573,19 @@ vRecv >> fRelay; } // Disconnect if we connected to ourself - if (pfrom.fInbound && !connman.CheckIncomingNonce(nNonce)) { + if (pfrom.IsInboundConn() && !connman.CheckIncomingNonce(nNonce)) { LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToString()); pfrom.fDisconnect = true; return true; } - if (pfrom.fInbound && addrMe.IsRoutable()) { + if (pfrom.IsInboundConn() && addrMe.IsRoutable()) { SeenLocal(addrMe); } // Be shy and don't send version until we hear - if (pfrom.fInbound) { + if (pfrom.IsInboundConn()) { PushNodeVersion(config, pfrom, connman, GetAdjustedTime()); } @@ -2625,7 +2626,7 @@ UpdatePreferredDownload(pfrom, State(pfrom.GetId())); } - if (!pfrom.fInbound && pfrom.IsAddrRelayPeer()) { + if (!pfrom.IsInboundConn() && pfrom.IsAddrRelayPeer()) { // Advertise our address if (fListen && !::ChainstateActive().IsInitialBlockDownload()) { CAddress addr = @@ -2697,7 +2698,7 @@ if (msg_type == NetMsgType::VERACK) { pfrom.SetRecvVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION)); - if (!pfrom.fInbound) { + if (!pfrom.IsInboundConn()) { // Mark this node as currently connected, so we update its timestamp // later. LOCK(cs_main); @@ -3921,7 +3922,7 @@ // sending getaddr messages. Making nodes which are behind NAT and can // only make outgoing connections ignore the getaddr message mitigates // the attack. - if (!pfrom.fInbound) { + if (!pfrom.IsInboundConn()) { LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom.GetId()); @@ -4892,7 +4893,7 @@ bool fSendTrickle = pto->HasPermission(PF_NOBAN); if (pto->m_tx_relay->nNextInvSend < current_time) { fSendTrickle = true; - if (pto->fInbound) { + if (pto->IsInboundConn()) { pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{ connman->PoissonNextSendInbound( nNow, INVENTORY_BROADCAST_INTERVAL)}; diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -188,12 +188,12 @@ auto pnode1 = std::make_unique(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, ConnectionType::OUTBOUND); - BOOST_CHECK(pnode1->fInbound == false); + BOOST_CHECK(pnode1->IsInboundConn() == false); auto pnode2 = std::make_unique(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, ConnectionType::INBOUND); - BOOST_CHECK(pnode2->fInbound == true); + BOOST_CHECK(pnode2->IsInboundConn() == true); } BOOST_AUTO_TEST_CASE(test_getSubVersionEB) {