From f7b6db5dad8bf361703f7851c6e9296f58754aa7 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Jun 2022 21:26:23 -0400 Subject: [PATCH] PeerTest for connection through introducer --- libi2pd/SSU2.cpp | 36 ++++++++++++++++-------------------- libi2pd/SSU2.h | 5 ++--- libi2pd/SSU2Session.h | 1 + 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index f83883f8..6f765a7c 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -400,29 +400,29 @@ namespace transport } bool SSU2Server::CreateSession (std::shared_ptr router, - std::shared_ptr address) + std::shared_ptr address, bool peerTest) { if (router && address) { + auto session = std::make_shared (*this, router, address); + if (peerTest) + session->SetOnEstablished ([session]() {session->SendPeerTest (); }); + if (address->UsesIntroducer ()) - GetService ().post (std::bind (&SSU2Server::ConnectThroughIntroducer, this, router, address)); + GetService ().post (std::bind (&SSU2Server::ConnectThroughIntroducer, this, session)); else - GetService ().post ( - [this, router, address]() - { - auto session = std::make_shared (*this, router, address); - session->Connect (); - }); + GetService ().post ([session]() { session->Connect (); }); } else return false; return true; } - void SSU2Server::ConnectThroughIntroducer (std::shared_ptr router, - std::shared_ptr address) + void SSU2Server::ConnectThroughIntroducer (std::shared_ptr session) { - auto session = std::make_shared (*this, router, address); + if (!session) return; + auto address = session->GetAddress (); + if (!address) return; session->SetState (eSSU2SessionStateIntroduced); // try to find existing session first for (auto& it: address->ssu->introducers) @@ -480,18 +480,14 @@ namespace transport auto it = m_SessionsByRouterHash.find (router->GetIdentHash ()); if (it != m_SessionsByRouterHash.end ()) { + auto s = it->second; if (it->second->IsEstablished ()) - it->second->SendPeerTest (); - else - { - auto s = it->second; - s->SetOnEstablished ([s]() { s->SendPeerTest (); }); - } + GetService ().post ([s]() { s->SendPeerTest (); }); + else + s->SetOnEstablished ([s]() { s->SendPeerTest (); }); return true; } - auto s = std::make_shared (*this, router, addr); - s->SetOnEstablished ([s]() {s->SendPeerTest (); }); - s->Connect (); + CreateSession (router, addr, true); return true; } diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index e13524c4..98bbc4b1 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -66,7 +66,7 @@ namespace transport const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to); bool CreateSession (std::shared_ptr router, - std::shared_ptr address); + std::shared_ptr address, bool peerTest = false); bool StartPeerTest (std::shared_ptr router, bool v4); void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp); @@ -89,8 +89,7 @@ namespace transport void ScheduleResend (); void HandleResendTimer (const boost::system::error_code& ecode); - void ConnectThroughIntroducer (std::shared_ptr router, - std::shared_ptr address); + void ConnectThroughIntroducer (std::shared_ptr session); private: diff --git a/libi2pd/SSU2Session.h b/libi2pd/SSU2Session.h index 364c65c6..d68e0733 100644 --- a/libi2pd/SSU2Session.h +++ b/libi2pd/SSU2Session.h @@ -162,6 +162,7 @@ namespace transport void SetRemoteEndpoint (const boost::asio::ip::udp::endpoint& ep) { m_RemoteEndpoint = ep; }; const boost::asio::ip::udp::endpoint& GetRemoteEndpoint () const { return m_RemoteEndpoint; }; i2p::data::RouterInfo::CompatibleTransports GetRemoteTransports () const { return m_RemoteTransports; }; + std::shared_ptr GetAddress () const { return m_Address; }; void SetOnEstablished (OnEstablished e) { m_OnEstablished = e; }; void Connect ();