mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
invalidate excluded leases
This commit is contained in:
parent
7d927b0e28
commit
481fafc11d
33
LeaseSet.cpp
33
LeaseSet.cpp
|
@ -69,7 +69,6 @@ namespace data
|
||||||
|
|
||||||
void LeaseSet::Update (const uint8_t * buf, size_t len)
|
void LeaseSet::Update (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
m_Leases.clear ();
|
|
||||||
if (len > m_BufferLen)
|
if (len > m_BufferLen)
|
||||||
{
|
{
|
||||||
auto oldBuffer = m_Buffer;
|
auto oldBuffer = m_Buffer;
|
||||||
|
@ -84,7 +83,6 @@ namespace data
|
||||||
void LeaseSet::PopulateLeases ()
|
void LeaseSet::PopulateLeases ()
|
||||||
{
|
{
|
||||||
m_StoreLeases = true;
|
m_StoreLeases = true;
|
||||||
m_Leases.clear ();
|
|
||||||
ReadFromBuffer (false);
|
ReadFromBuffer (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,6 +110,13 @@ namespace data
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset existing leases
|
||||||
|
if (m_StoreLeases)
|
||||||
|
for (auto it: m_Leases)
|
||||||
|
it->isUpdated = false;
|
||||||
|
else
|
||||||
|
m_Leases.clear ();
|
||||||
|
|
||||||
// process leases
|
// process leases
|
||||||
m_ExpirationTime = 0;
|
m_ExpirationTime = 0;
|
||||||
const uint8_t * leases = m_Buffer + size;
|
const uint8_t * leases = m_Buffer + size;
|
||||||
|
@ -128,9 +133,9 @@ namespace data
|
||||||
m_ExpirationTime = lease.endDate;
|
m_ExpirationTime = lease.endDate;
|
||||||
if (m_StoreLeases)
|
if (m_StoreLeases)
|
||||||
{
|
{
|
||||||
auto l = std::make_shared<Lease>();
|
auto ret = m_Leases.insert (std::make_shared<Lease>(lease));
|
||||||
*l = lease;
|
if (!ret.second) *(*ret.first) = lease; // update existing
|
||||||
m_Leases.push_back (l);
|
(*ret.first)->isUpdated = true;
|
||||||
// check if lease's gateway is in our netDb
|
// check if lease's gateway is in our netDb
|
||||||
if (!netdb.FindRouter (lease.tunnelGateway))
|
if (!netdb.FindRouter (lease.tunnelGateway))
|
||||||
{
|
{
|
||||||
|
@ -140,6 +145,20 @@ namespace data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// delete old leases
|
||||||
|
if (m_StoreLeases)
|
||||||
|
{
|
||||||
|
for (auto it = m_Leases.begin (); it != m_Leases.end ();)
|
||||||
|
{
|
||||||
|
if (!(*it)->isUpdated)
|
||||||
|
{
|
||||||
|
(*it)->endDate = 0; // somebody might still hold it
|
||||||
|
m_Leases.erase (it++);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// verify
|
// verify
|
||||||
if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
|
if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
|
||||||
|
@ -153,7 +172,7 @@ namespace data
|
||||||
{
|
{
|
||||||
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
std::vector<std::shared_ptr<Lease> > leases;
|
std::vector<std::shared_ptr<Lease> > leases;
|
||||||
for (auto& it: m_Leases)
|
for (auto it: m_Leases)
|
||||||
{
|
{
|
||||||
auto endDate = it->endDate;
|
auto endDate = it->endDate;
|
||||||
if (!withThreshold)
|
if (!withThreshold)
|
||||||
|
@ -167,7 +186,7 @@ namespace data
|
||||||
bool LeaseSet::HasExpiredLeases () const
|
bool LeaseSet::HasExpiredLeases () const
|
||||||
{
|
{
|
||||||
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
for (auto& it: m_Leases)
|
for (auto it: m_Leases)
|
||||||
if (ts >= it->endDate) return true;
|
if (ts >= it->endDate) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
20
LeaseSet.h
20
LeaseSet.h
|
@ -21,7 +21,19 @@ namespace data
|
||||||
{
|
{
|
||||||
IdentHash tunnelGateway;
|
IdentHash tunnelGateway;
|
||||||
uint32_t tunnelID;
|
uint32_t tunnelID;
|
||||||
uint64_t endDate;
|
uint64_t endDate; // 0 means invalid
|
||||||
|
bool isUpdated; // trasient
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LeaseCmp
|
||||||
|
{
|
||||||
|
bool operator() (std::shared_ptr<const Lease> l1, std::shared_ptr<const Lease> l2) const
|
||||||
|
{
|
||||||
|
if (l1->tunnelID != l2->tunnelID)
|
||||||
|
return l1->tunnelID < l2->tunnelID;
|
||||||
|
else
|
||||||
|
return l1->tunnelGateway < l2->tunnelGateway;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const int MAX_LS_BUFFER_SIZE = 3072;
|
const int MAX_LS_BUFFER_SIZE = 3072;
|
||||||
|
@ -34,14 +46,12 @@ namespace data
|
||||||
LeaseSet (std::shared_ptr<const i2p::tunnel::TunnelPool> pool);
|
LeaseSet (std::shared_ptr<const i2p::tunnel::TunnelPool> pool);
|
||||||
~LeaseSet () { delete[] m_Buffer; };
|
~LeaseSet () { delete[] m_Buffer; };
|
||||||
void Update (const uint8_t * buf, size_t len);
|
void Update (const uint8_t * buf, size_t len);
|
||||||
void PopulateLeases (); /// from buffer
|
void PopulateLeases (); // from buffer
|
||||||
std::shared_ptr<const IdentityEx> GetIdentity () const { return m_Identity; };
|
std::shared_ptr<const IdentityEx> GetIdentity () const { return m_Identity; };
|
||||||
|
|
||||||
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 IsValid () const { return m_IsValid; };
|
bool IsValid () const { return m_IsValid; };
|
||||||
|
|
||||||
const std::vector<std::shared_ptr<Lease> >& GetLeases () const { return m_Leases; };
|
|
||||||
const std::vector<std::shared_ptr<Lease> > GetNonExpiredLeases (bool withThreshold = true) const;
|
const std::vector<std::shared_ptr<Lease> > GetNonExpiredLeases (bool withThreshold = true) const;
|
||||||
bool HasExpiredLeases () const;
|
bool HasExpiredLeases () const;
|
||||||
bool IsExpired () const;
|
bool IsExpired () const;
|
||||||
|
@ -59,7 +69,7 @@ namespace data
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill
|
bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill
|
||||||
std::vector<std::shared_ptr<Lease> > m_Leases;
|
std::set<std::shared_ptr<Lease>, LeaseCmp> m_Leases;
|
||||||
uint64_t m_ExpirationTime; // in milliseconds
|
uint64_t m_ExpirationTime; // in milliseconds
|
||||||
std::shared_ptr<const IdentityEx> m_Identity;
|
std::shared_ptr<const IdentityEx> m_Identity;
|
||||||
uint8_t m_EncryptionKey[256];
|
uint8_t m_EncryptionKey[256];
|
||||||
|
|
Loading…
Reference in a new issue