mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 02:54:01 +01:00
improved stability
This commit is contained in:
parent
ca51567eba
commit
808d6d1fbf
|
@ -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);
|
||||
}
|
||||
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));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
22
Tunnel.cpp
22
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<OutboundTunnel> (
|
||||
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<InboundTunnel> (
|
||||
new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (),
|
||||
|
|
Loading…
Reference in a new issue