From 65c2c7d80b529842d08af375ec95393753ab9f7b Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 16 Jan 2019 19:00:17 -0500 Subject: [PATCH] re-create LeaseSet if store type has changed --- libi2pd/LeaseSet.cpp | 7 ++++ libi2pd/LeaseSet.h | 5 +-- libi2pd/NetDb.cpp | 82 +++++++++++++++++++++----------------------- libi2pd/NetDb.hpp | 2 +- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 71690c0b..3d72401d 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -260,6 +260,13 @@ namespace data ReadFromBuffer (buf, len); } + void LeaseSet2::Update (const uint8_t * buf, size_t len, bool verifySignature) + { + // shouldn't be called for now. Must be called from NetDb::AddLeaseSet later + SetBuffer (buf, len); + // TODO:verify signature if requested + } + void LeaseSet2::ReadFromBuffer (const uint8_t * buf, size_t len) { // standard LS2 header diff --git a/libi2pd/LeaseSet.h b/libi2pd/LeaseSet.h index e085ecc7..10e60c4a 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -60,7 +60,7 @@ namespace data LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true); virtual ~LeaseSet () { delete[] m_EncryptionKey; delete[] m_Buffer; }; - void Update (const uint8_t * buf, size_t len, bool verifySignature = true); + virtual void Update (const uint8_t * buf, size_t len, bool verifySignature = true); bool IsNewer (const uint8_t * buf, size_t len) const; void PopulateLeases (); // from buffer @@ -128,7 +128,8 @@ namespace data LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len, bool storeLeases = true); uint8_t GetStoreType () const { return m_StoreType; }; - + void Update (const uint8_t * buf, size_t len, bool verifySignature); + // implements RoutingDestination void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const; diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 25728f13..1ab246e1 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -250,43 +250,40 @@ namespace data return r; } - bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, - std::shared_ptr from) + bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len) { std::unique_lock lock(m_LeaseSetsMutex); bool updated = false; - if (!from) // unsolicited LS must be received directly + auto it = m_LeaseSets.find(ident); + if (it != m_LeaseSets.end () && it->second->GetStoreType () == i2p::data::NETDB_STORE_TYPE_LEASESET) { - auto it = m_LeaseSets.find(ident); - if (it != m_LeaseSets.end ()) + // we update only is existing LeaseSet is not LeaseSet2 + uint64_t expires; + if(LeaseSetBufferValidate(buf, len, expires)) { - uint64_t expires; - if(LeaseSetBufferValidate(buf, len, expires)) + if(it->second->GetExpirationTime() < expires) { - if(it->second->GetExpirationTime() < expires) - { - it->second->Update (buf, len, false); // signature is verified already - LogPrint (eLogInfo, "NetDb: LeaseSet updated: ", ident.ToBase32()); - updated = true; - } - else - LogPrint(eLogDebug, "NetDb: LeaseSet is older: ", ident.ToBase32()); + it->second->Update (buf, len, false); // signature is verified already + LogPrint (eLogInfo, "NetDb: LeaseSet updated: ", ident.ToBase32()); + updated = true; } else - LogPrint(eLogError, "NetDb: LeaseSet is invalid: ", ident.ToBase32()); + LogPrint(eLogDebug, "NetDb: LeaseSet is older: ", ident.ToBase32()); } else + LogPrint(eLogError, "NetDb: LeaseSet is invalid: ", ident.ToBase32()); + } + else + { + auto leaseSet = std::make_shared (buf, len, false); // we don't need leases in netdb + if (leaseSet->IsValid ()) { - auto leaseSet = std::make_shared (buf, len, false); // we don't need leases in netdb - if (leaseSet->IsValid ()) - { - LogPrint (eLogInfo, "NetDb: LeaseSet added: ", ident.ToBase32()); - m_LeaseSets[ident] = leaseSet; - updated = true; - } - else - LogPrint (eLogError, "NetDb: new LeaseSet validation failed: ", ident.ToBase32()); + LogPrint (eLogInfo, "NetDb: LeaseSet added: ", ident.ToBase32()); + m_LeaseSets[ident] = leaseSet; + updated = true; } + else + LogPrint (eLogError, "NetDb: new LeaseSet validation failed: ", ident.ToBase32()); } return updated; } @@ -294,14 +291,10 @@ namespace data bool NetDb::AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType) { std::unique_lock lock(m_LeaseSetsMutex); - auto it = m_LeaseSets.find(ident); - if (it == m_LeaseSets.end ()) - { - auto leaseSet = std::make_shared (storeType, buf, len, false); // we don't need leases in netdb - m_LeaseSets[ident] = leaseSet; - return true; - } - return false; + // always new LS2 for now. TODO: implement update + auto leaseSet = std::make_shared (storeType, buf, len, false); // we don't need leases in netdb + m_LeaseSets[ident] = leaseSet; + return true; } std::shared_ptr NetDb::FindRouter (const IdentHash& ident) const @@ -661,16 +654,19 @@ namespace data uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET]; if (storeType) // LeaseSet or LeaseSet2 { - if (storeType == NETDB_STORE_TYPE_LEASESET) // 1 - { - LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); - updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); - } - else // all others are considered as LeaseSet2 - { - LogPrint (eLogDebug, "NetDb: store request: LeaseSet2 of type ", storeType, " for ", ident.ToBase32()); - updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType); - } + if (!m->from) // unsolicited LS must be received directly + { + if (storeType == NETDB_STORE_TYPE_LEASESET) // 1 + { + LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); + updated = AddLeaseSet (ident, buf + offset, len - offset); + } + else // all others are considered as LeaseSet2 + { + LogPrint (eLogDebug, "NetDb: store request: LeaseSet2 of type ", storeType, " for ", ident.ToBase32()); + updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType); + } + } } else // RouterInfo { diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 94ed2ed2..93e9e48f 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -54,7 +54,7 @@ namespace data bool AddRouterInfo (const uint8_t * buf, int len); bool AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len); - bool AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, std::shared_ptr from); + bool AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len); bool AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType); std::shared_ptr FindRouter (const IdentHash& ident) const; std::shared_ptr FindLeaseSet (const IdentHash& destination) const;