Skip to content

Commit 1521c47

Browse files
committed
[net/refactor] Add manual connections to ConnectionType enum
1 parent 26304b4 commit 1521c47

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

src/net.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,10 @@ static CAddress GetBindAddress(SOCKET sock)
368368
return addr_bind;
369369
}
370370

371-
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, bool manual_connection, bool block_relay_only)
371+
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool block_relay_only)
372372
{
373+
assert(conn_type != ConnectionType::INBOUND);
374+
373375
if (pszDest == nullptr) {
374376
if (IsLocal(addrConnect))
375377
return nullptr;
@@ -432,7 +434,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
432434
if (hSocket == INVALID_SOCKET) {
433435
return nullptr;
434436
}
435-
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout, manual_connection);
437+
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout, conn_type == ConnectionType::MANUAL);
436438
}
437439
if (!proxyConnectionFailed) {
438440
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
@@ -459,7 +461,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
459461
NodeId id = GetNewNodeId();
460462
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
461463
CAddress addr_bind = GetBindAddress(hSocket);
462-
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", ConnectionType::OUTBOUND, block_relay_only);
464+
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", conn_type, block_relay_only);
463465
pnode->AddRef();
464466

465467
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -1705,17 +1707,6 @@ void CConnman::ThreadDNSAddressSeed()
17051707
LogPrintf("%d addresses found from DNS seeds\n", found);
17061708
}
17071709

1708-
1709-
1710-
1711-
1712-
1713-
1714-
1715-
1716-
1717-
1718-
17191710
void CConnman::DumpAddresses()
17201711
{
17211712
int64_t nStart = GetTimeMillis();
@@ -1786,7 +1777,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17861777
for (const std::string& strAddr : connect)
17871778
{
17881779
CAddress addr(CService(), NODE_NONE);
1789-
OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, true);
1780+
OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, ConnectionType::MANUAL);
17901781
for (int i = 0; i < 10 && i < nLoop; i++)
17911782
{
17921783
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
@@ -1952,7 +1943,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19521943
// well for sanity.)
19531944
bool block_relay_only = nOutboundBlockRelay < m_max_outbound_block_relay && !fFeeler && nOutboundFullRelay >= m_max_outbound_full_relay;
19541945

1955-
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler, false, block_relay_only);
1946+
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler, ConnectionType::OUTBOUND, block_relay_only);
19561947
}
19571948
}
19581949
}
@@ -2027,7 +2018,7 @@ void CConnman::ThreadOpenAddedConnections()
20272018
}
20282019
tried = true;
20292020
CAddress addr(CService(), NODE_NONE);
2030-
OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), false, false, true);
2021+
OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), false, false, ConnectionType::MANUAL);
20312022
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
20322023
return;
20332024
}
@@ -2039,8 +2030,10 @@ void CConnman::ThreadOpenAddedConnections()
20392030
}
20402031

20412032
// if successful, this moves the passed grant to the constructed node
2042-
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool m_addr_fetch, bool fFeeler, bool manual_connection, bool block_relay_only)
2033+
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool m_addr_fetch, bool fFeeler, ConnectionType conn_type, bool block_relay_only)
20432034
{
2035+
assert(conn_type != ConnectionType::INBOUND);
2036+
20442037
//
20452038
// Initiate outbound network connection
20462039
//
@@ -2058,7 +2051,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
20582051
} else if (FindNode(std::string(pszDest)))
20592052
return;
20602053

2061-
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, manual_connection, block_relay_only);
2054+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, block_relay_only);
20622055

20632056
if (!pnode)
20642057
return;
@@ -2068,8 +2061,6 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
20682061
pnode->m_addr_fetch = true;
20692062
if (fFeeler)
20702063
pnode->fFeeler = true;
2071-
if (manual_connection)
2072-
pnode->m_manual_connection = true;
20732064

20742065
m_msgproc->InitializeNode(pnode);
20752066
{
@@ -2127,11 +2118,6 @@ void CConnman::ThreadMessageHandler()
21272118
}
21282119
}
21292120

2130-
2131-
2132-
2133-
2134-
21352121
bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError, NetPermissionFlags permissions)
21362122
{
21372123
int nOne = 1;
@@ -2390,7 +2376,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23902376
else
23912377
threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
23922378

2393-
// Initiate outbound connections from -addnode
2379+
// Initiate manual connections
23942380
threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
23952381

23962382
if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
@@ -2752,6 +2738,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
27522738
: nTimeConnected(GetSystemTimeInSeconds()),
27532739
addr(addrIn),
27542740
addrBind(addrBindIn),
2741+
m_manual_connection(conn_type_in == ConnectionType::MANUAL),
27552742
fInbound(conn_type_in == ConnectionType::INBOUND),
27562743
nKeyedNetGroup(nKeyedNetGroupIn),
27572744
// Don't relay addr messages to peers that we connect to as block-relay-only

src/net.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct CSerializedNetMsg
116116
enum class ConnectionType {
117117
INBOUND,
118118
OUTBOUND,
119+
MANUAL,
119120
};
120121

121122
class NetEventsInterface;
@@ -201,7 +202,7 @@ class CConnman
201202
bool GetNetworkActive() const { return fNetworkActive; };
202203
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
203204
void SetNetworkActive(bool active);
204-
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool m_addr_fetch = false, bool fFeeler = false, bool manual_connection = false, bool block_relay_only = false);
205+
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool m_addr_fetch = false, bool fFeeler = false, ConnectionType conn_type = ConnectionType::OUTBOUND, bool block_relay_only = false);
205206
bool CheckIncomingNonce(uint64_t nonce);
206207

207208
bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func);
@@ -366,7 +367,7 @@ class CConnman
366367
CNode* FindNode(const CService& addr);
367368

368369
bool AttemptToEvictConnection();
369-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, bool manual_connection, bool block_relay_only);
370+
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool block_relay_only);
370371
void AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr) const;
371372

372373
void DeleteNode(CNode* pnode);

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static UniValue addnode(const JSONRPCRequest& request)
264264
if (strCommand == "onetry")
265265
{
266266
CAddress addr;
267-
node.connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), false, false, true);
267+
node.connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), false, false, ConnectionType::MANUAL);
268268
return NullUniValue;
269269
}
270270

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
4444
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
4545
for (int i = 0; i < num_peers_to_add; ++i) {
4646
const ServiceFlags service_flags = ServiceFlags(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
47-
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND});
47+
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND, ConnectionType::MANUAL});
4848
const bool block_relay_only{fuzzed_data_provider.ConsumeBool()};
4949
peers.push_back(MakeUnique<CNode>(i, service_flags, 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, conn_type, block_relay_only).release());
5050
CNode& p2p_node = *peers.back();

0 commit comments

Comments
 (0)