@@ -452,20 +452,18 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
452
452
return true ;
453
453
}
454
454
455
- bool ConnectSocketDirectly (const CService &addrConnect, SOCKET& hSocketRet, int nTimeout )
455
+ SOCKET CreateSocket (const CService &addrConnect)
456
456
{
457
- hSocketRet = INVALID_SOCKET;
458
-
459
457
struct sockaddr_storage sockaddr;
460
458
socklen_t len = sizeof (sockaddr);
461
459
if (!addrConnect.GetSockAddr ((struct sockaddr *)&sockaddr, &len)) {
462
- LogPrintf (" Cannot connect to %s: unsupported network\n " , addrConnect.ToString ());
463
- return false ;
460
+ LogPrintf (" Cannot create socket for %s: unsupported network\n " , addrConnect.ToString ());
461
+ return INVALID_SOCKET ;
464
462
}
465
463
466
464
SOCKET hSocket = socket (((struct sockaddr *)&sockaddr)->sa_family , SOCK_STREAM, IPPROTO_TCP);
467
465
if (hSocket == INVALID_SOCKET)
468
- return false ;
466
+ return INVALID_SOCKET ;
469
467
470
468
#ifdef SO_NOSIGPIPE
471
469
int set = 1 ;
@@ -479,9 +477,24 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int
479
477
// Set to non-blocking
480
478
if (!SetSocketNonBlocking (hSocket, true )) {
481
479
CloseSocket (hSocket);
482
- return error (" ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n " , NetworkErrorString (WSAGetLastError ()));
480
+ LogPrintf (" ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n " , NetworkErrorString (WSAGetLastError ()));
483
481
}
482
+ return hSocket;
483
+ }
484
484
485
+ bool ConnectSocketDirectly (const CService &addrConnect, SOCKET& hSocket, int nTimeout)
486
+ {
487
+ struct sockaddr_storage sockaddr;
488
+ socklen_t len = sizeof (sockaddr);
489
+ if (hSocket == INVALID_SOCKET) {
490
+ LogPrintf (" Cannot connect to %s: invalid socket\n " , addrConnect.ToString ());
491
+ return false ;
492
+ }
493
+ if (!addrConnect.GetSockAddr ((struct sockaddr *)&sockaddr, &len)) {
494
+ LogPrintf (" Cannot connect to %s: unsupported network\n " , addrConnect.ToString ());
495
+ CloseSocket (hSocket);
496
+ return false ;
497
+ }
485
498
if (connect (hSocket, (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR)
486
499
{
487
500
int nErr = WSAGetLastError ();
@@ -534,8 +547,6 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int
534
547
return false ;
535
548
}
536
549
}
537
-
538
- hSocketRet = hSocket;
539
550
return true ;
540
551
}
541
552
@@ -587,9 +598,8 @@ bool IsProxy(const CNetAddr &addr) {
587
598
return false ;
588
599
}
589
600
590
- bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocketRet , int nTimeout, bool *outProxyConnectionFailed)
601
+ bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocket , int nTimeout, bool *outProxyConnectionFailed)
591
602
{
592
- SOCKET hSocket = INVALID_SOCKET;
593
603
// first connect to proxy server
594
604
if (!ConnectSocketDirectly (proxy.proxy , hSocket, nTimeout)) {
595
605
if (outProxyConnectionFailed)
@@ -601,14 +611,16 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
601
611
ProxyCredentials random_auth;
602
612
static std::atomic_int counter (0 );
603
613
random_auth.username = random_auth.password = strprintf (" %i" , counter++);
604
- if (!Socks5 (strDest, (unsigned short )port, &random_auth, hSocket))
614
+ if (!Socks5 (strDest, (unsigned short )port, &random_auth, hSocket)) {
615
+ CloseSocket (hSocket);
605
616
return false ;
617
+ }
606
618
} else {
607
- if (!Socks5 (strDest, (unsigned short )port, 0 , hSocket))
619
+ if (!Socks5 (strDest, (unsigned short )port, 0 , hSocket)) {
620
+ CloseSocket (hSocket);
608
621
return false ;
622
+ }
609
623
}
610
-
611
- hSocketRet = hSocket;
612
624
return true ;
613
625
}
614
626
bool LookupSubNet (const char * pszName, CSubNet& ret)
0 commit comments