|
|
@ -23,7 +23,7 @@ namespace i2p |
|
|
|
namespace transport |
|
|
|
namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter): |
|
|
|
NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter): |
|
|
|
TransportSession (in_RemoteRouter, NTCP_TERMINATION_TIMEOUT), |
|
|
|
TransportSession (in_RemoteRouter, NTCP_CONNECT_TIMEOUT), |
|
|
|
m_Server (server), m_Socket (m_Server.GetService ()), |
|
|
|
m_Server (server), m_Socket (m_Server.GetService ()), |
|
|
|
m_IsEstablished (false), m_IsTerminated (false), |
|
|
|
m_IsEstablished (false), m_IsTerminated (false), |
|
|
|
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false) |
|
|
|
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false) |
|
|
@ -98,6 +98,7 @@ namespace transport |
|
|
|
|
|
|
|
|
|
|
|
m_DHKeysPair = nullptr; |
|
|
|
m_DHKeysPair = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SetTerminationTimeout (NTCP_TERMINATION_TIMEOUT); |
|
|
|
SendTimeSyncMessage (); |
|
|
|
SendTimeSyncMessage (); |
|
|
|
transports.PeerConnected (shared_from_this ()); |
|
|
|
transports.PeerConnected (shared_from_this ()); |
|
|
|
} |
|
|
|
} |
|
|
@ -120,6 +121,7 @@ namespace transport |
|
|
|
|
|
|
|
|
|
|
|
void NTCPSession::ServerLogin () |
|
|
|
void NTCPSession::ServerLogin () |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
// receive Phase1
|
|
|
|
// receive Phase1
|
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (), |
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (), |
|
|
|
std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (), |
|
|
|
std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (), |
|
|
@ -858,6 +860,8 @@ namespace transport |
|
|
|
auto ntcpSessions = m_NTCPSessions; |
|
|
|
auto ntcpSessions = m_NTCPSessions; |
|
|
|
for (auto& it: ntcpSessions) |
|
|
|
for (auto& it: ntcpSessions) |
|
|
|
it.second->Terminate (); |
|
|
|
it.second->Terminate (); |
|
|
|
|
|
|
|
for (auto& it: m_PendingIncomingSessions) |
|
|
|
|
|
|
|
it->Terminate (); |
|
|
|
} |
|
|
|
} |
|
|
|
m_NTCPSessions.clear (); |
|
|
|
m_NTCPSessions.clear (); |
|
|
|
|
|
|
|
|
|
|
@ -940,7 +944,10 @@ namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep); |
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep); |
|
|
|
if (conn) |
|
|
|
if (conn) |
|
|
|
|
|
|
|
{ |
|
|
|
conn->ServerLogin (); |
|
|
|
conn->ServerLogin (); |
|
|
|
|
|
|
|
m_PendingIncomingSessions.push_back (conn); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); |
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); |
|
|
@ -965,7 +972,10 @@ namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep); |
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep); |
|
|
|
if (conn) |
|
|
|
if (conn) |
|
|
|
|
|
|
|
{ |
|
|
|
conn->ServerLogin (); |
|
|
|
conn->ServerLogin (); |
|
|
|
|
|
|
|
m_PendingIncomingSessions.push_back (conn); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); |
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); |
|
|
@ -1033,16 +1043,30 @@ namespace transport |
|
|
|
if (ecode != boost::asio::error::operation_aborted) |
|
|
|
if (ecode != boost::asio::error::operation_aborted) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
|
|
|
|
// established
|
|
|
|
for (auto& it: m_NTCPSessions) |
|
|
|
for (auto& it: m_NTCPSessions) |
|
|
|
if (it.second->IsTerminationTimeoutExpired (ts)) |
|
|
|
if (it.second->IsTerminationTimeoutExpired (ts)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto session = it.second; |
|
|
|
auto session = it.second; |
|
|
|
|
|
|
|
// Termniate modifies m_NTCPSession, so we postpone it
|
|
|
|
m_Service.post ([session] |
|
|
|
m_Service.post ([session] |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint (eLogDebug, "NTCP: No activity for ", session->GetTerminationTimeout (), " seconds"); |
|
|
|
LogPrint (eLogDebug, "NTCP: No activity for ", session->GetTerminationTimeout (), " seconds"); |
|
|
|
session->Terminate (); |
|
|
|
session->Terminate (); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// pending
|
|
|
|
|
|
|
|
for (auto it = m_PendingIncomingSessions.begin (); it != m_PendingIncomingSessions.end ();) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ((*it)->IsEstablished () || (*it)->IsTerminated ()) |
|
|
|
|
|
|
|
it = m_PendingIncomingSessions.erase (it); // established or terminated
|
|
|
|
|
|
|
|
else if ((*it)->IsTerminationTimeoutExpired (ts)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
(*it)->Terminate (); |
|
|
|
|
|
|
|
it = m_PendingIncomingSessions.erase (it); // expired
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ScheduleTermination (); |
|
|
|
ScheduleTermination (); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|