Browse Source

terminate incoming connection by timeout

pull/827/head
orignal 8 years ago
parent
commit
f0d6145fa6
  1. 26
      NTCPSession.cpp
  2. 2
      NTCPSession.h

26
NTCPSession.cpp

@ -23,7 +23,7 @@ namespace i2p @@ -23,7 +23,7 @@ namespace i2p
namespace transport
{
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_IsEstablished (false), m_IsTerminated (false),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false)
@ -98,6 +98,7 @@ namespace transport @@ -98,6 +98,7 @@ namespace transport
m_DHKeysPair = nullptr;
SetTerminationTimeout (NTCP_TERMINATION_TIMEOUT);
SendTimeSyncMessage ();
transports.PeerConnected (shared_from_this ());
}
@ -120,6 +121,7 @@ namespace transport @@ -120,6 +121,7 @@ namespace transport
void NTCPSession::ServerLogin ()
{
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
// receive Phase1
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 (),
@ -858,6 +860,8 @@ namespace transport @@ -858,6 +860,8 @@ namespace transport
auto ntcpSessions = m_NTCPSessions;
for (auto& it: ntcpSessions)
it.second->Terminate ();
for (auto& it: m_PendingIncomingSessions)
it->Terminate ();
}
m_NTCPSessions.clear ();
@ -940,7 +944,10 @@ namespace transport @@ -940,7 +944,10 @@ namespace transport
{
LogPrint (eLogDebug, "NTCP: Connected from ", ep);
if (conn)
{
conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn);
}
}
else
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ());
@ -965,7 +972,10 @@ namespace transport @@ -965,7 +972,10 @@ namespace transport
{
LogPrint (eLogDebug, "NTCP: Connected from ", ep);
if (conn)
{
conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn);
}
}
else
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ());
@ -1033,16 +1043,30 @@ namespace transport @@ -1033,16 +1043,30 @@ namespace transport
if (ecode != boost::asio::error::operation_aborted)
{
auto ts = i2p::util::GetSecondsSinceEpoch ();
// established
for (auto& it: m_NTCPSessions)
if (it.second->IsTerminationTimeoutExpired (ts))
{
auto session = it.second;
// Termniate modifies m_NTCPSession, so we postpone it
m_Service.post ([session]
{
LogPrint (eLogDebug, "NTCP: No activity for ", session->GetTerminationTimeout (), " seconds");
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 ();
}
}

2
NTCPSession.h

@ -55,6 +55,7 @@ namespace transport @@ -55,6 +55,7 @@ namespace transport
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
bool IsTerminated () const { return m_IsTerminated; };
void ClientLogin ();
void ServerLogin ();
@ -166,6 +167,7 @@ namespace transport @@ -166,6 +167,7 @@ namespace transport
boost::asio::deadline_timer m_TerminationTimer;
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor;
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
std::list<std::shared_ptr<NTCPSession> > m_PendingIncomingSessions;
public:

Loading…
Cancel
Save