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

284 lines
7.3 KiB
C++
Raw Normal View History

2018-08-30 20:48:43 +02:00
#include <llarp/nodedb.hpp>
2018-06-19 00:03:50 +02:00
#include <llarp/path.hpp>
2018-07-09 19:32:11 +02:00
#include <llarp/pathbuilder.hpp>
2018-10-09 14:06:30 +02:00
#include <functional>
#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-10-09 14:06:30 +02:00
typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler;
User* user = nullptr;
Handler result;
2018-06-21 14:55:02 +02:00
size_t idx = 0;
llarp_threadpool* worker = nullptr;
llarp_logic* logic = nullptr;
llarp_crypto* crypto = nullptr;
LR_CommitMessage LRCM;
2018-06-21 14:55:02 +02:00
static void
HandleDone(void* u)
{
AsyncPathKeyExchangeContext< User >* ctx =
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
ctx->result(ctx);
2018-10-09 14:06:30 +02:00
delete ctx;
2018-06-21 14:55:02 +02:00
}
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];
2018-06-21 14:55:02 +02:00
// generate key
ctx->crypto->encryption_keygen(hop.commkey);
hop.nonce.Randomize();
// do key exchange
2018-08-30 20:48:43 +02:00
if(!ctx->crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey,
2018-06-21 14:55:02 +02:00
hop.nonce))
{
llarp::LogError("Failed to generate shared key for path build");
delete ctx;
2018-06-21 14:55:02 +02:00
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-08-30 20:48:43 +02:00
hop.upstream = hop.rc.pubkey;
2018-06-21 14:55:02 +02:00
}
else
{
2018-08-30 20:48:43 +02:00
hop.upstream = ctx->path->hops[ctx->idx].rc.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");
delete ctx;
2018-06-21 14:55:02 +02:00
return;
}
// use ephameral keypair for frame
SecretKey framekey;
ctx->crypto->encryption_keygen(framekey);
2018-08-30 20:48:43 +02:00
if(!frame.EncryptInPlace(framekey, hop.rc.enckey, ctx->crypto))
2018-06-21 14:55:02 +02:00
{
llarp::LogError("Failed to encrypt LRCR");
delete ctx;
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;
for(size_t idx = 0; idx < MAXHOPS; ++idx)
{
LRCM.frames[idx].Randomize();
2018-06-21 14:55:02 +02:00
}
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
}
};
2018-06-19 00:03:50 +02:00
void
2018-08-30 20:48:43 +02:00
pathbuilder_generated_keys(AsyncPathKeyExchangeContext< path::Builder >* ctx)
2018-06-19 00:03:50 +02:00
{
2018-10-04 15:03:48 +02:00
RouterID remote = ctx->path->Upstream();
auto router = ctx->user->router;
if(!router->SendToOrQueue(remote, &ctx->LRCM))
2018-06-20 14:34:48 +02:00
{
llarp::LogError("failed to send LRCM");
2018-06-20 14:34:48 +02:00
return;
}
2018-08-14 23:17:18 +02:00
// persist session with router until this path is done
router->PersistSessionUntil(remote, ctx->path->ExpireTime());
// add own path
router->paths.AddOwnPath(ctx->pathset, ctx->path);
2018-06-19 00:03:50 +02:00
}
2018-08-30 20:48:43 +02:00
namespace path
2018-06-19 00:03:50 +02:00
{
2018-08-30 20:48:43 +02:00
Builder::Builder(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)
2018-06-19 00:03:50 +02:00
{
2018-08-30 20:48:43 +02:00
p_router->paths.AddPathBuilder(this);
p_router->crypto.encryption_keygen(enckey);
2018-06-19 00:03:50 +02:00
}
2018-08-30 20:48:43 +02:00
Builder::~Builder()
{
router->paths.RemovePathBuilder(this);
}
2018-08-30 20:48:43 +02:00
bool
Builder::SelectHop(llarp_nodedb* db, const RouterContact& prev,
RouterContact& cur, size_t hop)
{
if(hop == 0)
{
if(router->NumberOfConnectedRouters())
return router->GetRandomConnectedRouter(cur);
else
2018-08-31 16:41:04 +02:00
return llarp_nodedb_select_random_hop(db, prev, cur, 0);
2018-08-30 20:48:43 +02:00
}
2018-10-04 19:51:45 +02:00
size_t tries = 5;
do
{
--tries;
llarp_nodedb_select_random_hop(db, prev, cur, hop);
} while(router->routerProfiling.IsBad(cur.pubkey) && tries > 0);
return tries > 0;
2018-08-30 20:48:43 +02:00
}
2018-08-30 20:48:43 +02:00
const byte_t*
Builder::GetTunnelEncryptionSecretKey() const
{
return enckey;
}
2018-06-19 00:03:50 +02:00
2018-08-30 20:48:43 +02:00
bool
Builder::ShouldBuildMore() const
{
auto now = llarp_time_now_ms();
return llarp::path::PathSet::ShouldBuildMore() && now > lastBuild
&& now - lastBuild > buildIntervalLimit;
2018-08-30 20:48:43 +02:00
}
2018-08-12 19:22:29 +02:00
2018-08-30 20:48:43 +02:00
void
Builder::BuildOne()
{
2018-08-31 16:41:04 +02:00
std::vector< RouterContact > hops;
if(SelectHops(router->nodedb, hops))
Build(hops);
}
bool
Builder::SelectHops(llarp_nodedb* nodedb,
std::vector< RouterContact >& hops)
{
2018-09-06 13:46:19 +02:00
hops.resize(numHops);
2018-08-30 20:48:43 +02:00
size_t idx = 0;
while(idx < numHops)
{
if(idx == 0)
{
if(!SelectHop(nodedb, hops[0], hops[0], 0))
2018-08-30 20:48:43 +02:00
{
llarp::LogError("failed to select first hop");
return false;
2018-08-30 20:48:43 +02:00
}
}
else
{
if(!SelectHop(nodedb, hops[idx - 1], hops[idx], idx))
2018-08-30 20:48:43 +02:00
{
/// TODO: handle this failure properly
llarp::LogWarn("Failed to select hop ", idx);
return false;
2018-08-30 20:48:43 +02:00
}
}
++idx;
}
return true;
}
void
Builder::Build(const std::vector< RouterContact >& hops)
{
lastBuild = llarp_time_now_ms();
2018-08-30 20:48:43 +02:00
// async generate keys
AsyncPathKeyExchangeContext< Builder >* ctx =
new AsyncPathKeyExchangeContext< Builder >(&router->crypto);
ctx->pathset = this;
auto path = new llarp::path::Path(hops, this);
path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt,
this, std::placeholders::_1));
2018-08-30 20:48:43 +02:00
ctx->AsyncGenerateKeys(path, router->logic, router->tp, this,
&pathbuilder_generated_keys);
}
2018-06-19 00:03:50 +02:00
void
Builder::HandlePathBuilt(Path* p)
{
buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
PathSet::HandlePathBuilt(p);
}
void
Builder::HandlePathBuildTimeout(Path* p)
{
// linear backoff
buildIntervalLimit += 1000;
PathSet::HandlePathBuildTimeout(p);
}
2018-08-30 20:48:43 +02:00
void
Builder::ManualRebuild(size_t num)
{
llarp::LogDebug("manual rebuild ", num);
while(num--)
BuildOne();
}
2018-06-19 00:03:50 +02:00
2018-08-30 20:48:43 +02:00
} // namespace path
} // namespace llarp