Skip to content

Commit 3a7d654

Browse files
committed
net: move CreateSock() calls from ConnectNode() to netbase methods
1 parent 74f568c commit 3a7d654

File tree

4 files changed

+59
-72
lines changed

4 files changed

+59
-72
lines changed

src/i2p.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,9 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
326326

327327
std::unique_ptr<Sock> Session::Hello() const
328328
{
329-
auto sock = CreateSock(m_control_host.GetSAFamily());
329+
auto sock = ConnectDirectly(m_control_host, true);
330330

331331
if (!sock) {
332-
throw std::runtime_error("Cannot create socket");
333-
}
334-
335-
if (!ConnectSocketDirectly(m_control_host, *sock, nConnectTimeout, true)) {
336332
throw std::runtime_error(strprintf("Cannot connect to %s", m_control_host.ToStringAddrPort()));
337333
}
338334

src/net.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
442442
}
443443

444444
// Connect
445-
bool connected = false;
446445
std::unique_ptr<Sock> sock;
447446
Proxy proxy;
448447
CAddress addr_bind;
@@ -455,6 +454,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
455454

456455
if (addrConnect.IsI2P() && use_proxy) {
457456
i2p::Connection conn;
457+
bool connected{false};
458458

459459
if (m_i2p_sam_session) {
460460
connected = m_i2p_sam_session->Connect(addrConnect, conn, proxyConnectionFailed);
@@ -483,40 +483,25 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
483483
addr_bind = CAddress{conn.me, NODE_NONE};
484484
}
485485
} else if (use_proxy) {
486-
sock = CreateSock(proxy.proxy.GetSAFamily());
487-
if (!sock) {
488-
return nullptr;
489-
}
490486
LogPrintLevel(BCLog::PROXY, BCLog::Level::Debug, "Using proxy: %s to connect to %s:%s\n", proxy.proxy.ToStringAddrPort(), addrConnect.ToStringAddr(), addrConnect.GetPort());
491-
connected = ConnectThroughProxy(proxy, addrConnect.ToStringAddr(), addrConnect.GetPort(),
492-
*sock, nConnectTimeout, proxyConnectionFailed);
487+
sock = ConnectThroughProxy(proxy, addrConnect.ToStringAddr(), addrConnect.GetPort(), proxyConnectionFailed);
493488
} else {
494489
// no proxy needed (none set for target network)
495-
sock = CreateSock(addrConnect.GetSAFamily());
496-
if (!sock) {
497-
return nullptr;
498-
}
499-
connected = ConnectSocketDirectly(addrConnect, *sock, nConnectTimeout,
500-
conn_type == ConnectionType::MANUAL);
490+
sock = ConnectDirectly(addrConnect, conn_type == ConnectionType::MANUAL);
501491
}
502492
if (!proxyConnectionFailed) {
503493
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
504494
// the proxy, mark this as an attempt.
505495
addrman.Attempt(addrConnect, fCountFailure);
506496
}
507497
} else if (pszDest && GetNameProxy(proxy)) {
508-
sock = CreateSock(proxy.proxy.GetSAFamily());
509-
if (!sock) {
510-
return nullptr;
511-
}
512498
std::string host;
513499
uint16_t port{default_port};
514500
SplitHostPort(std::string(pszDest), port, host);
515501
bool proxyConnectionFailed;
516-
connected = ConnectThroughProxy(proxy, host, port, *sock, nConnectTimeout,
517-
proxyConnectionFailed);
502+
sock = ConnectThroughProxy(proxy, host, port, proxyConnectionFailed);
518503
}
519-
if (!connected) {
504+
if (!sock) {
520505
return nullptr;
521506
}
522507

src/netbase.cpp

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -517,18 +517,24 @@ static void LogConnectFailure(bool manual_connection, const char* fmt, const Arg
517517
}
518518
}
519519

520-
bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection)
520+
std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection)
521521
{
522+
auto sock = CreateSock(dest.GetSAFamily());
523+
if (!sock) {
524+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "Cannot create a socket for connecting to %s\n", dest.ToStringAddrPort());
525+
return {};
526+
}
527+
522528
// Create a sockaddr from the specified service.
523529
struct sockaddr_storage sockaddr;
524530
socklen_t len = sizeof(sockaddr);
525-
if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
526-
LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToStringAddrPort());
527-
return false;
531+
if (!dest.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
532+
LogPrintf("Cannot connect to %s: unsupported network\n", dest.ToStringAddrPort());
533+
return {};
528534
}
529535

530-
// Connect to the addrConnect service on the hSocket socket.
531-
if (sock.Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
536+
// Connect to the dest service on the hSocket socket.
537+
if (sock->Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
532538
int nErr = WSAGetLastError();
533539
// WSAEINVAL is here because some legacy version of winsock uses it
534540
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
@@ -538,14 +544,14 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
538544
// synchronously to check for successful connection with a timeout.
539545
const Sock::Event requested = Sock::RECV | Sock::SEND;
540546
Sock::Event occurred;
541-
if (!sock.Wait(std::chrono::milliseconds{nTimeout}, requested, &occurred)) {
547+
if (!sock->Wait(std::chrono::milliseconds{nConnectTimeout}, requested, &occurred)) {
542548
LogPrintf("wait for connect to %s failed: %s\n",
543-
addrConnect.ToStringAddrPort(),
549+
dest.ToStringAddrPort(),
544550
NetworkErrorString(WSAGetLastError()));
545-
return false;
551+
return {};
546552
} else if (occurred == 0) {
547-
LogPrint(BCLog::NET, "connection attempt to %s timed out\n", addrConnect.ToStringAddrPort());
548-
return false;
553+
LogPrint(BCLog::NET, "connection attempt to %s timed out\n", dest.ToStringAddrPort());
554+
return {};
549555
}
550556

551557
// Even if the wait was successful, the connect might not
@@ -554,17 +560,17 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
554560
// sockerr here.
555561
int sockerr;
556562
socklen_t sockerr_len = sizeof(sockerr);
557-
if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
563+
if (sock->GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
558564
SOCKET_ERROR) {
559-
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToStringAddrPort(), NetworkErrorString(WSAGetLastError()));
560-
return false;
565+
LogPrintf("getsockopt() for %s failed: %s\n", dest.ToStringAddrPort(), NetworkErrorString(WSAGetLastError()));
566+
return {};
561567
}
562568
if (sockerr != 0) {
563569
LogConnectFailure(manual_connection,
564570
"connect() to %s failed after wait: %s",
565-
addrConnect.ToStringAddrPort(),
571+
dest.ToStringAddrPort(),
566572
NetworkErrorString(sockerr));
567-
return false;
573+
return {};
568574
}
569575
}
570576
#ifdef WIN32
@@ -573,11 +579,11 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
573579
else
574580
#endif
575581
{
576-
LogConnectFailure(manual_connection, "connect() to %s failed: %s", addrConnect.ToStringAddrPort(), NetworkErrorString(WSAGetLastError()));
577-
return false;
582+
LogConnectFailure(manual_connection, "connect() to %s failed: %s", dest.ToStringAddrPort(), NetworkErrorString(WSAGetLastError()));
583+
return {};
578584
}
579585
}
580-
return true;
586+
return sock;
581587
}
582588

583589
bool SetProxy(enum Network net, const Proxy &addrProxy) {
@@ -628,27 +634,32 @@ bool IsProxy(const CNetAddr &addr) {
628634
return false;
629635
}
630636

631-
bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
637+
std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
638+
const std::string& dest,
639+
uint16_t port,
640+
bool& proxy_connection_failed)
632641
{
633642
// first connect to proxy server
634-
if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) {
635-
outProxyConnectionFailed = true;
636-
return false;
643+
auto sock = ConnectDirectly(proxy.proxy, /*manual_connection=*/true);
644+
if (!sock) {
645+
proxy_connection_failed = true;
646+
return {};
637647
}
648+
638649
// do socks negotiation
639650
if (proxy.randomize_credentials) {
640651
ProxyCredentials random_auth;
641652
static std::atomic_int counter(0);
642653
random_auth.username = random_auth.password = strprintf("%i", counter++);
643-
if (!Socks5(strDest, port, &random_auth, sock)) {
644-
return false;
654+
if (!Socks5(dest, port, &random_auth, *sock)) {
655+
return {};
645656
}
646657
} else {
647-
if (!Socks5(strDest, port, nullptr, sock)) {
648-
return false;
658+
if (!Socks5(dest, port, nullptr, *sock)) {
659+
return {};
649660
}
650661
}
651-
return true;
662+
return sock;
652663
}
653664

654665
CSubNet LookupSubNet(const std::string& subnet_str)

src/netbase.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,30 @@ std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family);
241241
extern std::function<std::unique_ptr<Sock>(const sa_family_t&)> CreateSock;
242242

243243
/**
244-
* Try to connect to the specified service on the specified socket.
244+
* Create a socket and try to connect to the specified service.
245245
*
246-
* @param addrConnect The service to which to connect.
247-
* @param sock The socket on which to connect.
248-
* @param nTimeout Wait this many milliseconds for the connection to be
249-
* established.
250-
* @param manual_connection Whether or not the connection was manually requested
251-
* (e.g. through the addnode RPC)
246+
* @param[in] dest The service to which to connect.
247+
* @param[in] manual_connection Whether or not the connection was manually requested (e.g. through the addnode RPC)
252248
*
253-
* @returns Whether or not a connection was successfully made.
249+
* @returns the connected socket if the operation succeeded, empty unique_ptr otherwise
254250
*/
255-
bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
251+
std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection);
256252

257253
/**
258254
* Connect to a specified destination service through a SOCKS5 proxy by first
259255
* connecting to the SOCKS5 proxy.
260256
*
261-
* @param proxy The SOCKS5 proxy.
262-
* @param strDest The destination service to which to connect.
263-
* @param port The destination port.
264-
* @param sock The socket on which to connect to the SOCKS5 proxy.
265-
* @param nTimeout Wait this many milliseconds for the connection to the SOCKS5
266-
* proxy to be established.
267-
* @param[out] outProxyConnectionFailed Whether or not the connection to the
268-
* SOCKS5 proxy failed.
257+
* @param[in] proxy The SOCKS5 proxy.
258+
* @param[in] dest The destination service to which to connect.
259+
* @param[in] port The destination port.
260+
* @param[out] proxy_connection_failed Whether or not the connection to the SOCKS5 proxy failed.
269261
*
270-
* @returns Whether or not the operation succeeded.
262+
* @returns the connected socket if the operation succeeded. Otherwise an empty unique_ptr.
271263
*/
272-
bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
264+
std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
265+
const std::string& dest,
266+
uint16_t port,
267+
bool& proxy_connection_failed);
273268

274269
/**
275270
* Interrupt SOCKS5 reads or writes.

0 commit comments

Comments
 (0)