From 5699b7bae579b7a8714cdb838cdeca9a527cbd39 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 29 Nov 2016 14:12:44 -0500 Subject: [PATCH] 5 seconds connection timeout for NTCP --- NTCPSession.cpp | 19 ++++++++++++++++--- NTCPSession.h | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index b65532be..a9e13fc5 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -951,15 +951,28 @@ namespace transport { LogPrint (eLogDebug, "NTCP: Connecting to ", address ,":", port); m_Service.post([=]() - { + { if (this->AddNTCPSession (conn)) + { + auto timer = std::make_shared(m_Service); + timer->expires_from_now (boost::posix_time::seconds(NTCP_CONNECT_TIMEOUT)); + timer->async_wait ([conn](const boost::system::error_code& ecode) + { + if (ecode != boost::asio::error::operation_aborted) + { + LogPrint (eLogError, "NTCP: Not connected in ", NTCP_CONNECT_TIMEOUT, " seconds"); + conn->Terminate (); + } + }); conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), - std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn)); + std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn, timer)); + } }); } - void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn) + void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer) { + timer->cancel (); if (ecode) { LogPrint (eLogError, "NTCP: Connect error ", ecode.message ()); diff --git a/NTCPSession.h b/NTCPSession.h index a3d95c62..639942f2 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -36,6 +36,7 @@ namespace transport const size_t NTCP_MAX_MESSAGE_SIZE = 16384; const size_t NTCP_BUFFER_SIZE = 4160; // fits 4 tunnel messages (4*1028) + const int NTCP_CONNECT_TIMEOUT = 5; // 5 seconds const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes const int NTCP_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448 @@ -153,7 +154,7 @@ namespace transport void HandleAccept (std::shared_ptr conn, const boost::system::error_code& error); void HandleAcceptV6 (std::shared_ptr conn, const boost::system::error_code& error); - void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn); + void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer); // timer void ScheduleTermination ();