Browse Source

re-create LeaseSet if store type has changed

pull/1288/head
orignal 6 years ago
parent
commit
65c2c7d80b
  1. 7
      libi2pd/LeaseSet.cpp
  2. 3
      libi2pd/LeaseSet.h
  3. 20
      libi2pd/NetDb.cpp
  4. 2
      libi2pd/NetDb.hpp

7
libi2pd/LeaseSet.cpp

@ -260,6 +260,13 @@ namespace data
ReadFromBuffer (buf, len); 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) void LeaseSet2::ReadFromBuffer (const uint8_t * buf, size_t len)
{ {
// standard LS2 header // standard LS2 header

3
libi2pd/LeaseSet.h

@ -60,7 +60,7 @@ namespace data
LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true); LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true);
virtual ~LeaseSet () { delete[] m_EncryptionKey; delete[] m_Buffer; }; 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; bool IsNewer (const uint8_t * buf, size_t len) const;
void PopulateLeases (); // from buffer void PopulateLeases (); // from buffer
@ -128,6 +128,7 @@ namespace data
LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len, bool storeLeases = true); LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len, bool storeLeases = true);
uint8_t GetStoreType () const { return m_StoreType; }; uint8_t GetStoreType () const { return m_StoreType; };
void Update (const uint8_t * buf, size_t len, bool verifySignature);
// implements RoutingDestination // implements RoutingDestination
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;

20
libi2pd/NetDb.cpp

@ -250,16 +250,14 @@ namespace data
return r; return r;
} }
bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len)
std::shared_ptr<i2p::tunnel::InboundTunnel> from)
{ {
std::unique_lock<std::mutex> lock(m_LeaseSetsMutex); std::unique_lock<std::mutex> lock(m_LeaseSetsMutex);
bool updated = false; bool updated = false;
if (!from) // unsolicited LS must be received directly
{
auto it = m_LeaseSets.find(ident); auto it = m_LeaseSets.find(ident);
if (it != m_LeaseSets.end ()) if (it != m_LeaseSets.end () && it->second->GetStoreType () == i2p::data::NETDB_STORE_TYPE_LEASESET)
{ {
// we update only is existing LeaseSet is not LeaseSet2
uint64_t expires; uint64_t expires;
if(LeaseSetBufferValidate(buf, len, expires)) if(LeaseSetBufferValidate(buf, len, expires))
{ {
@ -287,22 +285,17 @@ namespace data
else else
LogPrint (eLogError, "NetDb: new LeaseSet validation failed: ", ident.ToBase32()); LogPrint (eLogError, "NetDb: new LeaseSet validation failed: ", ident.ToBase32());
} }
}
return updated; return updated;
} }
bool NetDb::AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType) bool NetDb::AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType)
{ {
std::unique_lock<std::mutex> lock(m_LeaseSetsMutex); std::unique_lock<std::mutex> lock(m_LeaseSetsMutex);
auto it = m_LeaseSets.find(ident); // always new LS2 for now. TODO: implement update
if (it == m_LeaseSets.end ())
{
auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len, false); // we don't need leases in netdb auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len, false); // we don't need leases in netdb
m_LeaseSets[ident] = leaseSet; m_LeaseSets[ident] = leaseSet;
return true; return true;
} }
return false;
}
std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const
{ {
@ -660,11 +653,13 @@ namespace data
bool updated = false; bool updated = false;
uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET]; uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET];
if (storeType) // LeaseSet or LeaseSet2 if (storeType) // LeaseSet or LeaseSet2
{
if (!m->from) // unsolicited LS must be received directly
{ {
if (storeType == NETDB_STORE_TYPE_LEASESET) // 1 if (storeType == NETDB_STORE_TYPE_LEASESET) // 1
{ {
LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32()); LogPrint (eLogDebug, "NetDb: store request: LeaseSet for ", ident.ToBase32());
updated = AddLeaseSet (ident, buf + offset, len - offset, m->from); updated = AddLeaseSet (ident, buf + offset, len - offset);
} }
else // all others are considered as LeaseSet2 else // all others are considered as LeaseSet2
{ {
@ -672,6 +667,7 @@ namespace data
updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType); updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType);
} }
} }
}
else // RouterInfo else // RouterInfo
{ {
LogPrint (eLogDebug, "NetDb: store request: RouterInfo"); LogPrint (eLogDebug, "NetDb: store request: RouterInfo");

2
libi2pd/NetDb.hpp

@ -54,7 +54,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);
bool AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType); 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;

Loading…
Cancel
Save