Browse Source

doesn't store leases in netdb

pull/370/head
orignal 9 years ago
parent
commit
f3b277aeef
  1. 8
      Destination.cpp
  2. 2
      Destination.h
  3. 4
      HTTPServer.cpp
  4. 27
      LeaseSet.cpp
  5. 9
      LeaseSet.h
  6. 5
      NetDb.cpp

8
Destination.cpp

@ -187,16 +187,17 @@ namespace client
auto it = m_RemoteLeaseSets.find (ident); auto it = m_RemoteLeaseSets.find (ident);
if (it != m_RemoteLeaseSets.end ()) if (it != m_RemoteLeaseSets.end ())
{ {
if (it->second->HasNonExpiredLeases ()) if (!it->second->IsExpired ())
return it->second; return it->second;
else else
LogPrint (eLogWarning, "Destination: All leases of remote LeaseSet expired"); LogPrint (eLogWarning, "Destination: remote LeaseSet expired");
} }
else else
{ {
auto ls = i2p::data::netdb.FindLeaseSet (ident); auto ls = i2p::data::netdb.FindLeaseSet (ident);
if (ls) if (ls)
{ {
ls->PopulateLeases (); // since we don't store them in netdb
m_RemoteLeaseSets[ident] = ls; m_RemoteLeaseSets[ident] = ls;
return ls; return ls;
} }
@ -674,9 +675,10 @@ namespace client
void ClientDestination::CleanupRemoteLeaseSets () void ClientDestination::CleanupRemoteLeaseSets ()
{ {
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto it = m_RemoteLeaseSets.begin (); it != m_RemoteLeaseSets.end ();) for (auto it = m_RemoteLeaseSets.begin (); it != m_RemoteLeaseSets.end ();)
{ {
if (!it->second->HasNonExpiredLeases ()) // all leases expired if (ts > it->second->GetExpirationTime ()) // leaseset expired
{ {
LogPrint (eLogWarning, "Destination: Remote LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired"); LogPrint (eLogWarning, "Destination: Remote LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
it = m_RemoteLeaseSets.erase (it); it = m_RemoteLeaseSets.erase (it);

2
Destination.h

@ -72,7 +72,7 @@ namespace client
bool IsRunning () const { return m_IsRunning; }; bool IsRunning () const { return m_IsRunning; };
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; }; std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases () && m_Pool->GetOutboundTunnels ().size () > 0; }; bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; };
std::shared_ptr<const i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident); std::shared_ptr<const i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident);
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr); bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
void CancelDestinationRequest (const i2p::data::IdentHash& dest); void CancelDestinationRequest (const i2p::data::IdentHash& dest);

4
HTTPServer.cpp

@ -793,7 +793,7 @@ namespace util
} }
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination); auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination);
if (leaseSet && leaseSet->HasNonExpiredLeases ()) if (leaseSet && !leaseSet->IsExpired ())
SendToDestination (leaseSet, port, buf, len); SendToDestination (leaseSet, port, buf, len);
else else
{ {
@ -812,7 +812,7 @@ namespace util
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
{ {
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination); auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination);
if (leaseSet && leaseSet->HasNonExpiredLeases ()) if (leaseSet && !leaseSet->IsExpired ())
SendToDestination (leaseSet, port, buf, len); SendToDestination (leaseSet, port, buf, len);
else else
// still no LeaseSet // still no LeaseSet

27
LeaseSet.cpp

@ -12,8 +12,8 @@ namespace i2p
namespace data namespace data
{ {
LeaseSet::LeaseSet (const uint8_t * buf, size_t len): LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases):
m_IsValid (true) m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0)
{ {
m_Buffer = new uint8_t[len]; m_Buffer = new uint8_t[len];
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
@ -22,7 +22,7 @@ namespace data
} }
LeaseSet::LeaseSet (std::shared_ptr<const i2p::tunnel::TunnelPool> pool): LeaseSet::LeaseSet (std::shared_ptr<const i2p::tunnel::TunnelPool> pool):
m_IsValid (true) m_IsValid (true), m_StoreLeases (true), m_ExpirationTime (0)
{ {
if (!pool) return; if (!pool) return;
// header // header
@ -55,6 +55,7 @@ namespace data
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration
ts *= 1000; // in milliseconds ts *= 1000; // in milliseconds
ts += rand () % 6; // + random milliseconds 0-5 ts += rand () % 6; // + random milliseconds 0-5
if (ts > m_ExpirationTime) m_ExpirationTime = ts;
htobe64buf (m_Buffer + m_BufferLen, ts); htobe64buf (m_Buffer + m_BufferLen, ts);
m_BufferLen += 8; // end date m_BufferLen += 8; // end date
} }
@ -80,6 +81,13 @@ namespace data
ReadFromBuffer (false); ReadFromBuffer (false);
} }
void LeaseSet::PopulateLeases ()
{
m_StoreLeases = true;
m_Leases.clear ();
ReadFromBuffer (false);
}
void LeaseSet::ReadFromBuffer (bool readIdentity) void LeaseSet::ReadFromBuffer (bool readIdentity)
{ {
if (readIdentity || !m_Identity) if (readIdentity || !m_Identity)
@ -105,6 +113,7 @@ namespace data
} }
// process leases // process leases
m_ExpirationTime = 0;
const uint8_t * leases = m_Buffer + size; const uint8_t * leases = m_Buffer + size;
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{ {
@ -115,8 +124,11 @@ namespace data
leases += 4; // tunnel ID leases += 4; // tunnel ID
lease.endDate = bufbe64toh (leases); lease.endDate = bufbe64toh (leases);
leases += 8; // end date leases += 8; // end date
if (lease.endDate > m_ExpirationTime)
m_ExpirationTime = lease.endDate;
if (m_StoreLeases)
{
m_Leases.push_back (lease); m_Leases.push_back (lease);
// 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))
{ {
@ -125,6 +137,7 @@ namespace data
netdb.RequestDestination (lease.tunnelGateway); netdb.RequestDestination (lease.tunnelGateway);
} }
} }
}
// verify // verify
if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases)) if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
@ -157,12 +170,10 @@ namespace data
return false; return false;
} }
bool LeaseSet::HasNonExpiredLeases () const bool LeaseSet::IsExpired () const
{ {
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto& it: m_Leases) return ts > m_ExpirationTime;
if (ts < it.endDate) return true;
return false;
} }
} }
} }

