Skip to content

Commit

Permalink
set unreachable if firewalled. Store router's hash of introducer inst…
Browse files Browse the repository at this point in the history
…ead session
  • Loading branch information
orignal committed Jul 21, 2022
1 parent 5f9f23e commit d33aeb4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
6 changes: 6 additions & 0 deletions libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,12 @@ namespace i2p
}
}

void RouterContext::SetUnreachableSSU2 (bool v4, bool v6)
{
if (IsSSU2Only ())
SetUnreachable (v4, v6);
}

void RouterContext::SetUnreachable (bool v4, bool v6)
{
if (v4 || (v6 && !SupportsV4 ()))
Expand Down
1 change: 1 addition & 0 deletions libi2pd/RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace garlic
void RemoveSSU2Introducer (const i2p::data::IdentHash& h, bool v4);
bool IsUnreachable () const;
void SetUnreachable (bool v4, bool v6);
void SetUnreachableSSU2 (bool v4, bool v6);
void SetReachable (bool v4, bool v6);
bool IsFloodfill () const { return m_IsFloodfill; };
void SetFloodfill (bool floodfill);
Expand Down
44 changes: 32 additions & 12 deletions libi2pd/SSU2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,25 +761,29 @@ namespace transport
void SSU2Server::UpdateIntroducers (bool v4)
{
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
std::list<std::shared_ptr<SSU2Session>> newList;
std::list<i2p::data::IdentHash> newList;
auto& introducers = v4 ? m_Introducers : m_IntroducersV6;
std::set<i2p::data::IdentHash> excluded;
for (const auto& it : introducers)
{
if (it->IsEstablished ())
std::shared_ptr<SSU2Session> session;
auto it1 = m_SessionsByRouterHash.find (it);
if (it1 != m_SessionsByRouterHash.end ())
session = it1->second;
if (session && session->IsEstablished ())
{
if (ts < it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION)
it->SendKeepAlive ();
if (ts < it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION)
if (ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION)
session->SendKeepAlive ();
if (ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION)
{
newList.push_back (it);
excluded.insert (it->GetRemoteIdentity ()->GetIdentHash ());
excluded.insert (it);
}
else
i2p::context.RemoveSSU2Introducer (it->GetRemoteIdentity ()->GetIdentHash (), it->GetAddress ()->IsV4 ());
session = nullptr;
}
else
i2p::context.RemoveSSU2Introducer (it->GetRemoteIdentity ()->GetIdentHash (), it->GetAddress ()->IsV4 ());
if (!session)
i2p::context.RemoveSSU2Introducer (it, v4);
}
if (newList.size () < SSU2_MAX_NUM_INTRODUCERS)
{
Expand All @@ -789,7 +793,11 @@ namespace transport
// 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);
{
auto it1 = m_SessionsByRouterHash.find (it);
if (it1 != m_SessionsByRouterHash.end ())
it1->second->SetCreationTime (it1->second->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION);
}
// try again
excluded.clear ();
sessions = FindIntroducers (SSU2_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded);
Expand All @@ -802,9 +810,11 @@ namespace transport
introducer.iKey = it->GetRemoteIdentity ()->GetIdentHash ();
introducer.iExp = it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION;
excluded.insert (it->GetRemoteIdentity ()->GetIdentHash ());
if (i2p::context.AddSSU2Introducer (introducer, it->GetAddress ()->IsV4 ()))
if (i2p::context.AddSSU2Introducer (introducer, v4))
{
newList.push_back (it);
LogPrint (eLogDebug, "SSU2: Introducer added ", it->GetRelayTag (), " at ",
i2p::data::GetIdentHashAbbreviation (it->GetRemoteIdentity ()->GetIdentHash ()));
newList.push_back (it->GetRemoteIdentity ()->GetIdentHash ());
if (newList.size () >= SSU2_MAX_NUM_INTRODUCERS) break;
}
}
Expand Down Expand Up @@ -895,6 +905,11 @@ namespace transport
m_Introducers.clear ();
return;
}
// we are firewalled
auto addr = i2p::context.GetRouterInfo ().GetSSU2V4Address ();
if (addr && addr->ssu && addr->ssu->introducers.empty ())
i2p::context.SetUnreachableSSU2 (true, false); // v4

UpdateIntroducers (true);
ScheduleIntroducersUpdateTimer ();
}
Expand All @@ -912,6 +927,11 @@ namespace transport
m_IntroducersV6.clear ();
return;
}
// we are firewalled
auto addr = i2p::context.GetRouterInfo ().GetSSU2V6Address ();
if (addr && addr->ssu && addr->ssu->introducers.empty ())
i2p::context.SetUnreachableSSU2 (false, true); // v6

UpdateIntroducers (false);
ScheduleIntroducersUpdateTimerV6 ();
}
Expand Down
2 changes: 1 addition & 1 deletion libi2pd/SSU2.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace transport
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSU2Session> > m_PendingOutgoingSessions;
std::map<boost::asio::ip::udp::endpoint, std::pair<uint64_t, uint32_t> > m_IncomingTokens, m_OutgoingTokens; // remote endpoint -> (token, expires in seconds)
std::map<uint32_t, std::shared_ptr<SSU2Session> > m_Relays; // we are introducer, relay tag -> session
std::list<std::shared_ptr<SSU2Session> > m_Introducers, m_IntroducersV6; // introducers we are connected to
std::list<i2p::data::IdentHash> m_Introducers, m_IntroducersV6; // introducers we are connected to
i2p::util::MemoryPoolMt<Packet> m_PacketsPool;
boost::asio::deadline_timer m_TerminationTimer, m_ResendTimer,
m_IntroducersUpdateTimer, m_IntroducersUpdateTimerV6;
Expand Down

0 comments on commit d33aeb4

Please sign in to comment.