From 12af68bdb5a905b27da99bb9f3005178ce869efc Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 21 Dec 2018 15:00:03 -0500 Subject: [PATCH] initial support of LeaseSet2 --- libi2pd/LeaseSet.cpp | 30 ++++++++++++++++++++++++++++++ libi2pd/LeaseSet.h | 20 ++++++++++++++++++++ libi2pd/NetDb.cpp | 32 ++++++++++++++++++++++++++++---- libi2pd/NetDb.hpp | 1 + 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 8b6063fc..52e736f9 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -12,6 +12,11 @@ namespace i2p namespace data { + LeaseSet::LeaseSet (): + m_IsValid (false), m_StoreLeases (false), m_ExpirationTime (0), m_Buffer (nullptr), m_BufferLen (0) + { + } + LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases): m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0) { @@ -215,6 +220,31 @@ namespace data encryptor->Encrypt (data, encrypted, ctx, true); } + void LeaseSet::SetBuffer (const uint8_t * buf, size_t len) + { + if (m_Buffer) delete[] m_Buffer; + m_Buffer = new uint8_t[len]; + m_BufferLen = len; + memcpy (m_Buffer, buf, len); + } + + LeaseSet2::LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len) + { + SetBuffer (buf, len); + ReadFromBuffer (storeType, buf, len); + } + + void LeaseSet2::ReadFromBuffer (uint8_t storeType, const uint8_t * buf, size_t len) + { + auto identity = std::make_shared(buf, len); + SetIdentity (identity); + size_t offset = identity->GetFullLen (); + uint32_t timestamp = bufbe32toh (buf + offset); offset += 4; // published timestamp (seconds) + uint16_t expires = bufbe16toh (buf + offset); offset += 2; // expires (seconds) + SetExpirationTime ((timestamp + expires)*1000LL); // in milliseconds + SetIsValid (true); // TODO:: verify signature + } + LocalLeaseSet::LocalLeaseSet (std::shared_ptr identity, const uint8_t * encryptionPublicKey, std::vector > tunnels): m_ExpirationTime (0), m_Identity (identity) { diff --git a/libi2pd/LeaseSet.h b/libi2pd/LeaseSet.h index fa24df2f..330f3410 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -79,6 +79,15 @@ namespace data void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const; bool IsDestination () const { return true; }; + protected: + + // called from LeaseSet2 + LeaseSet (); + void SetBuffer (const uint8_t * buf, size_t len); + void SetIdentity (std::shared_ptr identity) { m_Identity = identity; }; + void SetExpirationTime (uint64_t t) { m_ExpirationTime = t; }; + void SetIsValid (bool isValid) { m_IsValid = isValid; }; + private: void ReadFromBuffer (bool readIdentity = true, bool verifySignature = true); @@ -101,6 +110,17 @@ namespace data */ bool LeaseSetBufferValidate(const uint8_t * ptr, size_t sz, uint64_t & expires); + class LeaseSet2: public LeaseSet + { + public: + + LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len); + + private: + + void ReadFromBuffer (uint8_t storeType, const uint8_t * buf, size_t len); + }; + class LocalLeaseSet { public: diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 2e5c3a23..def4fa6a 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -291,6 +291,19 @@ namespace data return updated; } + 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); + m_LeaseSets[ident] = leaseSet; + return true; + } + return false; + } + std::shared_ptr NetDb::FindRouter (const IdentHash& ident) const { std::unique_lock l(m_RouterInfosMutex); @@ -645,12 +658,23 @@ namespace data size_t payloadOffset = offset; bool updated = false; - if (buf[DATABASE_STORE_TYPE_OFFSET]) // type + uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET]; + if (storeType) // LeaseSet or LeaseSet2 { - LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); - updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); + if (storeType == 1) + { + // 1 - LeaseSet + LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); + updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); + } + else + { + // 3- LeaseSet2 + LogPrint (eLogDebug, "NetDb: store request: LeaseSet2 of type ", storeType, " for ", ident.ToBase32()); + updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType); + } } - else + else // RouterInfo { LogPrint (eLogDebug, "NetDb: store request: RouterInfo"); size_t size = bufbe16toh (buf + offset); diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index b34458fb..94ed2ed2 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -55,6 +55,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 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; std::shared_ptr FindRouterProfile (const IdentHash& ident) const;