9
LeaseSet.h

@ -37,10 +37,11 @@ namespace data
{ {
public: public:
LeaseSet (const uint8_t * buf, size_t len); LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true);
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
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; };
@ -52,7 +53,8 @@ namespace data
const std::vector<Lease>& GetLeases () const { return m_Leases; }; const std::vector<Lease>& GetLeases () const { return m_Leases; };
const std::vector<Lease> GetNonExpiredLeases (bool withThreshold = true) const; const std::vector<Lease> GetNonExpiredLeases (bool withThreshold = true) const;
bool HasExpiredLeases () const; bool HasExpiredLeases () const;
bool HasNonExpiredLeases () const; bool IsExpired () const;
uint64_t GetExpirationTime () const { return m_ExpirationTime; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
bool IsDestination () const { return true; }; bool IsDestination () const { return true; };
@ -62,8 +64,9 @@ namespace data
private: private:
bool m_IsValid; bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill
std::vector<Lease> m_Leases; std::vector<Lease> m_Leases;
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];
uint8_t * m_Buffer; uint8_t * m_Buffer;

5
NetDb.cpp

@ -206,7 +206,7 @@ namespace data
} }
else else
{ {
auto leaseSet = std::make_shared<LeaseSet> (buf, len); auto leaseSet = std::make_shared<LeaseSet> (buf, len, false); // we don't need leases in netdb
if (leaseSet->IsValid ()) if (leaseSet->IsValid ())
{ {
LogPrint (eLogInfo, "NetDb: LeaseSet added: ", ident.ToBase64()); LogPrint (eLogInfo, "NetDb: LeaseSet added: ", ident.ToBase64());
@ -981,9 +981,10 @@ namespace data
void NetDb::ManageLeaseSets () void NetDb::ManageLeaseSets ()
{ {
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();) for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();)
{ {
if (!it->second->HasNonExpiredLeases ()) // all leases expired if (ts > it->second->GetExpirationTime ())
{ {
LogPrint (eLogWarning, "NetDb: LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired"); LogPrint (eLogWarning, "NetDb: LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
it = m_LeaseSets.erase (it); it = m_LeaseSets.erase (it);

Loading…
Cancel
Save