lokinet/llarp/pathbuilder.cpp

97 lines
2.4 KiB
C++
Raw Normal View History

2018-06-18 22:03:50 +00:00
#include <llarp/nodedb.h>
#include <llarp/path.hpp>
#include "pathbuilder.hpp"
#include "router.hpp"
namespace llarp
{
PathHopConfig::PathHopConfig()
{
llarp_rc_clear(&router);
}
PathHopConfig::~PathHopConfig()
{
llarp_rc_free(&router);
}
void
pathbuilder_generated_keys(
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx)
{
2018-06-20 12:34:48 +00:00
auto remote = ctx->path->Upstream();
llarp::Debug("Generated LRCM to", remote);
2018-06-19 17:11:24 +00:00
auto router = ctx->user->router;
2018-06-20 12:34:48 +00:00
if(!router->SendToOrQueue(remote, ctx->LRCM))
{
llarp::Error("failed to send LRCM");
return;
}
2018-06-19 17:11:24 +00:00
ctx->path->status = ePathBuilding;
router->paths.AddOwnPath(ctx->path);
ctx->user->pathBuildStarted(ctx->user);
2018-06-18 22:03:50 +00:00
}
void
pathbuilder_start_build(void* user)
{
llarp_pathbuild_job* job = static_cast< llarp_pathbuild_job* >(user);
2018-06-19 17:11:24 +00:00
// select hops
2018-06-20 12:34:48 +00:00
size_t idx = 0;
llarp_rc* prev = nullptr;
2018-06-18 22:03:50 +00:00
while(idx < job->hops.numHops)
{
2018-06-20 12:34:48 +00:00
llarp_rc* rc = &job->hops.hops[idx].router;
2018-06-19 17:11:24 +00:00
llarp_rc_clear(rc);
2018-06-20 12:34:48 +00:00
job->selectHop(job->router->nodedb, prev, rc, idx);
prev = rc;
2018-06-18 22:03:50 +00:00
++idx;
}
// async generate keys
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx =
new AsyncPathKeyExchangeContext< llarp_pathbuild_job >(
&job->router->crypto);
ctx->AsyncGenerateKeys(new Path(&job->hops), job->router->logic,
job->router->tp, job, &pathbuilder_generated_keys);
}
} // namespace llarp
llarp_pathbuilder_context::llarp_pathbuilder_context(
llarp_router* p_router, struct llarp_dht_context* p_dht)
2018-06-20 12:34:48 +00:00
: router(p_router), dht(p_dht)
2018-06-18 22:03:50 +00:00
{
}
2018-06-18 22:05:02 +00:00
extern "C" {
struct llarp_pathbuilder_context*
llarp_pathbuilder_context_new(struct llarp_router* router,
struct llarp_dht_context* dht)
2018-06-18 22:03:50 +00:00
{
2018-06-18 22:05:02 +00:00
return new llarp_pathbuilder_context(router, dht);
}
2018-06-18 22:03:50 +00:00
2018-06-18 22:05:02 +00:00
void
llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx)
{
delete ctx;
}
2018-06-18 22:03:50 +00:00
2018-06-18 22:05:02 +00:00
void
llarp_pathbuilder_build_path(struct llarp_pathbuild_job* job)
{
2018-06-19 09:19:23 +00:00
if (!job->context)
{
llarp::Error("failed to build path because no context is set in job");
return;
}
2018-06-18 22:05:02 +00:00
job->router = job->context->router;
2018-06-19 17:11:24 +00:00
if(job->selectHop == nullptr)
job->selectHop = &llarp_nodedb_select_random_hop;
2018-06-18 22:05:02 +00:00
llarp_logic_queue_job(job->router->logic,
{job, &llarp::pathbuilder_start_build});
}
2018-06-19 09:19:23 +00:00
}