Browse Source

delete expired LeaseSets

pull/93/head
orignal 10 years ago
parent
commit
acd21a04ae
  1. 6
      LeaseSet.cpp
  2. 6
      LeaseSet.h
  3. 51
      NetDb.cpp
  4. 3
      NetDb.h

6
LeaseSet.cpp

@ -12,14 +12,16 @@ namespace i2p
namespace data 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); memcpy (m_Buffer, buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (); ReadFromBuffer ();
} }
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool) LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool):
m_IsUnsolicited (false)
{ {
m_BufferLen = 0; m_BufferLen = 0;
// header // header

6
LeaseSet.h

@ -49,7 +49,7 @@ namespace data
{ {
public: public:
LeaseSet (const uint8_t * buf, int len); LeaseSet (const uint8_t * buf, int len, bool unsolicited = false);
LeaseSet (const LeaseSet& ) = default; LeaseSet (const LeaseSet& ) = default;
LeaseSet (const i2p::tunnel::TunnelPool& pool); LeaseSet (const i2p::tunnel::TunnelPool& pool);
LeaseSet& operator=(const LeaseSet& ) = default; LeaseSet& operator=(const LeaseSet& ) = default;
@ -58,6 +58,9 @@ namespace data
const uint8_t * GetBuffer () const { return m_Buffer; }; const uint8_t * GetBuffer () const { return m_Buffer; };
size_t GetBufferLen () const { return m_BufferLen; }; size_t GetBufferLen () const { return m_BufferLen; };
bool IsUnsolicited () const { return m_IsUnsolicited; };
void SetUnsolicited (bool unsolicited) { m_IsUnsolicited = unsolicited; };
// implements RoutingDestination // implements RoutingDestination
const Identity& GetIdentity () const { return m_Identity; }; const Identity& GetIdentity () const { return m_Identity; };
const IdentHash& GetIdentHash () const { return m_IdentHash; }; const IdentHash& GetIdentHash () const { return m_IdentHash; };
@ -80,6 +83,7 @@ namespace data
uint8_t m_EncryptionKey[256]; uint8_t m_EncryptionKey[256];
uint8_t m_Buffer[MAX_LS_BUFFER_SIZE]; uint8_t m_Buffer[MAX_LS_BUFFER_SIZE];
size_t m_BufferLen; size_t m_BufferLen;
bool m_IsUnsolicited;
}; };
} }
} }

51
NetDb.cpp

@ -135,11 +135,12 @@ namespace data
} }
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); 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) if (lastSave)
{ {
SaveUpdated (m_NetDbPath); SaveUpdated (m_NetDbPath);
ManageLeaseSets ();
ValidateSubscriptions (); ValidateSubscriptions ();
} }
lastSave = ts; lastSave = ts;
@ -185,7 +186,7 @@ namespace data
void NetDb::AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len) void NetDb::AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len)
{ {
DeleteRequestedDestination (ident); bool unsolicited = !DeleteRequestedDestination (ident);
auto it = m_LeaseSets.find(ident); auto it = m_LeaseSets.find(ident);
if (it != m_LeaseSets.end ()) if (it != m_LeaseSets.end ())
{ {
@ -195,7 +196,7 @@ namespace data
else else
{ {
LogPrint ("New LeaseSet added"); 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; I2NPMessage * replyMsg = nullptr;
{
auto router = FindRouter (buf); auto router = FindRouter (buf);
if (router) if (router)
{ {
LogPrint ("Requested ", key, " found"); LogPrint ("Requested RouterInfo ", key, " found");
router->LoadBuffer (); router->LoadBuffer ();
if (!router->GetBuffer ()) router = nullptr; if (router->GetBuffer ())
replyMsg = CreateDatabaseStoreMsg (router);
} }
if (router) }
if (!replyMsg)
{ {
replyMsg = CreateDatabaseStoreMsg (router); auto leaseSet = FindLeaseSet (buf);
excluded += numExcluded*32; // we don't care about exluded 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"); LogPrint ("Requested ", key, " not found. ", numExcluded, " excluded");
std::set<IdentHash> excludedRouters; std::set<IdentHash> excludedRouters;
@ -610,6 +619,9 @@ namespace data
} }
replyMsg = CreateDatabaseSearchReply (buf, GetClosestFloodfill (buf, excludedRouters)); replyMsg = CreateDatabaseSearchReply (buf, GetClosestFloodfill (buf, excludedRouters));
} }
else
excluded += numExcluded*32; // we don't care about exluded
if (replyMsg) if (replyMsg)
{ {
if (replyTunnelID) if (replyTunnelID)
@ -721,14 +733,16 @@ namespace data
return it->second; return it->second;
} }
void NetDb::DeleteRequestedDestination (const IdentHash& dest) bool NetDb::DeleteRequestedDestination (const IdentHash& dest)
{ {
auto it = m_RequestedDestinations.find (dest); auto it = m_RequestedDestinations.find (dest);
if (it != m_RequestedDestinations.end ()) if (it != m_RequestedDestinations.end ())
{ {
delete it->second; delete it->second;
m_RequestedDestinations.erase (it); m_RequestedDestinations.erase (it);
return true;
} }
return false;
} }
void NetDb::DeleteRequestedDestination (RequestedDestination * dest) void NetDb::DeleteRequestedDestination (RequestedDestination * dest)
@ -799,6 +813,8 @@ namespace data
LogPrint ("LeaseSet requested"); LogPrint ("LeaseSet requested");
RequestDestination (ident, true); RequestDestination (ident, true);
} }
else
leaseSet->SetUnsolicited (false);
m_Subscriptions.insert (ident); m_Subscriptions.insert (ident);
} }
@ -827,5 +843,20 @@ namespace data
LogPrint ("Keyspace rotation complete"); LogPrint ("Keyspace rotation complete");
Publish (); 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++;
}
}
} }
} }

3
NetDb.h

@ -92,10 +92,11 @@ namespace data
void ValidateSubscriptions (); void ValidateSubscriptions ();
const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const; const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
void KeyspaceRotation (); void KeyspaceRotation ();
void ManageLeaseSets ();
RequestedDestination * CreateRequestedDestination (const IdentHash& dest, RequestedDestination * CreateRequestedDestination (const IdentHash& dest,
bool isLeaseSet, bool isExploratory = false); bool isLeaseSet, bool isExploratory = false);
void DeleteRequestedDestination (const IdentHash& dest); bool DeleteRequestedDestination (const IdentHash& dest); // returns true if found
void DeleteRequestedDestination (RequestedDestination * dest); void DeleteRequestedDestination (RequestedDestination * dest);
private: private:

Loading…
Cancel
Save