lokinet/llarp/dht.cpp

861 lines
22 KiB
C++
Raw Normal View History

2018-06-13 16:32:34 +00:00
#include <llarp/bencode.hpp>
2018-06-01 14:08:54 +00:00
#include <llarp/dht.hpp>
2018-07-05 14:23:45 +00:00
#include <llarp/messages/dht.hpp>
2018-06-01 14:08:54 +00:00
#include <llarp/messages/dht_immediate.hpp>
#include "router.hpp"
2018-06-13 16:32:34 +00:00
#include "router_contact.hpp"
2018-06-01 14:08:54 +00:00
2018-06-01 21:35:17 +00:00
#include <sodium.h>
2018-06-13 16:32:34 +00:00
#include <algorithm> // std::find
2018-06-01 21:35:17 +00:00
#include <set>
2018-06-01 14:08:54 +00:00
namespace llarp
{
DHTImmeidateMessage::~DHTImmeidateMessage()
{
for(auto &msg : msgs)
delete msg;
msgs.clear();
}
bool
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
if(llarp_buffer_eq(key, "m"))
return llarp::dht::DecodeMesssageList(remote.data(), buf, msgs);
if(llarp_buffer_eq(key, "v"))
{
if(!bencode_read_integer(buf, &version))
2018-06-01 14:08:54 +00:00
return false;
return version == LLARP_PROTO_VERSION;
}
// bad key
return false;
}
bool
DHTImmeidateMessage::BEncode(llarp_buffer_t *buf) const
{
if(!bencode_start_dict(buf))
return false;
2018-06-01 21:35:17 +00:00
// message type
2018-06-01 14:08:54 +00:00
if(!bencode_write_bytestring(buf, "a", 1))
return false;
if(!bencode_write_bytestring(buf, "m", 1))
return false;
// dht messages
if(!bencode_write_bytestring(buf, "m", 1))
return false;
// begin list
if(!bencode_start_list(buf))
return false;
for(const auto &msg : msgs)
{
if(!msg->BEncode(buf))
return false;
}
// end list
if(!bencode_end(buf))
return false;
2018-06-01 21:35:17 +00:00
// protocol version
2018-06-01 14:08:54 +00:00
if(!bencode_write_version_entry(buf))
return false;
return bencode_end(buf);
}
bool
DHTImmeidateMessage::HandleMessage(llarp_router *router) const
{
DHTImmeidateMessage *reply = new DHTImmeidateMessage(remote);
bool result = true;
for(auto &msg : msgs)
{
2018-07-05 14:23:45 +00:00
result &= msg->HandleMessage(router->dht, reply->msgs);
2018-06-01 14:08:54 +00:00
}
2018-06-21 09:31:53 +00:00
return result && router->SendToOrQueue(remote.data(), reply);
2018-06-01 14:08:54 +00:00
}
namespace dht
{
2018-07-05 14:23:45 +00:00
struct PathLookupInformer
{
llarp_router *router;
PathID_t pathID;
uint64_t txid;
PathLookupInformer(llarp_router *r, const PathID_t &id, uint64_t tx)
: router(r), pathID(id), txid(tx)
{
}
void
SendReply(const llarp::routing::IMessage *msg)
{
auto path = router->paths.GetByUpstream(router->pubkey(), pathID);
if(path == nullptr)
{
llarp::LogWarn("Path not found for relayed DHT message txid=", txid,
" pathid=", pathID);
2018-07-05 14:23:45 +00:00
return;
}
if(!path->SendRoutingMessage(msg, router))
llarp::LogWarn("Failed to send reply for relayed DHT message txid=",
txid, "pathid=", pathID);
2018-07-05 14:23:45 +00:00
}
static void
InformReply(llarp_router_lookup_job *job)
{
PathLookupInformer *self =
static_cast< PathLookupInformer * >(job->user);
llarp::routing::DHTMessage reply;
if(job->found)
{
if(llarp_rc_verify_sig(&self->router->crypto, &job->result))
{
reply.M.push_back(
new GotRouterMessage(job->target, self->txid, &job->result));
}
llarp_rc_free(&job->result);
llarp_rc_clear(&job->result);
}
else
{
reply.M.push_back(
new GotRouterMessage(job->target, self->txid, nullptr));
}
self->SendReply(&reply);
// TODO: is this okay?
delete self;
delete job;
}
};
/// variant of FindRouterMessage relayed via path
struct RelayedFindRouterMessage : public FindRouterMessage
{
RelayedFindRouterMessage(const Key_t &from) : FindRouterMessage(from)
{
}
/// handle a relayed FindRouterMessage, do a lookup on the dht and inform
/// the path of the result
/// TODO: smart path expiration logic needs to be implemented
virtual bool
HandleMessage(llarp_dht_context *ctx, std::vector< IMessage * > &replies)
{
auto &dht = ctx->impl;
/// lookup for us, send an immeidate reply
if(K == dht.OurKey())
{
auto path = dht.router->paths.GetByUpstream(K, pathID);
if(path)
{
llarp::routing::DHTMessage reply;
reply.M.push_back(new GotRouterMessage(K, txid, &dht.router->rc));
return path->SendRoutingMessage(&reply, dht.router);
}
return false;
}
llarp_router_lookup_job *job = new llarp_router_lookup_job;
PathLookupInformer *informer =
new PathLookupInformer(dht.router, pathID, txid);
job->user = informer;
job->hook = &PathLookupInformer::InformReply;
job->found = false;
job->dht = ctx;
memcpy(job->target, K, sizeof(job->target));
Key_t peer;
if(dht.nodes->FindClosest(K, peer))
dht.LookupRouter(K, dht.OurKey(), txid, peer, job);
return false;
}
};
PublishIntroMessage::~PublishIntroMessage()
{
}
bool
PublishIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("I", I, read, key, val))
return false;
if(!BEncodeMaybeReadDictInt("R", R, read, key, val))
return false;
if(llarp_buffer_eq(key, "S"))
{
read = true;
hasS = true;
if(!bencode_read_integer(val, &S))
return false;
}
if(!BEncodeMaybeReadDictInt("V", V, read, key, val))
return false;
return read;
}
bool
PublishIntroMessage::HandleMessage(llarp_dht_context *ctx,
std::vector< IMessage * > &replies) const
{
// TODO: implement me
return false;
}
bool
PublishIntroMessage::BEncode(llarp_buffer_t *buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "I"))
return false;
if(!BEncodeWriteDictEntry("I", I, buf))
return false;
if(!BEncodeWriteDictInt(buf, "R", R))
return false;
if(hasS)
{
if(!BEncodeWriteDictInt(buf, "S", S))
return false;
}
if(!BEncodeWriteDictInt(buf, "V", LLARP_PROTO_VERSION))
return false;
return bencode_end(buf);
}
2018-06-01 14:08:54 +00:00
GotRouterMessage::~GotRouterMessage()
{
for(auto &rc : R)
llarp_rc_free(&rc);
R.clear();
}
bool
GotRouterMessage::BEncode(llarp_buffer_t *buf) const
{
2018-06-13 16:32:34 +00:00
if(!bencode_start_dict(buf))
return false;
// message type
if(!BEncodeWriteDictMsgType(buf, "A", "S"))
return false;
if(!BEncodeWriteDictList("R", R, buf))
return false;
// txid
if(!BEncodeWriteDictInt(buf, "T", txid))
return false;
// version
if(!BEncodeWriteDictInt(buf, "V", version))
return false;
return bencode_end(buf);
2018-06-01 14:08:54 +00:00
}
bool
GotRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
{
2018-06-21 14:20:14 +00:00
if(llarp_buffer_eq(key, "R"))
{
return BEncodeReadList(R, val);
}
if(llarp_buffer_eq(key, "T"))
{
return bencode_read_integer(val, &txid);
}
bool read = false;
if(!BEncodeMaybeReadVersion("V", version, LLARP_PROTO_VERSION, read, key,
val))
return false;
return read;
2018-06-01 14:08:54 +00:00
}
bool
2018-07-05 14:23:45 +00:00
GotRouterMessage::HandleMessage(llarp_dht_context *ctx,
2018-06-01 14:08:54 +00:00
std::vector< IMessage * > &replies) const
{
2018-07-05 14:23:45 +00:00
auto &dht = ctx->impl;
2018-06-01 21:35:17 +00:00
auto pending = dht.FindPendingTX(From, txid);
if(pending)
{
if(R.size())
2018-06-21 14:20:14 +00:00
{
2018-06-01 21:35:17 +00:00
pending->Completed(&R[0]);
2018-06-21 15:46:35 +00:00
if(pending->requester != dht.OurKey())
2018-06-21 14:20:14 +00:00
{
2018-06-21 15:46:35 +00:00
replies.push_back(new GotRouterMessage(
pending->target, pending->requesterTX, &R[0]));
2018-06-21 14:20:14 +00:00
}
}
2018-06-01 21:35:17 +00:00
else
2018-06-14 15:39:54 +00:00
{
// iterate to next closest peer
Key_t nextPeer;
pending->exclude.insert(From);
2018-06-21 14:20:14 +00:00
if(pending->exclude.size() < 3
&& dht.nodes->FindCloseExcluding(pending->target, nextPeer,
pending->exclude))
2018-06-14 15:39:54 +00:00
{
llarp::LogInfo(pending->target, " was not found via ", From,
" iterating to next peer ", nextPeer,
" already asked ", pending->exclude.size(),
" other peers");
2018-06-21 15:46:35 +00:00
dht.LookupRouter(pending->target, pending->requester,
pending->requesterTX, nextPeer, nullptr, true,
pending->exclude);
2018-06-14 15:39:54 +00:00
}
else
2018-06-21 14:20:14 +00:00
{
llarp::LogInfo(pending->target, " was not found via ", From,
" and we won't look it up");
2018-06-14 15:39:54 +00:00
pending->Completed(nullptr);
2018-06-21 15:46:35 +00:00
if(pending->requester != dht.OurKey())
2018-06-21 14:20:14 +00:00
{
2018-06-21 15:46:35 +00:00
replies.push_back(new GotRouterMessage(
pending->target, pending->requesterTX, nullptr));
2018-06-21 14:20:14 +00:00
}
}
2018-06-14 15:39:54 +00:00
}
2018-06-01 21:35:17 +00:00
dht.RemovePendingLookup(From, txid);
return true;
}
llarp::LogWarn(
"Got response for DHT transaction we are not tracking, txid=", txid);
2018-06-01 14:08:54 +00:00
return false;
}
FindRouterMessage::~FindRouterMessage()
{
}
bool
FindRouterMessage::BEncode(llarp_buffer_t *buf) const
{
2018-06-01 21:35:17 +00:00
if(!bencode_start_dict(buf))
return false;
// message type
if(!bencode_write_bytestring(buf, "A", 1))
return false;
if(!bencode_write_bytestring(buf, "R", 1))
return false;
2018-06-21 14:20:14 +00:00
// iterative or not?
if(!bencode_write_bytestring(buf, "I", 1))
return false;
if(!bencode_write_int(buf, iterative ? 1 : 0))
return false;
2018-06-01 21:35:17 +00:00
// key
if(!bencode_write_bytestring(buf, "K", 1))
return false;
if(!bencode_write_bytestring(buf, K.data(), K.size()))
return false;
// txid
if(!bencode_write_bytestring(buf, "T", 1))
return false;
if(!bencode_write_uint64(buf, txid))
return false;
// version
if(!bencode_write_bytestring(buf, "V", 1))
return false;
if(!bencode_write_uint64(buf, version))
return false;
return bencode_end(buf);
2018-06-01 14:08:54 +00:00
}
bool
FindRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
{
llarp_buffer_t strbuf;
2018-06-21 14:20:14 +00:00
if(llarp_buffer_eq(key, "I"))
{
uint64_t result;
if(!bencode_read_integer(val, &result))
return false;
iterative = result != 0;
return true;
}
2018-06-01 14:08:54 +00:00
if(llarp_buffer_eq(key, "K"))
{
if(!bencode_read_string(val, &strbuf))
2018-06-01 14:08:54 +00:00
return false;
if(strbuf.sz != K.size())
return false;
memcpy(K.data(), strbuf.base, K.size());
return true;
}
if(llarp_buffer_eq(key, "T"))
{
return bencode_read_integer(val, &txid);
2018-06-01 14:08:54 +00:00
}
if(llarp_buffer_eq(key, "V"))
{
2018-06-01 21:35:17 +00:00
return bencode_read_integer(val, &version);
2018-06-01 14:08:54 +00:00
}
return false;
}
bool
2018-07-05 14:23:45 +00:00
FindRouterMessage::HandleMessage(llarp_dht_context *ctx,
2018-06-01 14:08:54 +00:00
std::vector< IMessage * > &replies) const
{
2018-07-05 14:23:45 +00:00
auto &dht = ctx->impl;
2018-06-13 16:32:34 +00:00
if(!dht.allowTransit)
{
llarp::LogWarn("Got DHT lookup from ", From,
" when we are not allowing dht transit");
2018-06-13 16:32:34 +00:00
return false;
}
2018-06-01 21:35:17 +00:00
auto pending = dht.FindPendingTX(From, txid);
if(pending)
{
llarp::LogWarn("Got duplicate DHT lookup from ", From, " txid=", txid);
2018-06-01 21:35:17 +00:00
return false;
}
2018-06-21 14:20:14 +00:00
dht.LookupRouterRelayed(From, txid, K, !iterative, replies);
2018-06-01 21:35:17 +00:00
return true;
2018-06-01 14:08:54 +00:00
}
struct MessageDecoder
{
2018-06-01 21:35:17 +00:00
const Key_t &From;
2018-06-01 14:08:54 +00:00
bool firstKey = true;
IMessage *msg = nullptr;
2018-07-05 14:23:45 +00:00
bool relayed = false;
2018-06-01 14:08:54 +00:00
MessageDecoder(const Key_t &from) : From(from)
{
}
static bool
on_key(dict_reader *r, llarp_buffer_t *key)
{
llarp_buffer_t strbuf;
MessageDecoder *dec = static_cast< MessageDecoder * >(r->user);
// check for empty dict
if(!key)
return !dec->firstKey;
// first key
if(dec->firstKey)
{
if(!llarp_buffer_eq(*key, "A"))
return false;
if(!bencode_read_string(r->buffer, &strbuf))
2018-06-01 14:08:54 +00:00
return false;
// bad msg size?
if(strbuf.sz != 1)
return false;
switch(*strbuf.base)
{
case 'R':
2018-07-05 14:23:45 +00:00
if(dec->relayed)
dec->msg = new RelayedFindRouterMessage(dec->From);
else
dec->msg = new FindRouterMessage(dec->From);
2018-06-01 14:08:54 +00:00
break;
case 'S':
if(dec->relayed)
{
llarp::LogWarn(
"GotRouterMessage found when parsing relayed DHT "
"message");
return false;
}
else
dec->msg = new GotRouterMessage(dec->From);
break;
case 'I':
dec->msg = new PublishIntroMessage();
2018-06-01 14:08:54 +00:00
break;
default:
llarp::LogWarn("unknown dht message type: ", (char)*strbuf.base);
2018-06-01 14:08:54 +00:00
// bad msg type
return false;
}
dec->firstKey = false;
return dec->msg != nullptr;
2018-06-01 14:08:54 +00:00
}
else
return dec->msg->DecodeKey(*key, r->buffer);
}
};
IMessage *
2018-07-05 14:23:45 +00:00
DecodeMesssage(const Key_t &from, llarp_buffer_t *buf, bool relayed)
2018-06-01 14:08:54 +00:00
{
MessageDecoder dec(from);
2018-07-05 14:23:45 +00:00
dec.relayed = relayed;
2018-06-01 14:08:54 +00:00
dict_reader r;
r.user = &dec;
r.on_key = &MessageDecoder::on_key;
if(bencode_read_dict(buf, &r))
2018-06-01 14:08:54 +00:00
return dec.msg;
else
{
if(dec.msg)
delete dec.msg;
return nullptr;
}
}
struct ListDecoder
{
ListDecoder(const Key_t &from, std::vector< IMessage * > &list)
: From(from), l(list){};
2018-06-01 21:35:17 +00:00
2018-07-05 14:23:45 +00:00
bool relayed = false;
2018-06-01 21:35:17 +00:00
const Key_t &From;
2018-06-01 14:08:54 +00:00
std::vector< IMessage * > &l;
static bool
on_item(list_reader *r, bool has)
{
ListDecoder *dec = static_cast< ListDecoder * >(r->user);
if(!has)
return true;
2018-07-05 14:23:45 +00:00
auto msg = DecodeMesssage(dec->From, r->buffer, dec->relayed);
2018-06-01 14:08:54 +00:00
if(msg)
{
dec->l.push_back(msg);
return true;
}
else
return false;
}
};
bool
DecodeMesssageList(const Key_t &from, llarp_buffer_t *buf,
2018-07-05 14:23:45 +00:00
std::vector< IMessage * > &list, bool relayed)
2018-06-01 14:08:54 +00:00
{
ListDecoder dec(from, list);
2018-07-05 14:23:45 +00:00
dec.relayed = relayed;
2018-06-01 14:08:54 +00:00
list_reader r;
r.user = &dec;
r.on_item = &ListDecoder::on_item;
return bencode_read_list(buf, &r);
2018-06-01 14:08:54 +00:00
}
2018-06-01 21:35:17 +00:00
SearchJob::SearchJob()
{
started = 0;
2018-06-21 15:46:35 +00:00
requester.Zero();
2018-06-01 21:35:17 +00:00
target.Zero();
}
2018-06-21 15:46:35 +00:00
SearchJob::SearchJob(const Key_t &asker, uint64_t tx, const Key_t &key,
2018-06-21 14:20:14 +00:00
llarp_router_lookup_job *j,
const std::set< Key_t > &excludes)
: job(j)
, started(llarp_time_now_ms())
2018-06-21 15:46:35 +00:00
, requester(asker)
, requesterTX(tx)
2018-06-21 14:20:14 +00:00
, target(key)
, exclude(excludes)
2018-06-01 21:35:17 +00:00
{
}
void
SearchJob::Completed(const llarp_rc *router, bool timeout) const
{
if(job && job->hook)
{
if(router)
{
job->found = true;
llarp_rc_copy(&job->result, router);
}
job->hook(job);
}
}
bool
SearchJob::IsExpired(llarp_time_t now) const
{
return now - started >= JobTimeout;
}
2018-06-01 14:08:54 +00:00
Context::Context()
{
2018-06-01 21:35:17 +00:00
randombytes((byte_t *)&ids, sizeof(uint64_t));
2018-06-01 14:08:54 +00:00
}
Context::~Context()
{
if(nodes)
delete nodes;
if(services)
delete services;
2018-06-01 14:08:54 +00:00
}
void
2018-06-01 21:35:17 +00:00
Context::handle_cleaner_timer(void *u, uint64_t orig, uint64_t left)
{
if(left)
return;
Context *ctx = static_cast< Context * >(u);
ctx->CleanupTX();
2018-06-14 15:44:55 +00:00
ctx->ScheduleCleanupTimer();
2018-06-01 21:35:17 +00:00
}
void
Context::LookupRouterRelayed(const Key_t &requester, uint64_t txid,
2018-06-21 14:20:14 +00:00
const Key_t &target, bool recursive,
2018-06-01 21:35:17 +00:00
std::vector< IMessage * > &replies)
2018-06-01 14:08:54 +00:00
{
2018-06-01 21:35:17 +00:00
if(target == ourKey)
{
// we are the target, give them our RC
replies.push_back(new GotRouterMessage(requester, txid, &router->rc));
return;
}
2018-06-21 14:20:14 +00:00
Key_t next;
std::set< Key_t > excluding = {requester, ourKey};
if(nodes->FindCloseExcluding(target, next, excluding))
2018-06-01 21:35:17 +00:00
{
2018-06-21 14:20:14 +00:00
if(next == target)
{
// we know it
replies.push_back(
new GotRouterMessage(requester, txid, nodes->nodes[target].rc));
}
else if(recursive) // are we doing a recursive lookup?
{
2018-06-21 15:46:35 +00:00
if((requester ^ target) < (ourKey ^ target))
2018-06-21 14:20:14 +00:00
{
2018-06-21 15:46:35 +00:00
// we aren't closer to the target than next hop
2018-06-21 14:20:14 +00:00
// so we won't ask neighboor recursively, tell them we don't have it
llarp::LogInfo("we aren't closer to ", target, " than ", next,
" so we end it here");
2018-06-21 14:20:14 +00:00
replies.push_back(new GotRouterMessage(requester, txid, nullptr));
}
else
{
// yeah, ask neighboor recursively
2018-06-21 15:46:35 +00:00
LookupRouter(target, requester, txid, next);
2018-06-21 14:20:14 +00:00
}
}
else // otherwise tell them we don't have it
{
llarp::LogInfo("we don't have ", target,
" and this was an iterative request so telling ",
requester, " that we don't have it");
2018-06-21 14:20:14 +00:00
replies.push_back(new GotRouterMessage(requester, txid, nullptr));
}
2018-06-01 21:35:17 +00:00
}
2018-06-21 14:20:14 +00:00
else
2018-06-01 21:35:17 +00:00
{
2018-06-21 14:20:14 +00:00
// we don't know it and have no closer peers
llarp::LogInfo("we don't have ", target,
" and have no closer peers so telling ", requester,
" that we don't have it");
2018-06-21 14:20:14 +00:00
replies.push_back(new GotRouterMessage(requester, txid, nullptr));
2018-06-01 21:35:17 +00:00
}
}
void
Context::RemovePendingLookup(const Key_t &owner, uint64_t id)
{
2018-06-04 13:10:19 +00:00
TXOwner search;
2018-06-21 14:20:14 +00:00
search.node = owner;
search.txid = id;
auto itr = pendingTX.find(search);
2018-06-01 21:35:17 +00:00
if(itr == pendingTX.end())
return;
pendingTX.erase(itr);
}
SearchJob *
Context::FindPendingTX(const Key_t &owner, uint64_t id)
{
2018-06-04 13:10:19 +00:00
TXOwner search;
2018-06-21 14:20:14 +00:00
search.node = owner;
search.txid = id;
auto itr = pendingTX.find(search);
2018-06-01 21:35:17 +00:00
if(itr == pendingTX.end())
return nullptr;
else
return &itr->second;
}
void
Context::CleanupTX()
{
auto now = llarp_time_now_ms();
llarp::LogDebug("DHT tick");
2018-06-01 21:35:17 +00:00
2018-06-21 15:46:35 +00:00
auto itr = pendingTX.begin();
while(itr != pendingTX.end())
2018-06-01 21:35:17 +00:00
{
2018-06-21 15:46:35 +00:00
if(itr->second.IsExpired(now))
{
itr->second.Completed(nullptr, true);
itr = pendingTX.erase(itr);
}
else
++itr;
2018-06-01 21:35:17 +00:00
}
}
void
Context::Init(const Key_t &us, llarp_router *r)
{
router = r;
ourKey = us;
nodes = new Bucket< RCNode >(ourKey);
services = new Bucket< ISNode >(ourKey);
llarp::LogDebug("intialize dht with key ", ourKey);
2018-06-01 14:08:54 +00:00
}
2018-06-01 21:35:17 +00:00
void
Context::ScheduleCleanupTimer()
{
llarp_logic_call_later(router->logic,
{1000, this, &handle_cleaner_timer});
}
bool
Context::RelayRequestForPath(const llarp::PathID_t &id, const IMessage *msg)
{
2018-07-05 14:23:45 +00:00
llarp::routing::DHTMessage reply;
if(!msg->HandleMessage(router->dht, reply.M))
return false;
auto path = router->paths.GetByUpstream(router->pubkey(), id);
return path && path->SendRoutingMessage(&reply, router);
}
2018-06-01 21:35:17 +00:00
void
Context::LookupRouter(const Key_t &target, const Key_t &whoasked,
2018-06-21 15:46:35 +00:00
uint64_t txid, const Key_t &askpeer,
llarp_router_lookup_job *job, bool iterative,
std::set< Key_t > excludes)
2018-06-01 21:35:17 +00:00
{
2018-06-21 15:46:35 +00:00
if(target.IsZero() || whoasked.IsZero() || askpeer.IsZero())
{
return;
}
2018-06-13 16:32:34 +00:00
auto id = ++ids;
2018-06-04 13:10:19 +00:00
TXOwner ownerKey;
2018-06-21 14:20:14 +00:00
ownerKey.node = askpeer;
ownerKey.txid = id;
2018-06-21 15:46:35 +00:00
if(txid == 0)
txid = id;
2018-06-04 13:10:19 +00:00
2018-06-21 15:46:35 +00:00
pendingTX[ownerKey] = SearchJob(whoasked, txid, target, job, excludes);
2018-06-01 21:35:17 +00:00
llarp::LogInfo("Asking ", askpeer, " for router ", target, " for ",
whoasked);
2018-06-21 14:20:14 +00:00
auto msg = new llarp::DHTImmeidateMessage(askpeer);
auto dhtmsg = new FindRouterMessage(askpeer, target, id);
dhtmsg->iterative = iterative;
msg->msgs.push_back(dhtmsg);
2018-06-21 09:31:53 +00:00
router->SendToOrQueue(askpeer, msg);
2018-06-01 21:35:17 +00:00
}
void
Context::LookupRouterViaJob(llarp_router_lookup_job *job)
{
Key_t peer;
2018-06-14 15:39:54 +00:00
if(nodes->FindClosest(job->target, peer))
2018-06-21 15:46:35 +00:00
LookupRouter(job->target, ourKey, 0, peer, job);
2018-06-01 21:35:17 +00:00
else if(job->hook)
{
job->found = false;
job->hook(job);
}
}
void
Context::queue_router_lookup(void *user)
{
llarp_router_lookup_job *job =
static_cast< llarp_router_lookup_job * >(user);
job->dht->impl.LookupRouterViaJob(job);
}
2018-06-21 15:46:35 +00:00
2018-06-21 14:20:14 +00:00
} // namespace dht
} // namespace llarp
2018-06-01 14:08:54 +00:00
2018-06-01 21:35:17 +00:00
llarp_dht_context::llarp_dht_context(llarp_router *router)
{
parent = router;
}
extern "C"
2018-06-01 14:08:54 +00:00
{
struct llarp_dht_context *
llarp_dht_context_new(struct llarp_router *router)
{
return new llarp_dht_context(router);
}
2018-06-01 14:08:54 +00:00
void
llarp_dht_context_free(struct llarp_dht_context *ctx)
{
delete ctx;
}
2018-06-01 14:08:54 +00:00
void
llarp_dht_put_peer(struct llarp_dht_context *ctx, struct llarp_rc *rc)
2018-06-01 21:35:17 +00:00
{
llarp::dht::RCNode n(rc);
ctx->impl.nodes->PutNode(n);
}
2018-06-01 21:35:17 +00:00
void
llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id)
{
llarp::dht::Key_t k = id;
ctx->impl.nodes->DelNode(k);
}
2018-06-01 21:35:17 +00:00
void
llarp_dht_set_msg_handler(struct llarp_dht_context *ctx,
llarp_dht_msg_handler handler)
{
ctx->impl.custom_handler = handler;
}
2018-06-01 14:08:54 +00:00
void
llarp_dht_allow_transit(llarp_dht_context *ctx)
{
ctx->impl.allowTransit = true;
}
2018-06-13 16:32:34 +00:00
void
llarp_dht_context_start(struct llarp_dht_context *ctx, const byte_t *key)
{
ctx->impl.Init(key, ctx->parent);
}
2018-06-01 21:35:17 +00:00
void
llarp_dht_lookup_router(struct llarp_dht_context *ctx,
struct llarp_router_lookup_job *job)
{
job->dht = ctx;
job->found = false;
llarp_logic_queue_job(ctx->parent->logic,
{job, &llarp::dht::Context::queue_router_lookup});
}
2018-06-01 14:08:54 +00:00
}