Browse Source

initial support of LeaseSet2

pull/1278/head
orignal 6 years ago
parent
commit
12af68bdb5
  1. 30
      libi2pd/LeaseSet.cpp
  2. 20
      libi2pd/LeaseSet.h
  3. 32
      libi2pd/NetDb.cpp
  4. 1
      libi2pd/NetDb.hpp

30
libi2pd/LeaseSet.cpp

@ -12,6 +12,11 @@ namespace i2p
namespace data 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): LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases):
m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0) m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0)
{ {
@ -215,6 +220,31 @@ namespace data
encryptor->Encrypt (data, encrypted, ctx, true); 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<IdentityEx>(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<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels): LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):
m_ExpirationTime (0), m_Identity (identity) m_ExpirationTime (0), m_Identity (identity)
{ {

20
libi2pd/LeaseSet.h

@ -79,6 +79,15 @@ namespace data
void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const; void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const;
bool IsDestination () const { return true; }; bool IsDestination () const { return true; };
protected:
// called from LeaseSet2
LeaseSet ();
void SetBuffer (const uint8_t * buf, size_t len);
void SetIdentity (std::shared_ptr<const IdentityEx> identity) { m_Identity = identity; };
void SetExpirationTime (uint64_t t) { m_ExpirationTime = t; };
void SetIsValid (bool isValid) { m_IsValid = isValid; };
private: private:
void ReadFromBuffer (bool readIdentity = true, bool verifySignature = true); 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); 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 class LocalLeaseSet
{ {
public: public:

32
libi2pd/NetDb.cpp

@ -291,6 +291,19 @@ namespace data
return updated; return updated;
} }
bool NetDb::AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType)
{
std::unique_lock<std::mutex> lock(m_LeaseSetsMutex);
auto it = m_LeaseSets.find(ident);
if (it == m_LeaseSets.end ())
{
auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len);
m_LeaseSets[ident] = leaseSet;
return true;
}
return false;
}
std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const
{ {
std::unique_lock<std::mutex> l(m_RouterInfosMutex); std::unique_lock<std::mutex> l(m_RouterInfosMutex);
@ -645,12 +658,23 @@ namespace data
size_t payloadOffset = offset; size_t payloadOffset = offset;
bool updated = false; 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()); if (storeType == 1)
updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); {
// 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"); LogPrint (eLogDebug, "NetDb: store request: RouterInfo");
size_t size = bufbe16toh (buf + offset); size_t size = bufbe16toh (buf + offset);

1
libi2pd/NetDb.hpp

@ -55,6 +55,7 @@ namespace data
bool AddRouterInfo (const uint8_t * buf, int len); bool AddRouterInfo (const uint8_t * buf, int len);
bool AddRouterInfo (const IdentHash& ident, 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<i2p::tunnel::InboundTunnel> from); bool AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
bool AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType);
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const; std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const; std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const;
std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const; std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const;

Loading…
Cancel
Save