2019-01-11 01:19:36 +00:00
|
|
|
#include <path/pathbuilder.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <messages/relay_commit.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <nodedb.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <path/path.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <profiling.hpp>
|
|
|
|
#include <router/abstractrouter.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <util/logic.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
|
|
|
|
{
|
2018-06-21 12:55:02 +00:00
|
|
|
template < typename User >
|
|
|
|
struct AsyncPathKeyExchangeContext
|
|
|
|
{
|
2019-04-23 14:28:59 +00:00
|
|
|
typedef path::Path_ptr Path_t;
|
2019-04-23 16:13:22 +00:00
|
|
|
typedef path::PathSet_ptr PathSet_t;
|
2019-04-23 14:28:59 +00:00
|
|
|
PathSet_t pathset = nullptr;
|
|
|
|
Path_t path = nullptr;
|
2018-10-09 12:06:30 +00:00
|
|
|
typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler;
|
|
|
|
User* user = nullptr;
|
|
|
|
|
|
|
|
Handler result;
|
2019-05-22 16:20:50 +00:00
|
|
|
size_t idx = 0;
|
|
|
|
AbstractRouter* router = nullptr;
|
|
|
|
llarp_threadpool* worker = nullptr;
|
|
|
|
std::shared_ptr< Logic > logic = nullptr;
|
2018-09-19 16:36:12 +00:00
|
|
|
LR_CommitMessage LRCM;
|
2018-06-21 12:55:02 +00:00
|
|
|
|
2019-02-05 14:50:33 +00:00
|
|
|
~AsyncPathKeyExchangeContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
static void
|
|
|
|
HandleDone(void* u)
|
|
|
|
{
|
|
|
|
AsyncPathKeyExchangeContext< User >* ctx =
|
|
|
|
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
|
|
|
|
ctx->result(ctx);
|
2018-10-09 12:06:30 +00:00
|
|
|
delete ctx;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GenerateNextKey(void* u)
|
|
|
|
{
|
|
|
|
AsyncPathKeyExchangeContext< User >* ctx =
|
|
|
|
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
|
|
|
|
|
2018-06-21 14:20:14 +00:00
|
|
|
// current hop
|
2018-06-21 12:55:02 +00:00
|
|
|
auto& hop = ctx->path->hops[ctx->idx];
|
2018-09-19 16:36:12 +00:00
|
|
|
auto& frame = ctx->LRCM.frames[ctx->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
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey, hop.nonce))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2019-03-22 14:10:30 +00:00
|
|
|
LogError(ctx->pathset->Name(),
|
|
|
|
" Failed to generate shared key for path build");
|
2018-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
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));
|
2018-06-21 12:55:02 +00:00
|
|
|
++ctx->idx;
|
2018-06-21 14:20:14 +00:00
|
|
|
|
2018-06-22 13:59:28 +00:00
|
|
|
bool isFarthestHop = ctx->idx == ctx->path->hops.size();
|
2018-06-21 14:20:14 +00:00
|
|
|
|
2019-04-17 19:05:54 +00:00
|
|
|
LR_CommitRecord record;
|
2018-06-21 14:20:14 +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
|
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
hop.upstream = ctx->path->hops[ctx->idx].rc.pubkey;
|
2019-04-23 16:13:22 +00:00
|
|
|
record.nextRC =
|
|
|
|
std::make_unique< RouterContact >(ctx->path->hops[ctx->idx].rc);
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-21 15:46:35 +00:00
|
|
|
// build record
|
2019-04-17 19:05:54 +00:00
|
|
|
|
2018-06-21 15:46:35 +00:00
|
|
|
record.version = LLARP_PROTO_VERSION;
|
2018-06-22 12:45:46 +00:00
|
|
|
record.txid = hop.txID;
|
|
|
|
record.rxid = hop.rxID;
|
2018-06-21 15:46:35 +00:00
|
|
|
record.tunnelNonce = hop.nonce;
|
|
|
|
record.nextHop = hop.upstream;
|
2019-02-18 10:35:16 +00:00
|
|
|
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
|
2019-02-19 15:06:39 +00:00
|
|
|
if(!record.BEncode(&buf))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
// failed to encode?
|
2019-03-22 14:10:30 +00:00
|
|
|
LogError(ctx->pathset->Name(), " Failed to generate Commit Record");
|
2019-02-19 15:06:39 +00:00
|
|
|
DumpBuffer(buf);
|
2018-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
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);
|
|
|
|
if(!frame.EncryptInPlace(framekey, hop.rc.enckey))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2019-03-22 14:10:30 +00:00
|
|
|
LogError(ctx->pathset->Name(), " Failed to encrypt LRCR");
|
2018-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-21 14:20:14 +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
|
2018-12-10 14:14:55 +00:00
|
|
|
ctx->logic->queue_job({ctx, &HandleDone});
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// next hop
|
|
|
|
llarp_threadpool_queue_job(ctx->worker, {ctx, &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
|
2019-05-22 16:20:50 +00:00
|
|
|
AsyncGenerateKeys(Path_t p, std::shared_ptr< Logic > l,
|
|
|
|
llarp_threadpool* pool, User* u, Handler func)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
path = p;
|
|
|
|
logic = l;
|
|
|
|
user = u;
|
|
|
|
result = func;
|
|
|
|
worker = pool;
|
|
|
|
|
2019-04-25 23:21:19 +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
|
|
|
}
|
|
|
|
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
static void
|
|
|
|
PathBuilderKeysGenerated(AsyncPathKeyExchangeContext< path::Builder >* ctx)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2019-02-05 14:50:33 +00:00
|
|
|
if(!ctx->pathset->IsStopped())
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
2018-12-24 16:21:15 +00:00
|
|
|
RouterID remote = ctx->path->Upstream();
|
|
|
|
const ILinkMessage* msg = &ctx->LRCM;
|
|
|
|
if(ctx->router->SendToOrQueue(remote, msg))
|
|
|
|
{
|
|
|
|
// persist session with router until this path is done
|
|
|
|
ctx->router->PersistSessionUntil(remote, ctx->path->ExpireTime());
|
|
|
|
// add own path
|
2019-02-11 19:45:42 +00:00
|
|
|
ctx->router->pathContext().AddOwnPath(ctx->pathset, ctx->path);
|
2018-12-24 16:21:15 +00:00
|
|
|
}
|
|
|
|
else
|
2019-03-22 14:10:30 +00:00
|
|
|
LogError(ctx->pathset->Name(), " failed to send LRCM to ", remote);
|
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
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
Builder::Builder(AbstractRouter* p_router, struct llarp_dht_context* p_dht,
|
2018-08-30 18:48:43 +00:00
|
|
|
size_t pathNum, size_t hops)
|
2019-02-18 10:35:16 +00:00
|
|
|
: path::PathSet(pathNum), router(p_router), dht(p_dht), numHops(hops)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
CryptoManager::instance()->encryption_keygen(enckey);
|
2018-12-24 16:09:05 +00:00
|
|
|
_run.store(true);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
Builder::~Builder()
|
|
|
|
{
|
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2019-05-07 17:46:38 +00:00
|
|
|
void
|
|
|
|
Builder::ResetInternalState()
|
|
|
|
{
|
|
|
|
buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
|
2019-05-08 12:17:48 +00:00
|
|
|
lastBuild = 0;
|
2019-05-07 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
void
|
|
|
|
Builder::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
ExpirePaths(now);
|
|
|
|
if(ShouldBuildMore(now))
|
|
|
|
BuildOne();
|
|
|
|
TickPaths(now, router);
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
Builder::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-04-23 16:13:22 +00:00
|
|
|
util::StatusObject obj{{"numHops", uint64_t(numHops)},
|
2019-02-11 17:14:43 +00:00
|
|
|
{"numPaths", uint64_t(m_NumPaths)}};
|
|
|
|
std::vector< util::StatusObject > pathObjs;
|
|
|
|
std::transform(m_Paths.begin(), m_Paths.end(),
|
|
|
|
std::back_inserter(pathObjs),
|
|
|
|
[](const auto& item) -> util::StatusObject {
|
|
|
|
return item.second->ExtractStatus();
|
|
|
|
});
|
|
|
|
obj.Put("paths", pathObjs);
|
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
2019-05-08 14:01:31 +00:00
|
|
|
Builder::SelectHop(llarp_nodedb* db, const std::set< RouterID >& exclude,
|
2018-11-14 18:02:27 +00:00
|
|
|
RouterContact& cur, size_t hop, PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2018-11-15 13:47:46 +00:00
|
|
|
(void)roles;
|
2019-04-05 14:58:22 +00:00
|
|
|
size_t tries = 10;
|
2019-04-03 19:05:44 +00:00
|
|
|
if(hop == 0)
|
|
|
|
{
|
|
|
|
if(router->NumberOfConnectedRouters() == 0)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
|
|
|
// persist connection
|
|
|
|
router->ConnectToRandomRouters(1);
|
2019-04-03 19:05:44 +00:00
|
|
|
return false;
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
2019-04-03 19:05:44 +00:00
|
|
|
bool got = false;
|
2019-04-05 14:58:22 +00:00
|
|
|
router->ForEachPeer(
|
2019-05-07 13:04:43 +00:00
|
|
|
[&](const ILinkSession* s, bool isOutbound) {
|
|
|
|
if(s && s->IsEstablished() && isOutbound && !got)
|
|
|
|
{
|
|
|
|
const RouterContact rc = s->GetRemoteRC();
|
2019-05-16 18:55:12 +00:00
|
|
|
if(got || exclude.count(rc.pubkey))
|
2019-05-07 13:04:43 +00:00
|
|
|
return;
|
|
|
|
cur = rc;
|
|
|
|
got = true;
|
|
|
|
}
|
2019-04-05 14:58:22 +00:00
|
|
|
},
|
|
|
|
true);
|
2019-04-03 19:05:44 +00:00
|
|
|
return got;
|
|
|
|
}
|
2018-10-04 17:51:45 +00:00
|
|
|
do
|
|
|
|
{
|
2019-03-11 13:58:31 +00:00
|
|
|
cur.Clear();
|
2018-10-04 17:51:45 +00:00
|
|
|
--tries;
|
2019-05-08 14:01:31 +00:00
|
|
|
std::set< RouterID > excluding = exclude;
|
|
|
|
if(db->select_random_hop_excluding(cur, excluding))
|
2019-03-11 13:58:31 +00:00
|
|
|
{
|
2019-05-08 14:18:04 +00:00
|
|
|
excluding.insert(cur.pubkey);
|
2019-04-16 11:44:55 +00:00
|
|
|
if(!router->routerProfiling().IsBadForPath(cur.pubkey))
|
2019-03-11 13:58:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while(tries > 0);
|
2019-05-19 22:11:07 +00:00
|
|
|
return false;
|
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()
|
|
|
|
{
|
|
|
|
_run.store(false);
|
|
|
|
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
|
|
|
|
{
|
2019-04-23 16:13:22 +00:00
|
|
|
return IsStopped();
|
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
|
|
|
|
2018-12-27 12:00:28 +00:00
|
|
|
bool
|
|
|
|
Builder::BuildCooldownHit(llarp_time_t now) const
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
return now < lastBuild || 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
|
|
|
{
|
2019-04-16 11:44:55 +00:00
|
|
|
if(IsStopped())
|
|
|
|
return false;
|
2019-05-06 16:13:41 +00:00
|
|
|
return PathSet::ShouldBuildMore(now) && !BuildCooldownHit(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
|
|
|
{
|
2019-05-08 14:18:04 +00:00
|
|
|
std::vector< RouterContact > hops(numHops);
|
2019-01-29 02:16:31 +00:00
|
|
|
if(SelectHops(router->nodedb(), hops, roles))
|
2018-11-14 18:02:27 +00:00
|
|
|
Build(hops, roles);
|
2018-09-28 15:46:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 12:31:34 +00:00
|
|
|
bool Builder::UrgentBuild(llarp_time_t) const
|
|
|
|
{
|
|
|
|
return buildIntervalLimit > MIN_PATH_BUILD_INTERVAL * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Builder::BuildOneAlignedTo(const RouterID remote)
|
|
|
|
{
|
2019-05-08 14:30:55 +00:00
|
|
|
std::vector< RouterContact > hops(0);
|
2019-05-08 14:01:31 +00:00
|
|
|
std::set< RouterID > routers = {remote};
|
2019-05-07 12:31:34 +00:00
|
|
|
/// if we really need this path build it "dangerously"
|
|
|
|
if(UrgentBuild(router->Now()))
|
|
|
|
{
|
|
|
|
const auto aligned =
|
|
|
|
router->pathContext().FindOwnedPathsWithEndpoint(remote);
|
|
|
|
/// pick the lowest latency path that aligns to remote
|
2019-05-28 19:45:08 +00:00
|
|
|
/// note: peer exhaustion is made worse happen here
|
2019-05-07 12:31:34 +00:00
|
|
|
Path_ptr p;
|
|
|
|
llarp_time_t min = std::numeric_limits< llarp_time_t >::max();
|
|
|
|
for(const auto& path : aligned)
|
|
|
|
{
|
|
|
|
if(path->intro.latency < min && path->hops.size() == numHops)
|
|
|
|
{
|
|
|
|
p = path;
|
|
|
|
min = path->intro.latency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
for(const auto& hop : p->hops)
|
2019-05-08 14:30:55 +00:00
|
|
|
{
|
|
|
|
if(hop.rc.pubkey.IsZero())
|
|
|
|
return false;
|
2019-05-07 12:31:34 +00:00
|
|
|
hops.emplace_back(hop.rc);
|
2019-05-08 14:30:55 +00:00
|
|
|
}
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(hops.size() == 0)
|
|
|
|
{
|
2019-05-07 13:04:43 +00:00
|
|
|
hops.resize(numHops);
|
|
|
|
|
2019-05-07 12:31:34 +00:00
|
|
|
auto nodedb = router->nodedb();
|
2019-05-08 14:18:04 +00:00
|
|
|
for(size_t idx = 0; idx < hops.size(); idx++)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
2019-05-08 14:18:04 +00:00
|
|
|
hops[idx].Clear();
|
2019-05-08 14:30:55 +00:00
|
|
|
if(idx == numHops - 1)
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
|
|
|
// last hop
|
2019-05-08 14:18:04 +00:00
|
|
|
if(!nodedb->Get(remote, hops[idx]))
|
2019-05-07 12:31:34 +00:00
|
|
|
{
|
|
|
|
router->LookupRouter(remote, nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-08 14:30:55 +00:00
|
|
|
if(!SelectHop(nodedb, routers, hops[idx], idx, path::ePathRoleAny))
|
2019-05-08 14:01:31 +00:00
|
|
|
return false;
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
2019-05-08 14:30:55 +00:00
|
|
|
if(hops[idx].pubkey.IsZero())
|
|
|
|
return false;
|
2019-05-08 14:18:04 +00:00
|
|
|
routers.insert(hops[idx].pubkey);
|
2019-05-07 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 13:04:43 +00:00
|
|
|
LogInfo(Name(), " building path to ", remote);
|
2019-05-07 12:31:34 +00:00
|
|
|
Build(hops);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:46:47 +00:00
|
|
|
bool
|
|
|
|
Builder::SelectHops(llarp_nodedb* nodedb,
|
2018-11-14 18:02:27 +00:00
|
|
|
std::vector< RouterContact >& hops, PathRole roles)
|
2018-09-28 15:46:47 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
size_t idx = 0;
|
2019-05-08 14:01:31 +00:00
|
|
|
std::set< RouterID > exclude;
|
2019-05-08 14:18:04 +00:00
|
|
|
while(idx < hops.size())
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2019-05-08 14:18:04 +00:00
|
|
|
hops[idx].Clear();
|
2019-03-22 12:44:15 +00:00
|
|
|
size_t tries = 4;
|
2019-05-08 14:01:31 +00:00
|
|
|
while(tries > 0 && !SelectHop(nodedb, exclude, hops[idx], idx, roles))
|
|
|
|
--tries;
|
2019-05-08 14:30:55 +00:00
|
|
|
if(tries == 0 || hops[idx].pubkey.IsZero())
|
2019-03-22 12:44:15 +00:00
|
|
|
{
|
2019-03-22 14:10:30 +00:00
|
|
|
LogWarn(Name(), " failed to select hop ", idx);
|
2019-03-22 12:44:15 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2019-05-08 14:01:31 +00:00
|
|
|
exclude.insert(hops[idx].pubkey);
|
2018-08-30 18:48:43 +00:00
|
|
|
++idx;
|
|
|
|
}
|
2018-09-28 15:46:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
|
|
|
Builder::Now() const
|
|
|
|
{
|
|
|
|
return router->Now();
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:46:47 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::Build(const std::vector< RouterContact >& hops, PathRole roles)
|
2018-09-28 15:46:47 +00:00
|
|
|
{
|
2019-02-05 14:50:33 +00:00
|
|
|
if(IsStopped())
|
2018-12-24 16:09:05 +00:00
|
|
|
return;
|
2018-10-29 16:48:36 +00:00
|
|
|
lastBuild = Now();
|
2018-08-30 18:48:43 +00:00
|
|
|
// async generate keys
|
|
|
|
AsyncPathKeyExchangeContext< Builder >* ctx =
|
2019-05-28 19:45:08 +00:00
|
|
|
new AsyncPathKeyExchangeContext< Builder >();
|
2018-12-07 13:38:49 +00:00
|
|
|
ctx->router = router;
|
2019-04-23 16:13:22 +00:00
|
|
|
ctx->pathset = GetSelf();
|
|
|
|
auto path = std::make_shared< path::Path >(hops, this, roles);
|
2019-05-08 14:30:55 +00:00
|
|
|
LogInfo(Name(), " build ", path->HopsString());
|
2019-04-23 16:13:22 +00:00
|
|
|
path->SetBuildResultHook(
|
|
|
|
[this](Path_ptr p) { this->HandlePathBuilt(p); });
|
2019-02-11 19:45:42 +00:00
|
|
|
ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this,
|
2018-12-24 16:09:05 +00:00
|
|
|
&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
|
|
|
{
|
|
|
|
buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
|
2019-04-23 14:28:59 +00:00
|
|
|
router->routerProfiling().MarkPathSuccess(p.get());
|
2019-03-22 14:10:30 +00:00
|
|
|
LogInfo(p->Name(), " built latency=", p->intro.latency);
|
2018-09-26 13:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-23 14:28:59 +00:00
|
|
|
Builder::HandlePathBuildTimeout(Path_ptr p)
|
2018-09-26 13:04:25 +00:00
|
|
|
{
|
|
|
|
// linear backoff
|
2019-01-07 17:28:59 +00:00
|
|
|
static constexpr llarp_time_t MaxBuildInterval = 30 * 1000;
|
2019-05-07 12:31:34 +00:00
|
|
|
buildIntervalLimit = std::min(
|
|
|
|
MIN_PATH_BUILD_INTERVAL + buildIntervalLimit, MaxBuildInterval);
|
2019-04-23 14:28:59 +00:00
|
|
|
router->routerProfiling().MarkPathFail(p.get());
|
2018-09-26 13:04:25 +00:00
|
|
|
PathSet::HandlePathBuildTimeout(p);
|
2019-05-06 16:00:10 +00:00
|
|
|
LogWarn(Name(), " build interval is now ", buildIntervalLimit);
|
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);
|
2018-08-30 18:48:43 +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
|