@@ -317,12 +317,11 @@ std::string Socks5ErrorString(uint8_t err)
317
317
}
318
318
319
319
/* * Connect using SOCKS5 (as described in RFC1928) */
320
- static bool Socks5 (const std::string& strDest, int port, const ProxyCredentials *auth, SOCKET& hSocket)
320
+ static bool Socks5 (const std::string& strDest, int port, const ProxyCredentials *auth, const SOCKET& hSocket)
321
321
{
322
322
IntrRecvError recvr;
323
323
LogPrint (BCLog::NET, " SOCKS5 connecting %s\n " , strDest);
324
324
if (strDest.size () > 255 ) {
325
- CloseSocket (hSocket);
326
325
return error (" Hostname too long" );
327
326
}
328
327
// Accepted authentication methods
@@ -338,17 +337,14 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
338
337
}
339
338
ssize_t ret = send (hSocket, (const char *)vSocks5Init.data (), vSocks5Init.size (), MSG_NOSIGNAL);
340
339
if (ret != (ssize_t )vSocks5Init.size ()) {
341
- CloseSocket (hSocket);
342
340
return error (" Error sending to proxy" );
343
341
}
344
342
uint8_t pchRet1[2 ];
345
343
if ((recvr = InterruptibleRecv (pchRet1, 2 , SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
346
- CloseSocket (hSocket);
347
344
LogPrintf (" Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n " , strDest, port);
348
345
return false ;
349
346
}
350
347
if (pchRet1[0 ] != SOCKSVersion::SOCKS5) {
351
- CloseSocket (hSocket);
352
348
return error (" Proxy failed to initialize" );
353
349
}
354
350
if (pchRet1[1 ] == SOCKS5Method::USER_PASS && auth) {
@@ -363,23 +359,19 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
363
359
vAuth.insert (vAuth.end (), auth->password .begin (), auth->password .end ());
364
360
ret = send (hSocket, (const char *)vAuth.data (), vAuth.size (), MSG_NOSIGNAL);
365
361
if (ret != (ssize_t )vAuth.size ()) {
366
- CloseSocket (hSocket);
367
362
return error (" Error sending authentication to proxy" );
368
363
}
369
364
LogPrint (BCLog::PROXY, " SOCKS5 sending proxy authentication %s:%s\n " , auth->username , auth->password );
370
365
uint8_t pchRetA[2 ];
371
366
if ((recvr = InterruptibleRecv (pchRetA, 2 , SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
372
- CloseSocket (hSocket);
373
367
return error (" Error reading proxy authentication response" );
374
368
}
375
369
if (pchRetA[0 ] != 0x01 || pchRetA[1 ] != 0x00 ) {
376
- CloseSocket (hSocket);
377
370
return error (" Proxy authentication unsuccessful" );
378
371
}
379
372
} else if (pchRet1[1 ] == SOCKS5Method::NOAUTH) {
380
373
// Perform no authentication
381
374
} else {
382
- CloseSocket (hSocket);
383
375
return error (" Proxy requested wrong authentication method %02x" , pchRet1[1 ]);
384
376
}
385
377
std::vector<uint8_t > vSocks5;
@@ -393,12 +385,10 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
393
385
vSocks5.push_back ((port >> 0 ) & 0xFF );
394
386
ret = send (hSocket, (const char *)vSocks5.data (), vSocks5.size (), MSG_NOSIGNAL);
395
387
if (ret != (ssize_t )vSocks5.size ()) {
396
- CloseSocket (hSocket);
397
388
return error (" Error sending to proxy" );
398
389
}
399
390
uint8_t pchRet2[4 ];
400
391
if ((recvr = InterruptibleRecv (pchRet2, 4 , SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
401
- CloseSocket (hSocket);
402
392
if (recvr == IntrRecvError::Timeout) {
403
393
/* If a timeout happens here, this effectively means we timed out while connecting
404
394
* to the remote node. This is very common for Tor, so do not print an
@@ -409,17 +399,14 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
409
399
}
410
400
}
411
401
if (pchRet2[0 ] != SOCKSVersion::SOCKS5) {
412
- CloseSocket (hSocket);
413
402
return error (" Proxy failed to accept request" );
414
403
}
415
404
if (pchRet2[1 ] != SOCKS5Reply::SUCCEEDED) {
416
405
// Failures to connect to a peer that are not proxy errors
417
- CloseSocket (hSocket);
418
406
LogPrintf (" Socks5() connect to %s:%d failed: %s\n " , strDest, port, Socks5ErrorString (pchRet2[1 ]));
419
407
return false ;
420
408
}
421
409
if (pchRet2[2 ] != 0x00 ) { // Reserved field must be 0
422
- CloseSocket (hSocket);
423
410
return error (" Error: malformed proxy response" );
424
411
}
425
412
uint8_t pchRet3[256 ];
@@ -431,21 +418,18 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
431
418
{
432
419
recvr = InterruptibleRecv (pchRet3, 1 , SOCKS5_RECV_TIMEOUT, hSocket);
433
420
if (recvr != IntrRecvError::OK) {
434
- CloseSocket (hSocket);
435
421
return error (" Error reading from proxy" );
436
422
}
437
423
int nRecv = pchRet3[0 ];
438
424
recvr = InterruptibleRecv (pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
439
425
break ;
440
426
}
441
- default : CloseSocket (hSocket); return error (" Error: malformed proxy response" );
427
+ default : return error (" Error: malformed proxy response" );
442
428
}
443
429
if (recvr != IntrRecvError::OK) {
444
- CloseSocket (hSocket);
445
430
return error (" Error reading from proxy" );
446
431
}
447
432
if ((recvr = InterruptibleRecv (pchRet3, 2 , SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
448
- CloseSocket (hSocket);
449
433
return error (" Error reading from proxy" );
450
434
}
451
435
LogPrint (BCLog::NET, " SOCKS5 connected %s\n " , strDest);
@@ -488,7 +472,7 @@ SOCKET CreateSocket(const CService &addrConnect)
488
472
return hSocket;
489
473
}
490
474
491
- bool ConnectSocketDirectly (const CService &addrConnect, SOCKET& hSocket, int nTimeout)
475
+ bool ConnectSocketDirectly (const CService &addrConnect, const SOCKET& hSocket, int nTimeout)
492
476
{
493
477
struct sockaddr_storage sockaddr;
494
478
socklen_t len = sizeof (sockaddr);
@@ -498,7 +482,6 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocket, int nTi
498
482
}
499
483
if (!addrConnect.GetSockAddr ((struct sockaddr *)&sockaddr, &len)) {
500
484
LogPrintf (" Cannot connect to %s: unsupported network\n " , addrConnect.ToString ());
501
- CloseSocket (hSocket);
502
485
return false ;
503
486
}
504
487
if (connect (hSocket, (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR)
@@ -515,13 +498,11 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocket, int nTi
515
498
if (nRet == 0 )
516
499
{
517
500
LogPrint (BCLog::NET, " connection to %s timeout\n " , addrConnect.ToString ());
518
- CloseSocket (hSocket);
519
501
return false ;
520
502
}
521
503
if (nRet == SOCKET_ERROR)
522
504
{
523
505
LogPrintf (" select() for %s failed: %s\n " , addrConnect.ToString (), NetworkErrorString (WSAGetLastError ()));
524
- CloseSocket (hSocket);
525
506
return false ;
526
507
}
527
508
socklen_t nRetSize = sizeof (nRet);
@@ -532,13 +513,11 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocket, int nTi
532
513
#endif
533
514
{
534
515
LogPrintf (" getsockopt() for %s failed: %s\n " , addrConnect.ToString (), NetworkErrorString (WSAGetLastError ()));
535
- CloseSocket (hSocket);
536
516
return false ;
537
517
}
538
518
if (nRet != 0 )
539
519
{
540
520
LogPrintf (" connect() to %s failed after select(): %s\n " , addrConnect.ToString (), NetworkErrorString (nRet));
541
- CloseSocket (hSocket);
542
521
return false ;
543
522
}
544
523
}
@@ -549,7 +528,6 @@ bool ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocket, int nTi
549
528
#endif
550
529
{
551
530
LogPrintf (" connect() to %s failed: %s\n " , addrConnect.ToString (), NetworkErrorString (WSAGetLastError ()));
552
- CloseSocket (hSocket);
553
531
return false ;
554
532
}
555
533
}
@@ -604,7 +582,7 @@ bool IsProxy(const CNetAddr &addr) {
604
582
return false ;
605
583
}
606
584
607
- bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocket, int nTimeout, bool *outProxyConnectionFailed)
585
+ bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool *outProxyConnectionFailed)
608
586
{
609
587
// first connect to proxy server
610
588
if (!ConnectSocketDirectly (proxy.proxy , hSocket, nTimeout)) {
@@ -618,12 +596,10 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
618
596
static std::atomic_int counter (0 );
619
597
random_auth.username = random_auth.password = strprintf (" %i" , counter++);
620
598
if (!Socks5 (strDest, (unsigned short )port, &random_auth, hSocket)) {
621
- CloseSocket (hSocket);
622
599
return false ;
623
600
}
624
601
} else {
625
602
if (!Socks5 (strDest, (unsigned short )port, 0 , hSocket)) {
626
- CloseSocket (hSocket);
627
603
return false ;
628
604
}
629
605
}
0 commit comments