improved stability

This commit is contained in:
orignal 2013-12-29 10:48:57 -05:00
parent ca51567eba
commit 808d6d1fbf
7 changed files with 62 additions and 39 deletions

View File

@ -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<std::mutex> lock (m_EncryptionMutex);
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));

View File

@ -2,7 +2,6 @@
#define NTCP_SESSION_H__
#include <inttypes.h>
#include <mutex>
#include <list>
#include <boost/asio.hpp>
#include <cryptopp/modes.h>
@ -139,8 +138,6 @@ namespace ntcp
i2p::I2NPMessage * m_NextMessage;
std::list<i2p::I2NPMessage *> m_DelayedMessages;
size_t m_NextMessageOffset;
std::mutex m_EncryptionMutex;
};
class NTCPClient: public NTCPSession

View File

@ -34,6 +34,7 @@ namespace data
{
m_RouterIdentity = identity;
m_IdentHash = CalculateIdentHash (m_RouterIdentity);
UpdateIdentHashBase64 ();
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}
@ -124,6 +125,11 @@ 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);

View File

@ -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:

View File

@ -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)
{
@ -58,6 +60,8 @@ namespace i2p
}
void Transports::Run ()
{
while (m_IsRunning)
{
try
{
@ -68,6 +72,7 @@ namespace i2p
LogPrint ("Transports: ", ex.what ());
}
}
}
void Transports::AddNTCPSession (i2p::ntcp::NTCPSession * session)
{
@ -120,6 +125,10 @@ namespace i2p
// we send it to ourself
i2p::HandleI2NPMessage (msg);
else
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)
@ -140,5 +149,4 @@ namespace i2p
else
LogPrint ("Session not found");
}
}
}

View File

@ -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;

View File

@ -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<OutboundTunnel> (
new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),
i2p::data::netdb.GetRandomNTCPRouter (),
inboundTunnel->GetTunnelConfig ()),
outboundTunnel);
inboundTunnel->GetTunnelConfig ())/*,
outboundTunnel*/);
}
}
}