Skip to content

Commit 452ea59

Browse files
committed
Merge bitcoin#33454: net: support overriding the proxy selection in ConnectNode()
c76de2e net: support overriding the proxy selection in ConnectNode() (Vasil Dimov) Pull request description: Normally `ConnectNode()` would choose whether to use a proxy and which one. Make it possible to override this from the callers and same for `OpenNetworkConnection()` - pass down the proxy to `ConnectNode()`. Document both functions. This is useful if we want to open connections to IPv4 or IPv6 peers through the Tor SOCKS5 proxy. Also have `OpenNetworkConnection()` return whether the connection succeeded or not. This can be used when the caller needs to keep track of how many (successful) connections were opened. --- This is part of [bitcoin#29415 Broadcast own transactions only via short-lived Tor or I2P connections](bitcoin#29415). Putting it in its own PR to reduce the size of bitcoin#29415 and because it does not depend on the other commits from there. ACKs for top commit: stratospher: ACK c76de2e. optout21: ACK c76de2e mzumsande: Code Review ACK c76de2e andrewtoth: ACK c76de2e Tree-SHA512: 1d266e4280cdb1d0599971fa8b5da58b1b7451635be46abb15c0b823a1e18cf6e7bcba4a365ad198e6fd1afee4097d81a54253fa680c8b386ca6b9d68d795ff0
2 parents a33bd76 + c76de2e commit 452ea59

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

src/net.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,12 @@ static CService GetBindAddress(const Sock& sock)
373373
return addr_bind;
374374
}
375375

376-
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport)
376+
CNode* CConnman::ConnectNode(CAddress addrConnect,
377+
const char* pszDest,
378+
bool fCountFailure,
379+
ConnectionType conn_type,
380+
bool use_v2transport,
381+
const std::optional<Proxy>& proxy_override)
377382
{
378383
AssertLockNotHeld(m_unused_i2p_sessions_mutex);
379384
assert(conn_type != ConnectionType::INBOUND);
@@ -439,7 +444,13 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
439444

440445
for (auto& target_addr: connect_to) {
441446
if (target_addr.IsValid()) {
442-
const bool use_proxy{GetProxy(target_addr.GetNetwork(), proxy)};
447+
bool use_proxy;
448+
if (proxy_override.has_value()) {
449+
use_proxy = true;
450+
proxy = proxy_override.value();
451+
} else {
452+
use_proxy = GetProxy(target_addr.GetNetwork(), proxy);
453+
}
443454
bool proxyConnectionFailed = false;
444455

445456
if (target_addr.IsI2P() && use_proxy) {
@@ -2872,7 +2883,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, std
28722883
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(m_max_automatic_connections - 1, 2)};
28732884
// Use BIP324 transport when both us and them have NODE_V2_P2P set.
28742885
const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2);
2875-
OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*strDest=*/nullptr, conn_type, use_v2transport);
2886+
OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*pszDest=*/nullptr, conn_type, use_v2transport);
28762887
}
28772888
}
28782889
}
@@ -2981,7 +2992,13 @@ void CConnman::ThreadOpenAddedConnections()
29812992
}
29822993

29832994
// if successful, this moves the passed grant to the constructed node
2984-
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CountingSemaphoreGrant<>&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport)
2995+
bool CConnman::OpenNetworkConnection(const CAddress& addrConnect,
2996+
bool fCountFailure,
2997+
CountingSemaphoreGrant<>&& grant_outbound,
2998+
const char* pszDest,
2999+
ConnectionType conn_type,
3000+
bool use_v2transport,
3001+
const std::optional<Proxy>& proxy_override)
29853002
{
29863003
AssertLockNotHeld(m_unused_i2p_sessions_mutex);
29873004
assert(conn_type != ConnectionType::INBOUND);
@@ -2990,24 +3007,24 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
29903007
// Initiate outbound network connection
29913008
//
29923009
if (m_interrupt_net->interrupted()) {
2993-
return;
3010+
return false;
29943011
}
29953012
if (!fNetworkActive) {
2996-
return;
3013+
return false;
29973014
}
29983015
if (!pszDest) {
29993016
bool banned_or_discouraged = m_banman && (m_banman->IsDiscouraged(addrConnect) || m_banman->IsBanned(addrConnect));
30003017
if (IsLocal(addrConnect) || banned_or_discouraged || AlreadyConnectedToAddress(addrConnect)) {
3001-
return;
3018+
return false;
30023019
}
30033020
} else if (AlreadyConnectedToHost(pszDest)) {
3004-
return;
3021+
return false;
30053022
}
30063023

3007-
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, use_v2transport);
3024+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, use_v2transport, proxy_override);
30083025

30093026
if (!pnode)
3010-
return;
3027+
return false;
30113028
pnode->grantOutbound = std::move(grant_outbound);
30123029

30133030
m_msgproc->InitializeNode(*pnode, m_local_services);
@@ -3025,6 +3042,8 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
30253042
pnode->ConnectionTypeAsString().c_str(),
30263043
pnode->ConnectedThroughNetwork(),
30273044
GetNodeCount(ConnectionDirection::Out));
3045+
3046+
return true;
30283047
}
30293048

30303049
Mutex NetEventsInterface::g_msgproc_mutex;

src/net.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,28 @@ class CConnman
11481148
bool GetNetworkActive() const { return fNetworkActive; };
11491149
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
11501150
void SetNetworkActive(bool active);
1151-
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CountingSemaphoreGrant<>&& grant_outbound, const char* strDest, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1151+
1152+
/**
1153+
* Open a new P2P connection and initialize it with the PeerManager at `m_msgproc`.
1154+
* @param[in] addrConnect Address to connect to, if `pszDest` is `nullptr`.
1155+
* @param[in] fCountFailure Increment the number of connection attempts to this address in Addrman.
1156+
* @param[in] grant_outbound Take ownership of this grant, to be released later when the connection is closed.
1157+
* @param[in] pszDest Address to resolve and connect to.
1158+
* @param[in] conn_type Type of the connection to open, must not be `ConnectionType::INBOUND`.
1159+
* @param[in] use_v2transport Use P2P encryption, (aka V2 transport, BIP324).
1160+
* @param[in] proxy_override Optional proxy to use and override normal proxy selection.
1161+
* @retval true The connection was opened successfully.
1162+
* @retval false The connection attempt failed.
1163+
*/
1164+
bool OpenNetworkConnection(const CAddress& addrConnect,
1165+
bool fCountFailure,
1166+
CountingSemaphoreGrant<>&& grant_outbound,
1167+
const char* pszDest,
1168+
ConnectionType conn_type,
1169+
bool use_v2transport,
1170+
const std::optional<Proxy>& proxy_override = std::nullopt)
1171+
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1172+
11521173
bool CheckIncomingNonce(uint64_t nonce);
11531174
void ASMapHealthCheck();
11541175

@@ -1399,7 +1420,25 @@ class CConnman
13991420
bool AlreadyConnectedToAddress(const CNetAddr& addr) const;
14001421

14011422
bool AttemptToEvictConnection();
1402-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1423+
1424+
/**
1425+
* Open a new P2P connection.
1426+
* @param[in] addrConnect Address to connect to, if `pszDest` is `nullptr`.
1427+
* @param[in] pszDest Address to resolve and connect to.
1428+
* @param[in] fCountFailure Increment the number of connection attempts to this address in Addrman.
1429+
* @param[in] conn_type Type of the connection to open, must not be `ConnectionType::INBOUND`.
1430+
* @param[in] use_v2transport Use P2P encryption, (aka V2 transport, BIP324).
1431+
* @param[in] proxy_override Optional proxy to use and override normal proxy selection.
1432+
* @return Newly created CNode object or nullptr if the connection failed.
1433+
*/
1434+
CNode* ConnectNode(CAddress addrConnect,
1435+
const char* pszDest,
1436+
bool fCountFailure,
1437+
ConnectionType conn_type,
1438+
bool use_v2transport,
1439+
const std::optional<Proxy>& proxy_override)
1440+
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1441+
14031442
void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
14041443

14051444
void DeleteNode(CNode* pnode);

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
177177
/*addrConnect=*/random_address,
178178
/*fCountFailure=*/fuzzed_data_provider.ConsumeBool(),
179179
/*grant_outbound=*/{},
180-
/*strDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
180+
/*pszDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
181181
/*conn_type=*/conn_type,
182182
/*use_v2transport=*/fuzzed_data_provider.ConsumeBool());
183183
},

src/test/util/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) co
116116

117117
CNode* ConnmanTestMsg::ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
118118
{
119-
CNode* node = ConnectNode(CAddress{}, pszDest, /*fCountFailure=*/false, conn_type, /*use_v2transport=*/true);
119+
CNode* node = ConnectNode(CAddress{}, pszDest, /*fCountFailure=*/false, conn_type, /*use_v2transport=*/true, /*proxy_override=*/std::nullopt);
120120
if (!node) return nullptr;
121121
node->SetCommonVersion(PROTOCOL_VERSION);
122122
peerman.InitializeNode(*node, ServiceFlags(NODE_NETWORK | NODE_WITNESS));

0 commit comments

Comments
 (0)