2018-06-01 14:08:54 +00:00
|
|
|
#ifndef LLARP_DHT_HPP_
|
|
|
|
#define LLARP_DHT_HPP_
|
|
|
|
#include <llarp/buffer.h>
|
|
|
|
#include <llarp/dht.h>
|
|
|
|
#include <llarp/router.h>
|
|
|
|
#include <llarp/router_contact.h>
|
2018-06-01 21:35:17 +00:00
|
|
|
#include <llarp/time.h>
|
2018-06-01 14:08:54 +00:00
|
|
|
#include <llarp/aligned.hpp>
|
|
|
|
|
|
|
|
#include <array>
|
2018-06-01 21:35:17 +00:00
|
|
|
#include <functional>
|
2018-06-01 14:08:54 +00:00
|
|
|
#include <map>
|
2018-06-14 15:39:54 +00:00
|
|
|
#include <set>
|
2018-06-01 21:51:35 +00:00
|
|
|
#include <unordered_map>
|
2018-06-01 14:08:54 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
const size_t MAX_MSG_SIZE = 2048;
|
|
|
|
|
|
|
|
struct Key_t : public llarp::AlignedBuffer< 32 >
|
|
|
|
{
|
|
|
|
Key_t(const byte_t* val) : llarp::AlignedBuffer< 32 >(val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Key_t() : llarp::AlignedBuffer< 32 >()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Key_t
|
|
|
|
operator^(const Key_t& other) const
|
|
|
|
{
|
|
|
|
Key_t dist;
|
2018-06-14 14:04:42 +00:00
|
|
|
for(size_t idx = 0; idx < 4; ++idx)
|
|
|
|
dist.l[idx] = l[idx] ^ other.l[idx];
|
2018-06-01 14:08:54 +00:00
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Key_t& other) const
|
|
|
|
{
|
|
|
|
return memcmp(data_l(), other.data_l(), 32) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-01 21:47:38 +00:00
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
llarp_rc* rc;
|
|
|
|
|
|
|
|
Key_t ID;
|
|
|
|
|
|
|
|
Node() : rc(nullptr)
|
|
|
|
{
|
|
|
|
ID.Zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node(llarp_rc* other) : rc(other)
|
|
|
|
{
|
|
|
|
ID = other->pubkey;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-01 21:35:17 +00:00
|
|
|
struct SearchJob
|
|
|
|
{
|
|
|
|
const static uint64_t JobTimeout = 30000;
|
|
|
|
|
|
|
|
SearchJob();
|
|
|
|
|
2018-06-21 15:46:35 +00:00
|
|
|
SearchJob(const Key_t& requester, uint64_t requesterTX,
|
|
|
|
const Key_t& target, llarp_router_lookup_job* job,
|
2018-06-21 14:20:14 +00:00
|
|
|
const std::set< Key_t >& excludes);
|
2018-06-01 21:35:17 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Completed(const llarp_rc* router, bool timeout = false) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
|
|
|
|
2018-06-14 15:39:54 +00:00
|
|
|
llarp_router_lookup_job* job = nullptr;
|
2018-06-01 21:35:17 +00:00
|
|
|
llarp_time_t started;
|
2018-06-21 15:46:35 +00:00
|
|
|
Key_t requester;
|
|
|
|
uint64_t requesterTX;
|
2018-06-01 21:35:17 +00:00
|
|
|
Key_t target;
|
2018-06-14 15:39:54 +00:00
|
|
|
std::set< Key_t > exclude;
|
2018-06-01 21:35:17 +00:00
|
|
|
};
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
struct XorMetric
|
|
|
|
{
|
|
|
|
const Key_t& us;
|
|
|
|
|
|
|
|
XorMetric(const Key_t& ourKey) : us(ourKey){};
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator()(const Key_t& left, const Key_t& right) const
|
|
|
|
{
|
2018-06-14 14:04:42 +00:00
|
|
|
return (us ^ left) < (us ^ right);
|
2018-06-01 14:08:54 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IMessage
|
|
|
|
{
|
|
|
|
virtual ~IMessage(){};
|
|
|
|
|
|
|
|
IMessage(const Key_t& from) : From(from)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const = 0;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) = 0;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
HandleMessage(llarp_router* router,
|
|
|
|
std::vector< IMessage* >& replies) const = 0;
|
|
|
|
|
|
|
|
Key_t From;
|
|
|
|
};
|
|
|
|
|
|
|
|
IMessage*
|
|
|
|
DecodeMessage(const Key_t& from, llarp_buffer_t* buf);
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodeMesssageList(const Key_t& from, llarp_buffer_t* buf,
|
|
|
|
std::vector< IMessage* >& dst);
|
|
|
|
|
|
|
|
struct Bucket
|
|
|
|
{
|
|
|
|
typedef std::map< Key_t, Node, XorMetric > BucketStorage_t;
|
|
|
|
|
|
|
|
Bucket(const Key_t& us) : nodes(XorMetric(us)){};
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindClosest(const Key_t& target, Key_t& result) const;
|
|
|
|
|
2018-06-01 21:35:17 +00:00
|
|
|
bool
|
|
|
|
FindCloseExcluding(const Key_t& target, Key_t& result,
|
2018-06-14 15:39:54 +00:00
|
|
|
const std::set< Key_t >& exclude) const;
|
2018-06-01 21:35:17 +00:00
|
|
|
|
2018-06-14 14:04:42 +00:00
|
|
|
void
|
|
|
|
PutNode(const Node& val);
|
|
|
|
|
|
|
|
void
|
|
|
|
DelNode(const Key_t& key);
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
BucketStorage_t nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
Context();
|
|
|
|
~Context();
|
|
|
|
|
|
|
|
llarp_dht_msg_handler custom_handler = nullptr;
|
|
|
|
|
2018-06-01 21:35:17 +00:00
|
|
|
SearchJob*
|
|
|
|
FindPendingTX(const Key_t& owner, uint64_t txid);
|
|
|
|
|
|
|
|
void
|
|
|
|
RemovePendingLookup(const Key_t& owner, uint64_t txid);
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
void
|
2018-06-01 21:35:17 +00:00
|
|
|
LookupRouter(const Key_t& target, const Key_t& whoasked,
|
2018-06-21 15:46:35 +00:00
|
|
|
uint64_t whoaskedTX, const Key_t& askpeer,
|
|
|
|
llarp_router_lookup_job* job = nullptr,
|
2018-06-21 14:20:14 +00:00
|
|
|
bool iterative = false, std::set< Key_t > excludes = {});
|
2018-06-01 21:35:17 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
LookupRouterViaJob(llarp_router_lookup_job* job);
|
|
|
|
|
|
|
|
void
|
|
|
|
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);
|
|
|
|
|
|
|
|
void
|
|
|
|
Init(const Key_t& us, llarp_router* router);
|
|
|
|
|
|
|
|
void
|
|
|
|
QueueRouterLookup(llarp_router_lookup_job* job);
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_cleaner_timer(void* user, uint64_t orig, uint64_t left);
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_router_lookup(void* user);
|
|
|
|
|
|
|
|
llarp_router* router = nullptr;
|
|
|
|
Bucket* nodes = nullptr;
|
2018-06-13 16:32:34 +00:00
|
|
|
bool allowTransit = false;
|
2018-06-01 14:08:54 +00:00
|
|
|
|
2018-06-21 14:20:14 +00:00
|
|
|
const Key_t&
|
|
|
|
OurKey() const
|
|
|
|
{
|
|
|
|
return ourKey;
|
|
|
|
}
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
private:
|
2018-06-01 21:35:17 +00:00
|
|
|
void
|
|
|
|
ScheduleCleanupTimer();
|
|
|
|
|
|
|
|
void
|
|
|
|
CleanupTX();
|
|
|
|
|
|
|
|
uint64_t ids;
|
|
|
|
|
|
|
|
struct TXOwner
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
Key_t node;
|
2018-06-21 12:55:02 +00:00
|
|
|
uint64_t txid = 0;
|
2018-06-01 21:35:17 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const TXOwner& other) const
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
return txid == other.txid && node == other.node;
|
2018-06-01 21:35:17 +00:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
operator<(const TXOwner& other) const
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
return txid < other.txid || node < other.node;
|
2018-06-01 21:35:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TXOwnerHash
|
|
|
|
{
|
|
|
|
std::size_t
|
|
|
|
operator()(TXOwner const& o) const noexcept
|
|
|
|
{
|
|
|
|
std::size_t sz2;
|
2018-06-21 14:20:14 +00:00
|
|
|
memcpy(&sz2, &o.node[0], sizeof(std::size_t));
|
2018-06-01 21:35:17 +00:00
|
|
|
return o.txid ^ (sz2 << 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_map< TXOwner, SearchJob, TXOwnerHash > pendingTX;
|
2018-06-01 14:08:54 +00:00
|
|
|
Key_t ourKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GotRouterMessage : public IMessage
|
|
|
|
{
|
|
|
|
GotRouterMessage(const Key_t& from) : IMessage(from)
|
|
|
|
{
|
|
|
|
}
|
2018-06-01 21:35:17 +00:00
|
|
|
GotRouterMessage(const Key_t& from, uint64_t id, const llarp_rc* result)
|
|
|
|
: IMessage(from), txid(id)
|
|
|
|
{
|
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
R.emplace_back();
|
|
|
|
llarp_rc_clear(&R.back());
|
|
|
|
llarp_rc_copy(&R.back(), result);
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 14:08:54 +00:00
|
|
|
|
|
|
|
~GotRouterMessage();
|
|
|
|
|
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleMessage(llarp_router* router,
|
|
|
|
std::vector< IMessage* >& replies) const;
|
|
|
|
|
|
|
|
std::vector< llarp_rc > R;
|
|
|
|
uint64_t txid = 0;
|
|
|
|
uint64_t version = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FindRouterMessage : public IMessage
|
|
|
|
{
|
|
|
|
FindRouterMessage(const Key_t& from) : IMessage(from)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-01 21:35:17 +00:00
|
|
|
FindRouterMessage(const Key_t& from, const Key_t& target, uint64_t id)
|
|
|
|
: IMessage(from), K(target), txid(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
~FindRouterMessage();
|
|
|
|
|
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleMessage(llarp_router* router,
|
|
|
|
std::vector< IMessage* >& replies) const;
|
|
|
|
|
|
|
|
Key_t K;
|
2018-06-21 14:20:14 +00:00
|
|
|
bool iterative = false;
|
2018-06-01 14:08:54 +00:00
|
|
|
uint64_t txid = 0;
|
|
|
|
uint64_t version = 0;
|
|
|
|
};
|
2018-06-21 12:55:02 +00:00
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|
2018-06-01 14:08:54 +00:00
|
|
|
|
|
|
|
struct llarp_dht_context
|
|
|
|
{
|
|
|
|
llarp::dht::Context impl;
|
2018-06-01 21:35:17 +00:00
|
|
|
llarp_router* parent;
|
|
|
|
llarp_dht_context(llarp_router* router);
|
2018-06-01 14:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|