| 437 | } |
| 438 | |
| 439 | CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type) |
| 440 | { |
| 441 | assert(conn_type != ConnectionType::INBOUND); |
| 442 | |
| 443 | if (pszDest == nullptr) { |
| 444 | if (IsLocal(addrConnect)) |
| 445 | return nullptr; |
| 446 | |
| 447 | // Look for an existing connection |
| 448 | CNode* pnode = FindNode(static_cast<CService>(addrConnect)); |
| 449 | if (pnode) |
| 450 | { |
| 451 | LogPrintf("Failed to open new connection, already connected\n"); |
| 452 | return nullptr; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /// debug print |
| 457 | LogPrintLevel(BCLog::Level::Debug, BCLog::NET, "trying connection %s lastseen=%.1fhrs\n", |
| 458 | pszDest ? pszDest : addrConnect.ToString(), |
| 459 | pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0); |
| 460 | |
| 461 | // Resolve |
| 462 | const uint16_t default_port{pszDest != nullptr ? Params().GetDefaultPort(pszDest) : |
| 463 | Params().GetDefaultPort()}; |
| 464 | if (pszDest) { |
| 465 | std::vector<CService> resolved; |
| 466 | if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) { |
| 467 | const CService rnd{resolved[GetRand(resolved.size())]}; |
| 468 | addrConnect = CAddress{MaybeFlipIPv6toCJDNS(rnd), NODE_NONE}; |
| 469 | if (!addrConnect.IsValid()) { |
| 470 | LogPrint(BCLog::NET, "Resolver returned invalid address %s for %s\n", addrConnect.ToString(), pszDest); |
| 471 | return nullptr; |
| 472 | } |
| 473 | // It is possible that we already have a connection to the IP/port pszDest resolved to. |
| 474 | // In that case, drop the connection that was just created. |
| 475 | LOCK(m_nodes_mutex); |
| 476 | CNode* pnode = FindNode(static_cast<CService>(addrConnect)); |
| 477 | if (pnode) { |
| 478 | LogPrintf("Failed to open new connection, already connected\n"); |
| 479 | return nullptr; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Connect |
| 485 | bool connected = false; |
| 486 | std::unique_ptr<Sock> sock; |
| 487 | Proxy proxy; |
| 488 | CAddress addr_bind; |
| 489 | assert(!addr_bind.IsValid()); |
| 490 | |
| 491 | if (addrConnect.IsValid()) { |
| 492 | bool proxyConnectionFailed = false; |
| 493 | |
| 494 | if (addrConnect.GetNetwork() == NET_I2P && m_i2p_sam_session.get() != nullptr) { |
| 495 | i2p::Connection conn; |
| 496 | if (m_i2p_sam_session->Connect(addrConnect, conn, proxyConnectionFailed)) { |
nothing calls this directly
no test coverage detected