Browse Source

publish ipv6 introducers

pull/1656/head
orignal 4 years ago
parent
commit
ad019da553
  1. 4
      libi2pd/RouterContext.cpp
  2. 103
      libi2pd/SSU.cpp
  3. 12
      libi2pd/SSU.h
  4. 3
      libi2pd/SSUSession.cpp

4
libi2pd/RouterContext.cpp

@ -466,7 +466,7 @@ namespace i2p
void RouterContext::SetUnreachable (bool v4, bool v6) void RouterContext::SetUnreachable (bool v4, bool v6)
{ {
if (v4) if (v4 || (v6 && !SupportsV4 ()))
{ {
// set caps // set caps
uint8_t caps = m_RouterInfo.GetCaps (); uint8_t caps = m_RouterInfo.GetCaps ();
@ -496,7 +496,7 @@ namespace i2p
void RouterContext::SetReachable (bool v4, bool v6) void RouterContext::SetReachable (bool v4, bool v6)
{ {
if (v4) if (v4 || (v6 && !SupportsV4 ()))
{ {
// update caps // update caps
uint8_t caps = m_RouterInfo.GetCaps (); uint8_t caps = m_RouterInfo.GetCaps ();

103
libi2pd/SSU.cpp

@ -28,8 +28,8 @@ namespace transport
m_ReceiversWork (m_ReceiversService), m_ReceiversWorkV6 (m_ReceiversServiceV6), m_ReceiversWork (m_ReceiversService), m_ReceiversWorkV6 (m_ReceiversServiceV6),
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_EndpointV6 (boost::asio::ip::udp::v6 (), port), m_Endpoint (boost::asio::ip::udp::v4 (), port), m_EndpointV6 (boost::asio::ip::udp::v6 (), port),
m_Socket (m_ReceiversService), m_SocketV6 (m_ReceiversServiceV6), m_Socket (m_ReceiversService), m_SocketV6 (m_ReceiversServiceV6),
m_IntroducersUpdateTimer (m_Service), m_PeerTestsCleanupTimer (m_Service), m_IntroducersUpdateTimer (m_Service), m_IntroducersUpdateTimerV6 (m_Service),
m_TerminationTimer (m_Service), m_TerminationTimerV6 (m_Service) m_PeerTestsCleanupTimer (m_Service), m_TerminationTimer (m_Service), m_TerminationTimerV6 (m_Service)
{ {
} }
@ -82,6 +82,7 @@ namespace transport
m_ReceiversThread = new std::thread (std::bind (&SSUServer::RunReceivers, this)); m_ReceiversThread = new std::thread (std::bind (&SSUServer::RunReceivers, this));
m_ReceiversService.post (std::bind (&SSUServer::Receive, this)); m_ReceiversService.post (std::bind (&SSUServer::Receive, this));
ScheduleTermination (); ScheduleTermination ();
ScheduleIntroducersUpdateTimer (); // wait for 30 seconds and decide if we need introducers
} }
if (context.SupportsV6 ()) if (context.SupportsV6 ())
{ {
@ -89,9 +90,9 @@ namespace transport
m_ReceiversThreadV6 = new std::thread (std::bind (&SSUServer::RunReceiversV6, this)); m_ReceiversThreadV6 = new std::thread (std::bind (&SSUServer::RunReceiversV6, this));
m_ReceiversServiceV6.post (std::bind (&SSUServer::ReceiveV6, this)); m_ReceiversServiceV6.post (std::bind (&SSUServer::ReceiveV6, this));
ScheduleTerminationV6 (); ScheduleTerminationV6 ();
ScheduleIntroducersUpdateTimerV6 (); // wait for 30 seconds and decide if we need introducers
} }
SchedulePeerTestsCleanupTimer (); SchedulePeerTestsCleanupTimer ();
ScheduleIntroducersUpdateTimer (); // wait for 30 seconds and decide if we need introducers
} }
void SSUServer::Stop () void SSUServer::Stop ()
@ -100,6 +101,8 @@ namespace transport
m_IsRunning = false; m_IsRunning = false;
m_TerminationTimer.cancel (); m_TerminationTimer.cancel ();
m_TerminationTimerV6.cancel (); m_TerminationTimerV6.cancel ();
m_IntroducersUpdateTimer.cancel ();
m_IntroducersUpdateTimerV6.cancel ();
m_Service.stop (); m_Service.stop ();
m_Socket.close (); m_Socket.close ();
m_SocketV6.close (); m_SocketV6.close ();
@ -659,20 +662,19 @@ namespace transport
); );
} }
std::set<SSUSession *> SSUServer::FindIntroducers (int maxNumIntroducers) std::set<SSUSession *> SSUServer::FindIntroducers (int maxNumIntroducers, bool v4)
{ {
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
std::set<SSUSession *> ret; std::set<SSUSession *> ret;
auto filter = [&ret, ts](std::shared_ptr<SSUSession> session)->bool
{
return session->GetRelayTag () && !ret.count (session.get ()) &&
session->GetState () == eSessionStateEstablished &&
ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION;
};
for (int i = 0; i < maxNumIntroducers; i++) for (int i = 0; i < maxNumIntroducers; i++)
{ {
auto session = GetRandomV4Session ( auto session = v4 ? GetRandomV4Session (filter) : GetRandomV6Session (filter);
[&ret, ts](std::shared_ptr<SSUSession> session)->bool
{
return session->GetRelayTag () && !ret.count (session.get ()) &&
session->GetState () == eSessionStateEstablished &&
ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION;
}
);
if (session) if (session)
{ {
ret.insert (session.get ()); ret.insert (session.get ());
@ -687,39 +689,73 @@ namespace transport
m_IntroducersUpdateTimer.cancel (); m_IntroducersUpdateTimer.cancel ();
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL/2)); m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL/2));
m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer, m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
this, std::placeholders::_1)); this, std::placeholders::_1, true));
} }
void SSUServer::ScheduleIntroducersUpdateTimer () void SSUServer::ScheduleIntroducersUpdateTimer ()
{ {
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL)); m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL));
m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer, m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
this, std::placeholders::_1)); this, std::placeholders::_1, true));
}
void SSUServer::RescheduleIntroducersUpdateTimerV6 ()
{
m_IntroducersUpdateTimerV6.cancel ();
m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL/2));
m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
this, std::placeholders::_1, false));
} }
void SSUServer::HandleIntroducersUpdateTimer (const boost::system::error_code& ecode) void SSUServer::ScheduleIntroducersUpdateTimerV6 ()
{
m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL));
m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
this, std::placeholders::_1, false));
}
void SSUServer::HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4)
{ {
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
{ {
// timeout expired // timeout expired
if (i2p::context.GetStatus () == eRouterStatusTesting) if (v4)
{ {
// we still don't know if we need introducers if (i2p::context.GetStatus () == eRouterStatusTesting)
ScheduleIntroducersUpdateTimer (); {
return; // we still don't know if we need introducers
ScheduleIntroducersUpdateTimer ();
return;
}
if (i2p::context.GetStatus () != eRouterStatusFirewalled)
{
// we don't need introducers
m_Introducers.clear ();
return;
}
} }
if (i2p::context.GetStatus () != eRouterStatusFirewalled) else
{ {
// we don't need introducers if (i2p::context.GetStatusV6 () == eRouterStatusTesting)
m_Introducers.clear (); {
return; // we still don't know if we need introducers
ScheduleIntroducersUpdateTimerV6 ();
return;
}
if (i2p::context.GetStatusV6 () != eRouterStatusFirewalled)
{
// we don't need introducers
m_IntroducersV6.clear ();
return;
}
} }
// we are firewalled // we are firewalled
if (!i2p::context.IsUnreachable ()) i2p::context.SetUnreachable (true, false); // ipv4 if (!i2p::context.IsUnreachable () || !v4) i2p::context.SetUnreachable (v4, !v4);
std::list<boost::asio::ip::udp::endpoint> newList; std::list<boost::asio::ip::udp::endpoint> newList;
size_t numIntroducers = 0; size_t numIntroducers = 0;
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
for (const auto& it : m_Introducers) auto& introducers = v4 ? m_Introducers : m_IntroducersV6;
for (const auto& it : introducers)
{ {
auto session = FindSession (it); auto session = FindSession (it);
if (session && ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION) if (session && ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION)
@ -735,8 +771,8 @@ namespace transport
if (numIntroducers < SSU_MAX_NUM_INTRODUCERS) if (numIntroducers < SSU_MAX_NUM_INTRODUCERS)
{ {
// create new // create new
auto introducers = FindIntroducers (SSU_MAX_NUM_INTRODUCERS); auto sessions = FindIntroducers (SSU_MAX_NUM_INTRODUCERS, true);
for (const auto& it1: introducers) for (const auto& it1: sessions)
{ {
const auto& ep = it1->GetRemoteEndpoint (); const auto& ep = it1->GetRemoteEndpoint ();
i2p::data::RouterInfo::Introducer introducer; i2p::data::RouterInfo::Introducer introducer;
@ -752,11 +788,11 @@ namespace transport
} }
} }
} }
m_Introducers = newList; introducers = newList;
if (m_Introducers.size () < SSU_MAX_NUM_INTRODUCERS) if (introducers.size () < SSU_MAX_NUM_INTRODUCERS)
{ {
std::set<std::shared_ptr<const i2p::data::RouterInfo> > requested; std::set<std::shared_ptr<const i2p::data::RouterInfo> > requested;
for (auto i = m_Introducers.size (); i < SSU_MAX_NUM_INTRODUCERS; i++) for (auto i = introducers.size (); i < SSU_MAX_NUM_INTRODUCERS; i++)
{ {
auto introducer = i2p::data::netdb.GetRandomIntroducer (); auto introducer = i2p::data::netdb.GetRandomIntroducer ();
if (introducer && !requested.count (introducer)) // not requested already if (introducer && !requested.count (introducer)) // not requested already
@ -765,7 +801,7 @@ namespace transport
if (address && !address->host.is_unspecified ()) if (address && !address->host.is_unspecified ())
{ {
boost::asio::ip::udp::endpoint ep (address->host, address->port); boost::asio::ip::udp::endpoint ep (address->host, address->port);
if (std::find (m_Introducers.begin (), m_Introducers.end (), ep) == m_Introducers.end ()) // not connected yet if (std::find (introducers.begin (), introducers.end (), ep) == introducers.end ()) // not connected yet
{ {
CreateDirectSession (introducer, ep, false); CreateDirectSession (introducer, ep, false);
requested.insert (introducer); requested.insert (introducer);
@ -774,7 +810,10 @@ namespace transport
} }
} }
} }
ScheduleIntroducersUpdateTimer (); if (v4)
ScheduleIntroducersUpdateTimer ();
else
ScheduleIntroducersUpdateTimerV6 ();
} }
} }

12
libi2pd/SSU.h

@ -71,6 +71,7 @@ namespace transport
void RemoveRelay (uint32_t tag); void RemoveRelay (uint32_t tag);
std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag); std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
void RescheduleIntroducersUpdateTimer (); void RescheduleIntroducersUpdateTimer ();
void RescheduleIntroducersUpdateTimerV6 ();
void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr); void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr);
PeerTestParticipant GetPeerTestParticipant (uint32_t nonce); PeerTestParticipant GetPeerTestParticipant (uint32_t nonce);
@ -99,9 +100,10 @@ namespace transport
template<typename Filter> template<typename Filter>
std::shared_ptr<SSUSession> GetRandomV6Session (Filter filter); std::shared_ptr<SSUSession> GetRandomV6Session (Filter filter);
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers); std::set<SSUSession *> FindIntroducers (int maxNumIntroducers, bool v4);
void ScheduleIntroducersUpdateTimer (); void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode); void ScheduleIntroducersUpdateTimerV6 ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
void SchedulePeerTestsCleanupTimer (); void SchedulePeerTestsCleanupTimer ();
void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode); void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode);
@ -127,9 +129,9 @@ namespace transport
boost::asio::io_service::work m_Work, m_ReceiversWork, m_ReceiversWorkV6; boost::asio::io_service::work m_Work, m_ReceiversWork, m_ReceiversWorkV6;
boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6; boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
boost::asio::ip::udp::socket m_Socket, m_SocketV6; boost::asio::ip::udp::socket m_Socket, m_SocketV6;
boost::asio::deadline_timer m_IntroducersUpdateTimer, m_PeerTestsCleanupTimer, boost::asio::deadline_timer m_IntroducersUpdateTimer, m_IntroducersUpdateTimerV6,
m_TerminationTimer, m_TerminationTimerV6; m_PeerTestsCleanupTimer, m_TerminationTimer, m_TerminationTimerV6;
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to std::list<boost::asio::ip::udp::endpoint> m_Introducers, m_IntroducersV6; // introducers we are connected to
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions, m_SessionsV6; std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions, m_SessionsV6;
std::map<uint32_t, std::shared_ptr<SSUSession> > m_Relays; // we are introducer std::map<uint32_t, std::shared_ptr<SSUSession> > m_Relays; // we are introducer
std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds

3
libi2pd/SSUSession.cpp

@ -1017,7 +1017,10 @@ namespace transport
if (IsV6 ()) if (IsV6 ())
{ {
if (i2p::context.GetStatusV6 () == eRouterStatusTesting) if (i2p::context.GetStatusV6 () == eRouterStatusTesting)
{
i2p::context.SetStatusV6 (eRouterStatusFirewalled); i2p::context.SetStatusV6 (eRouterStatusFirewalled);
m_Server.RescheduleIntroducersUpdateTimerV6 ();
}
} }
else if (i2p::context.GetStatus () == eRouterStatusTesting) // still not OK else if (i2p::context.GetStatus () == eRouterStatusTesting) // still not OK
{ {

Loading…
Cancel
Save