diff --git a/Garlic.cpp b/Garlic.cpp index e75552e3..e86374f6 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -56,7 +56,7 @@ namespace garlic } } - I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, const I2NPMessage * leaseSet) + I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet) { I2NPMessage * m = NewI2NPMessage (); size_t len = 0; @@ -122,6 +122,8 @@ namespace garlic FillI2NPMessageHeader (m, eI2NPGarlic); if (msg) DeleteI2NPMessage (msg); + if (leaseSet) + DeleteI2NPMessage (leaseSet); return m; } @@ -294,7 +296,7 @@ namespace garlic } I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination& destination, - I2NPMessage * msg, const I2NPMessage * leaseSet) + I2NPMessage * msg, I2NPMessage * leaseSet) { auto it = m_Sessions.find (destination.GetIdentHash ()); GarlicRoutingSession * session = nullptr; diff --git a/Garlic.h b/Garlic.h index 65b6d711..0eb58dee 100644 --- a/Garlic.h +++ b/Garlic.h @@ -45,7 +45,7 @@ namespace garlic GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags); GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption ~GarlicRoutingSession (); - I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const I2NPMessage * leaseSet); + I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet); int GetNextTag () const { return m_NextTag; }; uint32_t GetFirstMsgID () const { return m_FirstMsgID; }; @@ -106,7 +106,7 @@ namespace garlic I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg); I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination, - I2NPMessage * msg, const I2NPMessage * leaseSet = nullptr); + I2NPMessage * msg, I2NPMessage * leaseSet = nullptr); private: diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 78817aed..8909e556 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -209,15 +209,16 @@ namespace i2p return m; } - I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet& leaseSet) + I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet * leaseSet) { + if (!leaseSet) return nullptr; I2NPMessage * m = NewI2NPMessage (); I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload (); - memcpy (msg->key, leaseSet.GetIdentHash (), 32); + memcpy (msg->key, leaseSet->GetIdentHash (), 32); msg->type = 1; // LeaseSet msg->replyToken = 0; - memcpy (m->GetPayload () + sizeof (I2NPDatabaseStoreMsg), leaseSet.GetBuffer (), leaseSet.GetBufferLen ()); - m->len += leaseSet.GetBufferLen () + sizeof (I2NPDatabaseStoreMsg); + memcpy (m->GetPayload () + sizeof (I2NPDatabaseStoreMsg), leaseSet->GetBuffer (), leaseSet->GetBufferLen ()); + m->len += leaseSet->GetBufferLen () + sizeof (I2NPDatabaseStoreMsg); FillI2NPMessageHeader (m, eI2NPDatabaseStore); return m; } diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 4d4c5a9e..07bebc94 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -158,7 +158,7 @@ namespace tunnel I2NPMessage * CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, const i2p::data::RouterInfo * floodfill); I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router = nullptr); - I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet& leaseSet); + I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet * leaseSet); I2NPBuildRequestRecordClearText CreateBuildRequestRecord ( const uint8_t * ourIdent, uint32_t receiveTunnelID, diff --git a/Identity.h b/Identity.h index 62995a27..a5de6173 100644 --- a/Identity.h +++ b/Identity.h @@ -156,7 +156,6 @@ namespace data virtual const Identity& GetIdentity () const = 0; virtual const uint8_t * GetEncryptionPrivateKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0; - virtual void UpdateLeaseSet () = 0; // LeaseSet must be updated virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0; }; } diff --git a/RouterContext.h b/RouterContext.h index e1838e86..5e7a8858 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -28,7 +28,6 @@ namespace i2p void UpdateAddress (const char * host); // called from SSU // implements LocalDestination - void UpdateLeaseSet () {}; const i2p::data::IdentHash& GetIdentHash () const { return m_RouterInfo.GetIdentHash (); }; const i2p::data::Identity& GetIdentity () const { return GetRouterIdentity (); }; const uint8_t * GetEncryptionPrivateKey () const { return GetPrivateKey (); }; diff --git a/Streaming.cpp b/Streaming.cpp index fc16c5b7..e00ec13e 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -281,11 +281,11 @@ namespace stream bool Stream::SendPacket (const uint8_t * buf, size_t len) { - const I2NPMessage * leaseSet = nullptr; + I2NPMessage * leaseSet = nullptr; if (m_LeaseSetUpdated) { - leaseSet = m_LocalDestination->GetLeaseSet (); + leaseSet = m_LocalDestination->GetLeaseSetMsg (); m_LeaseSetUpdated = false; } @@ -359,10 +359,9 @@ namespace stream StreamingDestination::~StreamingDestination () { - if (m_LeaseSet) - DeleteI2NPMessage (m_LeaseSet); if (m_Pool) i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); + delete m_LeaseSet; } void StreamingDestination::HandleNextPacket (Packet * packet) @@ -402,34 +401,21 @@ namespace stream delete stream; } } - - void StreamingDestination::UpdateLeaseSet () - { - auto newLeaseSet = CreateLeaseSet (); - // TODO: make it atomic - auto oldLeaseSet = m_LeaseSet; - m_LeaseSet = newLeaseSet; - if (oldLeaseSet) - DeleteI2NPMessage (oldLeaseSet); - for (auto it: m_Streams) - it.second->SetLeaseSetUpdated (); - } - - const I2NPMessage * StreamingDestination::GetLeaseSet () - { - if (!m_LeaseSet) - m_LeaseSet = CreateLeaseSet (); - else - RenewI2NPMessageHeader (m_LeaseSet); - return m_LeaseSet; - } - I2NPMessage * StreamingDestination::CreateLeaseSet () const + I2NPMessage * StreamingDestination::GetLeaseSetMsg () { - // TODO: should store actual LeaseSet rather than msg if (!m_Pool) return nullptr; - i2p::data::LeaseSet leaseSet(*m_Pool); - return CreateDatabaseStoreMsg (leaseSet); + if (!m_LeaseSet || m_LeaseSet->HasExpiredLeases ()) + { + auto newLeaseSet = new i2p::data::LeaseSet (*m_Pool); + // TODO: make it atomic + auto oldLeaseSet = m_LeaseSet; + m_LeaseSet = newLeaseSet; + delete oldLeaseSet; + for (auto it: m_Streams) + it.second->SetLeaseSetUpdated (); + } + return CreateDatabaseStoreMsg (m_LeaseSet); } void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const diff --git a/Streaming.h b/Streaming.h index 4542155d..c138ae03 100644 --- a/Streaming.h +++ b/Streaming.h @@ -124,7 +124,7 @@ namespace stream ~StreamingDestination (); const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; }; - const I2NPMessage * GetLeaseSet (); + I2NPMessage * GetLeaseSetMsg (); i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; }; Stream * CreateNewStream (boost::asio::io_service& service, const i2p::data::LeaseSet& remote); @@ -132,17 +132,12 @@ namespace stream void HandleNextPacket (Packet * packet); // implements LocalDestination - void UpdateLeaseSet (); const i2p::data::IdentHash& GetIdentHash () const { return m_IdentHash; }; const i2p::data::Identity& GetIdentity () const { return m_Keys.pub; }; const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; void Sign (const uint8_t * buf, int len, uint8_t * signature) const; - private: - - I2NPMessage * CreateLeaseSet () const; - private: std::map m_Streams; @@ -151,7 +146,7 @@ namespace stream uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; i2p::tunnel::TunnelPool * m_Pool; - I2NPMessage * m_LeaseSet; + i2p::data::LeaseSet * m_LeaseSet; CryptoPP::DSA::PrivateKey m_SigningPrivateKey; }; diff --git a/TunnelPool.cpp b/TunnelPool.cpp index a4aec192..e370cfe7 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -38,7 +38,6 @@ namespace tunnel if (it.second.second == expiredTunnel) it.second.second = nullptr; } - m_LocalDestination.UpdateLeaseSet (); } void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel)