diff --git a/NetDb.cpp b/NetDb.cpp index 2b3598fc..8b706589 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -55,7 +55,7 @@ namespace data NetDb::~NetDb () { - Stop (); + Stop (); for (auto l:m_LeaseSets) delete l.second; for (auto r:m_RouterInfos) @@ -160,6 +160,8 @@ namespace data { LogPrint ("New RouterInfo added"); m_RouterInfos[r->GetIdentHash ()] = r; + if (r->IsFloodfill ()) + m_Floodfills.push_back (r); } } @@ -237,6 +239,7 @@ namespace data for (auto r: m_RouterInfos) delete r.second; m_RouterInfos.clear (); + m_Floodfills.clear (); // load routers now int numRouters = 0; @@ -253,11 +256,14 @@ namespace data RouterInfo * r = new RouterInfo(it1->path().c_str()); #endif m_RouterInfos[r->GetIdentHash ()] = r; + if (r->IsFloodfill ()) + m_Floodfills.push_back (r); numRouters++; } } } LogPrint (numRouters, " routers loaded"); + LogPrint (m_Floodfills.size (), " floodfills loaded"); } void NetDb::SaveUpdated (const char * directory) @@ -594,22 +600,6 @@ namespace data delete dest; } } - - const RouterInfo * NetDb::GetRandomNTCPRouter (bool floodfillOnly) const - { - CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); - uint32_t ind = rnd.GenerateWord32 (0, m_RouterInfos.size () - 1), i = 0; - RouterInfo * last = nullptr; - for (auto it: m_RouterInfos) - { - if (it.second->IsNTCP () && !it.second->IsUnreachable () && - (!floodfillOnly || it.second->IsFloodfill ())) - last = it.second; - if (i >= ind) break; - else i++; - } - return last; - } const RouterInfo * NetDb::GetRandomRouter (const RouterInfo * compatibleWith, bool floodfillOnly) const { @@ -633,7 +623,7 @@ namespace data // we couldn't find anything, try second pass ind = 0; } - return nullptr; // seem we have too few routers + return nullptr; // seems we have too few routers } void NetDb::PostI2NPMsg (I2NPMessage * msg) @@ -648,15 +638,15 @@ namespace data XORMetric minMetric; RoutingKey destKey = CreateRoutingKey (destination); minMetric.SetMax (); - for (auto it: m_RouterInfos) + for (auto it: m_Floodfills) { - if (it.second->IsFloodfill () &&! it.second->IsUnreachable () && !excluded.count (it.first)) + if (!it->IsUnreachable () && !excluded.count (it->GetIdentHash ())) { - XORMetric m = destKey ^ it.second->GetRoutingKey (); + XORMetric m = destKey ^ it->GetRoutingKey (); if (m < minMetric) { minMetric = m; - r = it.second; + r = it; } } } diff --git a/NetDb.h b/NetDb.h index f9cf3900..4e1e1038 100644 --- a/NetDb.h +++ b/NetDb.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -73,7 +74,6 @@ namespace data void HandleDatabaseStoreMsg (uint8_t * buf, size_t len); void HandleDatabaseSearchReplyMsg (I2NPMessage * msg); - const RouterInfo * GetRandomNTCPRouter (bool floodfillOnly = false) const; const RouterInfo * GetRandomRouter (const RouterInfo * compatibleWith = nullptr, bool floodfillOnly = false) const; void PostI2NPMsg (I2NPMessage * msg); @@ -98,6 +98,7 @@ namespace data std::map m_LeaseSets; std::map m_RouterInfos; + std::vector m_Floodfills; std::map m_RequestedDestinations; std::set m_Subscriptions;