mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
783 lines
23 KiB
783 lines
23 KiB
11 years ago
|
#include <string.h>
|
||
11 years ago
|
#include <boost/bind.hpp>
|
||
|
#include "Log.h"
|
||
11 years ago
|
#include "Timestamp.h"
|
||
11 years ago
|
#include "RouterContext.h"
|
||
10 years ago
|
#include "NetDb.h"
|
||
11 years ago
|
#include "SSU.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace transport
|
||
11 years ago
|
{
|
||
9 years ago
|
|
||
|
SSUServer::SSUServer (const boost::asio::ip::address & addr, int port):
|
||
|
m_OnlyV6(true), m_IsRunning(false),
|
||
8 years ago
|
m_Thread (nullptr), m_ThreadV6 (nullptr), m_ReceiversThread (nullptr),
|
||
|
m_ReceiversThreadV6 (nullptr), m_Work (m_Service), m_WorkV6 (m_ServiceV6),
|
||
|
m_ReceiversWork (m_ReceiversService), m_ReceiversWorkV6 (m_ReceiversServiceV6),
|
||
|
m_EndpointV6 (addr, port), m_Socket (m_ReceiversService, m_Endpoint),
|
||
|
m_SocketV6 (m_ReceiversServiceV6), m_IntroducersUpdateTimer (m_Service),
|
||
|
m_PeerTestsCleanupTimer (m_Service), m_TerminationTimer (m_Service),
|
||
|
m_TerminationTimerV6 (m_ServiceV6)
|
||
9 years ago
|
{
|
||
8 years ago
|
OpenSocketV6 ();
|
||
9 years ago
|
}
|
||
|
|
||
|
SSUServer::SSUServer (int port):
|
||
|
m_OnlyV6(false), m_IsRunning(false),
|
||
|
m_Thread (nullptr), m_ThreadV6 (nullptr), m_ReceiversThread (nullptr),
|
||
8 years ago
|
m_ReceiversThreadV6 (nullptr), m_Work (m_Service), m_WorkV6 (m_ServiceV6),
|
||
|
m_ReceiversWork (m_ReceiversService), m_ReceiversWorkV6 (m_ReceiversServiceV6),
|
||
10 years ago
|
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_EndpointV6 (boost::asio::ip::udp::v6 (), port),
|
||
8 years ago
|
m_Socket (m_ReceiversService), m_SocketV6 (m_ReceiversServiceV6),
|
||
8 years ago
|
m_IntroducersUpdateTimer (m_Service), m_PeerTestsCleanupTimer (m_Service),
|
||
|
m_TerminationTimer (m_Service), m_TerminationTimerV6 (m_ServiceV6)
|
||
11 years ago
|
{
|
||
8 years ago
|
OpenSocket ();
|
||
10 years ago
|
if (context.SupportsV6 ())
|
||
8 years ago
|
OpenSocketV6 ();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
SSUServer::~SSUServer ()
|
||
|
{
|
||
|
}
|
||
|
|
||
8 years ago
|
void SSUServer::OpenSocket ()
|
||
|
{
|
||
|
m_Socket.open (boost::asio::ip::udp::v4());
|
||
8 years ago
|
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (SSU_SOCKET_RECEIVE_BUFFER_SIZE));
|
||
|
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (SSU_SOCKET_SEND_BUFFER_SIZE));
|
||
8 years ago
|
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));
|
||
8 years ago
|
m_SocketV6.set_option (boost::asio::socket_base::receive_buffer_size (SSU_SOCKET_RECEIVE_BUFFER_SIZE));
|
||
|
m_SocketV6.set_option (boost::asio::socket_base::send_buffer_size (SSU_SOCKET_SEND_BUFFER_SIZE));
|
||
8 years ago
|
m_SocketV6.bind (m_EndpointV6);
|
||
|
}
|
||
|
|
||
11 years ago
|
void SSUServer::Start ()
|
||
|
{
|
||
11 years ago
|
m_IsRunning = true;
|
||
9 years ago
|
if (!m_OnlyV6)
|
||
|
{
|
||
8 years ago
|
m_ReceiversThread = new std::thread (std::bind (&SSUServer::RunReceivers, this));
|
||
9 years ago
|
m_Thread = new std::thread (std::bind (&SSUServer::Run, this));
|
||
|
m_ReceiversService.post (std::bind (&SSUServer::Receive, this));
|
||
8 years ago
|
ScheduleTermination ();
|
||
9 years ago
|
}
|
||
10 years ago
|
if (context.SupportsV6 ())
|
||
10 years ago
|
{
|
||
8 years ago
|
m_ReceiversThreadV6 = new std::thread (std::bind (&SSUServer::RunReceiversV6, this));
|
||
10 years ago
|
m_ThreadV6 = new std::thread (std::bind (&SSUServer::RunV6, this));
|
||
8 years ago
|
m_ReceiversServiceV6.post (std::bind (&SSUServer::ReceiveV6, this));
|
||
8 years ago
|
ScheduleTerminationV6 ();
|
||
10 years ago
|
}
|
||
10 years ago
|
SchedulePeerTestsCleanupTimer ();
|
||
10 years ago
|
ScheduleIntroducersUpdateTimer (); // wait for 30 seconds and decide if we need introducers
|
||
11 years ago
|
}
|
||
|
|
||
|
void SSUServer::Stop ()
|
||
|
{
|
||
11 years ago
|
DeleteAllSessions ();
|
||
11 years ago
|
m_IsRunning = false;
|
||
8 years ago
|
m_TerminationTimer.cancel ();
|
||
|
m_TerminationTimerV6.cancel ();
|
||
11 years ago
|
m_Service.stop ();
|
||
11 years ago
|
m_Socket.close ();
|
||
10 years ago
|
m_ServiceV6.stop ();
|
||
|
m_SocketV6.close ();
|
||
10 years ago
|
m_ReceiversService.stop ();
|
||
8 years ago
|
m_ReceiversServiceV6.stop ();
|
||
10 years ago
|
if (m_ReceiversThread)
|
||
|
{
|
||
|
m_ReceiversThread->join ();
|
||
|
delete m_ReceiversThread;
|
||
|
m_ReceiversThread = nullptr;
|
||
|
}
|
||
11 years ago
|
if (m_Thread)
|
||
|
{
|
||
|
m_Thread->join ();
|
||
|
delete m_Thread;
|
||
10 years ago
|
m_Thread = nullptr;
|
||
|
}
|
||
8 years ago
|
if (m_ReceiversThreadV6)
|
||
|
{
|
||
|
m_ReceiversThreadV6->join ();
|
||
|
delete m_ReceiversThreadV6;
|
||
|
m_ReceiversThreadV6 = nullptr;
|
||
|
}
|
||
10 years ago
|
if (m_ThreadV6)
|
||
|
{
|
||
|
m_ThreadV6->join ();
|
||
|
delete m_ThreadV6;
|
||
|
m_ThreadV6 = nullptr;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void SSUServer::Run ()
|
||
|
{
|
||
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_Service.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: server runtime exception: ", ex.what ());
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
void SSUServer::RunV6 ()
|
||
|
{
|
||
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_ServiceV6.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: v6 server runtime exception: ", ex.what ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
void SSUServer::RunReceivers ()
|
||
|
{
|
||
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_ReceiversService.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: receivers runtime exception: ", ex.what ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
void SSUServer::RunReceiversV6 ()
|
||
|
{
|
||
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_ReceiversServiceV6.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU: v6 receivers runtime exception: ", ex.what ());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void SSUServer::AddRelay (uint32_t tag, std::shared_ptr<SSUSession> relay)
|
||
11 years ago
|
{
|
||
|
m_Relays[tag] = relay;
|
||
|
}
|
||
|
|
||
8 years ago
|
void SSUServer::RemoveRelay (uint32_t tag)
|
||
|
{
|
||
|
m_Relays.erase (tag);
|
||
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<SSUSession> SSUServer::FindRelaySession (uint32_t tag)
|
||
11 years ago
|
{
|
||
|
auto it = m_Relays.find (tag);
|
||
|
if (it != m_Relays.end ())
|
||
8 years ago
|
{
|
||
|
if (it->second->GetState () == eSessionStateEstablished)
|
||
|
return it->second;
|
||
|
else
|
||
|
m_Relays.erase (it);
|
||
|
}
|
||
11 years ago
|
return nullptr;
|
||
|
}
|
||
|
|
||
11 years ago
|
void SSUServer::Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to)
|
||
11 years ago
|
{
|
||
10 years ago
|
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);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void SSUServer::Receive ()
|
||
|
{
|
||
8 years ago
|
SSUPacket * packet = new SSUPacket ();
|
||
10 years ago
|
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));
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUServer::ReceiveV6 ()
|
||
|
{
|
||
8 years ago
|
SSUPacket * packet = new SSUPacket ();
|
||
10 years ago
|
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));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUServer::HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet)
|
||
11 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
10 years ago
|
packet->len = bytes_transferred;
|
||
10 years ago
|
std::vector<SSUPacket *> packets;
|
||
|
packets.push_back (packet);
|
||
|
|
||
10 years ago
|
boost::system::error_code ec;
|
||
|
size_t moreBytes = m_Socket.available(ec);
|
||
8 years ago
|
if (!ec)
|
||
|
{
|
||
|
while (moreBytes && packets.size () < 25)
|
||
|
{
|
||
8 years ago
|
packet = new SSUPacket ();
|
||
8 years ago
|
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 ());
|
||
8 years ago
|
delete packet;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
m_Service.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets, &m_Sessions));
|
||
11 years ago
|
Receive ();
|
||
|
}
|
||
|
else
|
||
10 years ago
|
{
|
||
|
delete packet;
|
||
8 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU: receive error: ", ecode.message ());
|
||
8 years ago
|
m_Socket.close ();
|
||
|
OpenSocket ();
|
||
8 years ago
|
Receive ();
|
||
|
}
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void SSUServer::HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet)
|
||
10 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
10 years ago
|
packet->len = bytes_transferred;
|
||
10 years ago
|
std::vector<SSUPacket *> packets;
|
||
|
packets.push_back (packet);
|
||
|
|
||
8 years ago
|
boost::system::error_code ec;
|
||
|
size_t moreBytes = m_SocketV6.available (ec);
|
||
|
if (!ec)
|
||
10 years ago
|
{
|
||
8 years ago
|
while (moreBytes && packets.size () < 25)
|
||
|
{
|
||
8 years ago
|
packet = new SSUPacket ();
|
||
8 years ago
|
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 ());
|
||
8 years ago
|
delete packet;
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
9 years ago
|
m_ServiceV6.post (std::bind (&SSUServer::HandleReceivedPackets, this, packets, &m_SessionsV6));
|
||
10 years ago
|
ReceiveV6 ();
|
||
|
}
|
||
|
else
|
||
10 years ago
|
{
|
||
8 years ago
|
delete packet;
|
||
8 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU: v6 receive error: ", ecode.message ());
|
||
8 years ago
|
m_SocketV6.close ();
|
||
|
OpenSocketV6 ();
|
||
8 years ago
|
ReceiveV6 ();
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void SSUServer::HandleReceivedPackets (std::vector<SSUPacket *> packets,
|
||
9 years ago
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > * sessions)
|
||
10 years ago
|
{
|
||
10 years ago
|
std::shared_ptr<SSUSession> session;
|
||
8 years ago
|
for (auto& packet: packets)
|
||
10 years ago
|
{
|
||
10 years ago
|
try
|
||
|
{
|
||
|
if (!session || session->GetRemoteEndpoint () != packet->from) // we received packet for other session than previous
|
||
10 years ago
|
{
|
||
10 years ago
|
if (session) session->FlushData ();
|
||
9 years ago
|
auto it = sessions->find (packet->from);
|
||
|
if (it != sessions->end ())
|
||
10 years ago
|
session = it->second;
|
||
|
if (!session)
|
||
10 years ago
|
{
|
||
10 years ago
|
session = std::make_shared<SSUSession> (*this, packet->from);
|
||
|
session->WaitForConnect ();
|
||
9 years ago
|
(*sessions)[packet->from] = session;
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: new session from ", packet->from.address ().to_string (), ":", packet->from.port (), " created");
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
session->ProcessNextMessage (packet->buf, packet->len, packet->from);
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU: HandleReceivedPackets ", ex.what ());
|
||
|
if (session) session->FlushData ();
|
||
|
session = nullptr;
|
||
|
}
|
||
8 years ago
|
delete packet;
|
||
10 years ago
|
}
|
||
10 years ago
|
if (session) session->FlushData ();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<SSUSession> SSUServer::FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const
|
||
11 years ago
|
{
|
||
|
if (!router) return nullptr;
|
||
10 years ago
|
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 ();
|
||
11 years ago
|
if (!address) return nullptr;
|
||
11 years ago
|
return FindSession (boost::asio::ip::udp::endpoint (address->host, address->port));
|
||
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<SSUSession> SSUServer::FindSession (const boost::asio::ip::udp::endpoint& e) const
|
||
11 years ago
|
{
|
||
9 years ago
|
auto& sessions = e.address ().is_v6 () ? m_SessionsV6 : m_Sessions;
|
||
|
auto it = sessions.find (e);
|
||
|
if (it != sessions.end ())
|
||
11 years ago
|
return it->second;
|
||
|
else
|
||
|
return nullptr;
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest, bool v4only)
|
||
9 years ago
|
{
|
||
8 years ago
|
auto address = router->GetSSUAddress (v4only || !context.SupportsV6 ());
|
||
9 years ago
|
if (address)
|
||
|
CreateSession (router, address->host, address->port, peerTest);
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");
|
||
9 years ago
|
}
|
||
|
|
||
|
void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
|
||
|
const boost::asio::ip::address& addr, int port, bool peerTest)
|
||
11 years ago
|
{
|
||
|
if (router)
|
||
|
{
|
||
9 years ago
|
if (router->UsesIntroducer ())
|
||
9 years ago
|
m_Service.post (std::bind (&SSUServer::CreateSessionThroughIntroducer, this, router, peerTest)); // always V4 thread
|
||
9 years ago
|
else
|
||
11 years ago
|
{
|
||
9 years ago
|
boost::asio::ip::udp::endpoint remoteEndpoint (addr, port);
|
||
|
auto& s = addr.is_v6 () ? m_ServiceV6 : m_Service;
|
||
9 years ago
|
s.post (std::bind (&SSUServer::CreateDirectSession, this, router, remoteEndpoint, peerTest));
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUServer::CreateDirectSession (std::shared_ptr<const i2p::data::RouterInfo> router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest)
|
||
|
{
|
||
9 years ago
|
auto& sessions = remoteEndpoint.address ().is_v6 () ? m_SessionsV6 : m_Sessions;
|
||
|
auto it = sessions.find (remoteEndpoint);
|
||
|
if (it != sessions.end ())
|
||
9 years ago
|
{
|
||
|
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);
|
||
9 years ago
|
sessions[remoteEndpoint] = session;
|
||
9 years ago
|
// connect
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: Creating new session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ",
|
||
9 years ago
|
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ());
|
||
|
session->Connect ();
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUServer::CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest)
|
||
|
{
|
||
|
if (router && router->UsesIntroducer ())
|
||
|
{
|
||
9 years ago
|
auto address = router->GetSSUAddress (true); // v4 only for now
|
||
9 years ago
|
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
|
||
8 years ago
|
int numIntroducers = address->ssu->introducers.size ();
|
||
9 years ago
|
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++)
|
||
11 years ago
|
{
|
||
8 years ago
|
auto intr = &(address->ssu->introducers[i]);
|
||
9 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
9 years ago
|
if (!introducer)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Can't connect to unreachable router and no ipv4 introducers present");
|
||
9 years ago
|
return;
|
||
|
}
|
||
9 years ago
|
|
||
|
if (introducerSession) // session found
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Session to introducer already exists");
|
||
9 years ago
|
else // create new
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: Creating new session to introducer ", introducer->iHost);
|
||
9 years ago
|
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);
|
||
9 years ago
|
m_Sessions[remoteEndpoint] = session;
|
||
9 years ago
|
// introduce
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU: Introduce new session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()),
|
||
9 years ago
|
"] 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
|
||
|
}
|
||
9 years ago
|
introducerSession->Introduce (*introducer, router);
|
||
11 years ago
|
}
|
||
9 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Can't connect to unreachable router and no introducers present");
|
||
11 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void SSUServer::DeleteSession (std::shared_ptr<SSUSession> session)
|
||
11 years ago
|
{
|
||
|
if (session)
|
||
|
{
|
||
|
session->Close ();
|
||
9 years ago
|
auto& ep = session->GetRemoteEndpoint ();
|
||
|
if (ep.address ().is_v6 ())
|
||
|
m_SessionsV6.erase (ep);
|
||
|
else
|
||
|
m_Sessions.erase (ep);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SSUServer::DeleteAllSessions ()
|
||
|
{
|
||
8 years ago
|
for (auto& it: m_Sessions)
|
||
11 years ago
|
it.second->Close ();
|
||
|
m_Sessions.clear ();
|
||
9 years ago
|
|
||
8 years ago
|
for (auto& it: m_SessionsV6)
|
||
9 years ago
|
it.second->Close ();
|
||
|
m_SessionsV6.clear ();
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
template<typename Filter>
|
||
9 years ago
|
std::shared_ptr<SSUSession> SSUServer::GetRandomV4Session (Filter filter) // v4 only
|
||
10 years ago
|
{
|
||
10 years ago
|
std::vector<std::shared_ptr<SSUSession> > filteredSessions;
|
||
8 years ago
|
for (const auto& s :m_Sessions)
|
||
10 years ago
|
if (filter (s.second)) filteredSessions.push_back (s.second);
|
||
|
if (filteredSessions.size () > 0)
|
||
|
{
|
||
9 years ago
|
auto ind = rand () % filteredSessions.size ();
|
||
10 years ago
|
return filteredSessions[ind];
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
9 years ago
|
std::shared_ptr<SSUSession> SSUServer::GetRandomEstablishedV4Session (std::shared_ptr<const SSUSession> excluded) // v4 only
|
||
10 years ago
|
{
|
||
9 years ago
|
return GetRandomV4Session (
|
||
10 years ago
|
[excluded](std::shared_ptr<SSUSession> session)->bool
|
||
10 years ago
|
{
|
||
8 years ago
|
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;
|
||
8 years ago
|
for (const auto& s :m_SessionsV6)
|
||
8 years ago
|
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;
|
||
10 years ago
|
}
|
||
|
);
|
||
|
}
|
||
|
|
||
10 years ago
|
std::set<SSUSession *> SSUServer::FindIntroducers (int maxNumIntroducers)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
std::set<SSUSession *> ret;
|
||
|
for (int i = 0; i < maxNumIntroducers; i++)
|
||
|
{
|
||
9 years ago
|
auto session = GetRandomV4Session (
|
||
10 years ago
|
[&ret, ts](std::shared_ptr<SSUSession> session)->bool
|
||
10 years ago
|
{
|
||
10 years ago
|
return session->GetRelayTag () && !ret.count (session.get ()) &&
|
||
10 years ago
|
session->GetState () == eSessionStateEstablished &&
|
||
|
ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION;
|
||
10 years ago
|
}
|
||
10 years ago
|
);
|
||
|
if (session)
|
||
|
{
|
||
10 years ago
|
ret.insert (session.get ());
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void SSUServer::ScheduleIntroducersUpdateTimer ()
|
||
|
{
|
||
|
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(SSU_KEEP_ALIVE_INTERVAL));
|
||
10 years ago
|
m_IntroducersUpdateTimer.async_wait (std::bind (&SSUServer::HandleIntroducersUpdateTimer,
|
||
|
this, std::placeholders::_1));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUServer::HandleIntroducersUpdateTimer (const boost::system::error_code& ecode)
|
||
|
{
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
10 years ago
|
{
|
||
|
// timeout expired
|
||
10 years ago
|
if (i2p::context.GetStatus () == eRouterStatusTesting)
|
||
10 years ago
|
{
|
||
|
// 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
|
||
10 years ago
|
if (!i2p::context.IsUnreachable ()) i2p::context.SetUnreachable ();
|
||
10 years ago
|
std::list<boost::asio::ip::udp::endpoint> newList;
|
||
10 years ago
|
size_t numIntroducers = 0;
|
||
10 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
8 years ago
|
for (const auto& it : m_Introducers)
|
||
10 years ago
|
{
|
||
|
auto session = FindSession (it);
|
||
10 years ago
|
if (session && ts < session->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_DURATION)
|
||
10 years ago
|
{
|
||
|
session->SendKeepAlive ();
|
||
|
newList.push_back (it);
|
||
|
numIntroducers++;
|
||
|
}
|
||
|
else
|
||
|
i2p::context.RemoveIntroducer (it);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (numIntroducers < SSU_MAX_NUM_INTRODUCERS)
|
||
10 years ago
|
{
|
||
|
// create new
|
||
10 years ago
|
auto introducers = FindIntroducers (SSU_MAX_NUM_INTRODUCERS);
|
||
8 years ago
|
for (const auto& it1: introducers)
|
||
10 years ago
|
{
|
||
8 years ago
|
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))
|
||
10 years ago
|
{
|
||
8 years ago
|
newList.push_back (ep);
|
||
|
if (newList.size () >= SSU_MAX_NUM_INTRODUCERS) break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
m_Introducers = newList;
|
||
9 years ago
|
if (m_Introducers.size () < SSU_MAX_NUM_INTRODUCERS)
|
||
10 years ago
|
{
|
||
|
auto introducer = i2p::data::netdb.GetRandomIntroducer ();
|
||
|
if (introducer)
|
||
9 years ago
|
CreateSession (introducer);
|
||
10 years ago
|
}
|
||
10 years ago
|
ScheduleIntroducersUpdateTimer ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUServer::NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_PeerTests[nonce] = { i2p::util::GetMillisecondsSinceEpoch (), role, session };
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
PeerTestParticipant SSUServer::GetPeerTestParticipant (uint32_t nonce)
|
||
|
{
|
||
|
auto it = m_PeerTests.find (nonce);
|
||
|
if (it != m_PeerTests.end ())
|
||
|
return it->second.role;
|
||
|
else
|
||
|
return ePeerTestParticipantUnknown;
|
||
|
}
|
||
|
|
||
10 years ago
|
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;
|
||
|
}
|
||
|
|
||
10 years ago
|
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)
|
||
10 years ago
|
{
|
||
|
m_PeerTests.erase (nonce);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
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
|
||
8 years ago
|
++it;
|
||
10 years ago
|
}
|
||
|
if (numDeleted > 0)
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: ", numDeleted, " peer tests have been expired");
|
||
10 years ago
|
SchedulePeerTestsCleanupTimer ();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
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 ();
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|