2013-10-27 15:26:39 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include "Log.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Transports.h"
|
|
|
|
|
|
|
|
using namespace i2p::data;
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
Transports transports;
|
|
|
|
|
|
|
|
Transports::Transports ():
|
2014-01-23 21:10:33 +00:00
|
|
|
m_Thread (nullptr), m_Work (m_Service),m_NTCPAcceptor (nullptr), m_SSUServer (nullptr)
|
2013-10-27 15:26:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Transports::~Transports ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::Start ()
|
|
|
|
{
|
2013-12-29 15:48:57 +00:00
|
|
|
m_IsRunning = true;
|
2013-10-27 15:26:39 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&Transports::Run, this));
|
2014-02-09 02:06:40 +00:00
|
|
|
m_Timer = new boost::asio::deadline_timer (m_Service);
|
2013-10-27 15:26:39 +00:00
|
|
|
// create acceptors
|
|
|
|
auto addresses = context.GetRouterInfo ().GetAddresses ();
|
|
|
|
for (auto& address : addresses)
|
|
|
|
{
|
|
|
|
if (address.transportStyle == RouterInfo::eTransportNTCP)
|
|
|
|
{
|
|
|
|
m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service,
|
|
|
|
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port));
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
LogPrint ("Start listening TCP port ", address.port);
|
2013-10-27 15:26:39 +00:00
|
|
|
auto conn = new i2p::ntcp::NTCPServerConnection (m_Service);
|
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this,
|
|
|
|
conn, boost::asio::placeholders::error));
|
2014-02-25 00:25:26 +00:00
|
|
|
}
|
2014-01-23 21:10:33 +00:00
|
|
|
else if (address.transportStyle == RouterInfo::eTransportSSU)
|
2014-02-25 00:25:26 +00:00
|
|
|
{
|
2014-01-23 21:10:33 +00:00
|
|
|
if (!m_SSUServer)
|
|
|
|
{
|
|
|
|
m_SSUServer = new i2p::ssu::SSUServer (m_Service, address.port);
|
|
|
|
LogPrint ("Start listening UDP port ", address.port);
|
|
|
|
m_SSUServer->Start ();
|
2014-02-25 00:25:26 +00:00
|
|
|
DetectExternalIP ();
|
2014-01-23 21:10:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("SSU server already exists");
|
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::Stop ()
|
|
|
|
{
|
|
|
|
for (auto session: m_NTCPSessions)
|
|
|
|
delete session.second;
|
|
|
|
m_NTCPSessions.clear ();
|
|
|
|
delete m_NTCPAcceptor;
|
2014-02-16 13:57:50 +00:00
|
|
|
|
|
|
|
if (m_Timer)
|
|
|
|
{
|
|
|
|
m_Timer->cancel ();
|
|
|
|
delete m_Timer;
|
|
|
|
}
|
2014-02-09 02:06:40 +00:00
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
if (m_SSUServer)
|
|
|
|
{
|
|
|
|
m_SSUServer->Stop ();
|
|
|
|
delete m_SSUServer;
|
|
|
|
}
|
|
|
|
|
2013-12-29 15:48:57 +00:00
|
|
|
m_IsRunning = false;
|
2013-10-27 15:26:39 +00:00
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
void Transports::Run ()
|
|
|
|
{
|
2013-12-29 15:48:57 +00:00
|
|
|
while (m_IsRunning)
|
2013-11-29 12:52:09 +00:00
|
|
|
{
|
2013-12-29 15:48:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint ("Transports: ", ex.what ());
|
|
|
|
}
|
2013-11-29 12:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:26:39 +00:00
|
|
|
void Transports::AddNTCPSession (i2p::ntcp::NTCPSession * session)
|
|
|
|
{
|
|
|
|
if (session)
|
2013-11-29 12:52:09 +00:00
|
|
|
m_NTCPSessions[session->GetRemoteRouterInfo ().GetIdentHash ()] = session;
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::RemoveNTCPSession (i2p::ntcp::NTCPSession * session)
|
|
|
|
{
|
|
|
|
if (session)
|
2013-11-29 12:52:09 +00:00
|
|
|
m_NTCPSessions.erase (session->GetRemoteRouterInfo ().GetIdentHash ());
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
LogPrint ("Connected from ", conn->GetSocket ().remote_endpoint().address ().to_string ());
|
|
|
|
conn->ServerLogin ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = new i2p::ntcp::NTCPServerConnection (m_Service);
|
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this,
|
|
|
|
conn, boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
|
|
|
|
i2p::ntcp::NTCPSession * Transports::GetNextNTCPSession ()
|
|
|
|
{
|
|
|
|
for (auto session: m_NTCPSessions)
|
|
|
|
if (session.second->IsEstablished ())
|
|
|
|
return session.second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
i2p::ntcp::NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident)
|
2013-10-27 15:26:39 +00:00
|
|
|
{
|
2013-11-29 12:52:09 +00:00
|
|
|
auto it = m_NTCPSessions.find (ident);
|
2013-10-27 15:26:39 +00:00
|
|
|
if (it != m_NTCPSessions.end ())
|
|
|
|
return it->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
void Transports::SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg)
|
2013-10-27 15:26:39 +00:00
|
|
|
{
|
2013-11-29 12:52:09 +00:00
|
|
|
if (ident == i2p::context.GetRouterInfo ().GetIdentHash ())
|
2013-10-27 15:26:39 +00:00
|
|
|
// we send it to ourself
|
2014-03-13 00:13:49 +00:00
|
|
|
i2p::HandleI2NPMessage (msg);
|
2013-10-27 15:26:39 +00:00
|
|
|
else
|
2013-12-29 15:48:57 +00:00
|
|
|
m_Service.post (boost::bind (&Transports::PostMessage, this, ident, msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
auto session = FindNTCPSession (ident);
|
2014-03-26 01:17:03 +00:00
|
|
|
if (session)
|
|
|
|
session->SendI2NPMessage (msg);
|
|
|
|
else
|
2013-12-29 15:48:57 +00:00
|
|
|
{
|
|
|
|
RouterInfo * r = netdb.FindRouter (ident);
|
|
|
|
if (r)
|
|
|
|
{
|
2014-03-26 01:17:03 +00:00
|
|
|
auto ssuSession = m_SSUServer ? m_SSUServer->FindSession (r) : nullptr;
|
|
|
|
if (ssuSession)
|
|
|
|
ssuSession->SendI2NPMessage (msg);
|
2014-01-19 15:05:54 +00:00
|
|
|
else
|
2014-02-11 00:27:55 +00:00
|
|
|
{
|
2014-03-26 01:17:03 +00:00
|
|
|
// existing session not found. create new
|
|
|
|
// try NTCP first
|
|
|
|
auto address = r->GetNTCPAddress ();
|
|
|
|
if (address)
|
|
|
|
{
|
|
|
|
auto s = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r);
|
|
|
|
AddNTCPSession (s);
|
|
|
|
s->SendI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// then SSU
|
|
|
|
auto s = m_SSUServer ? m_SSUServer->GetSession (r) : nullptr;
|
2014-02-11 00:27:55 +00:00
|
|
|
if (s)
|
|
|
|
s->SendI2NPMessage (msg);
|
2014-03-26 01:17:03 +00:00
|
|
|
else
|
|
|
|
LogPrint ("No NTCP and SSU addresses available");
|
2014-02-11 00:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-29 15:48:57 +00:00
|
|
|
}
|
2014-01-19 15:05:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("Router not found. Requested");
|
|
|
|
i2p::data::netdb.RequestDestination (ident);
|
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 02:06:40 +00:00
|
|
|
|
|
|
|
void Transports::DetectExternalIP ()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 5; i ++)
|
|
|
|
{
|
|
|
|
auto router = i2p::data::netdb.GetRandomRouter ();
|
|
|
|
if (router && router->IsSSU () && m_SSUServer)
|
2014-02-09 13:52:56 +00:00
|
|
|
m_SSUServer->GetSession (router);
|
2014-02-09 02:06:40 +00:00
|
|
|
}
|
|
|
|
if (m_Timer)
|
|
|
|
{
|
|
|
|
m_Timer->expires_from_now (boost::posix_time::seconds(5)); // 5 seconds
|
|
|
|
m_Timer->async_wait (boost::bind (&Transports::HandleTimer, this, boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::HandleTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
// end of external IP detection
|
|
|
|
if (m_SSUServer)
|
|
|
|
m_SSUServer->DeleteAllSessions ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|