Browse Source

don't copy RouterInfos and LeaseSets

pull/93/head
orignal 10 years ago
parent
commit
f3c6dd4d3d
  1. 15
      LeaseSet.cpp
  2. 5
      LeaseSet.h
  3. 34
      NetDb.cpp
  4. 4
      NetDb.h
  5. 15
      RouterInfo.cpp
  6. 1
      RouterInfo.h

15
LeaseSet.cpp

@ -13,6 +13,17 @@ namespace data @@ -13,6 +13,17 @@ namespace data
LeaseSet::LeaseSet (const uint8_t * buf, int len)
{
ReadFromBuffer (buf, len);
}
void LeaseSet::Update (const uint8_t * buf, int len)
{
m_Leases.clear ();
ReadFromBuffer (buf, len);
}
void LeaseSet::ReadFromBuffer (const uint8_t * buf, int len)
{
#pragma pack(1)
struct H
{
@ -55,8 +66,8 @@ namespace data @@ -55,8 +66,8 @@ namespace data
CryptoPP::DSA::Verifier verifier (pubKey);
if (!verifier.VerifyMessage (buf, leases - buf, leases, 40))
LogPrint ("LeaseSet verification failed");
}
}
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();

5
LeaseSet.h

@ -37,6 +37,7 @@ namespace data @@ -37,6 +37,7 @@ namespace data
LeaseSet (const uint8_t * buf, int len);
LeaseSet (const LeaseSet& ) = default;
LeaseSet& operator=(const LeaseSet& ) = default;
void Update (const uint8_t * buf, int len);
// implements RoutingDestination
const Identity& GetIdentity () const { return m_Identity; };
@ -47,6 +48,10 @@ namespace data @@ -47,6 +48,10 @@ namespace data
bool HasNonExpiredLeases () const;
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
bool IsDestination () const { return true; };
private:
void ReadFromBuffer (const uint8_t * buf, int len);
private:

34
NetDb.cpp

@ -155,44 +155,40 @@ namespace data @@ -155,44 +155,40 @@ namespace data
}
}
void NetDb::AddRouterInfo (uint8_t * buf, int len)
{
RouterInfo * r = new RouterInfo (buf, len);
DeleteRequestedDestination (r->GetIdentHash ());
auto it = m_RouterInfos.find(r->GetIdentHash ());
void NetDb::AddRouterInfo (const IdentHash& ident, uint8_t * buf, int len)
{
DeleteRequestedDestination (ident);
auto it = m_RouterInfos.find(ident);
if (it != m_RouterInfos.end ())
{
if (r->GetTimestamp () > it->second->GetTimestamp ())
{
auto ts = it->second->GetTimestamp ();
it->second->Update (buf, len);
if (it->second->GetTimestamp () > ts)
LogPrint ("RouterInfo updated");
*(it->second) = *r; // we can't replace pointer because it's used by tunnels
}
delete r;
}
else
{
LogPrint ("New RouterInfo added");
RouterInfo * r = new RouterInfo (buf, len);
m_RouterInfos[r->GetIdentHash ()] = r;
if (r->IsFloodfill ())
m_Floodfills.push_back (r);
}
}
void NetDb::AddLeaseSet (uint8_t * buf, int len)
void NetDb::AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len)
{
LeaseSet * l = new LeaseSet (buf, len);
DeleteRequestedDestination (l->GetIdentHash ());
auto it = m_LeaseSets.find(l->GetIdentHash ());
DeleteRequestedDestination (ident);
auto it = m_LeaseSets.find(ident);
if (it != m_LeaseSets.end ())
{
it->second->Update (buf, len);
LogPrint ("LeaseSet updated");
*(it->second) = *l; // we can't replace pointer because it's used by streams
delete l;
}
else
{
LogPrint ("New LeaseSet added");
m_LeaseSets[l->GetIdentHash ()] = l;
m_LeaseSets[ident] = new LeaseSet (buf, len);
}
}
@ -395,7 +391,7 @@ namespace data @@ -395,7 +391,7 @@ namespace data
if (msg->type)
{
LogPrint ("LeaseSet");
AddLeaseSet (buf + offset, len - offset);
AddLeaseSet (msg->key, buf + offset, len - offset);
}
else
{
@ -413,7 +409,7 @@ namespace data @@ -413,7 +409,7 @@ namespace data
uint8_t uncompressed[2048];
size_t uncomressedSize = decompressor.MaxRetrievable ();
decompressor.Get (uncompressed, uncomressedSize);
AddRouterInfo (uncompressed, uncomressedSize);
AddRouterInfo (msg->key, uncompressed, uncomressedSize);
}
}

4
NetDb.h

@ -58,8 +58,8 @@ namespace data @@ -58,8 +58,8 @@ namespace data
void Start ();
void Stop ();
void AddRouterInfo (uint8_t * buf, int len);
void AddLeaseSet (uint8_t * buf, int len);
void AddRouterInfo (const IdentHash& ident, uint8_t * buf, int len);
void AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len);
RouterInfo * FindRouter (const IdentHash& ident) const;
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
const IdentHash * FindAddress (const std::string& address) { return m_AddressBook.FindAddress (address); }; // TODO: move AddressBook away from NetDb

15
RouterInfo.cpp

@ -30,7 +30,20 @@ namespace data @@ -30,7 +30,20 @@ namespace data
m_BufferLen = len;
ReadFromBuffer ();
}
void RouterInfo::Update (const uint8_t * buf, int len)
{
m_IsUpdated = true;
m_IsUnreachable = false;
m_SupportedTransports = 0;
m_Caps = 0;
m_Addresses.clear ();
m_Properties.clear ();
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
}
void RouterInfo::SetRouterIdentity (const Identity& identity)
{
m_RouterIdentity = identity;

1
RouterInfo.h

@ -68,6 +68,7 @@ namespace data @@ -68,6 +68,7 @@ namespace data
RouterInfo (const RouterInfo& ) = default;
RouterInfo& operator=(const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len);
void Update (const uint8_t * buf, int len);
const Identity& GetRouterIdentity () const { return m_RouterIdentity; };
void SetRouterIdentity (const Identity& identity);

Loading…
Cancel
Save