From 5d562430de90ec7f0bc6df2828bb7cd7d374e980 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Wed, 20 May 2026 13:31:38 +0200 Subject: [PATCH] netbase: Add timeout parameter to ConnectDirectly Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com> --- src/netbase.cpp | 25 +++++++++++++++++++++---- src/netbase.h | 6 ++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/netbase.cpp b/src/netbase.cpp index 5434ec9f14c..65d8c28d63a 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 ConnectDirectly(const CService& dest, bool manual_connection) +{ + return ConnectDirectly(dest, manual_connection, std::chrono::milliseconds{nConnectTimeout}); +} + +std::unique_ptr 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 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 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 88cc69810b0..af51853a51a 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -305,6 +306,11 @@ extern std::function(int, int, int)> CreateSock; */ std::unique_ptr 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 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.