From c54e6bafdbffc8b77d6f7df3d1d5ad742c66fe55 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 2 Jan 2019 14:19:10 -0500 Subject: [PATCH] process meta LS2 --- libi2pd/LeaseSet.cpp | 89 +++++++++++++++++++++++++++++++++++--------- libi2pd/LeaseSet.h | 8 +++- libi2pd/NetDb.cpp | 6 +-- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 23d7a86c..d6a92174 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -237,6 +237,7 @@ namespace data void LeaseSet2::ReadFromBuffer (const uint8_t * buf, size_t len) { + // standard LS2 header auto identity = std::make_shared(buf, len); SetIdentity (identity); size_t offset = identity->GetFullLen (); @@ -262,24 +263,21 @@ namespace data if (!identity->Verify (signedData, keyLen + 6, buf + offset)) return; offset += offlineVerifier->GetSignatureLen (); } - // properties - uint16_t propertiesLen = bufbe16toh (buf + offset); offset += 2; - offset += propertiesLen; // skip for now. TODO: implement properties - if (offset + 1 >= len) return; - // key sections - int numKeySections = buf[offset]; offset++; - for (int i = 0; i < numKeySections; i++) + // type specific part + size_t s = 0; + switch (m_StoreType) { - // skip key for now. TODO: implement encryption key - offset += 2; // encryption key type - if (offset + 2 >= len) return; - uint16_t encryptionKeyLen = bufbe16toh (buf + offset); offset += 2; - offset += encryptionKeyLen; - if (offset >= len) return; - } - // leases - int numLeases = buf[offset]; offset++; - offset += numLeases*40; // 40 bytes each + case NETDB_STORE_TYPE_STANDARD_LEASESET2: + s = ReadStandardLS2TypeSpecificPart (buf + offset, len - offset); + break; + case NETDB_STORE_TYPE_META_LEASESET2: + s = ReadMetaLS2TypeSpecificPart (buf + offset, len - offset); + break; + default: + LogPrint (eLogWarning, "LeaseSet2: Unexpected store type ", (int)m_StoreType); + } + if (!s) return; + offset += s; // verify signature if (offset + identity->GetSignatureLen () > len) return; uint8_t * buf1 = new uint8_t[offset + 1]; @@ -292,6 +290,63 @@ namespace data SetIsValid (verified); } + size_t LeaseSet2::ReadStandardLS2TypeSpecificPart (const uint8_t * buf, size_t len) + { + size_t offset = 0; + // properties + uint16_t propertiesLen = bufbe16toh (buf + offset); offset += 2; + offset += propertiesLen; // skip for now. TODO: implement properties + if (offset + 1 >= len) return 0; + // key sections + int numKeySections = buf[offset]; offset++; + for (int i = 0; i < numKeySections; i++) + { + // skip key for now. TODO: implement encryption key + offset += 2; // encryption key type + if (offset + 2 >= len) return 0; + uint16_t encryptionKeyLen = bufbe16toh (buf + offset); offset += 2; + offset += encryptionKeyLen; + if (offset >= len) return 0; + } + // leases + if (offset + 1 >= len) return 0; + int numLeases = buf[offset]; offset++; + for (int i = 0; i < numLeases; i++) + { + if (offset + 40 > len) return 0; + offset += 40; // lease + } + return offset; + } + + size_t LeaseSet2::ReadMetaLS2TypeSpecificPart (const uint8_t * buf, size_t len) + { + size_t offset = 0; + // properties + uint16_t propertiesLen = bufbe16toh (buf + offset); offset += 2; + offset += propertiesLen; // skip for now. TODO: implement properties + // entries + if (offset + 1 >= len) return 0; + int numEntries = buf[offset]; offset++; + for (int i = 0; i < numEntries; i++) + { + if (offset + 40 >= len) return 0; + offset += 32; // hash + offset += 3; // flags + offset += 1; // cost + offset += 4; // expires + } + // revocations + if (offset + 1 >= len) return 0; + int numRevocations = buf[offset]; offset++; + for (int i = 0; i < numRevocations; i++) + { + if (offset + 32 > len) return 0; + offset += 32; // hash + } + return offset; + } + 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 b2c96d1d..606672f2 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -51,6 +51,8 @@ namespace data const size_t MAX_LS_BUFFER_SIZE = 3072; const size_t LEASE_SIZE = 44; // 32 + 4 + 8 const uint8_t MAX_NUM_LEASES = 16; + + const uint8_t NETDB_STORE_TYPE_LEASESET = 1; class LeaseSet: public RoutingDestination { public: @@ -73,7 +75,7 @@ namespace data bool ExpiresSoon(const uint64_t dlt=1000 * 5, const uint64_t fudge = 0) const ; bool operator== (const LeaseSet& other) const { return m_BufferLen == other.m_BufferLen && !memcmp (m_Buffer, other.m_Buffer, m_BufferLen); }; - virtual uint8_t GetStoreType () const { return 1; }; + virtual uint8_t GetStoreType () const { return NETDB_STORE_TYPE_LEASESET; }; // implements RoutingDestination std::shared_ptr GetIdentity () const { return m_Identity; }; @@ -111,6 +113,8 @@ namespace data */ bool LeaseSetBufferValidate(const uint8_t * ptr, size_t sz, uint64_t & expires); + const uint8_t NETDB_STORE_TYPE_STANDARD_LEASESET2 = 3; + const uint8_t NETDB_STORE_TYPE_META_LEASESET2 = 7; class LeaseSet2: public LeaseSet { public: @@ -121,6 +125,8 @@ namespace data private: void ReadFromBuffer (const uint8_t * buf, size_t len); + size_t ReadStandardLS2TypeSpecificPart (const uint8_t * buf, size_t len); + size_t ReadMetaLS2TypeSpecificPart (const uint8_t * buf, size_t len); private: diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index def4fa6a..714c327d 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -661,15 +661,13 @@ namespace data uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET]; if (storeType) // LeaseSet or LeaseSet2 { - if (storeType == 1) + if (storeType == NETDB_STORE_TYPE_LEASESET) // 1 { - // 1 - LeaseSet LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); } - else + else // all others are considered as LeaseSet2 { - // 3- LeaseSet2 LogPrint (eLogDebug, "NetDb: store request: LeaseSet2 of type ", storeType, " for ", ident.ToBase32()); updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType); }