diff --git a/LeaseSet.cpp b/LeaseSet.cpp index 8928e761..0b79a0ab 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -12,14 +12,16 @@ namespace i2p namespace data { - LeaseSet::LeaseSet (const uint8_t * buf, int len) + LeaseSet::LeaseSet (const uint8_t * buf, int len, bool unsolicited): + m_IsUnsolicited (unsolicited) { memcpy (m_Buffer, buf, len); m_BufferLen = len; ReadFromBuffer (); } - LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool) + LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool): + m_IsUnsolicited (false) { m_BufferLen = 0; // header diff --git a/LeaseSet.h b/LeaseSet.h index fb1a9f17..a8e70d43 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -49,7 +49,7 @@ namespace data { public: - LeaseSet (const uint8_t * buf, int len); + LeaseSet (const uint8_t * buf, int len, bool unsolicited = false); LeaseSet (const LeaseSet& ) = default; LeaseSet (const i2p::tunnel::TunnelPool& pool); LeaseSet& operator=(const LeaseSet& ) = default; @@ -58,6 +58,9 @@ namespace data const uint8_t * GetBuffer () const { return m_Buffer; }; size_t GetBufferLen () const { return m_BufferLen; }; + bool IsUnsolicited () const { return m_IsUnsolicited; }; + void SetUnsolicited (bool unsolicited) { m_IsUnsolicited = unsolicited; }; + // implements RoutingDestination const Identity& GetIdentity () const { return m_Identity; }; const IdentHash& GetIdentHash () const { return m_IdentHash; }; @@ -80,6 +83,7 @@ namespace data uint8_t m_EncryptionKey[256]; uint8_t m_Buffer[MAX_LS_BUFFER_SIZE]; size_t m_BufferLen; + bool m_IsUnsolicited; }; } } diff --git a/NetDb.cpp b/NetDb.cpp index 5739b5d0..81e7d984 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -135,11 +135,12 @@ namespace data } uint64_t ts = i2p::util::GetSecondsSinceEpoch (); - if (ts - lastSave >= 60) // save routers and validate subscriptions every minute + if (ts - lastSave >= 60) // save routers, manage leasesets and validate subscriptions every minute { if (lastSave) { SaveUpdated (m_NetDbPath); + ManageLeaseSets (); ValidateSubscriptions (); } lastSave = ts; @@ -185,7 +186,7 @@ namespace data void NetDb::AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len) { - DeleteRequestedDestination (ident); + bool unsolicited = !DeleteRequestedDestination (ident); auto it = m_LeaseSets.find(ident); if (it != m_LeaseSets.end ()) { @@ -195,7 +196,7 @@ namespace data else { LogPrint ("New LeaseSet added"); - m_LeaseSets[ident] = new LeaseSet (buf, len); + m_LeaseSets[ident] = new LeaseSet (buf, len, unsolicited); } } @@ -586,19 +587,27 @@ namespace data } I2NPMessage * replyMsg = nullptr; - auto router = FindRouter (buf); - if (router) + { - LogPrint ("Requested ", key, " found"); - router->LoadBuffer (); - if (!router->GetBuffer ()) router = nullptr; + auto router = FindRouter (buf); + if (router) + { + LogPrint ("Requested RouterInfo ", key, " found"); + router->LoadBuffer (); + if (router->GetBuffer ()) + replyMsg = CreateDatabaseStoreMsg (router); + } } - if (router) - { - replyMsg = CreateDatabaseStoreMsg (router); - excluded += numExcluded*32; // we don't care about exluded + if (!replyMsg) + { + auto leaseSet = FindLeaseSet (buf); + if (leaseSet && leaseSet->IsUnsolicited ()) // we don't send back our LeaseSets + { + LogPrint ("Requested LeaseSet ", key, " found"); + replyMsg = CreateDatabaseStoreMsg (leaseSet); + } } - else + if (!replyMsg) { LogPrint ("Requested ", key, " not found. ", numExcluded, " excluded"); std::set excludedRouters; @@ -609,7 +618,10 @@ namespace data excluded += 32; } replyMsg = CreateDatabaseSearchReply (buf, GetClosestFloodfill (buf, excludedRouters)); - } + } + else + excluded += numExcluded*32; // we don't care about exluded + if (replyMsg) { if (replyTunnelID) @@ -721,14 +733,16 @@ namespace data return it->second; } - void NetDb::DeleteRequestedDestination (const IdentHash& dest) + bool NetDb::DeleteRequestedDestination (const IdentHash& dest) { auto it = m_RequestedDestinations.find (dest); if (it != m_RequestedDestinations.end ()) { delete it->second; m_RequestedDestinations.erase (it); + return true; } + return false; } void NetDb::DeleteRequestedDestination (RequestedDestination * dest) @@ -799,6 +813,8 @@ namespace data LogPrint ("LeaseSet requested"); RequestDestination (ident, true); } + else + leaseSet->SetUnsolicited (false); m_Subscriptions.insert (ident); } @@ -827,5 +843,20 @@ namespace data LogPrint ("Keyspace rotation complete"); Publish (); } + + void NetDb::ManageLeaseSets () + { + for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();) + { + if (it->second->IsUnsolicited () && !it->second->HasNonExpiredLeases ()) // all leases expired + { + LogPrint ("LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired"); + delete it->second; + it = m_LeaseSets.erase (it); + } + else + it++; + } + } } } diff --git a/NetDb.h b/NetDb.h index 8ab97359..95c692ca 100644 --- a/NetDb.h +++ b/NetDb.h @@ -92,10 +92,11 @@ namespace data void ValidateSubscriptions (); const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set& excluded) const; void KeyspaceRotation (); + void ManageLeaseSets (); RequestedDestination * CreateRequestedDestination (const IdentHash& dest, bool isLeaseSet, bool isExploratory = false); - void DeleteRequestedDestination (const IdentHash& dest); + bool DeleteRequestedDestination (const IdentHash& dest); // returns true if found void DeleteRequestedDestination (RequestedDestination * dest); private: