netbase: Add timeout parameter to ConnectDirectly
What changed, and why it matters
This commit is a straightforward code cleanup: it adds a timeout parameter to a low-level network connection helper function. Existing callers keep using the same default timeout they used before, so observable behavior is unchanged. There is no indication this fixes a security bug.
No security action required; treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors ConnectDirectly in src/netbase.cpp/h to accept an explicit std::chrono::milliseconds timeout instead of hardcoding nConnectTimeout inside ConnectToSocket. A new overload preserves the old signature and forwards nConnectTimeout as the default. Proxy::Connect() and the original ConnectDirectly continue to use nConnectTimeout. No security boundary, resource limit, or trust assumption is altered.
Changed components
src/netbase.cppsrc/netbase.hInspect captured patch +27 / −4
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 5434ec9f..65d8c28d 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -587,7 +587,12 @@ static void LogConnectFailure(bool manual_connection, util::ConstevalFormatStrin
}
}
-static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen_t len, const std::string& dest_str, bool manual_connection)
+static bool ConnectToSocket(const Sock& sock,
+ struct sockaddr* sockaddr,
+ socklen_t len,
+ const std::string& dest_str,
+ bool manual_connection,
+ std::chrono::milliseconds timeout)
{
// Connect to `sockaddr` using `sock`.
if (sock.Connect(sockaddr, len) == SOCKET_ERROR) {
@@ -600,7 +605,7 @@ static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen
// synchronously to check for successful connection with a timeout.
const Sock::Event requested = Sock::RECV | Sock::SEND;
Sock::Event occurred;
- if (!sock.Wait(std::chrono::milliseconds{nConnectTimeout}, requested, &occurred)) {
+ if (!sock.Wait(timeout, requested, &occurred)) {
LogInfo("wait for connect to %s failed: %s\n",
dest_str,
NetworkErrorString(WSAGetLastError()));
@@ -643,6 +648,13 @@ static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen
}
std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection)
+{
+ return ConnectDirectly(dest, manual_connection, std::chrono::milliseconds{nConnectTimeout});
+}
+
+std::unique_ptr<Sock> ConnectDirectly(const CService& dest,
+ bool manual_connection,
+ std::chrono::milliseconds timeout)
{
auto sock = CreateSock(dest.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP);
if (!sock) {
@@ -658,7 +670,7 @@ std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connecti
return {};
}
- if (!ConnectToSocket(*sock, (struct sockaddr*)&sockaddr, len, dest.ToStringAddrPort(), manual_connection)) {
+ if (!ConnectToSocket(*sock, (struct sockaddr*)&sockaddr, len, dest.ToStringAddrPort(), manual_connection, timeout)) {
return {};
}
@@ -687,7 +699,12 @@ std::unique_ptr<Sock> Proxy::Connect() const
memcpy(addrun.sun_path, path.c_str(), std::min(sizeof(addrun.sun_path) - 1, path.length()));
socklen_t len = sizeof(addrun);
- if(!ConnectToSocket(*sock, (struct sockaddr*)&addrun, len, path, /*manual_connection=*/true)) {
+ if (!ConnectToSocket(*sock,
+ (struct sockaddr*)&addrun,
+ len,
+ path,
+ /*manual_connection=*/true,
+ std::chrono::milliseconds{nConnectTimeout})) {
return {};
}
diff --git a/src/netbase.h b/src/netbase.h
index 88cc6981..af51853a 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -11,6 +11,7 @@
#include <util/sock.h>
#include <util/threadinterrupt.h>
+#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
@@ -305,6 +306,11 @@ extern std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock;
*/
std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection);
+/** Create a socket and try to connect to the specified service, using the provided timeout. */
+std::unique_ptr<Sock> ConnectDirectly(const CService& dest,
+ bool manual_connection,
+ std::chrono::milliseconds timeout);
+
/**
* Connect to a specified destination service through a SOCKS5 proxy by first
* connecting to the SOCKS5 proxy.
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.