From f0d6145fa677ea4f7ec651ee6a6cd92991ba8661 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Mar 2017 21:17:19 -0500 Subject: [PATCH] terminate incoming connection by timeout --- NTCPSession.cpp | 26 +++++++++++++++++++++++++- NTCPSession.h | 4 +++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 8dbe4fe7..e26265a3 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -23,7 +23,7 @@ namespace i2p namespace transport { NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr 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 m_DHKeysPair = nullptr; + SetTerminationTimeout (NTCP_TERMINATION_TIMEOUT); SendTimeSyncMessage (); transports.PeerConnected (shared_from_this ()); } @@ -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 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 { 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 { 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 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 (); } } diff --git a/NTCPSession.h b/NTCPSession.h index d9acd5ce..3891dd62 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -55,7 +55,8 @@ 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 (); void SendI2NPMessages (const std::vector >& msgs); @@ -166,6 +167,7 @@ namespace transport boost::asio::deadline_timer m_TerminationTimer; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor; std::map > m_NTCPSessions; // access from m_Thread only + std::list > m_PendingIncomingSessions; public: