i2pd/SSU.cpp

758 lines
22 KiB
C++
Raw Normal View History

2014-01-28 21:49:54 +00:00
#include <string.h>
2014-01-23 21:10:33 +00:00
#include <boost/bind.hpp>
#include "Log.h"
2014-01-29 21:49:53 +00:00
#include "Timestamp.h"
2014-01-28 21:49:54 +00:00
#include "RouterContext.h"
2015-02-27 02:05:35 +00:00
#include "NetDb.h"
2014-01-23 21:10:33 +00:00
#include "SSU.h"
namespace i2p
{
namespace transport
2014-01-23 21:10:33 +00:00
{
2016-06-27 14:24:37 +00:00
SSUServer::SSUServer (const boost::asio::ip::address & addr, int port):
m_OnlyV6(true), m_IsRunning(false),
m_Thread (nullptr), m_ThreadV6 (nullptr), m_ReceiversThread (nullptr),
m_Work (m_Service), m_WorkV6 (m_ServiceV6), m_ReceiversWork (m_ReceiversService),
m_EndpointV6 (addr, port),
m_Socket (m_ReceiversService, m_Endpoint), m_SocketV6 (m_ReceiversService),
m_IntroducersUpdateTimer (m_Service), m_PeerTestsCleanupTimer (m_Service),
m_TerminationTimer (m_Service), m_TerminationTimerV6 (m_ServiceV6)
2016-06-27 14:24:37 +00:00
{
2016-12-01 02:14:10 +00:00
OpenSocketV6 ();
2016-06-27 14:24:37 +00:00
}
SSUServer::SSUServer (int port):
m_OnlyV6(false), m_IsRunning(false),
m_Thread (nullptr), m_ThreadV6 (nullptr), m_ReceiversThread (nullptr),
2015-02-09 01:28:18 +00:00
m_Work (m_Service), m_WorkV6 (m_ServiceV6), m_ReceiversWork (m_ReceiversService),
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_EndpointV6 (boost::asio::ip::udp::v6 (), port),
2016-12-01 02:14:10 +00:00
m_Socket (m_ReceiversService), m_SocketV6 (m_ReceiversService),
m_IntroducersUpdateTimer (m_Service), m_PeerTestsCleanupTimer (m_Service),
m_TerminationTimer (m_Service), m_TerminationTimerV6 (m_ServiceV6)
2014-01-23 21:10:33 +00:00
{
2016-12-01 02:14:10 +00:00
OpenSocket ();
2014-10-29 19:02:48 +00:00
if (context.SupportsV6 ())
2016-12-01 02:14:10 +00:00
OpenSocketV6 ();
2014-01-23 21:10:33 +00:00
}
2014-01-24 21:30:07 +00:00
SSUServer::~SSUServer ()
{
}
2016-12-01 02:14:10 +00:00
void SSUServer::OpenSocket ()
{
m_Socket.open (boost::asio::ip::udp::v4());
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (65535));
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535));
m_Socket.bind (m_Endpoint);
}
void SSUServer::OpenSocketV6 ()
{
m_SocketV6.open (boost::asio::ip::udp::v6());
m_SocketV6.set_option (boost::asio::ip::v6_only (true));
m_SocketV6.set_option (boost::asio::socket_base::receive_buffer_size (65535));
m_SocketV6.set_option (boost::asio::socket_base::send_buffer_size (65535));
m_SocketV6.bind (m_EndpointV6);
}
2014-01-23 21:10:33 +00:00
void SSUServer::Start ()
{
2014-04-20 00:45:41 +00:00
m_IsRunning = true;
2016-06-27 14:24:37 +00:00
m_ReceiversThread = new std::thread (std::bind (&SSUServer::RunReceivers, this));
if (!m_OnlyV6)
{
m_Thread = new std::thread (std::bind (&SSUServer::Run, this));
m_ReceiversService.post (std::bind (&SSUServer::Receive, this));
ScheduleTermination ();
2016-06-27 14:24:37 +00:00
}
2014-10-29 19:02:48 +00:00
if (context.SupportsV6 ())
{
m_ThreadV6 = new std::thread (std::bind (&SSUServer::RunV6, this));
2016-06-27 14:24:37 +00:00
m_ReceiversService.post (std::bind (&SSUServer::ReceiveV6, this));
ScheduleTerminationV6 ();
}
2015-02-26 19:54:28 +00:00
SchedulePeerTestsCleanupTimer ();
ScheduleIntroducersUpdateTimer (); // wait for 30 seconds and decide if we need introducers
2014-01-23 21:10:33 +00:00
}
void SSUServer::Stop ()
{
2014-02-11 00:27:55 +00:00
DeleteAllSessions ();
2014-04-20 00:45:41 +00:00
m_IsRunning = false;
2016-08-24 16:34:18 +00:00
m_TerminationTimer.cancel ();
m_TerminationTimerV6.cancel ();
2014-04-20 00:45:41 +00:00
m_Service.stop ();
2014-01-23 21:10:33 +00:00
m_Socket.close ();
m_ServiceV6.stop ();
m_SocketV6.close ();
2015-02-09 02:12:38 +00:00
m_ReceiversService.stop ();
2015-02-09 01:28:18 +00:00
if (m_ReceiversThread)
{
m_ReceiversThread->join ();
delete m_ReceiversThread;
m_ReceiversThread = nullptr;
}
2014-04-20 00:45:41 +00:00
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = nullptr;
}
if (m_ThreadV6)
{
m_ThreadV6->join ();
delete m_ThreadV6;
m_ThreadV6 = nullptr;
}
2014-01-23 21:10:33 +00:00
}
2014-04-20 00:45:41 +00:00
void SSUServer::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
2015-12-18 13:46:56 +00:00
LogPrint (eLogError, "SSU: server runtime exception: ", ex.what ());
2014-04-20 00:45:41 +00:00
}
}
}
void SSUServer::RunV6 ()
{
while (m_IsRunning)
{
try
{
m_ServiceV6.run ();
}
catch (std::exception& ex)
{
2015-12-18 13:46:56 +00:00
LogPrint (eLogError, "SSU: v6 server runtime exception: ", ex.what ());
}
}
}
2015-02-09 01:28:18 +00:00
void SSUServer::RunReceivers ()
{
while (m_IsRunning)
{
try
{
m_ReceiversService.run ();
}
catch (std::exception& ex)
{
2015-12-18 13:46:56 +00:00
LogPrint (eLogError, "SSU: receivers runtime exception: ", ex.what ());
2015-02-09 01:28:18 +00:00
}
}
}
2016-12-30 22:53:54 +00:00
void SSUServer::AddRelay (uint32_t tag, std::shared_ptr<SSUSession> relay)
2014-04-16 19:54:28 +00:00
{
m_Relays[tag] = relay;
}
2016-12-30 03:06:33 +00:00
void SSUServer::RemoveRelay (uint32_t tag)
{
m_Relays.erase (tag);
}
2014-11-24 17:26:11 +00:00
std::shared_ptr<SSUSession> SSUServer::FindRelaySession (uint32_t tag)
2014-04-16 19:54:28 +00:00
{
auto it = m_Relays.find (tag);
if (it != m_Relays.end ())
2016-12-30 22:53:54 +00:00
{
if (it->second->GetState () == eSessionStateEstablished)
return it->second;
else
m_Relays.erase (it);
}
2014-04-16 19:54:28 +00:00
return nullptr;
}
2014-06-10 14:39:29 +00:00
void SSUServer::Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to)
2014-01-28 21:49:54 +00:00
{
2014-10-29 22:46:35 +00:00
if (to.protocol () == boost::asio::ip::udp::v4())
m_Socket.send_to (boost::asio::buffer (buf, len), to);
else
m_SocketV6.send_to (boost::asio::buffer (buf, len), to);
2014-01-28 21:49:54 +00:00
}
2014-01-23 21:10:33 +00:00
void SSUServer::Receive ()
{
2015-02-09 01:28:18 +00:00
SSUPacket * packet = new SSUPacket ();
m_Socket.async_receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V4), packet->from,
std::bind (&SSUServer::HandleReceivedFrom, this, std::placeholders::_1, std::placeholders::_2, packet));
2014-01-23 21:10:33 +00:00
}
2014-10-29 19:02:48 +00:00
void SSUServer::ReceiveV6 ()
{
2015-02-09 01:28:18 +00:00
SSUPacket * packet = new SSUPacket ();
m_SocketV6.async_receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V6), packet->from,
std::bind (&SSUServer::HandleReceivedFromV6, this, std::placeholders::_1, std::placeholders::_2, packet));
2014-10-29 19:02:48 +00:00
}
2015-02-09 01:28:18 +00:00
void SSUServer::HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet)
2014-01-23 21:10:33 +00:00
{
if (!ecode)
{
2015-02-09 01:28:18 +00:00
packet->len = bytes_transferred;
2015-02-09 16:53:11 +00:00
std::vector<SSUPacket *> packets;
packets.push_back (packet);
2015-02-14 22:20:21 +00:00
boost::system::error_code ec;
size_t moreBytes = m_Socket.available(ec);
2016-12-02 00:24:15 +00:00
if (!ec)
{
while (moreBytes && packets.size () < 25)
{
packet = new SSUPacket ();
packet->len = m_Socket.receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V4), packet->from, 0, ec);
if (!ec)
{
packets.push_back (packet);
moreBytes = m_Socket.available(ec);
if (ec) break;
}
else
{
LogPrint (eLogError, "SSU: receive_from error: ", ec.message ());
delete packet;
break;
}
}
}
2015-02-09 16:53:11 +00:00
2015-12-01 00:45:57 +00:00
m_Service.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets, &m_Sessions));
2014-01-23 21:10:33 +00:00
Receive ();
}
else
2015-02-09 01:28:18 +00:00
{
delete packet;
2016-11-30 19:51:26 +00:00
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint (eLogError, "SSU: receive error: ", ecode.message ());
2016-12-01 02:14:10 +00:00
m_Socket.close ();
OpenSocket ();
2016-11-30 19:51:26 +00:00
Receive ();
}
2015-02-09 01:28:18 +00:00
}
2014-01-23 21:10:33 +00:00
}
2014-01-28 21:49:54 +00:00
2015-02-09 01:28:18 +00:00
void SSUServer::HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet)
2014-10-29 19:02:48 +00:00
{
if (!ecode)
{
2015-02-09 01:28:18 +00:00
packet->len = bytes_transferred;
2015-02-09 16:53:11 +00:00
std::vector<SSUPacket *> packets;
packets.push_back (packet);
2016-12-02 00:24:15 +00:00
boost::system::error_code ec;
size_t moreBytes = m_SocketV6.available (ec);
if (!ec)
2015-02-09 16:53:11 +00:00
{
2016-12-02 00:24:15 +00:00
while (moreBytes && packets.size () < 25)
{
packet = new SSUPacket ();
packet->len = m_SocketV6.receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V6), packet->from, 0, ec);
if (!ec)
{
packets.push_back (packet);
moreBytes = m_SocketV6.available(ec);
if (ec) break;
}
else
{
LogPrint (eLogError, "SSU: v6 receive_from error: ", ec.message ());
delete packet;
break;
}
}
2015-02-09 16:53:11 +00:00
}
2016-12-02 00:24:15 +00:00
2015-12-01 00:45:57 +00:00
m_ServiceV6.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets, &m_SessionsV6));
2014-10-29 19:02:48 +00:00
ReceiveV6 ();
}
else
2015-02-09 01:28:18 +00:00
{
delete packet;
2016-11-30 19:51:26 +00:00
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint (eLogError, "SSU: v6 receive error: ", ecode.message ());
2016-12-01 02:14:10 +00:00
m_SocketV6.close ();
OpenSocketV6 ();
2016-11-30 19:51:26 +00:00
ReceiveV6 ();
}
2015-02-09 01:28:18 +00:00
}
2014-10-29 19:02:48 +00:00
}
void SSUServer::HandleReceivedPackets (std::vector<SSUPacket *> packets,
2015-12-01 00:45:57 +00:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > * sessions)
2014-10-29 19:02:48 +00:00
{
std::shared_ptr<SSUSession> session;
2016-08-08 22:53:37 +00:00
for (auto& packet: packets)
2014-10-29 19:02:48 +00:00
{
2015-04-18 17:55:15 +00:00
try
{
if (!session || session->GetRemoteEndpoint () != packet->from) // we received packet for other session than previous
2015-02-09 16:53:11 +00:00
{
2015-04-18 17:55:15 +00:00
if (session) session->FlushData ();
2015-12-01 00:45:57 +00:00
auto it = sessions->find (packet->from);
if (it != sessions->end ())
2015-04-18 17:55:15 +00:00
session = it->second;
if (!session)
{
2015-04-18 17:55:15 +00:00
session = std::make_shared<SSUSession> (*this, packet->from);
session->WaitForConnect ();
2015-12-01 00:45:57 +00:00
(*sessions)[packet->from] = session;
2016-06-27 13:00:00 +00:00
LogPrint (eLogDebug, "SSU: new session from ", packet->from.address ().to_string (), ":", packet->from.port (), " created");
2015-04-18 17:55:15 +00:00
}
}
2015-04-18 17:55:15 +00:00
session->ProcessNextMessage (packet->buf, packet->len, packet->from);
}
catch (std::exception& ex)
{
LogPrint (eLogError, "SSU: HandleReceivedPackets ", ex.what ());
if (session) session->FlushData ();
session = nullptr;
}
2015-02-09 16:53:11 +00:00
delete packet;
2014-10-29 19:02:48 +00:00
}
2015-02-15 19:17:55 +00:00
if (session) session->FlushData ();
2014-10-29 19:02:48 +00:00
}
2014-11-24 17:26:11 +00:00
std::shared_ptr<SSUSession> SSUServer::FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const
2014-03-26 01:17:03 +00:00
{
if (!router) return nullptr;
2014-10-30 01:56:45 +00:00
auto address = router->GetSSUAddress (true); // v4 only
if (!address) return nullptr;
auto session = FindSession (boost::asio::ip::udp::endpoint (address->host, address->port));
if (session || !context.SupportsV6 ())
return session;
// try v6
address = router->GetSSUV6Address ();
2014-03-26 01:17:03 +00:00
if (!address) return nullptr;
2014-04-16 19:54:28 +00:00
return FindSession (boost::asio::ip::udp::endpoint (address->host, address->port));
}
2014-11-24 17:26:11 +00:00
std::shared_ptr<SSUSession> SSUServer::FindSession (const boost::asio::ip::udp::endpoint& e) const
2014-04-16 19:54:28 +00:00
{
auto& sessions = e.address ().is_v6 () ? m_SessionsV6 : m_Sessions;
auto it = sessions.find (e);
if (it != sessions.end ())
2014-03-26 01:17:03 +00:00
return it->second;
else
return nullptr;
2014-04-16 19:54:28 +00:00
}
2016-12-02 16:17:22 +00:00
void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest, bool v4only)
{
2016-12-02 16:17:22 +00:00
auto address = router->GetSSUAddress (v4only || !context.SupportsV6 ());
if (address)
CreateSession (router, address->host, address->port, peerTest);
else
2015-12-18 13:46:56 +00:00
LogPrint (eLogWarning, "SSU: Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");
}
void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
const boost::asio::ip::address& addr, int port, bool peerTest)
2014-01-28 21:49:54 +00:00
{
if (router)
{
if (router->UsesIntroducer ())
m_Service.post (std::bind (&SSUServer::CreateSessionThroughIntroducer, this, router, peerTest)); // always V4 thread
else
2014-01-28 21:49:54 +00:00
{
boost::asio::ip::udp::endpoint remoteEndpoint (addr, port);
auto& s = addr.is_v6 () ? m_ServiceV6 : m_Service;
2015-11-29 14:10:49 +00:00
s.post (std::bind (&SSUServer::CreateDirectSession, this, router, remoteEndpoint, peerTest));
}
}
}
2015-11-29 14:10:49 +00:00
void SSUServer::CreateDirectSession (std::shared_ptr<const i2p::data::RouterInfo> router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest)
{
auto& sessions = remoteEndpoint.address ().is_v6 () ? m_SessionsV6 : m_Sessions;
auto it = sessions.find (remoteEndpoint);
if (it != sessions.end ())
2015-11-29 14:10:49 +00:00
{
auto session = it->second;
if (peerTest && session->GetState () == eSessionStateEstablished)
session->SendPeerTest ();
}
else
{
// otherwise create new session
auto session = std::make_shared<SSUSession> (*this, remoteEndpoint, router, peerTest);
sessions[remoteEndpoint] = session;
2015-11-29 14:10:49 +00:00
// connect
2016-06-27 13:00:00 +00:00
LogPrint (eLogDebug, "SSU: Creating new session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ",
2015-11-29 14:10:49 +00:00
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ());
session->Connect ();
}
}
void SSUServer::CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest)
{
if (router && router->UsesIntroducer ())
{
auto address = router->GetSSUAddress (true); // v4 only for now
if (address)
{
boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port);
auto it = m_Sessions.find (remoteEndpoint);
// check if session if presented alredy
if (it != m_Sessions.end ())
{
auto session = it->second;
if (peerTest && session->GetState () == eSessionStateEstablished)
session->SendPeerTest ();
return;
}
// create new session
int numIntroducers = address->introducers.size ();
if (numIntroducers > 0)
{
std::shared_ptr<SSUSession> introducerSession;
const i2p::data::RouterInfo::Introducer * introducer = nullptr;
// we might have a session to introducer already
for (int i = 0; i < numIntroducers; i++)
2014-02-21 21:13:36 +00:00
{
2015-12-01 14:21:02 +00:00
auto intr = &(address->introducers[i]);
boost::asio::ip::udp::endpoint ep (intr->iHost, intr->iPort);
if (ep.address ().is_v4 ()) // ipv4 only
{
if (!introducer) introducer = intr; // we pick first one for now
it = m_Sessions.find (ep);
if (it != m_Sessions.end ())
{
introducerSession = it->second;
break;
}
}
2014-02-21 21:13:36 +00:00
}
2015-12-01 14:21:02 +00:00
if (!introducer)
{
2015-12-18 13:46:56 +00:00
LogPrint (eLogWarning, "SSU: Can't connect to unreachable router and no ipv4 introducers present");
2015-12-01 14:21:02 +00:00
return;
}
if (introducerSession) // session found
2016-06-27 13:00:00 +00:00
LogPrint (eLogWarning, "SSU: Session to introducer already exists");
else // create new
{
2016-06-27 13:00:00 +00:00
LogPrint (eLogDebug, "SSU: Creating new session to introducer ", introducer->iHost);
boost::asio::ip::udp::endpoint introducerEndpoint (introducer->iHost, introducer->iPort);
introducerSession = std::make_shared<SSUSession> (*this, introducerEndpoint, router);
m_Sessions[introducerEndpoint] = introducerSession;
}
// create session
auto session = std::make_shared<SSUSession> (*this, remoteEndpoint, router, peerTest);
m_Sessions[remoteEndpoint] = session;
// introduce
2015-12-18 13:46:56 +00:00
LogPrint (eLogInfo, "SSU: Introduce new session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()),
"] through introducer ", introducer->iHost, ":", introducer->iPort);
session->WaitForIntroduction ();
if (i2p::context.GetRouterInfo ().UsesIntroducer ()) // if we are unreachable
{
uint8_t buf[1];
Send (buf, 0, remoteEndpoint); // send HolePunch
}
2015-12-10 03:17:43 +00:00
introducerSession->Introduce (*introducer, router);
2014-01-28 21:49:54 +00:00
}
else
2015-12-18 13:46:56 +00:00
LogPrint (eLogWarning, "SSU: Can't connect to unreachable router and no introducers present");
2014-01-28 21:49:54 +00:00
}
else
2015-12-18 13:46:56 +00:00
LogPrint (eLogWarning, "SSU: Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");
2014-01-28 21:49:54 +00:00
}
}
2014-02-09 02:06:40 +00:00
2014-11-24 17:26:11 +00:00
void SSUServer::DeleteSession (std::shared_ptr<SSUSession> session)
2014-02-09 02:06:40 +00:00
{
if (session)
{
session->Close ();
auto& ep = session->GetRemoteEndpoint ();
if (ep.address ().is_v6 ())
m_SessionsV6.erase (ep);
else
m_Sessions.erase (ep);
2014-02-09 02:06:40 +00:00
}
}
void SSUServer::DeleteAllSessions ()
{
2016-08-08 22:53:37 +00:00
for (auto& it: m_Sessions)
2014-02-09 02:06:40 +00:00
it.second->Close ();
m_Sessions.clear ();
2016-08-08 22:53:37 +00:00
for (auto& it: m_SessionsV6)
it.second->Close ();
m_SessionsV6.clear ();
2014-02-21 21:13:36 +00:00
}
2014-09-03 20:49:48 +00:00
template<typename Filter>
std::shared_ptr<SSUSession> SSUServer::GetRandomV4Session (Filter filter) // v4 only
2014-09-03 20:49:48 +00:00
{
2014-11-24 17:26:11 +00:00
std::vector<std::shared_ptr<SSUSession> > filteredSessions;
2016-08-08 22:53:37 +00:00
for (const auto& s :m_Sessions)
2014-09-03 20:49:48 +00:00
if (filter (s.second)) filteredSessions.push_back (s.second);
if (filteredSessions.size () > 0)
{
2015-11-03 14:15:49 +00:00
auto ind = rand () % filteredSessions.size ();
2014-09-03 20:49:48 +00:00
return filteredSessions[ind];
}
return nullptr;
}
std::shared_ptr<SSUSession> SSUServer::GetRandomEstablishedV4Session (std::shared_ptr<const SSUSession> excluded) // v4 only
2014-09-03 20:49:48 +00:00
{
return GetRandomV4Session (
2014-11-24 17:26:11 +00:00
[excluded](std::shared_ptr<SSUSession> session)->bool
2014-09-05 20:35:02 +00:00
{
return session->GetState () == eSessionStateEstablished && session != excluded;
}
);
}
template<typename Filter>
std::shared_ptr<SSUSession> SSUServer::GetRandomV6Session (Filter filter) // v6 only
{
std::vector<std::shared_ptr<SSUSession> > filteredSessions;
2016-08-08 22:53:37 +00:00
for (const auto& s :m_SessionsV6)
if (filter (s.second)) filteredSessions.push_back (s.second);
if (filteredSessions.size () > 0)
{
auto ind = rand () % filteredSessions.size ();
return filteredSessions[ind];
}
return nullptr;
}
std::shared_ptr<SSUSession> SSUServer::GetRandomEstablishedV6Session (std::shared_ptr<const SSUSession> excluded) // v6 only
{
return GetRandomV6Session (
[excluded](std::shared_ptr<SSUSession> session)->bool
{
return session->GetState () == eSessionStateEstablished && session != excluded;
2014-09-05 20:35:02 +00:00
}
);
}
2014-09-07 00:43:20 +00:00
std::set<SSUSession *> SSUServer::FindIntroducers (int maxNumIntroducers)
2014-09-05 20:35:02 +00:00
{
2014-09-09 17:45:12 +00:00
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
2014-09-05 20:35:02 +00:00
std::set<SSUSession *> ret;
for (int i = 0; i < maxNumIntroducers; i++)
{
auto session = GetRandomV4Session (
2014-11-24 17:26:11 +00:00
[&ret, ts](std::shared_ptr<SSUSession> session)->bool
2014-09-03 20:49:48 +00:00
{
2014-11-24 17:26:11 +00:00
return session->GetRelayTag () && !ret.count (session.get ()) &&
2014-09-09 17:45:12 +00:00
session->GetState () == eSessionStateEstablished &&
ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION;
2014-09-03 20:49:48 +00:00
}
2014-09-05 20:35:02 +00:00
);
if (session)
{
2014-11-24 17:26:11 +00:00
ret.insert (session.get ());
2014-09-05 20:35:02 +00:00
break;
}
}
return ret;
2014-09-03 20:49:48 +00:00
}
2014-09-07 00:43:20 +00:00
void SSUServer::ScheduleIntroducersUpdateTimer ()
{
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL));
2014-11-24 17:26:11 +00:00
m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
this, std::placeholders::_1));
2014-09-07 00:43:20 +00:00
}
void SSUServer::HandleIntroducersUpdateTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
2014-09-07 00:43:20 +00:00
{
// timeout expired
2015-03-30 11:21:47 +00:00
if (i2p::context.GetStatus () == eRouterStatusTesting)
{
// we still don't know if we need introducers
ScheduleIntroducersUpdateTimer ();
return;
}
if (i2p::context.GetStatus () == eRouterStatusOK) return; // we don't need introducers anymore
// we are firewalled
if (!i2p::context.IsUnreachable ()) i2p::context.SetUnreachable ();
2014-09-07 00:43:20 +00:00
std::list<boost::asio::ip::udp::endpoint> newList;
2014-09-09 12:03:05 +00:00
size_t numIntroducers = 0;
2014-09-09 17:45:12 +00:00
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
2016-08-08 22:53:37 +00:00
for (const auto& it : m_Introducers)
2014-09-07 00:43:20 +00:00
{
auto session = FindSession (it);
2014-09-09 17:45:12 +00:00
if (session && ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION)
2014-09-07 00:43:20 +00:00
{
session->SendKeepAlive ();
newList.push_back (it);
numIntroducers++;
}
else
i2p::context.RemoveIntroducer (it);
}
2014-09-09 01:53:55 +00:00
if (numIntroducers < SSU_MAX_NUM_INTRODUCERS)
2014-09-07 00:43:20 +00:00
{
// create new
2014-09-09 12:03:05 +00:00
auto introducers = FindIntroducers (SSU_MAX_NUM_INTRODUCERS);
2016-08-08 22:53:37 +00:00
for (const auto& it1: introducers)
2014-09-07 00:43:20 +00:00
{
2016-08-08 22:53:37 +00:00
const auto& ep = it1->GetRemoteEndpoint ();
i2p::data::RouterInfo::Introducer introducer;
introducer.iHost = ep.address ();
introducer.iPort = ep.port ();
introducer.iTag = it1->GetRelayTag ();
introducer.iKey = it1->GetIntroKey ();
if (i2p::context.AddIntroducer (introducer))
2014-09-07 00:43:20 +00:00
{
2016-08-08 22:53:37 +00:00
newList.push_back (ep);
if (newList.size () >= SSU_MAX_NUM_INTRODUCERS) break;
}
}
2014-09-07 00:43:20 +00:00
}
m_Introducers = newList;
2015-11-29 22:25:42 +00:00
if (m_Introducers.size () < SSU_MAX_NUM_INTRODUCERS)
2015-02-27 02:05:35 +00:00
{
auto introducer = i2p::data::netdb.GetRandomIntroducer ();
if (introducer)
2015-11-25 17:51:35 +00:00
CreateSession (introducer);
2015-02-27 02:05:35 +00:00
}
2014-09-07 00:43:20 +00:00
ScheduleIntroducersUpdateTimer ();
}
2015-02-25 20:26:06 +00:00
}
2015-03-26 20:10:52 +00:00
void SSUServer::NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session)
2015-02-25 20:26:06 +00:00
{
2015-03-26 20:10:52 +00:00
m_PeerTests[nonce] = { i2p::util::GetMillisecondsSinceEpoch (), role, session };
2015-02-25 20:26:06 +00:00
}
2015-02-26 02:56:51 +00:00
PeerTestParticipant SSUServer::GetPeerTestParticipant (uint32_t nonce)
{
auto it = m_PeerTests.find (nonce);
if (it != m_PeerTests.end ())
return it->second.role;
else
return ePeerTestParticipantUnknown;
}
2015-03-26 20:10:52 +00:00
std::shared_ptr<SSUSession> SSUServer::GetPeerTestSession (uint32_t nonce)
{
auto it = m_PeerTests.find (nonce);
if (it != m_PeerTests.end ())
return it->second.session;
else
return nullptr;
}
2015-02-26 02:56:51 +00:00
void SSUServer::UpdatePeerTest (uint32_t nonce, PeerTestParticipant role)
{
auto it = m_PeerTests.find (nonce);
if (it != m_PeerTests.end ())
it->second.role = role;
}
void SSUServer::RemovePeerTest (uint32_t nonce)
2015-02-25 20:26:06 +00:00
{
m_PeerTests.erase (nonce);
2014-09-07 00:43:20 +00:00
}
2015-02-26 19:54:28 +00:00
void SSUServer::SchedulePeerTestsCleanupTimer ()
{
m_PeerTestsCleanupTimer.expires_from_now (boost::posix_time::seconds(SSU_PEER_TEST_TIMEOUT));
m_PeerTestsCleanupTimer.async_wait (std::bind (&SSUServer::HandlePeerTestsCleanupTimer,
this, std::placeholders::_1));
}
void SSUServer::HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
int numDeleted = 0;
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto it = m_PeerTests.begin (); it != m_PeerTests.end ();)
{
if (ts > it->second.creationTime + SSU_PEER_TEST_TIMEOUT*1000LL)
{
numDeleted++;
it = m_PeerTests.erase (it);
}
else
2016-08-08 22:53:37 +00:00
++it;
2015-02-26 19:54:28 +00:00
}
if (numDeleted > 0)
2015-12-18 13:46:56 +00:00
LogPrint (eLogDebug, "SSU: ", numDeleted, " peer tests have been expired");
2015-02-26 19:54:28 +00:00
SchedulePeerTestsCleanupTimer ();
}
}
void SSUServer::ScheduleTermination ()
{
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(SSU_TERMINATION_CHECK_TIMEOUT));
m_TerminationTimer.async_wait (std::bind (&SSUServer::HandleTerminationTimer,
this, std::placeholders::_1));
}
void SSUServer::HandleTerminationTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
auto ts = i2p::util::GetSecondsSinceEpoch ();
for (auto& it: m_Sessions)
if (it.second->IsTerminationTimeoutExpired (ts))
{
auto session = it.second;
m_Service.post ([session]
{
LogPrint (eLogWarning, "SSU: no activity with ", session->GetRemoteEndpoint (), " for ", session->GetTerminationTimeout (), " seconds");
session->Failed ();
});
}
ScheduleTermination ();
}
}
void SSUServer::ScheduleTerminationV6 ()
{
m_TerminationTimerV6.expires_from_now (boost::posix_time::seconds(SSU_TERMINATION_CHECK_TIMEOUT));
m_TerminationTimerV6.async_wait (std::bind (&SSUServer::HandleTerminationTimerV6,
this, std::placeholders::_1));
}
void SSUServer::HandleTerminationTimerV6 (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
auto ts = i2p::util::GetSecondsSinceEpoch ();
for (auto& it: m_SessionsV6)
if (it.second->IsTerminationTimeoutExpired (ts))
{
auto session = it.second;
m_ServiceV6.post ([session]
{
LogPrint (eLogWarning, "SSU: no activity with ", session->GetRemoteEndpoint (), " for ", session->GetTerminationTimeout (), " seconds");
session->Failed ();
});
}
ScheduleTerminationV6 ();
}
}
2014-01-23 21:10:33 +00:00
}
}