1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/pathbuilder.cpp

246 lines
6.9 KiB
C++
Raw Normal View History

2018-06-19 00:03:50 +02:00
#include <llarp/nodedb.h>
#include <llarp/path.hpp>
2018-07-09 19:32:11 +02:00
#include <llarp/pathbuilder.hpp>
#include "buffer.hpp"
2018-06-19 00:03:50 +02:00
#include "router.hpp"
namespace llarp
{
2018-06-21 14:55:02 +02:00
template < typename User >
struct AsyncPathKeyExchangeContext
{
typedef llarp::path::Path Path_t;
typedef llarp::path::PathSet PathSet_t;
PathSet_t* pathset = nullptr;
Path_t* path = nullptr;
2018-06-21 14:55:02 +02:00
typedef void (*Handler)(AsyncPathKeyExchangeContext< User >*);
User* user = nullptr;
Handler result = nullptr;
size_t idx = 0;
llarp_threadpool* worker = nullptr;
llarp_logic* logic = nullptr;
llarp_crypto* crypto = nullptr;
LR_CommitMessage* LRCM = nullptr;
static void
HandleDone(void* u)
{
AsyncPathKeyExchangeContext< User >* ctx =
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
ctx->result(ctx);
}
static void
GenerateNextKey(void* u)
{
AsyncPathKeyExchangeContext< User >* ctx =
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
2018-06-21 16:20:14 +02:00
// current hop
2018-06-21 14:55:02 +02:00
auto& hop = ctx->path->hops[ctx->idx];
auto& frame = ctx->LRCM->frames[ctx->idx];
// generate key
ctx->crypto->encryption_keygen(hop.commkey);
hop.nonce.Randomize();
// do key exchange
if(!ctx->crypto->dh_client(hop.shared, hop.router.enckey, hop.commkey,
hop.nonce))
{
llarp::LogError("Failed to generate shared key for path build");
2018-06-21 14:55:02 +02:00
abort();
return;
}
// generate nonceXOR valueself->hop->pathKey
ctx->crypto->shorthash(hop.nonceXOR, llarp::Buffer(hop.shared));
2018-06-21 14:55:02 +02:00
++ctx->idx;
2018-06-21 16:20:14 +02:00
bool isFarthestHop = ctx->idx == ctx->path->hops.size();
2018-06-21 16:20:14 +02:00
if(isFarthestHop)
2018-06-21 14:55:02 +02:00
{
2018-06-21 16:20:14 +02:00
hop.upstream = hop.router.pubkey;
2018-06-21 14:55:02 +02:00
}
else
{
2018-06-21 16:20:14 +02:00
hop.upstream = ctx->path->hops[ctx->idx].router.pubkey;
2018-06-21 14:55:02 +02:00
}
2018-06-21 17:46:35 +02:00
// build record
LR_CommitRecord record;
record.version = LLARP_PROTO_VERSION;
record.txid = hop.txID;
record.rxid = hop.rxID;
2018-06-21 17:46:35 +02:00
record.tunnelNonce = hop.nonce;
record.nextHop = hop.upstream;
record.commkey = llarp::seckey_topublic(hop.commkey);
2018-06-21 14:55:02 +02:00
auto buf = frame.Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize;
2018-06-21 17:46:35 +02:00
// encode record
2018-06-21 14:55:02 +02:00
if(!record.BEncode(buf))
{
// failed to encode?
llarp::LogError("Failed to generate Commit Record");
2018-06-21 14:55:02 +02:00
return;
}
// use ephameral keypair for frame
SecretKey framekey;
ctx->crypto->encryption_keygen(framekey);
if(!frame.EncryptInPlace(framekey, hop.router.enckey, ctx->crypto))
{
llarp::LogError("Failed to encrypt LRCR");
2018-06-21 14:55:02 +02:00
return;
}
2018-06-21 16:20:14 +02:00
if(isFarthestHop)
2018-06-21 14:55:02 +02:00
{
2018-06-21 16:20:14 +02:00
// farthest hop
llarp_logic_queue_job(ctx->logic, {ctx, &HandleDone});
2018-06-21 14:55:02 +02:00
}
else
{
2018-06-21 16:20:14 +02:00
// next hop
llarp_threadpool_queue_job(ctx->worker, {ctx, &GenerateNextKey});
2018-06-21 14:55:02 +02:00
}
}
AsyncPathKeyExchangeContext(llarp_crypto* c) : crypto(c)
{
}
/// Generate all keys asynchronously and call hadler when done
void
AsyncGenerateKeys(Path_t* p, llarp_logic* l, llarp_threadpool* pool,
User* u, Handler func)
2018-06-21 14:55:02 +02:00
{
path = p;
logic = l;
user = u;
result = func;
worker = pool;
LRCM = new LR_CommitMessage;
for(size_t idx = 0; idx < MAXHOPS; ++idx)
{
LRCM->frames.emplace_back();
LRCM->frames.back().Randomize();
}
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
}
};
2018-06-19 00:03:50 +02:00
void
pathbuilder_generated_keys(
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx)
{
2018-06-20 14:34:48 +02:00
auto remote = ctx->path->Upstream();
2018-06-19 19:11:24 +02:00
auto router = ctx->user->router;
2018-06-20 14:34:48 +02:00
if(!router->SendToOrQueue(remote, ctx->LRCM))
{
llarp::LogError("failed to send LRCM");
2018-06-20 14:34:48 +02:00
return;
}
2018-06-26 15:39:29 +02:00
ctx->path->status = llarp::path::ePathBuilding;
ctx->path->buildStarted = llarp_time_now_ms();
router->paths.AddOwnPath(ctx->pathset, ctx->path);
2018-06-19 19:11:24 +02:00
ctx->user->pathBuildStarted(ctx->user);
2018-06-19 00:03:50 +02:00
}
void
pathbuilder_start_build(void* user)
{
llarp_pathbuild_job* job = static_cast< llarp_pathbuild_job* >(user);
2018-06-19 19:11:24 +02:00
// select hops
2018-06-20 14:34:48 +02:00
size_t idx = 0;
llarp_rc* prev = nullptr;
2018-06-19 00:03:50 +02:00
while(idx < job->hops.numHops)
{
2018-06-20 14:34:48 +02:00
llarp_rc* rc = &job->hops.hops[idx].router;
2018-06-19 19:11:24 +02:00
llarp_rc_clear(rc);
2018-07-25 03:54:37 +02:00
if(!job->selectHop(job->user, job->router->nodedb, prev, rc, idx))
{
/// TODO: handle this failure properly
llarp::LogWarn("Failed to select hop ", idx);
return;
}
2018-06-20 14:34:48 +02:00
prev = rc;
2018-06-19 00:03:50 +02:00
++idx;
}
// async generate keys
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx =
new AsyncPathKeyExchangeContext< llarp_pathbuild_job >(
&job->router->crypto);
ctx->pathset = job->context;
auto path = new llarp::path::Path(&job->hops);
path->SetBuildResultHook(std::bind(&llarp::path::PathSet::HandlePathBuilt,
ctx->pathset, std::placeholders::_1));
ctx->AsyncGenerateKeys(path, job->router->logic, job->router->tp, job,
&pathbuilder_generated_keys);
2018-06-19 00:03:50 +02:00
}
} // namespace llarp
llarp_pathbuilder_context::llarp_pathbuilder_context(
llarp_router* p_router, struct llarp_dht_context* p_dht, size_t pathNum,
size_t hops)
: llarp::path::PathSet(pathNum), router(p_router), dht(p_dht), numHops(hops)
{
p_router->paths.AddPathBuilder(this);
}
bool
llarp_pathbuilder_context::SelectHop(llarp_nodedb* db, llarp_rc* prev,
llarp_rc* cur, size_t hop)
{
if(hop == 0)
return router->GetRandomConnectedRouter(cur);
else
llarp_nodedb_select_random_hop(db, prev, cur, hop);
return true;
}
void
llarp_pathbuilder_context::BuildOne()
2018-06-19 00:03:50 +02:00
{
llarp_pathbuild_job* job = new llarp_pathbuild_job;
job->context = this;
job->selectHop = &PathSet::SelectHopCallback;
job->hops.numHops = numHops;
job->user = this;
job->pathBuildStarted = [](llarp_pathbuild_job* j) { delete j; };
llarp_pathbuilder_build_path(job);
2018-06-19 00:03:50 +02:00
}
2018-06-22 02:25:51 +02:00
struct llarp_pathbuilder_context*
llarp_pathbuilder_context_new(struct llarp_router* router,
struct llarp_dht_context* dht, size_t sz,
size_t hops)
2018-06-19 00:03:50 +02:00
{
return new llarp_pathbuilder_context(router, dht, sz, hops);
2018-06-22 02:25:51 +02:00
}
2018-06-19 00:03:50 +02:00
2018-06-22 02:25:51 +02:00
void
llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx)
{
delete ctx;
}
2018-06-19 00:03:50 +02:00
2018-06-22 02:25:51 +02:00
void
llarp_pathbuilder_build_path(struct llarp_pathbuild_job* job)
{
if(!job->context)
2018-06-19 11:19:23 +02:00
{
llarp::LogError("failed to build path because no context is set in job");
return;
2018-06-19 11:19:23 +02:00
}
2018-06-22 02:25:51 +02:00
if(job->selectHop == nullptr)
{
llarp::LogError("No callback provided for hop selection");
return;
}
job->router = job->context->router;
2018-06-22 02:25:51 +02:00
llarp_logic_queue_job(job->router->logic,
{job, &llarp::pathbuilder_start_build});
2018-06-19 11:19:23 +02:00
}