From 808d6d1fbf978286b499d9d9990c94e3dea68617 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 29 Dec 2013 10:48:57 -0500 Subject: [PATCH] improved stability --- NTCPSession.cpp | 8 +++---- NTCPSession.h | 3 --- RouterInfo.cpp | 8 ++++++- RouterInfo.h | 2 ++ Transports.cpp | 56 ++++++++++++++++++++++++++++--------------------- Transports.h | 2 ++ Tunnel.cpp | 22 +++++++++++++------ 7 files changed, 62 insertions(+), 39 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 1d8a93f0..14b74c27 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -184,7 +184,8 @@ namespace ntcp { if (ecode) { - LogPrint ("Phase 2 read error: ", ecode.message ()); + LogPrint ("Phase 2 read error: ", ecode.message (), ". Wrong ident assumed"); + GetRemoteRouterInfo ().SetUnreachable (true); // this RouterInfo is not valid Terminate (); } else @@ -459,10 +460,7 @@ namespace ntcp m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding); int l = len + padding + 6; - { - std::lock_guard lock (m_EncryptionMutex); - m_Encryption.ProcessData(sendBuffer, sendBuffer, l); - } + m_Encryption.ProcessData(sendBuffer, sendBuffer, l); 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)); diff --git a/NTCPSession.h b/NTCPSession.h index 925643b4..5d33ef10 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -2,7 +2,6 @@ #define NTCP_SESSION_H__ #include -#include #include #include #include @@ -139,8 +138,6 @@ namespace ntcp i2p::I2NPMessage * m_NextMessage; std::list m_DelayedMessages; size_t m_NextMessageOffset; - - std::mutex m_EncryptionMutex; }; class NTCPClient: public NTCPSession diff --git a/RouterInfo.cpp b/RouterInfo.cpp index c6666dfd..417466c6 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -34,6 +34,7 @@ namespace data { m_RouterIdentity = identity; m_IdentHash = CalculateIdentHash (m_RouterIdentity); + UpdateIdentHashBase64 (); m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); } @@ -124,12 +125,17 @@ namespace data } CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity)); + UpdateIdentHashBase64 (); + } + + void RouterInfo::UpdateIdentHashBase64 () + { size_t l = i2p::data::ByteStreamToBase64 (m_IdentHash, 32, m_IdentHashBase64, 48); m_IdentHashBase64[l] = 0; memcpy (m_IdentHashAbbreviation, m_IdentHashBase64, 4); m_IdentHashAbbreviation[4] = 0; } - + void RouterInfo::WriteToStream (std::ostream& s) { s.write ((char *)&m_RouterIdentity, sizeof (m_RouterIdentity)); diff --git a/RouterInfo.h b/RouterInfo.h index 780eb8c7..3111fbf3 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -36,6 +36,7 @@ namespace data RouterInfo (const char * filename); RouterInfo () = default; RouterInfo (const RouterInfo& ) = default; + RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo (const uint8_t * buf, int len); const Identity& GetRouterIdentity () const { return m_RouterIdentity; }; @@ -74,6 +75,7 @@ namespace data void WriteToStream (std::ostream& s); size_t ReadString (char * str, std::istream& s); void WriteString (const std::string& str, std::ostream& s); + void UpdateIdentHashBase64 (); private: diff --git a/Transports.cpp b/Transports.cpp index e938c85b..9204cea9 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -23,6 +23,7 @@ namespace i2p void Transports::Start () { + m_IsRunning = true; m_Thread = new std::thread (std::bind (&Transports::Run, this)); // create acceptors auto addresses = context.GetRouterInfo ().GetAddresses (); @@ -48,6 +49,7 @@ namespace i2p m_NTCPSessions.clear (); delete m_NTCPAcceptor; + m_IsRunning = false; m_Service.stop (); if (m_Thread) { @@ -59,13 +61,16 @@ namespace i2p void Transports::Run () { - try - { - m_Service.run (); - } - catch (std::exception& ex) + while (m_IsRunning) { - LogPrint ("Transports: ", ex.what ()); + try + { + m_Service.run (); + } + catch (std::exception& ex) + { + LogPrint ("Transports: ", ex.what ()); + } } } @@ -120,25 +125,28 @@ namespace i2p // we send it to ourself i2p::HandleI2NPMessage (msg); else - { - auto session = FindNTCPSession (ident); - if (!session) - { - RouterInfo * r = netdb.FindRouter (ident); - if (r) + m_Service.post (boost::bind (&Transports::PostMessage, this, ident, msg)); + } + + void Transports::PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg) + { + auto session = FindNTCPSession (ident); + if (!session) + { + RouterInfo * r = netdb.FindRouter (ident); + if (r) + { + auto address = r->GetNTCPAddress (); + if (address) { - auto address = r->GetNTCPAddress (); - if (address) - { - session = new i2p::ntcp::NTCPClient (m_Service, address->host.c_str (), address->port, *r); - AddNTCPSession (session); - } - } - } - if (session) - session->SendI2NPMessage (msg); - else - LogPrint ("Session not found"); + session = new i2p::ntcp::NTCPClient (m_Service, address->host.c_str (), address->port, *r); + AddNTCPSession (session); + } + } } + if (session) + session->SendI2NPMessage (msg); + else + LogPrint ("Session not found"); } } diff --git a/Transports.h b/Transports.h index ea5453cc..d6b08227 100644 --- a/Transports.h +++ b/Transports.h @@ -36,9 +36,11 @@ namespace i2p void Run (); void HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error); + void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); private: + bool m_IsRunning; std::thread * m_Thread; boost::asio::io_service m_Service; boost::asio::io_service::work m_Work; diff --git a/Tunnel.cpp b/Tunnel.cpp index f45ba1d4..eebb85fa 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -219,7 +219,17 @@ namespace tunnel OutboundTunnel * Tunnels::GetNextOutboundTunnel () { - OutboundTunnel * tunnel = nullptr; + CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); + uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0; + for (auto it: m_OutboundTunnels) + { + if (i >= ind) return it; + else i++; + } + return nullptr; + + // TODO: implement it base on profiling + /*OutboundTunnel * tunnel = nullptr; size_t minSent = 0; for (auto it : m_OutboundTunnels) if (!tunnel || it->GetNumSentBytes () < minSent) @@ -227,7 +237,7 @@ namespace tunnel tunnel = it; minSent = it->GetNumSentBytes (); } - return tunnel; + return tunnel;*/ } void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) @@ -353,13 +363,13 @@ namespace tunnel } else { - OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); + //OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); LogPrint ("Creating two hops outbound tunnel..."); CreateTunnel ( new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (), i2p::data::netdb.GetRandomNTCPRouter (), - inboundTunnel->GetTunnelConfig ()), - outboundTunnel); + inboundTunnel->GetTunnelConfig ())/*, + outboundTunnel*/); } } } @@ -395,7 +405,7 @@ namespace tunnel } else { - OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); + OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); LogPrint ("Creating two hops inbound tunnel..."); CreateTunnel ( new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),