@@ -385,19 +385,16 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
385
385
pszDest ? pszDest : addrConnect.ToString (),
386
386
pszDest ? 0.0 : (double )(GetAdjustedTime () - addrConnect.nTime )/3600.0 );
387
387
388
- // Connect
389
- SOCKET hSocket;
390
- bool proxyConnectionFailed = false ;
391
- if (pszDest ? ConnectSocketByName (addrConnect, hSocket, pszDest, Params ().GetDefaultPort (), nConnectTimeout, &proxyConnectionFailed) :
392
- ConnectSocket (addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
393
- {
394
- if (!IsSelectableSocket (hSocket)) {
395
- LogPrintf (" Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n " );
396
- CloseSocket (hSocket);
397
- return nullptr ;
398
- }
399
-
400
- if (pszDest && addrConnect.IsValid ()) {
388
+ // Resolve
389
+ const int default_port = Params ().GetDefaultPort ();
390
+ if (pszDest) {
391
+ std::vector<CService> resolved;
392
+ if (Lookup (pszDest, resolved, default_port, fNameLookup && !HaveNameProxy (), 256 ) && !resolved.empty ()) {
393
+ addrConnect = CAddress (resolved[GetRand (resolved.size ())], NODE_NONE);
394
+ if (!addrConnect.IsValid ()) {
395
+ LogPrint (BCLog::NET, " Resolver returned invalid address %s for %s" , addrConnect.ToString (), pszDest);
396
+ return nullptr ;
397
+ }
401
398
// It is possible that we already have a connection to the IP/port pszDest resolved to.
402
399
// In that case, drop the connection that was just created, and return the existing CNode instead.
403
400
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
@@ -407,13 +404,40 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
407
404
if (pnode)
408
405
{
409
406
pnode->MaybeSetAddrName (std::string (pszDest));
410
- CloseSocket (hSocket);
411
407
LogPrintf (" Failed to open new connection, already connected\n " );
412
408
return nullptr ;
413
409
}
414
410
}
411
+ }
415
412
416
- addrman.Attempt (addrConnect, fCountFailure );
413
+ // Connect
414
+ bool connected = false ;
415
+ SOCKET hSocket;
416
+ proxyType proxy;
417
+ if (addrConnect.IsValid ()) {
418
+ bool proxyConnectionFailed = false ;
419
+
420
+ if (GetProxy (addrConnect.GetNetwork (), proxy))
421
+ connected = ConnectThroughProxy (proxy, addrConnect.ToStringIP (), addrConnect.GetPort (), hSocket, nConnectTimeout, &proxyConnectionFailed);
422
+ else // no proxy needed (none set for target network)
423
+ connected = ConnectSocketDirectly (addrConnect, hSocket, nConnectTimeout);
424
+ if (!proxyConnectionFailed) {
425
+ // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
426
+ // the proxy, mark this as an attempt.
427
+ addrman.Attempt (addrConnect, fCountFailure );
428
+ }
429
+ } else if (pszDest && GetNameProxy (proxy)) {
430
+ std::string host;
431
+ int port = default_port;
432
+ SplitHostPort (std::string (pszDest), port, host);
433
+ connected = ConnectThroughProxy (proxy, host, port, hSocket, nConnectTimeout, nullptr );
434
+ }
435
+ if (connected) {
436
+ if (!IsSelectableSocket (hSocket)) {
437
+ LogPrintf (" Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n " );
438
+ CloseSocket (hSocket);
439
+ return nullptr ;
440
+ }
417
441
418
442
// Add node
419
443
NodeId id = GetNewNodeId ();
@@ -424,10 +448,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
424
448
pnode->AddRef ();
425
449
426
450
return pnode;
427
- } else if (!proxyConnectionFailed) {
428
- // If connecting to the node failed, and failure is not caused by a problem connecting to
429
- // the proxy, mark this as an attempt.
430
- addrman.Attempt (addrConnect, fCountFailure );
431
451
}
432
452
433
453
return nullptr ;
@@ -1912,11 +1932,9 @@ void CConnman::ThreadOpenAddedConnections()
1912
1932
// the addednodeinfo state might change.
1913
1933
break ;
1914
1934
}
1915
- // If strAddedNode is an IP/port, decode it immediately, so
1916
- // OpenNetworkConnection can detect existing connections to that IP/port.
1917
1935
tried = true ;
1918
- CService service ( LookupNumeric (info. strAddedNode . c_str ( ), Params (). GetDefaultPort ()) );
1919
- OpenNetworkConnection (CAddress (service, NODE_NONE) , false , &grant, info.strAddedNode .c_str (), false , false , true );
1936
+ CAddress addr ( CService ( ), NODE_NONE );
1937
+ OpenNetworkConnection (addr , false , &grant, info.strAddedNode .c_str (), false , false , true );
1920
1938
if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
1921
1939
return ;
1922
1940
}
0 commit comments