Skip to content

Commit 654d9bc

Browse files
p2p: Introduce data struct to track connection counts by network
Connman uses this new map to keep a count of active OUTBOUND_FULL_RELAY and MANUAL connections. Unused until next commit. Co-authored-by: Martin Zumsande <[email protected]>
1 parent 6a47337 commit 654d9bc

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/net.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,9 @@ void CConnman::DisconnectNodes()
11351135
// close socket and cleanup
11361136
pnode->CloseSocketDisconnect();
11371137

1138+
// update connection count by network
1139+
if (pnode->IsManualOrFullOutboundConn()) --m_network_conn_counts[pnode->addr.GetNetwork()];
1140+
11381141
// hold in disconnected pool until all refs are released
11391142
pnode->Release();
11401143
m_nodes_disconnected.push_back(pnode);
@@ -2035,6 +2038,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
20352038
{
20362039
LOCK(m_nodes_mutex);
20372040
m_nodes.push_back(pnode);
2041+
2042+
// update connection count by network
2043+
if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()];
20382044
}
20392045
}
20402046

src/net.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,22 @@ class CNode
465465
return m_conn_type == ConnectionType::MANUAL;
466466
}
467467

468+
bool IsManualOrFullOutboundConn() const
469+
{
470+
switch (m_conn_type) {
471+
case ConnectionType::INBOUND:
472+
case ConnectionType::FEELER:
473+
case ConnectionType::BLOCK_RELAY:
474+
case ConnectionType::ADDR_FETCH:
475+
return false;
476+
case ConnectionType::OUTBOUND_FULL_RELAY:
477+
case ConnectionType::MANUAL:
478+
return true;
479+
} // no default case, so the compiler can warn about missing cases
480+
481+
assert(false);
482+
}
483+
468484
bool IsBlockOnlyConn() const {
469485
return m_conn_type == ConnectionType::BLOCK_RELAY;
470486
}
@@ -1048,6 +1064,9 @@ class CConnman
10481064
std::atomic<NodeId> nLastNodeId{0};
10491065
unsigned int nPrevNodeCount{0};
10501066

1067+
// Stores number of full-tx connections (outbound and manual) per network
1068+
std::array<unsigned int, Network::NET_MAX> m_network_conn_counts GUARDED_BY(m_nodes_mutex) = {};
1069+
10511070
/**
10521071
* Cache responses to addr requests to minimize privacy leak.
10531072
* Attack example: scraping addrs in real-time may allow an attacker

src/test/util/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ struct ConnmanTestMsg : public CConnman {
2929
{
3030
LOCK(m_nodes_mutex);
3131
m_nodes.push_back(&node);
32+
33+
if (node.IsManualOrFullOutboundConn()) ++m_network_conn_counts[node.addr.GetNetwork()];
3234
}
35+
3336
void ClearTestNodes()
3437
{
3538
LOCK(m_nodes_mutex);

0 commit comments

Comments
 (0)