Skip to content

Commit

Permalink
don't copy RouterInfos and LeaseSets
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 22, 2014
1 parent 207022a commit f3c6dd4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
15 changes: 13 additions & 2 deletions LeaseSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 ();
Expand Down
5 changes: 5 additions & 0 deletions LeaseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand All @@ -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:

Expand Down
34 changes: 15 additions & 19 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -395,7 +391,7 @@ namespace data
if (msg->type)
{
LogPrint ("LeaseSet");
AddLeaseSet (buf + offset, len - offset);
AddLeaseSet (msg->key, buf + offset, len - offset);
}
else
{
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions NetDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion RouterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions RouterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f3c6dd4

Please sign in to comment.