2021-03-09 22:24:35 +00:00
|
|
|
#include "pathbuilder.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
#include <llarp/messages/relay_commit.hpp>
|
|
|
|
#include <llarp/nodedb.hpp>
|
|
|
|
#include "path_context.hpp"
|
2022-07-16 00:41:14 +00:00
|
|
|
#include "util/logging.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/profiling.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
2021-06-06 10:56:14 +00:00
|
|
|
#include <llarp/router/i_rc_lookup_handler.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
|
|
|
#include <llarp/tooling/path_event.hpp>
|
2021-06-06 10:56:14 +00:00
|
|
|
#include <llarp/link/link_manager.hpp>
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
#include <functional>
|
2018-06-18 22:03:50 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
auto log_path = log::Cat("path");
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct AsyncPathKeyExchangeContext : std::enable_shared_from_this<AsyncPathKeyExchangeContext>
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
using WorkFunc_t = std::function<void(void)>;
|
|
|
|
using WorkerFunc_t = std::function<void(WorkFunc_t)>;
|
2020-04-07 18:38:56 +00:00
|
|
|
using Path_t = path::Path_ptr;
|
|
|
|
using PathSet_t = path::PathSet_ptr;
|
2019-04-23 14:28:59 +00:00
|
|
|
PathSet_t pathset = nullptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
Path_t path = nullptr;
|
|
|
|
using Handler = std::function<void(std::shared_ptr<AsyncPathKeyExchangeContext>)>;
|
2018-10-09 12:06:30 +00:00
|
|
|
|
|
|
|
Handler result;
|
2020-04-07 18:38:56 +00:00
|
|
|
size_t idx = 0;
|
2019-07-09 13:47:24 +00:00
|
|
|
AbstractRouter* router = nullptr;
|
2020-06-11 11:44:02 +00:00
|
|
|
WorkerFunc_t work;
|
2021-03-02 07:02:59 +00:00
|
|
|
EventLoop_ptr loop;
|
2018-09-19 16:36:12 +00:00
|
|
|
LR_CommitMessage LRCM;
|
2018-06-21 12:55:02 +00:00
|
|
|
|
2019-06-20 16:22:29 +00:00
|
|
|
void
|
|
|
|
GenerateNextKey()
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// current hop
|
2020-04-07 18:38:56 +00:00
|
|
|
auto& hop = path->hops[idx];
|
2019-06-20 16:22:29 +00:00
|
|
|
auto& frame = LRCM.frames[idx];
|
2019-04-18 11:49:54 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
// generate key
|
2019-05-28 19:45:08 +00:00
|
|
|
crypto->encryption_keygen(hop.commkey);
|
2018-06-21 12:55:02 +00:00
|
|
|
hop.nonce.Randomize();
|
|
|
|
// do key exchange
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey, hop.nonce))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogError(pathset->Name(), " Failed to generate shared key for path build");
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-28 22:20:32 +00:00
|
|
|
// generate nonceXOR valueself->hop->pathKey
|
2019-05-28 19:45:08 +00:00
|
|
|
crypto->shorthash(hop.nonceXOR, llarp_buffer_t(hop.shared));
|
2019-06-20 16:22:29 +00:00
|
|
|
++idx;
|
2018-06-21 14:20:14 +00:00
|
|
|
|
2019-06-20 16:22:29 +00:00
|
|
|
bool isFarthestHop = idx == path->hops.size();
|
2018-06-21 14:20:14 +00:00
|
|
|
|
2019-04-17 19:05:54 +00:00
|
|
|
LR_CommitRecord record;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isFarthestHop)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
hop.upstream = hop.rc.pubkey;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
hop.upstream = path->hops[idx].rc.pubkey;
|
|
|
|
record.nextRC = std::make_unique<RouterContact>(path->hops[idx].rc);
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-21 15:46:35 +00:00
|
|
|
// build record
|
2020-04-07 18:38:56 +00:00
|
|
|
record.lifetime = path::default_lifetime;
|
2022-05-26 15:59:44 +00:00
|
|
|
record.version = llarp::constants::proto_version;
|
2020-04-07 18:38:56 +00:00
|
|
|
record.txid = hop.txID;
|
|
|
|
record.rxid = hop.rxID;
|
2018-06-21 15:46:35 +00:00
|
|
|
record.tunnelNonce = hop.nonce;
|
2020-04-07 18:38:56 +00:00
|
|
|
record.nextHop = hop.upstream;
|
|
|
|
record.commkey = seckey_topublic(hop.commkey);
|
2018-06-21 15:46:35 +00:00
|
|
|
|
2019-02-19 15:06:39 +00:00
|
|
|
llarp_buffer_t buf(frame.data(), frame.size());
|
|
|
|
buf.cur = buf.base + EncryptedFrameOverheadSize;
|
2018-06-21 15:46:35 +00:00
|
|
|
// encode record
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!record.BEncode(&buf))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
// failed to encode?
|
2019-06-20 16:22:29 +00:00
|
|
|
LogError(pathset->Name(), " Failed to generate Commit Record");
|
2019-02-19 15:06:39 +00:00
|
|
|
DumpBuffer(buf);
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-02 01:04:04 +00:00
|
|
|
// use ephemeral keypair for frame
|
2018-06-21 12:55:02 +00:00
|
|
|
SecretKey framekey;
|
2019-05-28 19:45:08 +00:00
|
|
|
crypto->encryption_keygen(framekey);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!frame.EncryptInPlace(framekey, hop.rc.enckey))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2019-06-20 16:22:29 +00:00
|
|
|
LogError(pathset->Name(), " Failed to encrypt LRCR");
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isFarthestHop)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// farthest hop
|
2019-05-15 14:55:01 +00:00
|
|
|
// TODO: encrypt junk frames because our public keys are not eligator
|
2021-05-02 21:52:29 +00:00
|
|
|
loop->call([self = shared_from_this()] {
|
|
|
|
self->result(self);
|
|
|
|
self->result = nullptr;
|
|
|
|
});
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// next hop
|
2021-03-02 07:02:59 +00:00
|
|
|
work([self = shared_from_this()] { self->GenerateNextKey(); });
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
/// Generate all keys asynchronously and call handler when done
|
2018-06-21 12:55:02 +00:00
|
|
|
void
|
2021-03-02 07:02:59 +00:00
|
|
|
AsyncGenerateKeys(Path_t p, EventLoop_ptr l, WorkerFunc_t worker, Handler func)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
path = p;
|
2021-03-02 07:02:59 +00:00
|
|
|
loop = std::move(l);
|
2018-06-21 12:55:02 +00:00
|
|
|
result = func;
|
2020-06-11 11:44:02 +00:00
|
|
|
work = worker;
|
2018-06-21 12:55:02 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t i = 0; i < path::max_len; ++i)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2019-04-25 23:21:19 +00:00
|
|
|
LRCM.frames[i].Randomize();
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2021-03-02 07:02:59 +00:00
|
|
|
work([self = shared_from_this()] { self->GenerateNextKey(); });
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
PathBuilderKeysGenerated(std::shared_ptr<AsyncPathKeyExchangeContext> ctx)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2021-05-02 21:52:29 +00:00
|
|
|
if (ctx->pathset->IsStopped())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx->router->NotifyRouterEvent<tooling::PathAttemptEvent>(ctx->router->pubkey(), ctx->path);
|
|
|
|
|
|
|
|
ctx->router->pathContext().AddOwnPath(ctx->pathset, ctx->path);
|
|
|
|
ctx->pathset->PathBuildStarted(ctx->path);
|
|
|
|
|
|
|
|
const RouterID remote = ctx->path->Upstream();
|
|
|
|
auto sentHandler = [router = ctx->router, path = ctx->path](auto status) {
|
|
|
|
if (status != SendStatus::Success)
|
2020-11-02 13:06:46 +00:00
|
|
|
{
|
2021-05-02 21:52:29 +00:00
|
|
|
path->EnterState(path::ePathFailed, router->Now());
|
2020-11-02 13:06:46 +00:00
|
|
|
}
|
2021-05-02 21:52:29 +00:00
|
|
|
};
|
|
|
|
if (ctx->router->SendToOrQueue(remote, ctx->LRCM, sentHandler))
|
|
|
|
{
|
|
|
|
// persist session with router until this path is done
|
|
|
|
if (ctx->path)
|
|
|
|
ctx->router->PersistSessionUntil(remote, ctx->path->ExpireTime());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError(ctx->pathset->Name(), " failed to queue LRCM to ", remote);
|
|
|
|
sentHandler(SendStatus::NoLink);
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
namespace path
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2021-05-05 12:21:39 +00:00
|
|
|
bool
|
|
|
|
BuildLimiter::Attempt(const RouterID& router)
|
|
|
|
{
|
|
|
|
return m_EdgeLimiter.Insert(router);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BuildLimiter::Decay(llarp_time_t now)
|
|
|
|
{
|
|
|
|
m_EdgeLimiter.Decay(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BuildLimiter::Limited(const RouterID& router) const
|
|
|
|
{
|
|
|
|
return m_EdgeLimiter.Contains(router);
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:22:29 +00:00
|
|
|
Builder::Builder(AbstractRouter* p_router, size_t pathNum, size_t hops)
|
2021-05-05 12:21:39 +00:00
|
|
|
: path::PathSet{pathNum}, _run{true}, m_router{p_router}, numHops{hops}
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
CryptoManager::instance()->encryption_keygen(enckey);
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2019-05-07 17:46:38 +00:00
|
|
|
void
|
|
|
|
Builder::ResetInternalState()
|
|
|
|
{
|
2021-03-31 10:54:28 +00:00
|
|
|
buildIntervalLimit = PATH_BUILD_RATE;
|
2020-04-07 18:38:56 +00:00
|
|
|
lastBuild = 0s;
|
2019-05-07 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:11:47 +00:00
|
|
|
void
|
|
|
|
Builder::Tick(llarp_time_t now)
|
2019-04-23 16:13:22 +00:00
|
|
|
{
|
2022-03-28 22:11:47 +00:00
|
|
|
PathSet::Tick(now);
|
|
|
|
now = llarp::time_now_ms();
|
2021-06-03 15:57:18 +00:00
|
|
|
m_router->pathBuildLimiter().Decay(now);
|
|
|
|
|
2019-11-07 18:23:06 +00:00
|
|
|
ExpirePaths(now, m_router);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ShouldBuildMore(now))
|
2019-04-23 16:13:22 +00:00
|
|
|
BuildOne();
|
2019-11-05 16:58:53 +00:00
|
|
|
TickPaths(m_router);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_BuildStats.attempts > 50)
|
2019-07-01 13:44:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_BuildStats.SuccessRatio() <= BuildStats::MinGoodRatio && now - m_LastWarn > 5s)
|
2019-07-01 14:56:56 +00:00
|
|
|
{
|
|
|
|
LogWarn(Name(), " has a low path build success. ", m_BuildStats);
|
|
|
|
m_LastWarn = now;
|
|
|
|
}
|
2019-07-01 13:44:25 +00:00
|
|
|
}
|
2019-04-23 16:13:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
Builder::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2021-03-05 17:31:52 +00:00
|
|
|
util::StatusObject obj{
|
|
|
|
{"buildStats", m_BuildStats.ExtractStatus()},
|
2021-06-02 19:14:21 +00:00
|
|
|
{"numHops", uint64_t{numHops}},
|
|
|
|
{"numPaths", uint64_t{numDesiredPaths}}};
|
2020-04-07 18:38:56 +00:00
|
|
|
std::transform(
|
|
|
|
m_Paths.begin(),
|
|
|
|
m_Paths.end(),
|
|
|
|
std::back_inserter(obj["paths"]),
|
|
|
|
[](const auto& item) -> util::StatusObject { return item.second->ExtractStatus(); });
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::optional<RouterContact>
|
2021-02-18 17:20:58 +00:00
|
|
|
Builder::SelectFirstHop(const std::set<RouterID>& exclude) const
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
std::optional<RouterContact> found = std::nullopt;
|
|
|
|
m_router->ForEachPeer(
|
|
|
|
[&](const ILinkSession* s, bool isOutbound) {
|
|
|
|
if (s && s->IsEstablished() && isOutbound && not found.has_value())
|
|
|
|
{
|
|
|
|
const RouterContact rc = s->GetRemoteRC();
|
|
|
|
#ifndef TESTNET
|
|
|
|
if (m_router->IsBootstrapNode(rc.pubkey))
|
|
|
|
return;
|
2019-09-05 20:20:55 +00:00
|
|
|
#endif
|
2021-02-18 13:28:53 +00:00
|
|
|
if (exclude.count(rc.pubkey))
|
|
|
|
return;
|
|
|
|
|
2021-05-05 12:21:39 +00:00
|
|
|
if (BuildCooldownHit(rc.pubkey))
|
2021-03-31 10:54:28 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-02 19:32:05 +00:00
|
|
|
if (m_router->routerProfiling().IsBadForPath(rc.pubkey))
|
|
|
|
return;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
found = rc;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
return found;
|
|
|
|
}
|
2019-06-20 16:22:29 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::optional<std::vector<RouterContact>>
|
|
|
|
Builder::GetHopsForBuild()
|
|
|
|
{
|
2021-02-03 19:49:33 +00:00
|
|
|
auto filter = [r = m_router](const auto& rc) -> bool {
|
2021-06-02 19:32:05 +00:00
|
|
|
return not r->routerProfiling().IsBadForPath(rc.pubkey, 1);
|
2021-02-03 19:49:33 +00:00
|
|
|
};
|
2021-02-03 21:38:31 +00:00
|
|
|
if (const auto maybe = m_router->nodedb()->GetRandom(filter))
|
2018-10-04 17:51:45 +00:00
|
|
|
{
|
2021-02-03 18:44:43 +00:00
|
|
|
return GetHopsAlignedToForBuild(maybe->pubkey);
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
2021-02-03 18:44:43 +00:00
|
|
|
return std::nullopt;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-08-23 15:19:16 +00:00
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
Builder::Stop()
|
|
|
|
{
|
2019-06-20 16:22:29 +00:00
|
|
|
_run = false;
|
2021-06-02 19:34:42 +00:00
|
|
|
// tell all our paths that they are to be ignored
|
2021-05-12 16:48:24 +00:00
|
|
|
const auto now = Now();
|
|
|
|
for (auto& item : m_Paths)
|
|
|
|
{
|
2021-06-02 19:34:42 +00:00
|
|
|
item.second->EnterState(ePathIgnore, now);
|
2021-05-12 16:48:24 +00:00
|
|
|
}
|
2018-12-24 16:09:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:50:33 +00:00
|
|
|
bool
|
|
|
|
Builder::IsStopped() const
|
|
|
|
{
|
|
|
|
return !_run.load();
|
|
|
|
}
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
Builder::ShouldRemove() const
|
|
|
|
{
|
2021-06-02 19:34:42 +00:00
|
|
|
return IsStopped() and NumInStatus(ePathEstablished) == 0;
|
2018-12-24 16:09:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
const SecretKey&
|
2018-08-30 18:48:43 +00:00
|
|
|
Builder::GetTunnelEncryptionSecretKey() const
|
|
|
|
{
|
|
|
|
return enckey;
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2021-03-31 10:54:28 +00:00
|
|
|
bool
|
|
|
|
Builder::BuildCooldownHit(RouterID edge) const
|
|
|
|
{
|
2021-05-05 12:21:39 +00:00
|
|
|
return m_router->pathBuildLimiter().Limited(edge);
|
2021-03-31 10:54:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 12:00:28 +00:00
|
|
|
bool
|
|
|
|
Builder::BuildCooldownHit(llarp_time_t now) const
|
|
|
|
{
|
2020-03-01 15:58:08 +00:00
|
|
|
return now < lastBuild + buildIntervalLimit;
|
2018-12-27 12:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
2018-10-29 16:48:36 +00:00
|
|
|
Builder::ShouldBuildMore(llarp_time_t now) const
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (IsStopped())
|
2019-04-16 11:44:55 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (BuildCooldownHit(now))
|
2019-11-05 16:58:53 +00:00
|
|
|
return false;
|
|
|
|
return PathSet::ShouldBuildMore(now);
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-08-12 17:22:29 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::BuildOne(PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2021-04-12 11:39:07 +00:00
|
|
|
if (const auto maybe = GetHopsForBuild())
|
2021-02-02 14:35:40 +00:00
|
|
|
Build(*maybe, roles);
|
2018-09-28 15:46:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 22:23:14 +00:00
|
|
|
bool
|
|
|
|
Builder::UrgentBuild(llarp_time_t) const
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
|
|
|
return buildIntervalLimit > MIN_PATH_BUILD_INTERVAL * 4;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::optional<std::vector<RouterContact>>
|
2021-02-18 17:20:58 +00:00
|
|
|
Builder::GetHopsAlignedToForBuild(RouterID endpoint, const std::set<RouterID>& exclude)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2021-02-18 13:28:53 +00:00
|
|
|
const auto pathConfig = m_router->GetConfig()->paths;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::vector<RouterContact> hops;
|
2019-06-20 16:22:29 +00:00
|
|
|
{
|
2021-02-18 13:28:53 +00:00
|
|
|
const auto maybe = SelectFirstHop(exclude);
|
2021-02-02 14:35:40 +00:00
|
|
|
if (not maybe.has_value())
|
2021-04-12 11:39:07 +00:00
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
log::warning(log_path, "{} has no first hop candidate", Name());
|
2021-02-02 14:35:40 +00:00
|
|
|
return std::nullopt;
|
2021-04-12 11:39:07 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
hops.emplace_back(*maybe);
|
|
|
|
};
|
2021-02-18 13:28:53 +00:00
|
|
|
|
|
|
|
RouterContact endpointRC;
|
|
|
|
if (const auto maybe = m_router->nodedb()->Get(endpoint))
|
|
|
|
{
|
|
|
|
endpointRC = *maybe;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
for (size_t idx = hops.size(); idx < numHops; ++idx)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
if (idx + 1 == numHops)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2021-02-18 13:28:53 +00:00
|
|
|
hops.emplace_back(endpointRC);
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
2019-06-20 16:22:29 +00:00
|
|
|
else
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2021-02-18 13:28:53 +00:00
|
|
|
auto filter =
|
|
|
|
[&hops, r = m_router, endpointRC, pathConfig, exclude](const auto& rc) -> bool {
|
|
|
|
if (exclude.count(rc.pubkey))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::set<RouterContact> hopsSet;
|
2021-02-18 17:20:58 +00:00
|
|
|
hopsSet.insert(endpointRC);
|
|
|
|
hopsSet.insert(hops.begin(), hops.end());
|
2021-02-18 13:28:53 +00:00
|
|
|
|
2021-06-02 19:32:05 +00:00
|
|
|
if (r->routerProfiling().IsBadForPath(rc.pubkey, 1))
|
2021-02-18 13:28:53 +00:00
|
|
|
return false;
|
|
|
|
for (const auto& hop : hopsSet)
|
|
|
|
{
|
|
|
|
if (hop.pubkey == rc.pubkey)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-18 17:20:58 +00:00
|
|
|
hopsSet.insert(rc);
|
2021-04-12 11:38:12 +00:00
|
|
|
#ifndef TESTNET
|
2021-02-18 13:28:53 +00:00
|
|
|
if (not pathConfig.Acceptable(hopsSet))
|
|
|
|
return false;
|
2021-04-12 11:38:12 +00:00
|
|
|
#endif
|
2021-02-18 13:28:53 +00:00
|
|
|
return rc.pubkey != endpointRC.pubkey;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (const auto maybe = m_router->nodedb()->GetRandom(filter))
|
|
|
|
hops.emplace_back(*maybe);
|
|
|
|
else
|
2021-02-02 14:35:40 +00:00
|
|
|
return std::nullopt;
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
return hops;
|
2019-06-20 16:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Builder::BuildOneAlignedTo(const RouterID remote)
|
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
if (const auto maybe = GetHopsAlignedToForBuild(remote); maybe.has_value())
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
LogInfo(Name(), " building path to ", remote);
|
|
|
|
Build(*maybe);
|
|
|
|
return true;
|
2019-06-20 16:22:29 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
return false;
|
2018-09-28 15:46:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
|
|
|
Builder::Now() const
|
|
|
|
{
|
2019-08-02 09:27:27 +00:00
|
|
|
return m_router->Now();
|
2018-10-29 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 15:46:47 +00:00
|
|
|
void
|
2021-02-02 14:35:40 +00:00
|
|
|
Builder::Build(std::vector<RouterContact> hops, PathRole roles)
|
2018-09-28 15:46:47 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (IsStopped())
|
2018-12-24 16:09:05 +00:00
|
|
|
return;
|
2021-04-12 11:39:07 +00:00
|
|
|
lastBuild = Now();
|
2021-03-31 10:54:28 +00:00
|
|
|
const RouterID edge{hops[0].pubkey};
|
2021-05-05 12:21:39 +00:00
|
|
|
if (not m_router->pathBuildLimiter().Attempt(edge))
|
2021-03-31 10:54:28 +00:00
|
|
|
{
|
|
|
|
LogWarn(Name(), " building too fast to edge router ", edge);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
// async generate keys
|
2020-04-07 18:38:56 +00:00
|
|
|
auto ctx = std::make_shared<AsyncPathKeyExchangeContext>();
|
|
|
|
ctx->router = m_router;
|
|
|
|
auto self = GetSelf();
|
2019-11-05 16:58:53 +00:00
|
|
|
ctx->pathset = self;
|
2020-02-20 22:20:17 +00:00
|
|
|
std::string path_shortName = "[path " + m_router->ShortName() + "-";
|
2020-04-07 18:38:56 +00:00
|
|
|
path_shortName = path_shortName + std::to_string(m_router->NextPathBuildNumber()) + "]";
|
2021-06-04 12:34:41 +00:00
|
|
|
auto path = std::make_shared<path::Path>(hops, GetWeak(), roles, std::move(path_shortName));
|
2020-02-20 21:37:39 +00:00
|
|
|
LogInfo(Name(), " build ", path->ShortName(), ": ", path->HopsString());
|
2020-02-28 04:01:14 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); });
|
|
|
|
ctx->AsyncGenerateKeys(
|
2020-06-11 11:44:02 +00:00
|
|
|
path,
|
2021-03-02 07:02:59 +00:00
|
|
|
m_router->loop(),
|
2020-06-11 11:44:02 +00:00
|
|
|
[r = m_router](auto func) { r->QueueWork(std::move(func)); },
|
|
|
|
&PathBuilderKeysGenerated);
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-09-26 13:04:25 +00:00
|
|
|
void
|
2019-04-23 14:28:59 +00:00
|
|
|
Builder::HandlePathBuilt(Path_ptr p)
|
2018-09-26 13:04:25 +00:00
|
|
|
{
|
2021-03-31 10:54:28 +00:00
|
|
|
buildIntervalLimit = PATH_BUILD_RATE;
|
2019-08-02 09:27:27 +00:00
|
|
|
m_router->routerProfiling().MarkPathSuccess(p.get());
|
2020-02-29 22:53:54 +00:00
|
|
|
|
2022-10-27 00:20:14 +00:00
|
|
|
LogInfo(p->Name(), " built latency=", ToString(p->intro.latency));
|
2019-07-01 13:44:25 +00:00
|
|
|
m_BuildStats.success++;
|
2018-09-26 13:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-31 10:54:28 +00:00
|
|
|
Builder::HandlePathBuildFailedAt(Path_ptr p, RouterID edge)
|
2019-07-31 12:51:24 +00:00
|
|
|
{
|
2021-03-31 10:54:28 +00:00
|
|
|
PathSet::HandlePathBuildFailedAt(p, edge);
|
2019-07-31 12:51:24 +00:00
|
|
|
DoPathBuildBackoff();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Builder::DoPathBuildBackoff()
|
2018-09-26 13:04:25 +00:00
|
|
|
{
|
2020-02-25 17:05:13 +00:00
|
|
|
static constexpr std::chrono::milliseconds MaxBuildInterval = 30s;
|
2018-09-26 13:04:25 +00:00
|
|
|
// linear backoff
|
2021-03-31 10:54:28 +00:00
|
|
|
buildIntervalLimit = std::min(PATH_BUILD_RATE + buildIntervalLimit, MaxBuildInterval);
|
2022-10-27 00:20:14 +00:00
|
|
|
LogWarn(Name(), " build interval is now ", ToString(buildIntervalLimit));
|
2019-07-31 12:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Builder::HandlePathBuildTimeout(Path_ptr p)
|
|
|
|
{
|
2020-10-27 21:34:09 +00:00
|
|
|
m_router->routerProfiling().MarkPathTimeout(p.get());
|
2018-09-26 13:04:25 +00:00
|
|
|
PathSet::HandlePathBuildTimeout(p);
|
2019-07-31 12:51:24 +00:00
|
|
|
DoPathBuildBackoff();
|
2021-06-04 15:36:57 +00:00
|
|
|
for (const auto& hop : p->hops)
|
|
|
|
{
|
2021-06-06 10:56:14 +00:00
|
|
|
const RouterID router{hop.rc.pubkey};
|
|
|
|
// look up router and see if it's still on the network
|
|
|
|
m_router->loop()->call_soon([router, r = m_router]() {
|
|
|
|
LogInfo("looking up ", router, " because of path build timeout");
|
|
|
|
r->rcLookupHandler().GetRC(
|
|
|
|
router,
|
|
|
|
[r](const auto& router, const auto* rc, auto result) {
|
|
|
|
if (result == RCRequestResult::Success && rc != nullptr)
|
|
|
|
{
|
|
|
|
LogInfo("refreshed rc for ", router);
|
|
|
|
r->nodedb()->PutIfNewer(*rc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remove all connections to this router as it's probably not registered anymore
|
|
|
|
LogWarn("removing router ", router, " because of path build timeout");
|
|
|
|
r->linkManager().DeregisterPeer(router);
|
|
|
|
r->nodedb()->Remove(router);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
});
|
2021-06-04 15:36:57 +00:00
|
|
|
}
|
2018-09-26 13:04:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::ManualRebuild(size_t num, PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2019-03-22 14:10:30 +00:00
|
|
|
LogDebug(Name(), " manual rebuild ", num);
|
2020-04-07 18:38:56 +00:00
|
|
|
while (num--)
|
2018-11-14 18:02:27 +00:00
|
|
|
BuildOne(roles);
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
} // namespace path
|
|
|
|
} // namespace llarp
|