Browse Source

create new list of SSU2 introducers

pull/1786/head
orignal 2 years ago
parent
commit
cf0d3b5f61
  1. 10
      libi2pd/NetDb.cpp
  2. 1
      libi2pd/NetDb.hpp
  3. 11
      libi2pd/RouterInfo.cpp
  4. 1
      libi2pd/RouterInfo.h
  5. 52
      libi2pd/SSU2.cpp

10
libi2pd/NetDb.cpp

@ -1233,6 +1233,16 @@ namespace data
}); });
} }
std::shared_ptr<const RouterInfo> NetDb::GetRandomSSU2Introducer (bool v4, const std::set<IdentHash>& excluded) const
{
return GetRandomRouter (
[v4, &excluded](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router->IsSSU2Introducer (v4) &&
!excluded.count (router->GetIdentHash ());
});
}
std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const
{ {
return GetRandomRouter ( return GetRandomRouter (

1
libi2pd/NetDb.hpp

@ -93,6 +93,7 @@ namespace data
std::shared_ptr<const RouterInfo> GetRandomSSU2PeerTestRouter (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> GetRandomSSU2Introducer (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;
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num, std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const; std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;

11
libi2pd/RouterInfo.cpp

@ -1065,6 +1065,17 @@ namespace data
}); });
} }
bool RouterInfo::IsSSU2Introducer (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->IsIntroducer () &&
((v4 && address->IsV4 ()) || (!v4 && address->IsV6 ())) && !address->host.is_unspecified ();
});
}
void RouterInfo::SetUnreachableAddressesTransportCaps (uint8_t transports) void RouterInfo::SetUnreachableAddressesTransportCaps (uint8_t transports)
{ {
for (auto& addr: *m_Addresses) for (auto& addr: *m_Addresses)

1
libi2pd/RouterInfo.h

@ -235,6 +235,7 @@ namespace data
bool IsPeerTesting (bool v4) const; bool IsPeerTesting (bool v4) const;
bool IsSSU2PeerTesting (bool v4) const; bool IsSSU2PeerTesting (bool v4) const;
bool IsIntroducer (bool v4) const; bool IsIntroducer (bool v4) const;
bool IsSSU2Introducer (bool v4) const;
uint8_t GetCaps () const { return m_Caps; }; uint8_t GetCaps () const { return m_Caps; };
void SetCaps (uint8_t caps) { m_Caps = caps; }; void SetCaps (uint8_t caps) { m_Caps = caps; };

52
libi2pd/SSU2.cpp

@ -755,28 +755,68 @@ namespace transport
void SSU2Server::UpdateIntroducers (bool v4) void SSU2Server::UpdateIntroducers (bool v4)
{ {
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
std::list<std::shared_ptr<SSU2Session>> newList; std::list<std::shared_ptr<SSU2Session>> newList;
auto& introducers = v4 ? m_Introducers : m_IntroducersV6; auto& introducers = v4 ? m_Introducers : m_IntroducersV6;
std::set<i2p::data::IdentHash> excluded;
for (const auto& it : introducers) for (const auto& it : introducers)
{ {
if (it->IsEstablished ()) if (it->IsEstablished ())
{ {
it->SendKeepAlive (); if (ts < it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION)
newList.push_back (it); it->SendKeepAlive ();
if (ts < it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION)
{
newList.push_back (it);
excluded.insert (it->GetRemoteIdentity ()->GetIdentHash ());
}
// TODO: remove introducer
} }
} }
if (newList.size () < SSU2_MAX_NUM_INTRODUCERS) if (newList.size () < SSU2_MAX_NUM_INTRODUCERS)
{ {
std::set<i2p::data::IdentHash> excluded; auto sessions = FindIntroducers (SSU2_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded);
for (auto& it1: newList) if (sessions.empty () && !introducers.empty ())
excluded.insert (it1->GetRemoteIdentity ()->GetIdentHash ()); {
auto sessions = FindIntroducers (SSU_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded); // bump creation time for previous introducers if no new sessions found
LogPrint (eLogDebug, "SSU2: No new introducers found. Trying to reuse existing");
for (auto& it : introducers)
it->SetCreationTime (it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION);
// try again
excluded.clear ();
sessions = FindIntroducers (SSU2_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded);
}
for (const auto& it : sessions) for (const auto& it : sessions)
{ {
// TODO: add introducer
newList.push_back (it); newList.push_back (it);
if (newList.size () >= SSU2_MAX_NUM_INTRODUCERS) break;
} }
} }
introducers = newList; introducers = newList;
if (introducers.size () < SSU2_MAX_NUM_INTRODUCERS)
{
for (auto i = introducers.size (); i < SSU2_MAX_NUM_INTRODUCERS; i++)
{
auto introducer = i2p::data::netdb.GetRandomSSU2Introducer (v4, excluded);
if (introducer)
{
auto address = v4 ? introducer->GetSSU2V4Address () : introducer->GetSSU2V6Address ();
if (address)
{
CreateSession (introducer, address);
excluded.insert (introducer->GetIdentHash ());
}
}
else
{
LogPrint (eLogDebug, "SSU2: Can't find more introducers");
break;
}
}
}
} }
} }
} }

Loading…
Cancel
Save