diff --git a/Identity.cpp b/Identity.cpp index bbdfdbe8..b80d1cbf 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -9,6 +9,14 @@ namespace i2p { namespace data { + Identity& Identity::operator=(const Keys& keys) + { + // copy public and signing keys together + memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey)); + memset (certificate, 0, sizeof (certificate)); + return *this; + } + IdentHash CalculateIdentHash (const Identity& identity) { IdentHash hash; diff --git a/Identity.h b/Identity.h index 09af9332..549e9cea 100644 --- a/Identity.h +++ b/Identity.h @@ -23,6 +23,8 @@ namespace data uint8_t publicKey[256]; uint8_t signingKey[128]; uint8_t certificate[3]; + + Identity& operator=(const Keys& keys); }; #pragma pack() diff --git a/NTCPSession.cpp b/NTCPSession.cpp index b52a6223..1d8a93f0 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -20,12 +20,10 @@ namespace i2p { namespace ntcp { - NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo): + NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo): m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false), - m_ReceiveBufferOffset (0), m_NextMessage (nullptr) + m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr) { - if (in_RemoteRouterInfo) - m_RemoteRouterInfo = *in_RemoteRouterInfo; } void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey) @@ -521,7 +519,8 @@ namespace ntcp NTCPClient::NTCPClient (boost::asio::io_service& service, const char * address, - int port, i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, &in_RouterInfo), + int port, i2p::data::RouterInfo& in_RouterInfo): + NTCPSession (service, in_RouterInfo), m_Endpoint (boost::asio::ip::address::from_string (address), port) { Connect (); diff --git a/NTCPSession.h b/NTCPSession.h index 4ef0e3dd..925643b4 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -66,7 +66,7 @@ namespace ntcp { public: - NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo = 0); + NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo); virtual ~NTCPSession () {}; boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; @@ -126,7 +126,7 @@ namespace ntcp CryptoPP::CBC_Mode::Encryption m_Encryption; CryptoPP::Adler32 m_Adler; - i2p::data::RouterInfo m_RemoteRouterInfo; + i2p::data::RouterInfo& m_RemoteRouterInfo; NTCPPhase1 m_Phase1; NTCPPhase2 m_Phase2; @@ -163,11 +163,16 @@ namespace ntcp { public: - NTCPServerConnection (boost::asio::io_service& service): NTCPSession (service) {}; + NTCPServerConnection (boost::asio::io_service& service): + NTCPSession (service, m_DummyRemoteRouterInfo) {}; protected: virtual void Connected (); + + private: + + i2p::data::RouterInfo m_DummyRemoteRouterInfo; }; } } diff --git a/NetDb.cpp b/NetDb.cpp index 41440582..79cd903a 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -128,6 +128,15 @@ namespace data else return nullptr; } + + LeaseSet * NetDb::FindLeaseSet (const IdentHash& destination) const + { + auto it = m_LeaseSets.find (destination); + if (it != m_LeaseSets.end ()) + return it->second; + else + return nullptr; + } void NetDb::Load (const char * directory) { @@ -156,19 +165,36 @@ namespace data void NetDb::SaveUpdated (const char * directory) { - int count = 0; + auto GetFilePath = [](const char * directory, const RouterInfo * routerInfo) + { + return std::string (directory) + "/r" + + routerInfo->GetIdentHashBase64 ()[0] + "/routerInfo-" + + routerInfo->GetIdentHashBase64 () + ".dat"; + }; + + int count = 0, deletedCount = 0; for (auto it: m_RouterInfos) + { if (it.second->IsUpdated ()) { - std::ofstream r (std::string (directory) + "/r" + - it.second->GetIdentHashBase64 ()[0] + "/routerInfo-" + - it.second->GetIdentHashBase64 () + ".dat"); + std::ofstream r (GetFilePath(directory, it.second)); r.write ((char *)it.second->GetBuffer (), it.second->GetBufferLen ()); it.second->SetUpdated (false); count++; } + else if (it.second->IsUnreachable ()) + { + if (boost::filesystem::exists (GetFilePath (directory, it.second))) + { + boost::filesystem::remove (GetFilePath (directory, it.second)); + deletedCount++; + } + } + } if (count > 0) LogPrint (count," new/updated routers saved"); + if (deletedCount > 0) + LogPrint (deletedCount," routers deleted"); } void NetDb::RequestDestination (const char * b32, const uint8_t * router) @@ -197,6 +223,11 @@ namespace data LogPrint ("No outbound tunnels found"); } + void NetDb::RequestDestination (const IdentHash& destination) + { + RequestDestination ((const uint8_t *)destination, GetRandomNTCPRouter (true)->GetIdentHash ()); + } + void NetDb::HandleDatabaseStoreMsg (uint8_t * buf, size_t len) { I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)buf; diff --git a/NetDb.h b/NetDb.h index c76bd9f7..4d9ebbb1 100644 --- a/NetDb.h +++ b/NetDb.h @@ -27,9 +27,12 @@ namespace data void AddRouterInfo (uint8_t * buf, int len); void AddLeaseSet (uint8_t * buf, int len); RouterInfo * FindRouter (const IdentHash& ident) const; - + LeaseSet * FindLeaseSet (const IdentHash& destination) const; + void RequestDestination (const char * b32, const uint8_t * router); // in base32 void RequestDestination (const uint8_t * destination, const uint8_t * router); + void RequestDestination (const IdentHash& destination); + void HandleDatabaseStoreMsg (uint8_t * buf, size_t len); void HandleDatabaseSearchReplyMsg (I2NPMessage * msg); diff --git a/RouterContext.cpp b/RouterContext.cpp index 84fac346..718a9f36 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -26,9 +26,7 @@ namespace i2p CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); i2p::data::Identity ident; - // copy public and signing keys together - memcpy (ident.publicKey, m_Keys.publicKey, sizeof (ident.publicKey) + sizeof (ident.signingKey)); - memset (ident.certificate, 0, sizeof (ident.certificate)); + ident = m_Keys; m_RouterInfo.SetRouterIdentity (ident); m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO: