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"
|
2014-01-23 21:10:33 +00:00
|
|
|
#include "SSU.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-21 16:25:53 +00:00
|
|
|
namespace transport
|
2014-01-23 21:10:33 +00:00
|
|
|
{
|
2015-02-09 01:28:18 +00:00
|
|
|
SSUServer::SSUServer (int port): m_Thread (nullptr), m_ThreadV6 (nullptr), m_ReceiversThread (nullptr),
|
|
|
|
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),
|
|
|
|
m_Socket (m_ReceiversService, m_Endpoint), m_SocketV6 (m_ReceiversService),
|
|
|
|
m_IntroducersUpdateTimer (m_Service)
|
2014-01-23 21:10:33 +00:00
|
|
|
{
|
2014-02-07 20:47:10 +00:00
|
|
|
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (65535));
|
|
|
|
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535));
|
2014-10-29 19:02:48 +00:00
|
|
|
if (context.SupportsV6 ())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-24 21:30:07 +00:00
|
|
|
SSUServer::~SSUServer ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
void SSUServer::Start ()
|
|
|
|
{
|
2014-04-20 00:45:41 +00:00
|
|
|
m_IsRunning = true;
|
2015-02-09 01:28:18 +00:00
|
|
|
m_ReceiversThread = new std::thread (std::bind (&SSUServer::RunReceivers, this));
|
2014-04-20 00:45:41 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&SSUServer::Run, this));
|
2015-02-09 01:28:18 +00:00
|
|
|
m_ReceiversService.post (std::bind (&SSUServer::Receive, this));
|
2014-10-29 19:02:48 +00:00
|
|
|
if (context.SupportsV6 ())
|
2014-11-30 22:19:21 +00:00
|
|
|
{
|
|
|
|
m_ThreadV6 = new std::thread (std::bind (&SSUServer::RunV6, this));
|
2015-02-09 01:28:18 +00:00
|
|
|
m_ReceiversService.post (std::bind (&SSUServer::ReceiveV6, this));
|
2014-11-30 22:19:21 +00:00
|
|
|
}
|
2014-09-08 20:43:20 +00:00
|
|
|
if (i2p::context.IsUnreachable ())
|
|
|
|
ScheduleIntroducersUpdateTimer ();
|
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;
|
|
|
|
m_Service.stop ();
|
2014-01-23 21:10:33 +00:00
|
|
|
m_Socket.close ();
|
2014-11-30 22:19:21 +00:00
|
|
|
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;
|
2014-11-30 22:19:21 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-10-28 21:55:58 +00:00
|
|
|
LogPrint (eLogError, "SSU server: ", ex.what ());
|
2014-04-20 00:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-30 22:19:21 +00:00
|
|
|
|
|
|
|
void SSUServer::RunV6 ()
|
|
|
|
{
|
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_ServiceV6.run ();
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU V6 server: ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 01:28:18 +00:00
|
|
|
|
|
|
|
void SSUServer::RunReceivers ()
|
|
|
|
{
|
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_ReceiversService.run ();
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU receivers: ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-30 22:19:21 +00:00
|
|
|
|
2014-04-16 19:54:28 +00:00
|
|
|
void SSUServer::AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay)
|
|
|
|
{
|
|
|
|
m_Relays[tag] = relay;
|
|
|
|
}
|
|
|
|
|
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 ())
|
|
|
|
return FindSession (it->second);
|
|
|
|
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);
|
|
|
|
|
|
|
|
size_t moreBytes = m_Socket.available();
|
|
|
|
while (moreBytes && packets.size () < 25)
|
|
|
|
{
|
|
|
|
packet = new SSUPacket ();
|
|
|
|
packet->len = m_Socket.receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V4), packet->from);
|
|
|
|
packets.push_back (packet);
|
|
|
|
moreBytes = m_Socket.available();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Service.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets));
|
2014-01-23 21:10:33 +00:00
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
else
|
2015-02-09 01:28:18 +00:00
|
|
|
{
|
2014-01-23 21:10:33 +00:00
|
|
|
LogPrint ("SSU receive error: ", ecode.message ());
|
2015-02-09 01:28:18 +00:00
|
|
|
delete packet;
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
size_t moreBytes = m_SocketV6.available ();
|
|
|
|
while (moreBytes && packets.size () < 25)
|
|
|
|
{
|
|
|
|
packet = new SSUPacket ();
|
|
|
|
packet->len = m_SocketV6.receive_from (boost::asio::buffer (packet->buf, SSU_MTU_V6), packet->from);
|
|
|
|
packets.push_back (packet);
|
|
|
|
moreBytes = m_SocketV6.available();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ServiceV6.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets));
|
2014-10-29 19:02:48 +00:00
|
|
|
ReceiveV6 ();
|
|
|
|
}
|
|
|
|
else
|
2015-02-09 01:28:18 +00:00
|
|
|
{
|
2014-10-29 19:02:48 +00:00
|
|
|
LogPrint ("SSU V6 receive error: ", ecode.message ());
|
2015-02-09 01:28:18 +00:00
|
|
|
delete packet;
|
|
|
|
}
|
2014-10-29 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 16:53:11 +00:00
|
|
|
void SSUServer::HandleReceivedPackets (std::vector<SSUPacket *> packets)
|
2014-10-29 19:02:48 +00:00
|
|
|
{
|
2015-02-09 19:48:01 +00:00
|
|
|
std::shared_ptr<SSUSession> session;
|
2015-02-09 16:53:11 +00:00
|
|
|
for (auto it1: packets)
|
2014-10-29 19:02:48 +00:00
|
|
|
{
|
2015-02-09 16:53:11 +00:00
|
|
|
auto packet = it1;
|
2015-02-09 19:50:19 +00:00
|
|
|
if (!session || session->GetRemoteEndpoint () != packet->from) // we received packet for other session than previous
|
2015-02-07 20:25:06 +00:00
|
|
|
{
|
2015-02-09 19:48:01 +00:00
|
|
|
auto it = m_Sessions.find (packet->from);
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
session = it->second;
|
|
|
|
if (!session)
|
2015-02-09 16:53:11 +00:00
|
|
|
{
|
2015-02-09 19:48:01 +00:00
|
|
|
session = std::make_shared<SSUSession> (*this, packet->from);
|
|
|
|
session->WaitForConnect ();
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
m_Sessions[packet->from] = session;
|
|
|
|
}
|
|
|
|
LogPrint ("New SSU session from ", packet->from.address ().to_string (), ":", packet->from.port (), " created");
|
|
|
|
}
|
2015-02-09 16:53:11 +00:00
|
|
|
}
|
|
|
|
session->ProcessNextMessage (packet->buf, packet->len, packet->from);
|
|
|
|
delete packet;
|
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 it = m_Sessions.find (e);
|
2014-03-26 01:17:03 +00:00
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
2014-04-16 19:54:28 +00:00
|
|
|
}
|
2014-03-26 01:17:03 +00:00
|
|
|
|
2014-11-24 17:26:11 +00:00
|
|
|
std::shared_ptr<SSUSession> SSUServer::GetSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest)
|
2014-01-28 21:49:54 +00:00
|
|
|
{
|
2014-11-24 17:26:11 +00:00
|
|
|
std::shared_ptr<SSUSession> session;
|
2014-01-28 21:49:54 +00:00
|
|
|
if (router)
|
|
|
|
{
|
2014-10-29 22:46:35 +00:00
|
|
|
auto address = router->GetSSUAddress (!context.SupportsV6 ());
|
2014-01-28 21:49:54 +00:00
|
|
|
if (address)
|
|
|
|
{
|
|
|
|
boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port);
|
|
|
|
auto it = m_Sessions.find (remoteEndpoint);
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
session = it->second;
|
|
|
|
else
|
|
|
|
{
|
2014-02-21 21:13:36 +00:00
|
|
|
// otherwise create new session
|
2014-11-24 17:26:11 +00:00
|
|
|
session = std::make_shared<SSUSession> (*this, remoteEndpoint, router, peerTest);
|
2015-02-07 20:25:06 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
m_Sessions[remoteEndpoint] = session;
|
|
|
|
}
|
2014-02-21 21:13:36 +00:00
|
|
|
if (!router->UsesIntroducer ())
|
|
|
|
{
|
2014-04-09 15:58:57 +00:00
|
|
|
// connect directly
|
2014-04-07 23:28:06 +00:00
|
|
|
LogPrint ("Creating new SSU session to [", router->GetIdentHashAbbreviation (), "] ",
|
|
|
|
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ());
|
2014-02-21 21:13:36 +00:00
|
|
|
session->Connect ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-19 16:02:51 +00:00
|
|
|
// connect through introducer
|
2014-07-11 16:23:29 +00:00
|
|
|
int numIntroducers = address->introducers.size ();
|
2014-09-13 23:43:25 +00:00
|
|
|
if (numIntroducers > 0)
|
2014-03-19 16:02:51 +00:00
|
|
|
{
|
2014-11-24 17:26:11 +00:00
|
|
|
std::shared_ptr<SSUSession> introducerSession;
|
2014-07-11 16:23:29 +00:00
|
|
|
const i2p::data::RouterInfo::Introducer * introducer = nullptr;
|
|
|
|
// we might have a session to introducer already
|
|
|
|
for (int i = 0; i < numIntroducers; i++)
|
2014-04-08 19:35:08 +00:00
|
|
|
{
|
2014-07-11 16:23:29 +00:00
|
|
|
introducer = &(address->introducers[i]);
|
|
|
|
it = m_Sessions.find (boost::asio::ip::udp::endpoint (introducer->iHost, introducer->iPort));
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
{
|
|
|
|
introducerSession = it->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (introducerSession) // session found
|
2014-04-08 19:35:08 +00:00
|
|
|
LogPrint ("Session to introducer already exists");
|
2014-07-11 16:23:29 +00:00
|
|
|
else // create new
|
2014-04-09 15:58:57 +00:00
|
|
|
{
|
2014-07-11 16:23:29 +00:00
|
|
|
LogPrint ("Creating new session to introducer");
|
|
|
|
introducer = &(address->introducers[0]); // TODO:
|
|
|
|
boost::asio::ip::udp::endpoint introducerEndpoint (introducer->iHost, introducer->iPort);
|
2014-11-24 17:26:11 +00:00
|
|
|
introducerSession = std::make_shared<SSUSession> (*this, introducerEndpoint, router);
|
2015-02-07 20:25:06 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-07-11 16:23:29 +00:00
|
|
|
m_Sessions[introducerEndpoint] = introducerSession;
|
|
|
|
}
|
|
|
|
// introduce
|
|
|
|
LogPrint ("Introduce new SSU session to [", router->GetIdentHashAbbreviation (),
|
|
|
|
"] through introducer ", introducer->iHost, ":", introducer->iPort);
|
|
|
|
session->WaitForIntroduction ();
|
2014-09-13 23:43:25 +00:00
|
|
|
if (i2p::context.GetRouterInfo ().UsesIntroducer ()) // if we are unreachable
|
2015-02-09 01:28:18 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[1];
|
|
|
|
Send (buf, 0, remoteEndpoint); // send HolePunch
|
|
|
|
}
|
2014-07-11 16:23:29 +00:00
|
|
|
introducerSession->Introduce (introducer->iTag, introducer->iKey);
|
2014-03-19 16:02:51 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-20 22:16:55 +00:00
|
|
|
{
|
2014-10-28 21:55:58 +00:00
|
|
|
LogPrint (eLogWarning, "Can't connect to unreachable router. No introducers presented");
|
2015-02-07 20:25:06 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-07-20 22:16:55 +00:00
|
|
|
m_Sessions.erase (remoteEndpoint);
|
2014-11-24 17:26:11 +00:00
|
|
|
session.reset ();
|
2014-07-20 22:16:55 +00:00
|
|
|
}
|
2014-02-21 21:13:36 +00:00
|
|
|
}
|
2014-01-28 21:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-10-28 21:55:58 +00:00
|
|
|
LogPrint (eLogWarning, "Router ", router->GetIdentHashAbbreviation (), " doesn't have SSU address");
|
2014-01-28 21:49:54 +00:00
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|
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 ();
|
2015-02-07 20:25:06 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-02-09 02:06:40 +00:00
|
|
|
m_Sessions.erase (session->GetRemoteEndpoint ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUServer::DeleteAllSessions ()
|
|
|
|
{
|
2015-02-07 20:25:06 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-02-09 02:06:40 +00:00
|
|
|
for (auto it: m_Sessions)
|
|
|
|
it.second->Close ();
|
|
|
|
m_Sessions.clear ();
|
2014-02-21 21:13:36 +00:00
|
|
|
}
|
2014-09-03 20:49:48 +00:00
|
|
|
|
|
|
|
template<typename Filter>
|
2014-11-24 17:26:11 +00:00
|
|
|
std::shared_ptr<SSUSession> SSUServer::GetRandomSession (Filter filter)
|
2014-09-03 20:49:48 +00:00
|
|
|
{
|
2014-11-24 17:26:11 +00:00
|
|
|
std::vector<std::shared_ptr<SSUSession> > filteredSessions;
|
2014-09-03 20:49:48 +00:00
|
|
|
for (auto s :m_Sessions)
|
|
|
|
if (filter (s.second)) filteredSessions.push_back (s.second);
|
|
|
|
if (filteredSessions.size () > 0)
|
|
|
|
{
|
|
|
|
auto ind = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, filteredSessions.size ()-1);
|
|
|
|
return filteredSessions[ind];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:26:11 +00:00
|
|
|
std::shared_ptr<SSUSession> SSUServer::GetRandomEstablishedSession (std::shared_ptr<const SSUSession> excluded)
|
2014-09-03 20:49:48 +00:00
|
|
|
{
|
|
|
|
return GetRandomSession (
|
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;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 = GetRandomSession (
|
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)
|
|
|
|
{
|
|
|
|
// timeout expired
|
|
|
|
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 ();
|
2014-09-07 00:43:20 +00:00
|
|
|
for (auto it :m_Introducers)
|
|
|
|
{
|
|
|
|
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);
|
2014-09-07 00:43:20 +00:00
|
|
|
if (introducers.size () > 0)
|
|
|
|
{
|
|
|
|
for (auto it1: introducers)
|
|
|
|
{
|
2014-09-15 01:53:00 +00:00
|
|
|
auto router = it1->GetRemoteRouter ();
|
|
|
|
if (router && i2p::context.AddIntroducer (*router, it1->GetRelayTag ()))
|
2014-09-09 12:03:05 +00:00
|
|
|
{
|
|
|
|
newList.push_back (it1->GetRemoteEndpoint ());
|
|
|
|
if (newList.size () >= SSU_MAX_NUM_INTRODUCERS) break;
|
|
|
|
}
|
2014-09-07 00:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_Introducers = newList;
|
|
|
|
ScheduleIntroducersUpdateTimer ();
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 21:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|