Skip to content

Commit

Permalink
improved stability
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Dec 29, 2013
1 parent ca51567 commit 808d6d1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 39 deletions.
8 changes: 3 additions & 5 deletions NTCPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
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));
Expand Down
3 changes: 0 additions & 3 deletions NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define NTCP_SESSION_H__

#include <inttypes.h>
#include <mutex>
#include <list>
#include <boost/asio.hpp>
#include <cryptopp/modes.h>
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion RouterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace data
{
m_RouterIdentity = identity;
m_IdentHash = CalculateIdentHash (m_RouterIdentity);
UpdateIdentHashBase64 ();
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}

Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions RouterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down Expand Up @@ -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:

Expand Down
56 changes: 32 additions & 24 deletions Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand All @@ -48,6 +49,7 @@ namespace i2p
m_NTCPSessions.clear ();
delete m_NTCPAcceptor;

m_IsRunning = false;
m_Service.stop ();
if (m_Thread)
{
Expand All @@ -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 ());
}
}
}

Expand Down Expand Up @@ -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");
}
}
2 changes: 2 additions & 0 deletions Transports.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 16 additions & 6 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,25 @@ 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)
{
tunnel = it;
minSent = it->GetNumSentBytes ();
}
return tunnel;
return tunnel;*/
}

void Tunnels::AddTransitTunnel (TransitTunnel * tunnel)
Expand Down Expand Up @@ -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*/);
}
}
}
Expand Down Expand Up @@ -395,7 +405,7 @@ namespace tunnel
}
else
{
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
LogPrint ("Creating two hops inbound tunnel...");
CreateTunnel<InboundTunnel> (
new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),
Expand Down

0 comments on commit 808d6d1

Please sign in to comment.