@@ -380,19 +380,16 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
380
380
pszDest ? pszDest : addrConnect.ToString (),
381
381
pszDest ? 0.0 : (double )(GetAdjustedTime () - addrConnect.nTime )/3600.0 );
382
382
383
- // Connect
384
- SOCKET hSocket;
385
- bool proxyConnectionFailed = false ;
386
- if (pszDest ? ConnectSocketByName (addrConnect, hSocket, pszDest, Params ().GetDefaultPort (), nConnectTimeout, &proxyConnectionFailed) :
387
- ConnectSocket (addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
388
- {
389
- if (!IsSelectableSocket (hSocket)) {
390
- LogPrintf (" Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n " );
391
- CloseSocket (hSocket);
392
- return nullptr ;
393
- }
394
-
395
- if (pszDest && addrConnect.IsValid ()) {
383
+ // Resolve
384
+ const int default_port = Params ().GetDefaultPort ();
385
+ if (pszDest) {
386
+ std::vector<CService> resolved;
387
+ if (Lookup (pszDest, resolved, default_port, fNameLookup && !HaveNameProxy (), 256 ) && !resolved.empty ()) {
388
+ addrConnect = CAddress (resolved[GetRand (resolved.size ())], NODE_NONE);
389
+ if (!addrConnect.IsValid ()) {
390
+ LogPrint (BCLog::NET, " Resolver returned invalid address %s for %s" , addrConnect.ToString (), pszDest);
391
+ return nullptr ;
392
+ }
396
393
// It is possible that we already have a connection to the IP/port pszDest resolved to.
397
394
// In that case, drop the connection that was just created, and return the existing CNode instead.
398
395
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
@@ -402,13 +399,40 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
402
399
if (pnode)
403
400
{
404
401
pnode->MaybeSetAddrName (std::string (pszDest));
405
- CloseSocket (hSocket);
406
402
LogPrintf (" Failed to open new connection, already connected\n " );
407
403
return nullptr ;
408
404
}
409
405
}
406
+ }
410
407
411
- addrman.Attempt (addrConnect, fCountFailure );
408
+ // Connect
409
+ bool connected = false ;
410
+ SOCKET hSocket;
411
+ proxyType proxy;
412
+ if (addrConnect.IsValid ()) {
413
+ bool proxyConnectionFailed = false ;
414
+
415
+ if (GetProxy (addrConnect.GetNetwork (), proxy))
416
+ connected = ConnectThroughProxy (proxy, addrConnect.ToStringIP (), addrConnect.GetPort (), hSocket, nConnectTimeout, &proxyConnectionFailed);
417
+ else // no proxy needed (none set for target network)
418
+ connected = ConnectSocketDirectly (addrConnect, hSocket, nConnectTimeout);
419
+ if (!proxyConnectionFailed) {
420
+ // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
421
+ // the proxy, mark this as an attempt.
422
+ addrman.Attempt (addrConnect, fCountFailure );
423
+ }
424
+ } else if (pszDest && GetNameProxy (proxy)) {
425
+ std::string host;
426
+ int port = default_port;
427
+ SplitHostPort (std::string (pszDest), port, host);
428
+ connected = ConnectThroughProxy (proxy, host, port, hSocket, nConnectTimeout, nullptr );
429
+ }
430
+ if (connected) {
431
+ if (!IsSelectableSocket (hSocket)) {
432
+ LogPrintf (" Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n " );
433
+ CloseSocket (hSocket);
434
+ return nullptr ;
435
+ }
412
436
413
437
// Add node
414
438
NodeId id = GetNewNodeId ();
@@ -419,10 +443,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
419
443
pnode->AddRef ();
420
444
421
445
return pnode;
422
- } else if (!proxyConnectionFailed) {
423
- // If connecting to the node failed, and failure is not caused by a problem connecting to
424
- // the proxy, mark this as an attempt.
425
- addrman.Attempt (addrConnect, fCountFailure );
426
446
}
427
447
428
448
return nullptr ;
0 commit comments