Browse Source

start peer test for SSU2

pull/1763/head
orignal 3 years ago
parent
commit
87a434c377
  1. 10
      libi2pd/NetDb.cpp
  2. 1
      libi2pd/NetDb.hpp
  3. 11
      libi2pd/RouterInfo.cpp
  4. 1
      libi2pd/RouterInfo.h
  5. 47
      libi2pd/SSU2.cpp
  6. 2
      libi2pd/SSU2.h
  7. 20
      libi2pd/Transports.cpp

10
libi2pd/NetDb.cpp

@ -1203,6 +1203,16 @@ namespace data
}); });
} }
std::shared_ptr<const RouterInfo> NetDb::GetRandomSSU2PeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const
{
return GetRandomRouter (
[v4, &excluded](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router->IsECIES () &&
router->IsSSU2PeerTesting (v4) && !excluded.count (router->GetIdentHash ());
});
}
std::shared_ptr<const RouterInfo> NetDb::GetRandomSSUV6Router () const std::shared_ptr<const RouterInfo> NetDb::GetRandomSSUV6Router () const
{ {
return GetRandomRouter ( return GetRandomRouter (

1
libi2pd/NetDb.hpp

@ -90,6 +90,7 @@ namespace data
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const; std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const;
std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const; std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const;
std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const; std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetRandomSSU2PeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetRandomSSUV6Router () const; // TODO: change to v6 peer test later std::shared_ptr<const RouterInfo> GetRandomSSUV6Router () const; // TODO: change to v6 peer test later
std::shared_ptr<const RouterInfo> GetRandomIntroducer (bool v4, const std::set<IdentHash>& excluded) const; std::shared_ptr<const RouterInfo> GetRandomIntroducer (bool v4, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const; std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;

11
libi2pd/RouterInfo.cpp

@ -1000,6 +1000,17 @@ namespace data
}); });
} }
bool RouterInfo::IsSSU2PeerTesting (bool v4) const
{
if (!(m_SupportedTransports & (v4 ? eSSU2V4 : eSSU2V6))) return false;
return (bool)GetAddress (
[v4](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return (address->IsSSU2 ()) && address->IsPeerTesting () &&
((v4 && address->IsV4 ()) || (!v4 && address->IsV6 ())) && address->IsReachableSSU ();
});
}
bool RouterInfo::IsIntroducer (bool v4) const bool RouterInfo::IsIntroducer (bool v4) const
{ {
if (!(m_SupportedTransports & (v4 ? eSSUV4 : eSSUV6))) return false; if (!(m_SupportedTransports & (v4 ? eSSUV4 : eSSUV6))) return false;

1
libi2pd/RouterInfo.h

@ -232,6 +232,7 @@ namespace data
bool IsExtraBandwidth () const { return m_Caps & RouterInfo::eExtraBandwidth; }; bool IsExtraBandwidth () const { return m_Caps & RouterInfo::eExtraBandwidth; };
bool IsEligibleFloodfill () const; bool IsEligibleFloodfill () const;
bool IsPeerTesting (bool v4) const; bool IsPeerTesting (bool v4) const;
bool IsSSU2PeerTesting (bool v4) const;
bool IsIntroducer (bool v4) const; bool IsIntroducer (bool v4) const;
uint8_t GetCaps () const { return m_Caps; }; uint8_t GetCaps () const { return m_Caps; };

47
libi2pd/SSU2.cpp

@ -106,12 +106,22 @@ namespace transport
return true; return true;
} }
void SSU2Session::SendPeerTest ()
{
// we are Alice
uint8_t payload[SSU2_MAX_PAYLOAD_SIZE];
size_t payloadSize = CreatePeerTestBlock (payload, SSU2_MAX_PAYLOAD_SIZE);
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize);
SendData (payload, payloadSize);
}
void SSU2Session::Terminate () void SSU2Session::Terminate ()
{ {
if (m_State != eSSU2SessionStateTerminated) if (m_State != eSSU2SessionStateTerminated)
{ {
m_State = eSSU2SessionStateTerminated; m_State = eSSU2SessionStateTerminated;
transports.PeerDisconnected (shared_from_this ()); transports.PeerDisconnected (shared_from_this ());
m_OnEstablished = nullptr;
m_Server.RemoveSession (m_SourceConnID); m_Server.RemoveSession (m_SourceConnID);
if (m_RelayTag) if (m_RelayTag)
m_Server.RemoveRelay (m_RelayTag); m_Server.RemoveRelay (m_RelayTag);
@ -134,7 +144,11 @@ namespace transport
m_SessionConfirmedFragment1.reset (nullptr); m_SessionConfirmedFragment1.reset (nullptr);
SetTerminationTimeout (SSU2_TERMINATION_TIMEOUT); SetTerminationTimeout (SSU2_TERMINATION_TIMEOUT);
transports.PeerConnected (shared_from_this ()); transports.PeerConnected (shared_from_this ());
if (m_OnEstablished) m_OnEstablished (); if (m_OnEstablished)
{
m_OnEstablished ();
m_OnEstablished = nullptr;
}
} }
void SSU2Session::Done () void SSU2Session::Done ()
@ -1279,7 +1293,7 @@ namespace transport
uint32_t nonce = bufbe32toh (buf + 37); uint32_t nonce = bufbe32toh (buf + 37);
switch (buf[0]) // msg switch (buf[0]) // msg
{ {
case 1: // Bob for Alice case 1: // Bob from Alice
break; break;
case 2: // Charlie from Bob case 2: // Charlie from Bob
break; break;
@ -1293,11 +1307,20 @@ namespace transport
if (payloadSize < SSU2_MAX_PAYLOAD_SIZE) if (payloadSize < SSU2_MAX_PAYLOAD_SIZE)
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize); payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize);
it->second.first->SendData (payload, payloadSize); it->second.first->SendData (payload, payloadSize);
m_PeerTests.erase (it);
} }
break; break;
} }
case 4: // Alice from Bob case 4: // Alice from Bob
{
auto it = m_PeerTests.find (nonce);
if (it != m_PeerTests.end ())
{
// TODO: send to Charlie
m_PeerTests.erase (it);
}
break; break;
}
case 5: // Alice from Chralie 1 case 5: // Alice from Chralie 1
break; break;
case 6: // Chralie from Alice case 6: // Chralie from Alice
@ -2119,6 +2142,26 @@ namespace transport
} }
} }
bool SSU2Server::StartPeerTest (std::shared_ptr<const i2p::data::RouterInfo> router, bool v4)
{
if (!router) return false;
auto addr = v4 ? router->GetSSU2V4Address () : router->GetSSU2V6Address ();
if (!addr) return false;
auto it = m_SessionsByRouterHash.find (router->GetIdentHash ());
if (it != m_SessionsByRouterHash.end ())
{
it->second->SendPeerTest ();
return true;
}
auto s = std::make_shared<SSU2Session> (*this, router, addr);
s->SetOnEstablished ([s]()
{
s->SendPeerTest ();
});
s->Connect ();
return true;
}
void SSU2Server::ScheduleTermination () void SSU2Server::ScheduleTermination ()
{ {
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(SSU2_TERMINATION_CHECK_TIMEOUT)); m_TerminationTimer.expires_from_now (boost::posix_time::seconds(SSU2_TERMINATION_CHECK_TIMEOUT));

2
libi2pd/SSU2.h

@ -150,6 +150,7 @@ namespace transport
void Connect (); void Connect ();
bool Introduce (std::shared_ptr<SSU2Session> session, uint32_t relayTag); bool Introduce (std::shared_ptr<SSU2Session> session, uint32_t relayTag);
void SendPeerTest ();
void Terminate (); void Terminate ();
void TerminateByTimeout (); void TerminateByTimeout ();
void CleanUp (uint64_t ts); void CleanUp (uint64_t ts);
@ -289,6 +290,7 @@ namespace transport
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
std::shared_ptr<const i2p::data::RouterInfo::Address> address); std::shared_ptr<const i2p::data::RouterInfo::Address> address);
bool StartPeerTest (std::shared_ptr<const i2p::data::RouterInfo> router, bool v4);
void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp); void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp);
uint64_t FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const; uint64_t FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const;

20
libi2pd/Transports.cpp

@ -654,6 +654,16 @@ namespace transport
} }
if (!statusChanged) if (!statusChanged)
LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv4"); LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv4");
// SSU2
if (m_SSU2Server)
{
excluded.clear ();
excluded.insert (i2p::context.GetIdentHash ());
auto router = i2p::data::netdb.GetRandomSSU2PeerTestRouter (true, excluded); // v4
if (router)
m_SSU2Server->StartPeerTest (router, true);
}
} }
if (ipv6 && i2p::context.SupportsV6 ()) if (ipv6 && i2p::context.SupportsV6 ())
{ {
@ -681,6 +691,16 @@ namespace transport
} }
if (!statusChanged) if (!statusChanged)
LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv6"); LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv6");
// SSU2
if (m_SSU2Server)
{
excluded.clear ();
excluded.insert (i2p::context.GetIdentHash ());
auto router = i2p::data::netdb.GetRandomSSU2PeerTestRouter (false, excluded); // v6
if (router)
m_SSU2Server->StartPeerTest (router, false);
}
} }
} }

Loading…
Cancel
Save