diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 9689b292..2e118497 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -1,7 +1,6 @@ #include #include #include "I2PEndian.h" -#include #include #include "base64.h" #include "Log.h" @@ -78,7 +77,6 @@ namespace transport { m_IsEstablished = false; m_Socket.close (); - transports.RemoveNTCPSession (this); int numDelayed = 0; for (auto it :m_DelayedMessages) { @@ -91,8 +89,7 @@ namespace transport if (numDelayed > 0) LogPrint (eLogWarning, "NTCP session ", numDelayed, " not sent"); // TODO: notify tunnels - - delete this; + transports.RemoveNTCPSession (shared_from_this ()); LogPrint ("NTCP session terminated"); } @@ -130,15 +127,15 @@ namespace transport m_Establisher->phase1.HXxorHI[i] ^= ident[i]; boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase1Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&NTCPSession::HandlePhase1Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2)); } void NTCPSession::ServerLogin () { // receive Phase1 boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase1Received, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (), + std::placeholders::_1, std::placeholders::_2)); } void NTCPSession::HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred) @@ -153,8 +150,8 @@ namespace transport { LogPrint (eLogDebug, "Phase 1 sent: ", bytes_transferred); boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase2Received, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&NTCPSession::HandlePhase2Received, shared_from_this (), + std::placeholders::_1, std::placeholders::_2)); } } @@ -210,7 +207,7 @@ namespace transport m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted); boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase2Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsB)); + std::bind(&NTCPSession::HandlePhase2Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsB)); } @@ -226,8 +223,8 @@ namespace transport { LogPrint (eLogDebug, "Phase 2 sent: ", bytes_transferred); boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, NTCP_DEFAULT_PHASE3_SIZE), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase3Received, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsB)); + std::bind(&NTCPSession::HandlePhase3Received, shared_from_this (), + std::placeholders::_1, std::placeholders::_2, tsB)); } } @@ -305,7 +302,7 @@ namespace transport m_Encryption.Encrypt(m_ReceiveBuffer, len, m_ReceiveBuffer); boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, len), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase3Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsA)); + std::bind(&NTCPSession::HandlePhase3Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsA)); } void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA) @@ -324,8 +321,8 @@ namespace transport size_t paddingSize = signatureLen & 0x0F; // %16 if (paddingSize > 0) signatureLen += (16 - paddingSize); boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase4Received, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsA)); + std::bind(&NTCPSession::HandlePhase4Received, shared_from_this (), + std::placeholders::_1, std::placeholders::_2, tsA)); } } @@ -353,8 +350,8 @@ namespace transport expectedSize += paddingLen; LogPrint (eLogDebug, "Wait for ", expectedSize, " more bytes for Phase3"); boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer + NTCP_DEFAULT_PHASE3_SIZE, expectedSize), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase3ExtraReceived, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsB, paddingLen)); + std::bind(&NTCPSession::HandlePhase3ExtraReceived, shared_from_this (), + std::placeholders::_1, std::placeholders::_2, tsB, paddingLen)); } else HandlePhase3 (tsB, paddingLen); @@ -416,7 +413,7 @@ namespace transport m_Encryption.Encrypt (m_ReceiveBuffer, signatureLen, m_ReceiveBuffer); boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandlePhase4Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&NTCPSession::HandlePhase4Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2)); } void NTCPSession::HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred) @@ -431,7 +428,7 @@ namespace transport { LogPrint (eLogDebug, "Phase 4 sent: ", bytes_transferred); LogPrint ("NTCP server session connected"); - transports.AddNTCPSession (this); + transports.AddNTCPSession (shared_from_this ()); Connected (); m_ReceiveBufferOffset = 0; @@ -483,8 +480,8 @@ namespace transport void NTCPSession::Receive () { m_Socket.async_read_some (boost::asio::buffer(m_ReceiveBuffer + m_ReceiveBufferOffset, NTCP_BUFFER_SIZE - m_ReceiveBufferOffset), - boost::bind(&NTCPSession::HandleReceived, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&NTCPSession::HandleReceived, shared_from_this (), + std::placeholders::_1, std::placeholders::_2)); } void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred) @@ -604,7 +601,7 @@ namespace transport m_Encryption.Encrypt(sendBuffer, l, sendBuffer); boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (), - boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg)); + std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msg)); } void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg) @@ -645,8 +642,8 @@ namespace transport { m_TerminationTimer.cancel (); m_TerminationTimer.expires_from_now (boost::posix_time::seconds(NTCP_TERMINATION_TIMEOUT)); - m_TerminationTimer.async_wait (boost::bind (&NTCPSession::HandleTerminationTimer, - this, boost::asio::placeholders::error)); + m_TerminationTimer.async_wait (std::bind (&NTCPSession::HandleTerminationTimer, + shared_from_this (), std::placeholders::_1)); } void NTCPSession::HandleTerminationTimer (const boost::system::error_code& ecode) diff --git a/NTCPSession.h b/NTCPSession.h index b384cfb9..1d94475d 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -43,7 +43,7 @@ namespace transport const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448 - class NTCPSession: public TransportSession + class NTCPSession: public TransportSession, public std::enable_shared_from_this { public: diff --git a/Transports.cpp b/Transports.cpp index 907ed415..3992290a 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -121,7 +121,7 @@ namespace transport boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); LogPrint ("Start listening TCP port ", address.port); - auto conn = new NTCPSession (m_Service); + auto conn = std::make_shared(m_Service); m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, conn, boost::asio::placeholders::error)); @@ -134,7 +134,7 @@ namespace transport m_NTCPV6Acceptor->listen (); LogPrint ("Start listening V6 TCP port ", address.port); - auto conn = new NTCPSession (m_Service); + auto conn = std::make_shared (m_Service); m_NTCPV6Acceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAcceptV6, this, conn, boost::asio::placeholders::error)); } @@ -162,10 +162,8 @@ namespace transport delete m_SSUServer; m_SSUServer = nullptr; } - - for (auto session: m_NTCPSessions) - delete session.second; m_NTCPSessions.clear (); + delete m_NTCPAcceptor; m_NTCPAcceptor = nullptr; delete m_NTCPV6Acceptor; @@ -197,62 +195,59 @@ namespace transport } } - void Transports::AddNTCPSession (NTCPSession * session) + void Transports::AddNTCPSession (std::shared_ptr session) { if (session) m_NTCPSessions[session->GetRemoteIdentity ().GetIdentHash ()] = session; } - void Transports::RemoveNTCPSession (NTCPSession * session) + void Transports::RemoveNTCPSession (std::shared_ptr session) { if (session) m_NTCPSessions.erase (session->GetRemoteIdentity ().GetIdentHash ()); } - void Transports::HandleAccept (NTCPSession * conn, const boost::system::error_code& error) + void Transports::HandleAccept (std::shared_ptr conn, const boost::system::error_code& error) { if (!error) { LogPrint ("Connected from ", conn->GetSocket ().remote_endpoint().address ().to_string ()); conn->ServerLogin (); } - else - delete conn; + if (error != boost::asio::error::operation_aborted) { - conn = new NTCPSession (m_Service); + conn = std::make_shared (m_Service); m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, conn, boost::asio::placeholders::error)); } } - void Transports::HandleAcceptV6 (NTCPSession * conn, const boost::system::error_code& error) + void Transports::HandleAcceptV6 (std::shared_ptr conn, const boost::system::error_code& error) { if (!error) { LogPrint ("Connected from ", conn->GetSocket ().remote_endpoint().address ().to_string ()); conn->ServerLogin (); } - else - delete conn; if (error != boost::asio::error::operation_aborted) { - conn = new NTCPSession (m_Service); + conn = std::make_shared (m_Service); m_NTCPV6Acceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAcceptV6, this, conn, boost::asio::placeholders::error)); } } - void Transports::Connect (const boost::asio::ip::address& address, int port, NTCPSession * conn) + void Transports::Connect (const boost::asio::ip::address& address, int port, std::shared_ptr conn) { LogPrint ("Connecting to ", address ,":", port); conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), boost::bind (&Transports::HandleConnect, this, boost::asio::placeholders::error, conn)); } - void Transports::HandleConnect (const boost::system::error_code& ecode, NTCPSession * conn) + void Transports::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn) { if (ecode) { @@ -272,7 +267,7 @@ namespace transport } } - NTCPSession * Transports::GetNextNTCPSession () + std::shared_ptr Transports::GetNextNTCPSession () { for (auto session: m_NTCPSessions) if (session.second->IsEstablished ()) @@ -280,7 +275,7 @@ namespace transport return 0; } - NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident) + std::shared_ptr Transports::FindNTCPSession (const i2p::data::IdentHash& ident) { auto it = m_NTCPSessions.find (ident); if (it != m_NTCPSessions.end ()) @@ -317,7 +312,7 @@ namespace transport auto address = r->GetNTCPAddress (!context.SupportsV6 ()); if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < NTCP_MAX_MESSAGE_SIZE) { - auto s = new NTCPSession (m_Service, r); + auto s = std::make_shared (m_Service, r); AddNTCPSession (s); s->SendI2NPMessage (msg); Connect (address->host, address->port, s); diff --git a/Transports.h b/Transports.h index 8133e39f..3b5b2d4e 100644 --- a/Transports.h +++ b/Transports.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "TransportSession.h" @@ -63,11 +64,11 @@ namespace transport i2p::transport::DHKeysPair * GetNextDHKeysPair (); void ReuseDHKeysPair (DHKeysPair * pair); - void AddNTCPSession (NTCPSession * session); - void RemoveNTCPSession (NTCPSession * session); + void AddNTCPSession (std::shared_ptr session); + void RemoveNTCPSession (std::shared_ptr session); - NTCPSession * GetNextNTCPSession (); - NTCPSession * FindNTCPSession (const i2p::data::IdentHash& ident); + std::shared_ptr GetNextNTCPSession (); + std::shared_ptr FindNTCPSession (const i2p::data::IdentHash& ident); void SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void CloseSession (std::shared_ptr router); @@ -75,15 +76,15 @@ namespace transport private: void Run (); - void HandleAccept (NTCPSession * conn, const boost::system::error_code& error); - void HandleAcceptV6 (NTCPSession * conn, const boost::system::error_code& error); + 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 HandleResendTimer (const boost::system::error_code& ecode, boost::asio::deadline_timer * timer, const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void PostCloseSession (std::shared_ptr router); - void Connect (const boost::asio::ip::address& address, int port, NTCPSession * conn); - void HandleConnect (const boost::system::error_code& ecode, NTCPSession * conn); + void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr conn); + void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn); void DetectExternalIP (); @@ -95,7 +96,7 @@ namespace transport boost::asio::io_service::work m_Work; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor; - std::map m_NTCPSessions; + std::map > m_NTCPSessions; SSUServer * m_SSUServer; DHKeysPairSupplier m_DHKeysPairSupplier;