@@ -537,12 +537,12 @@ static void LogConnectFailure(bool manual_connection, const char* fmt, const Arg
537
537
}
538
538
}
539
539
540
- bool ConnectSocketDirectly (const CService &addrConnect, const SOCKET& hSocket , int nTimeout, bool manual_connection)
540
+ bool ConnectSocketDirectly (const CService &addrConnect, const Sock& sock , int nTimeout, bool manual_connection)
541
541
{
542
542
// Create a sockaddr from the specified service.
543
543
struct sockaddr_storage sockaddr;
544
544
socklen_t len = sizeof (sockaddr);
545
- if (hSocket == INVALID_SOCKET) {
545
+ if (sock. Get () == INVALID_SOCKET) {
546
546
LogPrintf (" Cannot connect to %s: invalid socket\n " , addrConnect.ToString ());
547
547
return false ;
548
548
}
@@ -552,55 +552,42 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
552
552
}
553
553
554
554
// Connect to the addrConnect service on the hSocket socket.
555
- if (connect (hSocket, (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR)
556
- {
555
+ if (sock.Connect (reinterpret_cast <struct sockaddr *>(&sockaddr), len) == SOCKET_ERROR) {
557
556
int nErr = WSAGetLastError ();
558
557
// WSAEINVAL is here because some legacy version of winsock uses it
559
558
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
560
559
{
561
560
// Connection didn't actually fail, but is being established
562
561
// asynchronously. Thus, use async I/O api (select/poll)
563
562
// synchronously to check for successful connection with a timeout.
564
- #ifdef USE_POLL
565
- struct pollfd pollfd = {};
566
- pollfd.fd = hSocket;
567
- pollfd.events = POLLIN | POLLOUT;
568
- int nRet = poll (&pollfd, 1 , nTimeout);
569
- #else
570
- struct timeval timeout = MillisToTimeval (nTimeout);
571
- fd_set fdset;
572
- FD_ZERO (&fdset);
573
- FD_SET (hSocket, &fdset);
574
- int nRet = select (hSocket + 1 , nullptr , &fdset, nullptr , &timeout);
575
- #endif
576
- // Upon successful completion, both select and poll return the total
577
- // number of file descriptors that have been selected. A value of 0
578
- // indicates that the call timed out and no file descriptors have
579
- // been selected.
580
- if (nRet == 0 )
581
- {
582
- LogPrint (BCLog::NET, " connection to %s timeout\n " , addrConnect.ToString ());
563
+ const Sock::Event requested = Sock::RECV | Sock::SEND;
564
+ Sock::Event occurred;
565
+ if (!sock.Wait (std::chrono::milliseconds{nTimeout}, requested, &occurred)) {
566
+ LogPrintf (" wait for connect to %s failed: %s\n " ,
567
+ addrConnect.ToString (),
568
+ NetworkErrorString (WSAGetLastError ()));
583
569
return false ;
584
- }
585
- if (nRet == SOCKET_ERROR)
586
- {
587
- LogPrintf (" select() for %s failed: %s\n " , addrConnect.ToString (), NetworkErrorString (WSAGetLastError ()));
570
+ } else if (occurred == 0 ) {
571
+ LogPrint (BCLog::NET, " connection attempt to %s timed out\n " , addrConnect.ToString ());
588
572
return false ;
589
573
}
590
574
591
- // Even if the select/poll was successful, the connect might not
575
+ // Even if the wait was successful, the connect might not
592
576
// have been successful. The reason for this failure is hidden away
593
577
// in the SO_ERROR for the socket in modern systems. We read it into
594
- // nRet here.
595
- socklen_t nRetSize = sizeof (nRet);
596
- if (getsockopt (hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
597
- {
578
+ // sockerr here.
579
+ int sockerr;
580
+ socklen_t sockerr_len = sizeof (sockerr);
581
+ if (sock.GetSockOpt (SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
582
+ SOCKET_ERROR) {
598
583
LogPrintf (" getsockopt() for %s failed: %s\n " , addrConnect.ToString (), NetworkErrorString (WSAGetLastError ()));
599
584
return false ;
600
585
}
601
- if (nRet != 0 )
602
- {
603
- LogConnectFailure (manual_connection, " connect() to %s failed after select(): %s" , addrConnect.ToString (), NetworkErrorString (nRet));
586
+ if (sockerr != 0 ) {
587
+ LogConnectFailure (manual_connection,
588
+ " connect() to %s failed after wait: %s" ,
589
+ addrConnect.ToString (),
590
+ NetworkErrorString (sockerr));
604
591
return false ;
605
592
}
606
593
}
@@ -668,7 +655,7 @@ bool IsProxy(const CNetAddr &addr) {
668
655
bool ConnectThroughProxy (const proxyType& proxy, const std::string& strDest, int port, const Sock& sock, int nTimeout, bool & outProxyConnectionFailed)
669
656
{
670
657
// first connect to proxy server
671
- if (!ConnectSocketDirectly (proxy.proxy , sock. Get () , nTimeout, true )) {
658
+ if (!ConnectSocketDirectly (proxy.proxy , sock, nTimeout, true )) {
672
659
outProxyConnectionFailed = true ;
673
660
return false ;
674
661
}
0 commit comments