diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index 0614e6cef..a1088c6c4 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -5,6 +5,8 @@ #include #include +#include + namespace llarp { namespace path @@ -161,6 +163,33 @@ namespace llarp return chosen; } + Path_ptr + PathSet::GetRandomPathByRouter(RouterID id, PathRole roles) const + { + Lock_t l(m_PathsMutex); + std::vector chosen; + auto itr = m_Paths.begin(); + while (itr != m_Paths.end()) + { + if (itr->second->IsReady() && itr->second->SupportsAnyRoles(roles)) + { + if (itr->second->Endpoint() == id) + { + chosen.emplace_back(itr->second); + } + } + ++itr; + } + if (chosen.empty()) + return nullptr; + size_t idx = 0; + if (chosen.size() >= 2) + { + idx = rand() % chosen.size(); + } + return chosen[idx]; + } + Path_ptr PathSet::GetByEndpointWithID(RouterID ep, PathID_t id) const { diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 10215e9df..3c9a205da 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -230,6 +230,9 @@ namespace llarp Path_ptr GetNewestPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const; + Path_ptr + GetRandomPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const; + Path_ptr GetPathByID(PathID_t id) const; diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index 1415381c0..febc15305 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -74,7 +74,7 @@ namespace llarp f->T = currentConvoTag; f->S = ++sequenceNo; - auto path = m_PathSet->GetNewestPathByRouter(remoteIntro.router); + auto path = m_PathSet->GetRandomPathByRouter(remoteIntro.router); if (!path) { LogError(m_Endpoint->Name(), " cannot encrypt and send: no path for intro ", remoteIntro);