Browse Source

improved stability

pull/8/head
orignal 11 years ago
parent
commit
808d6d1fbf
  1. 8
      NTCPSession.cpp
  2. 3
      NTCPSession.h
  3. 6
      RouterInfo.cpp
  4. 2
      RouterInfo.h
  5. 48
      Transports.cpp
  6. 2
      Transports.h
  7. 22
      Tunnel.cpp

8
NTCPSession.cpp

@ -184,7 +184,8 @@ namespace ntcp
{ {
if (ecode) 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 (); Terminate ();
} }
else else
@ -459,10 +460,7 @@ namespace ntcp
m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding); m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding);
int l = len + padding + 6; int l = len + padding + 6;
{ m_Encryption.ProcessData(sendBuffer, sendBuffer, l);
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::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)); boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));

3
NTCPSession.h

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

6
RouterInfo.cpp

@ -34,6 +34,7 @@ namespace data
{ {
m_RouterIdentity = identity; m_RouterIdentity = identity;
m_IdentHash = CalculateIdentHash (m_RouterIdentity); m_IdentHash = CalculateIdentHash (m_RouterIdentity);
UpdateIdentHashBase64 ();
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
} }
@ -124,6 +125,11 @@ namespace data
} }
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity)); 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); size_t l = i2p::data::ByteStreamToBase64 (m_IdentHash, 32, m_IdentHashBase64, 48);
m_IdentHashBase64[l] = 0; m_IdentHashBase64[l] = 0;
memcpy (m_IdentHashAbbreviation, m_IdentHashBase64, 4); memcpy (m_IdentHashAbbreviation, m_IdentHashBase64, 4);

2
RouterInfo.h

@ -36,6 +36,7 @@ namespace data
RouterInfo (const char * filename); RouterInfo (const char * filename);
RouterInfo () = default; RouterInfo () = default;
RouterInfo (const RouterInfo& ) = default; RouterInfo (const RouterInfo& ) = default;
RouterInfo& operator=(const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len); RouterInfo (const uint8_t * buf, int len);
const Identity& GetRouterIdentity () const { return m_RouterIdentity; }; const Identity& GetRouterIdentity () const { return m_RouterIdentity; };
@ -74,6 +75,7 @@ namespace data
void WriteToStream (std::ostream& s); void WriteToStream (std::ostream& s);
size_t ReadString (char * str, std::istream& s); size_t ReadString (char * str, std::istream& s);
void WriteString (const std::string& str, std::ostream& s); void WriteString (const std::string& str, std::ostream& s);
void UpdateIdentHashBase64 ();
private: private:

48
Transports.cpp

@ -23,6 +23,7 @@ namespace i2p
void Transports::Start () void Transports::Start ()
{ {
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Transports::Run, this)); m_Thread = new std::thread (std::bind (&Transports::Run, this));
// create acceptors // create acceptors
auto addresses = context.GetRouterInfo ().GetAddresses (); auto addresses = context.GetRouterInfo ().GetAddresses ();
@ -48,6 +49,7 @@ namespace i2p
m_NTCPSessions.clear (); m_NTCPSessions.clear ();
delete m_NTCPAcceptor; delete m_NTCPAcceptor;
m_IsRunning = false;
m_Service.stop (); m_Service.stop ();
if (m_Thread) if (m_Thread)
{ {
@ -59,13 +61,16 @@ namespace i2p
void Transports::Run () void Transports::Run ()
{ {
try while (m_IsRunning)
{ {
m_Service.run (); try
} {
catch (std::exception& ex) m_Service.run ();
{ }
LogPrint ("Transports: ", ex.what ()); catch (std::exception& ex)
{
LogPrint ("Transports: ", ex.what ());
}
} }
} }
@ -120,25 +125,28 @@ namespace i2p
// we send it to ourself // we send it to ourself
i2p::HandleI2NPMessage (msg); i2p::HandleI2NPMessage (msg);
else 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)
{ {
auto session = FindNTCPSession (ident); RouterInfo * r = netdb.FindRouter (ident);
if (!session) if (r)
{ {
RouterInfo * r = netdb.FindRouter (ident); auto address = r->GetNTCPAddress ();
if (r) if (address)
{ {
auto address = r->GetNTCPAddress (); session = new i2p::ntcp::NTCPClient (m_Service, address->host.c_str (), address->port, *r);
if (address) AddNTCPSession (session);
{
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");
} }
if (session)
session->SendI2NPMessage (msg);
else
LogPrint ("Session not found");
} }
} }

2
Transports.h

@ -36,9 +36,11 @@ namespace i2p
void Run (); void Run ();
void HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error); void HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error);
void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg);
private: private:
bool m_IsRunning;
std::thread * m_Thread; std::thread * m_Thread;
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work; boost::asio::io_service::work m_Work;

22
Tunnel.cpp

@ -219,7 +219,17 @@ namespace tunnel
OutboundTunnel * Tunnels::GetNextOutboundTunnel () 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; size_t minSent = 0;
for (auto it : m_OutboundTunnels) for (auto it : m_OutboundTunnels)
if (!tunnel || it->GetNumSentBytes () < minSent) if (!tunnel || it->GetNumSentBytes () < minSent)
@ -227,7 +237,7 @@ namespace tunnel
tunnel = it; tunnel = it;
minSent = it->GetNumSentBytes (); minSent = it->GetNumSentBytes ();
} }
return tunnel; return tunnel;*/
} }
void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) void Tunnels::AddTransitTunnel (TransitTunnel * tunnel)
@ -353,13 +363,13 @@ namespace tunnel
} }
else else
{ {
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); //OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
LogPrint ("Creating two hops outbound tunnel..."); LogPrint ("Creating two hops outbound tunnel...");
CreateTunnel<OutboundTunnel> ( CreateTunnel<OutboundTunnel> (
new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (), new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),
i2p::data::netdb.GetRandomNTCPRouter (), i2p::data::netdb.GetRandomNTCPRouter (),
inboundTunnel->GetTunnelConfig ()), inboundTunnel->GetTunnelConfig ())/*,
outboundTunnel); outboundTunnel*/);
} }
} }
} }
@ -395,7 +405,7 @@ namespace tunnel
} }
else else
{ {
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
LogPrint ("Creating two hops inbound tunnel..."); LogPrint ("Creating two hops inbound tunnel...");
CreateTunnel<InboundTunnel> ( CreateTunnel<InboundTunnel> (
new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (), new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),

Loading…
Cancel
Save