Browse Source

don't connect if pending connection already

pull/1769/head
orignal 2 years ago
parent
commit
5de224d6bf
  1. 70
      libi2pd/SSU2.cpp
  2. 3
      libi2pd/SSU2.h
  3. 18
      libi2pd/SSU2Session.cpp
  4. 1
      libi2pd/SSU2Session.h

70
libi2pd/SSU2.cpp

@ -264,10 +264,10 @@ namespace transport @@ -264,10 +264,10 @@ namespace transport
}
}
void SSU2Server::AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session)
bool SSU2Server::AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session)
{
if (session)
m_PendingOutgoingSessions.emplace (session->GetRemoteEndpoint (), session);
if (!session) return false;
return m_PendingOutgoingSessions.emplace (session->GetRemoteEndpoint (), session).second;
}
std::shared_ptr<SSU2Session> SSU2Server::FindSession (const i2p::data::IdentHash& ident) const
@ -278,6 +278,14 @@ namespace transport @@ -278,6 +278,14 @@ namespace transport
return nullptr;
}
std::shared_ptr<SSU2Session> SSU2Server::FindPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) const
{
auto it = m_PendingOutgoingSessions.find (ep);
if (it != m_PendingOutgoingSessions.end ())
return it->second;
return nullptr;
}
std::shared_ptr<SSU2Session> SSU2Server::GetRandomSession (i2p::data::RouterInfo::CompatibleTransports remoteTransports) const
{
if (m_Sessions.empty ()) return nullptr;
@ -429,14 +437,40 @@ namespace transport @@ -429,14 +437,40 @@ namespace transport
{
if (router && address)
{
// check is no peding session
bool isValidEndpoint = !address->host.is_unspecified () && address->port;
if (isValidEndpoint)
{
auto s = FindPendingOutgoingSession (boost::asio::ip::udp::endpoint (address->host, address->port));
if (s)
{
if (peerTest)
{
// if peer test requested add it to the list for pending session
auto onEstablished = s->GetOnEstablished ();
if (onEstablished)
s->SetOnEstablished ([s, onEstablished]()
{
onEstablished ();
s->SendPeerTest ();
});
else
s->SetOnEstablished ([s]() { s->SendPeerTest (); });
}
return false;
}
}
auto session = std::make_shared<SSU2Session> (*this, router, address);
if (peerTest)
session->SetOnEstablished ([session]() {session->SendPeerTest (); });
if (address->UsesIntroducer ())
GetService ().post (std::bind (&SSU2Server::ConnectThroughIntroducer, this, session));
else
else if (isValidEndpoint) // we can't connect without endpoint
GetService ().post ([session]() { session->Connect (); });
else
return false;
}
else
return false;
@ -479,13 +513,29 @@ namespace transport @@ -479,13 +513,29 @@ namespace transport
auto addr = address->IsV6 () ? r->GetSSU2V6Address () : r->GetSSU2V4Address ();
if (addr)
{
auto s = std::make_shared<SSU2Session> (*this, r, addr);
s->SetOnEstablished (
[session, s, relayTag]()
bool isValidEndpoint = !addr->host.is_unspecified () && addr->port;
if (isValidEndpoint)
{
auto s = FindPendingOutgoingSession (boost::asio::ip::udp::endpoint (addr->host, addr->port));
if (!s)
{
s = std::make_shared<SSU2Session> (*this, r, addr);
s->SetOnEstablished ([session, s, relayTag]() { s->Introduce (session, relayTag); });
s->Connect ();
}
else
{
s->Introduce (session, relayTag);
});
s->Connect ();
auto onEstablished = s->GetOnEstablished ();
if (onEstablished)
s->SetOnEstablished ([session, s, relayTag, onEstablished]()
{
onEstablished ();
s->Introduce (session, relayTag);
});
else
s->SetOnEstablished ([session, s, relayTag]() {s->Introduce (session, relayTag); });
}
}
}
}
}

3
libi2pd/SSU2.h

@ -54,8 +54,9 @@ namespace transport @@ -54,8 +54,9 @@ namespace transport
void AddSession (std::shared_ptr<SSU2Session> session);
void RemoveSession (uint64_t connID);
void AddSessionByRouterHash (std::shared_ptr<SSU2Session> session);
void AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session);
bool AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session);
std::shared_ptr<SSU2Session> FindSession (const i2p::data::IdentHash& ident) const;
std::shared_ptr<SSU2Session> FindPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) const;
std::shared_ptr<SSU2Session> GetRandomSession (i2p::data::RouterInfo::CompatibleTransports remoteTransports) const;
void AddRelay (uint32_t tag, std::shared_ptr<SSU2Session> relay);

18
libi2pd/SSU2Session.cpp

@ -403,8 +403,13 @@ namespace transport @@ -403,8 +403,13 @@ namespace transport
i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated
// send
m_Server.AddPendingOutgoingSession (shared_from_this ());
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
if (m_Server.AddPendingOutgoingSession (shared_from_this ()))
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
else
{
LogPrint (eLogWarning, "SSU2: SessionRequest request to ", m_RemoteEndpoint, " already pending");
Terminate ();
}
}
void SSU2Session::ProcessSessionRequest (Header& header, uint8_t * buf, size_t len)
@ -739,8 +744,13 @@ namespace transport @@ -739,8 +744,13 @@ namespace transport
memset (nonce, 0, 12);
i2p::crypto::ChaCha20 (h + 16, 16, m_Address->i, nonce, h + 16);
// send
m_Server.AddPendingOutgoingSession (shared_from_this ());
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
if (m_Server.AddPendingOutgoingSession (shared_from_this ()))
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
else
{
LogPrint (eLogWarning, "SSU2: TokenRequest request to ", m_RemoteEndpoint, " already pending");
Terminate ();
}
}
void SSU2Session::ProcessTokenRequest (Header& header, uint8_t * buf, size_t len)

1
libi2pd/SSU2Session.h

@ -176,6 +176,7 @@ namespace transport @@ -176,6 +176,7 @@ namespace transport
i2p::data::RouterInfo::CompatibleTransports GetRemoteTransports () const { return m_RemoteTransports; };
std::shared_ptr<const i2p::data::RouterInfo::Address> GetAddress () const { return m_Address; };
void SetOnEstablished (OnEstablished e) { m_OnEstablished = e; };
OnEstablished GetOnEstablished () const { return m_OnEstablished; };
void Connect ();
bool Introduce (std::shared_ptr<SSU2Session> session, uint32_t relayTag);

Loading…
Cancel
Save