From d5e1d5db9c011faf68b7def8644d55b0c8108bb4 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 8 Apr 2015 10:34:16 -0400 Subject: [PATCH] validate leaseset for zero leases --- Destination.cpp | 21 ++++++++++++++++++--- LeaseSet.cpp | 15 +++++++++++---- LeaseSet.h | 2 ++ NetDb.cpp | 18 +++++++++++++++--- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Destination.cpp b/Destination.cpp index 7cd97d45..4c7d382a 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -223,13 +223,28 @@ namespace client { leaseSet = it->second; leaseSet->Update (buf + offset, len - offset); - LogPrint (eLogDebug, "Remote LeaseSet updated"); + if (leaseSet->IsValid ()) + LogPrint (eLogDebug, "Remote LeaseSet updated"); + else + { + LogPrint (eLogDebug, "Remote LeaseSet update failed"); + m_RemoteLeaseSets.erase (it); + leaseSet = nullptr; + } } else { - LogPrint (eLogDebug, "New remote LeaseSet added"); leaseSet = std::make_shared (buf + offset, len - offset); - m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = leaseSet; + if (leaseSet->IsValid ()) + { + LogPrint (eLogDebug, "New remote LeaseSet added"); + m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = leaseSet; + } + else + { + LogPrint (eLogError, "New remote LeaseSet verification failed"); + leaseSet = nullptr; + } } } else diff --git a/LeaseSet.cpp b/LeaseSet.cpp index 3bc5d306..b2e84d23 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -14,7 +14,8 @@ namespace i2p namespace data { - LeaseSet::LeaseSet (const uint8_t * buf, size_t len) + LeaseSet::LeaseSet (const uint8_t * buf, size_t len): + m_IsValid (true) { m_Buffer = new uint8_t[len]; memcpy (m_Buffer, buf, len); @@ -22,7 +23,8 @@ namespace data ReadFromBuffer (); } - LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool) + LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool): + m_IsValid (true) { // header const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination (); @@ -30,6 +32,7 @@ namespace data { m_Buffer = nullptr; m_BufferLen = 0; + m_IsValid = false; LogPrint (eLogError, "Destination for local LeaseSet doesn't exist"); return; } @@ -88,6 +91,7 @@ namespace data uint8_t num = m_Buffer[size]; size++; // num LogPrint ("LeaseSet num=", (int)num); + if (!num) m_IsValid = false; // process leases const uint8_t * leases = m_Buffer + size; @@ -106,14 +110,17 @@ namespace data if (!netdb.FindRouter (lease.tunnelGateway)) { // if not found request it - LogPrint ("Lease's tunnel gateway not found. Requested"); + LogPrint (eLogInfo, "Lease's tunnel gateway not found. Requested"); netdb.RequestDestination (lease.tunnelGateway); } } // verify if (!m_Identity.Verify (m_Buffer, leases - m_Buffer, leases)) - LogPrint ("LeaseSet verification failed"); + { + LogPrint (eLogWarning, "LeaseSet verification failed"); + m_IsValid = false; + } } const std::vector LeaseSet::GetNonExpiredLeases (bool withThreshold) const diff --git a/LeaseSet.h b/LeaseSet.h index d1164449..fee130ab 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -44,6 +44,7 @@ namespace data const uint8_t * GetBuffer () const { return m_Buffer; }; size_t GetBufferLen () const { return m_BufferLen; }; + bool IsValid () const { return m_IsValid; }; // implements RoutingDestination const IdentHash& GetIdentHash () const { return m_Identity.GetIdentHash (); }; @@ -60,6 +61,7 @@ namespace data private: + bool m_IsValid; std::vector m_Leases; IdentityEx m_Identity; uint8_t m_EncryptionKey[256]; diff --git a/NetDb.cpp b/NetDb.cpp index 38bc56e9..a647ba5b 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -252,12 +252,24 @@ namespace data if (it != m_LeaseSets.end ()) { it->second->Update (buf, len); - LogPrint ("LeaseSet updated"); + if (it->second->IsValid ()) + LogPrint (eLogInfo, "LeaseSet updated"); + else + { + LogPrint (eLogInfo, "LeaseSet update failed"); + m_LeaseSets.erase (it); + } } else { - LogPrint ("New LeaseSet added"); - m_LeaseSets[ident] = std::make_shared (buf, len); + auto leaseSet = std::make_shared (buf, len); + if (leaseSet->IsValid ()) + { + LogPrint (eLogInfo, "New LeaseSet added"); + m_LeaseSets[ident] = leaseSet; + } + else + LogPrint (eLogError, "New LeaseSet validation failed"); } } }