Browse Source

RAND_bytes from random router selection

pull/1687/head
orignal 3 years ago
parent
commit
367df4d0db
  1. 71
      libi2pd/NetDb.cpp

71
libi2pd/NetDb.cpp

@ -1223,47 +1223,54 @@ namespace data
{ {
if (m_RouterInfos.empty()) if (m_RouterInfos.empty())
return 0; return 0;
uint16_t inds[3];
RAND_bytes ((uint8_t *)inds, sizeof (inds));
std::unique_lock<std::mutex> l(m_RouterInfosMutex); std::unique_lock<std::mutex> l(m_RouterInfosMutex);
auto ind = rand () % m_RouterInfos.size (); inds[0] %= m_RouterInfos.size ();
auto it = m_RouterInfos.begin (); auto it = m_RouterInfos.begin ();
std::advance (it, ind); std::advance (it, inds[0]);
// try random router // try random router
if (it != m_RouterInfos.end () && !it->second->IsUnreachable () && filter (it->second)) if (it != m_RouterInfos.end () && !it->second->IsUnreachable () && filter (it->second))
return it->second; return it->second;
// try routers after // try some routers around
auto it1 = it; it1++; auto it1 = m_RouterInfos.begin ();
while (it1 != m_RouterInfos.end ()) if (inds[0])
{ {
if (!it1->second->IsUnreachable () && filter (it1->second)) // before
return it1->second; inds[1] %= inds[0];
it1++; std::advance (it1, inds[1]);
} }
// still not found, try some routers before auto it2 = it;
if (ind) if (inds[0] < m_RouterInfos.size () - 1)
{ {
ind = rand () % ind; // after
it1 = m_RouterInfos.begin (); inds[2] %= (m_RouterInfos.size () - 1 - inds[0]);
std::advance (it1, ind); std::advance (it2, inds[2]);
auto it2 = it1; }
while (it2 != it && it2 != m_RouterInfos.end ()) // it1 - from, it2 - to
{ it = it1;
if (!it2->second->IsUnreachable () && filter (it2->second)) while (it != it2 && it != m_RouterInfos.end ())
return it2->second; {
it2++; if (!it->second->IsUnreachable () && filter (it->second))
} return it->second;
if (ind) it++;
{ }
// still not found, try from the begining // still not found, try from the begining
it2 = m_RouterInfos.begin (); it = m_RouterInfos.begin ();
while (it2 != it1 && it2 != m_RouterInfos.end ()) while (it != it1 && it != m_RouterInfos.end ())
{ {
if (!it2->second->IsUnreachable () && filter (it2->second)) if (!it->second->IsUnreachable () && filter (it->second))
return it2->second; return it->second;
it2++; it++;
} }
} // still not found, try to the begining
it = it2;
while (it != m_RouterInfos.end ())
{
if (!it->second->IsUnreachable () && filter (it->second))
return it->second;
it++;
} }
return nullptr; // seems we have too few routers return nullptr; // seems we have too few routers
} }

Loading…
Cancel
Save