|
|
@ -10,6 +10,8 @@ |
|
|
|
using namespace i2p::data; |
|
|
|
using namespace i2p::data; |
|
|
|
|
|
|
|
|
|
|
|
namespace i2p |
|
|
|
namespace i2p |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
DHKeysPairSupplier::DHKeysPairSupplier (int size): |
|
|
|
DHKeysPairSupplier::DHKeysPairSupplier (int size): |
|
|
|
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) |
|
|
|
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) |
|
|
@ -66,7 +68,7 @@ namespace i2p |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire () |
|
|
|
DHKeysPair * DHKeysPairSupplier::Acquire () |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_Queue.empty ()) |
|
|
|
if (!m_Queue.empty ()) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -78,14 +80,14 @@ namespace i2p |
|
|
|
} |
|
|
|
} |
|
|
|
else // queue is empty, create new
|
|
|
|
else // queue is empty, create new
|
|
|
|
{ |
|
|
|
{ |
|
|
|
i2p::transport::DHKeysPair * pair = new i2p::transport::DHKeysPair (); |
|
|
|
DHKeysPair * pair = new DHKeysPair (); |
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); |
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); |
|
|
|
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey); |
|
|
|
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey); |
|
|
|
return pair; |
|
|
|
return pair; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DHKeysPairSupplier::Return (i2p::transport::DHKeysPair * pair) |
|
|
|
void DHKeysPairSupplier::Return (DHKeysPair * pair) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
m_Queue.push (pair); |
|
|
|
m_Queue.push (pair); |
|
|
@ -119,7 +121,7 @@ namespace i2p |
|
|
|
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); |
|
|
|
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); |
|
|
|
|
|
|
|
|
|
|
|
LogPrint ("Start listening TCP port ", address.port); |
|
|
|
LogPrint ("Start listening TCP port ", address.port); |
|
|
|
auto conn = new i2p::ntcp::NTCPServerConnection (m_Service); |
|
|
|
auto conn = new NTCPServerConnection (m_Service); |
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, |
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, |
|
|
|
conn, boost::asio::placeholders::error)); |
|
|
|
conn, boost::asio::placeholders::error)); |
|
|
|
} |
|
|
|
} |
|
|
@ -127,7 +129,7 @@ namespace i2p |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_SSUServer) |
|
|
|
if (!m_SSUServer) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_SSUServer = new i2p::ssu::SSUServer (address.port); |
|
|
|
m_SSUServer = new SSUServer (address.port); |
|
|
|
LogPrint ("Start listening UDP port ", address.port); |
|
|
|
LogPrint ("Start listening UDP port ", address.port); |
|
|
|
m_SSUServer->Start (); |
|
|
|
m_SSUServer->Start (); |
|
|
|
DetectExternalIP (); |
|
|
|
DetectExternalIP (); |
|
|
@ -179,19 +181,19 @@ namespace i2p |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Transports::AddNTCPSession (i2p::ntcp::NTCPSession * session) |
|
|
|
void Transports::AddNTCPSession (NTCPSession * session) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (session) |
|
|
|
if (session) |
|
|
|
m_NTCPSessions[session->GetRemoteRouterInfo ().GetIdentHash ()] = session; |
|
|
|
m_NTCPSessions[session->GetRemoteRouterInfo ().GetIdentHash ()] = session; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Transports::RemoveNTCPSession (i2p::ntcp::NTCPSession * session) |
|
|
|
void Transports::RemoveNTCPSession (NTCPSession * session) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (session) |
|
|
|
if (session) |
|
|
|
m_NTCPSessions.erase (session->GetRemoteRouterInfo ().GetIdentHash ()); |
|
|
|
m_NTCPSessions.erase (session->GetRemoteRouterInfo ().GetIdentHash ()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Transports::HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error) |
|
|
|
void Transports::HandleAccept (NTCPServerConnection * conn, const boost::system::error_code& error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!error) |
|
|
|
if (!error) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -203,13 +205,13 @@ namespace i2p |
|
|
|
|
|
|
|
|
|
|
|
if (error != boost::asio::error::operation_aborted) |
|
|
|
if (error != boost::asio::error::operation_aborted) |
|
|
|
{ |
|
|
|
{ |
|
|
|
conn = new i2p::ntcp::NTCPServerConnection (m_Service); |
|
|
|
conn = new NTCPServerConnection (m_Service); |
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, |
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, |
|
|
|
conn, boost::asio::placeholders::error)); |
|
|
|
conn, boost::asio::placeholders::error)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
i2p::ntcp::NTCPSession * Transports::GetNextNTCPSession () |
|
|
|
NTCPSession * Transports::GetNextNTCPSession () |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (auto session: m_NTCPSessions) |
|
|
|
for (auto session: m_NTCPSessions) |
|
|
|
if (session.second->IsEstablished ()) |
|
|
|
if (session.second->IsEstablished ()) |
|
|
@ -217,7 +219,7 @@ namespace i2p |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
i2p::ntcp::NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident) |
|
|
|
NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto it = m_NTCPSessions.find (ident); |
|
|
|
auto it = m_NTCPSessions.find (ident); |
|
|
|
if (it != m_NTCPSessions.end ()) |
|
|
|
if (it != m_NTCPSessions.end ()) |
|
|
@ -252,9 +254,9 @@ namespace i2p |
|
|
|
// existing session not found. create new
|
|
|
|
// existing session not found. create new
|
|
|
|
// try NTCP first if message size < 16K
|
|
|
|
// try NTCP first if message size < 16K
|
|
|
|
auto address = r->GetNTCPAddress (); |
|
|
|
auto address = r->GetNTCPAddress (); |
|
|
|
if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < i2p::ntcp::NTCP_MAX_MESSAGE_SIZE) |
|
|
|
if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < NTCP_MAX_MESSAGE_SIZE) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto s = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r); |
|
|
|
auto s = new NTCPClient (m_Service, address->host, address->port, *r); |
|
|
|
AddNTCPSession (s); |
|
|
|
AddNTCPSession (s); |
|
|
|
s->SendI2NPMessage (msg); |
|
|
|
s->SendI2NPMessage (msg); |
|
|
|
} |
|
|
|
} |
|
|
@ -328,13 +330,15 @@ namespace i2p |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
i2p::transport::DHKeysPair * Transports::GetNextDHKeysPair () |
|
|
|
DHKeysPair * Transports::GetNextDHKeysPair () |
|
|
|
{ |
|
|
|
{ |
|
|
|
return m_DHKeysPairSupplier.Acquire (); |
|
|
|
return m_DHKeysPairSupplier.Acquire (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Transports::ReuseDHKeysPair (i2p::transport::DHKeysPair * pair) |
|
|
|
void Transports::ReuseDHKeysPair (DHKeysPair * pair) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_DHKeysPairSupplier.Return (pair); |
|
|
|
m_DHKeysPairSupplier.Return (pair); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|