diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 3f385af6..35ef512c 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -704,9 +704,9 @@ namespace util if (it.second->IsEstablished ()) { // incoming connection doesn't have remote RI - bool outgoing = it.second->GetRemoteRouterInfo (); + bool outgoing = it.second->GetRemoteRouter (); if (outgoing) s << "-->"; - s << it.second->GetRemoteRouterIdentity ().GetIdentHash ().ToBase64 ().substr (0, 4) << ": " + s << it.second->GetRemoteIdentity ().GetIdentHash ().ToBase64 ().substr (0, 4) << ": " << it.second->GetSocket ().remote_endpoint().address ().to_string (); if (!outgoing) s << "-->"; s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 46670bb7..a1950200 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -10,6 +10,7 @@ #include "I2NPProtocol.h" #include "RouterContext.h" #include "Transports.h" +#include "NetDb.h" #include "NTCPSession.h" using namespace i2p::crypto; @@ -18,13 +19,13 @@ namespace i2p { namespace transport { - NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo): + NTCPSession::NTCPSession (boost::asio::io_service& service, const i2p::data::RouterInfo * in_RemoteRouter): m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false), - m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), + m_RemoteRouter (in_RemoteRouter), m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0) { - if (m_RemoteRouterInfo) - m_RemoteRouterIdentity = m_RemoteRouterInfo->GetRouterIdentity (); + if (m_RemoteRouter) + m_RemoteIdentity = m_RemoteRouter->GetRouterIdentity (); m_DHKeysPair = transports.GetNextDHKeysPair (); m_Establisher = new Establisher; } @@ -83,8 +84,8 @@ namespace transport for (auto it :m_DelayedMessages) { // try to send them again - if (m_RemoteRouterInfo) - transports.SendMessage (m_RemoteRouterInfo->GetIdentHash (), it); + if (m_RemoteRouter) + transports.SendMessage (m_RemoteRouter->GetIdentHash (), it); numDelayed++; } m_DelayedMessages.clear (); @@ -126,7 +127,7 @@ namespace transport const uint8_t * x = m_DHKeysPair->publicKey; memcpy (m_Establisher->phase1.pubKey, x, 256); CryptoPP::SHA256().CalculateDigest(m_Establisher->phase1.HXxorHI, x, 256); - const uint8_t * ident = m_RemoteRouterIdentity.GetIdentHash (); + const uint8_t * ident = m_RemoteIdentity.GetIdentHash (); for (int i = 0; i < 32; i++) m_Establisher->phase1.HXxorHI[i] ^= ident[i]; @@ -239,7 +240,8 @@ namespace transport LogPrint ("Phase 2 read error: ", ecode.message (), ". Wrong ident assumed"); if (ecode != boost::asio::error::operation_aborted) { - m_RemoteRouterInfo->SetUnreachable (true); // this RouterInfo is not valid + // this RI is not valid + i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true); transports.ReuseDHKeysPair (m_DHKeysPair); m_DHKeysPair = nullptr; Terminate (); @@ -284,7 +286,7 @@ namespace transport SignedData s; memcpy (s.x, m_Establisher->phase1.pubKey, 256); memcpy (s.y, m_Establisher->phase2.pubKey, 256); - memcpy (s.ident, m_RemoteRouterIdentity.GetIdentHash (), 32); + memcpy (s.ident, m_RemoteIdentity.GetIdentHash (), 32); s.tsA = tsA; s.tsB = m_Establisher->phase2.encrypted.timestamp; i2p::context.Sign ((uint8_t *)&s, sizeof (s), m_Establisher->phase3.signature); @@ -324,7 +326,7 @@ namespace transport { LogPrint ("Phase 3 received: ", bytes_transferred); m_Decryption.Decrypt ((uint8_t *)&m_Establisher->phase3, sizeof(NTCPPhase3), (uint8_t *)&m_Establisher->phase3); - m_RemoteRouterIdentity = m_Establisher->phase3.ident; + m_RemoteIdentity = m_Establisher->phase3.ident; SignedData s; memcpy (s.x, m_Establisher->phase1.pubKey, 256); @@ -333,7 +335,7 @@ namespace transport s.tsA = m_Establisher->phase3.timestamp; s.tsB = tsB; - if (!m_RemoteRouterIdentity.Verify ((uint8_t *)&s, sizeof(s), m_Establisher->phase3.signature)) + if (!m_RemoteIdentity.Verify ((uint8_t *)&s, sizeof(s), m_Establisher->phase3.signature)) { LogPrint ("signature verification failed"); Terminate (); @@ -349,7 +351,7 @@ namespace transport SignedData s; memcpy (s.x, m_Establisher->phase1.pubKey, 256); memcpy (s.y, m_Establisher->phase2.pubKey, 256); - memcpy (s.ident, m_RemoteRouterIdentity.GetIdentHash (), 32); + memcpy (s.ident, m_RemoteIdentity.GetIdentHash (), 32); s.tsA = m_Establisher->phase3.timestamp; s.tsB = tsB; i2p::context.Sign ((uint8_t *)&s, sizeof (s), m_Establisher->phase4.signature); @@ -384,7 +386,8 @@ namespace transport LogPrint ("Phase 4 read error: ", ecode.message ()); if (ecode != boost::asio::error::operation_aborted) { - m_RemoteRouterInfo->SetUnreachable (true); // this router doesn't like us + // this router doesn't like us + i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true); Terminate (); } } @@ -401,7 +404,7 @@ namespace transport s.tsA = tsA; s.tsB = m_Establisher->phase2.encrypted.timestamp; - if (!m_RemoteRouterIdentity.Verify ((uint8_t *)&s, sizeof(s), m_Establisher->phase4.signature)) + if (!m_RemoteIdentity.Verify ((uint8_t *)&s, sizeof(s), m_Establisher->phase4.signature)) { LogPrint ("signature verification failed"); Terminate (); @@ -595,7 +598,7 @@ namespace transport NTCPClient::NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, - int port, i2p::data::RouterInfo& in_RouterInfo): + int port, const i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, &in_RouterInfo), m_Endpoint (address, port) { Connect (); @@ -615,8 +618,7 @@ namespace transport LogPrint ("Connect error: ", ecode.message ()); if (ecode != boost::asio::error::operation_aborted) { - if (GetRemoteRouterInfo ()) - GetRemoteRouterInfo ()->SetUnreachable (true); + i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true); Terminate (); } } diff --git a/NTCPSession.h b/NTCPSession.h index 1af00eba..8d8b1d67 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -71,13 +71,13 @@ namespace transport { public: - NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo = nullptr); + NTCPSession (boost::asio::io_service& service, const i2p::data::RouterInfo * in_RemoteRouter = nullptr); virtual ~NTCPSession (); boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; bool IsEstablished () const { return m_IsEstablished; }; - i2p::data::RouterInfo * GetRemoteRouterInfo () { return m_RemoteRouterInfo; }; - const i2p::data::IdentityEx& GetRemoteRouterIdentity () { return m_RemoteRouterIdentity; }; + const i2p::data::RouterInfo * GetRemoteRouter () { return m_RemoteRouter; }; + const i2p::data::IdentityEx& GetRemoteIdentity () { return m_RemoteIdentity; }; void ClientLogin (); void ServerLogin (); @@ -135,8 +135,8 @@ namespace transport i2p::crypto::CBCEncryption m_Encryption; CryptoPP::Adler32 m_Adler; - i2p::data::RouterInfo * m_RemoteRouterInfo; - i2p::data::IdentityEx m_RemoteRouterIdentity; + const i2p::data::RouterInfo * m_RemoteRouter; + i2p::data::IdentityEx m_RemoteIdentity; struct Establisher { @@ -160,7 +160,7 @@ namespace transport { public: - NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo); + NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, const i2p::data::RouterInfo& in_RouterInfo); private: diff --git a/NetDb.cpp b/NetDb.cpp index f1c20498..eb2800a2 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -229,6 +229,13 @@ namespace data return nullptr; } + void NetDb::SetUnreachable (const IdentHash& ident, bool unreachable) + { + auto it = m_RouterInfos.find (ident); + if (it != m_RouterInfos.end ()) + return it->second->SetUnreachable (unreachable); + } + // TODO: Move to reseed and/or scheduled tasks. (In java version, scheduler fix this as well as sort RIs.) bool NetDb::CreateNetDb(boost::filesystem::path directory) { diff --git a/NetDb.h b/NetDb.h index 384d1c80..b90bef19 100644 --- a/NetDb.h +++ b/NetDb.h @@ -78,7 +78,8 @@ namespace data const RouterInfo * GetRandomRouter () const; const RouterInfo * GetRandomRouter (const RouterInfo * compatibleWith) const; const RouterInfo * GetHighBandwidthRandomRouter (const RouterInfo * compatibleWith) const; - + void SetUnreachable (const IdentHash& ident, bool unreachable); + void PostI2NPMsg (I2NPMessage * msg); // for web interface diff --git a/Transports.cpp b/Transports.cpp index 4661d439..b3a01aee 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -184,13 +184,13 @@ namespace transport void Transports::AddNTCPSession (NTCPSession * session) { if (session) - m_NTCPSessions[session->GetRemoteRouterIdentity ().GetIdentHash ()] = session; + m_NTCPSessions[session->GetRemoteIdentity ().GetIdentHash ()] = session; } void Transports::RemoveNTCPSession (NTCPSession * session) { if (session) - m_NTCPSessions.erase (session->GetRemoteRouterIdentity ().GetIdentHash ()); + m_NTCPSessions.erase (session->GetRemoteIdentity ().GetIdentHash ()); } void Transports::HandleAccept (NTCPServerConnection * conn, const boost::system::error_code& error)