2019-01-22 01:14:02 +00:00
|
|
|
#ifndef LLARP_DHT_CONTEXT
|
|
|
|
#define LLARP_DHT_CONTEXT
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/bucket.hpp>
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <dht/dht.h>
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/key.hpp>
|
|
|
|
#include <dht/message.hpp>
|
|
|
|
#include <dht/messages/findintro.hpp>
|
|
|
|
#include <dht/node.hpp>
|
2019-01-22 01:14:02 +00:00
|
|
|
#include <dht/tx.hpp>
|
|
|
|
#include <dht/txholder.hpp>
|
2019-01-19 18:16:40 +00:00
|
|
|
#include <dht/txowner.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/IntroSet.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
struct AbstractRouter;
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
namespace dht
|
|
|
|
{
|
2019-01-24 01:31:02 +00:00
|
|
|
struct AbstractContext
|
|
|
|
{
|
|
|
|
virtual ~AbstractContext() = 0;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
LookupRouter(const RouterID& target, RouterLookupHandler result) = 0;
|
|
|
|
|
|
|
|
/// on behalf of whoasked request introset for target from dht router with
|
|
|
|
/// key askpeer
|
|
|
|
virtual void
|
2019-01-28 19:39:17 +00:00
|
|
|
LookupIntroSetRecursive(const service::Address& target,
|
|
|
|
const Key_t& whoasked, uint64_t whoaskedTX,
|
|
|
|
const Key_t& askpeer, uint64_t R,
|
|
|
|
service::IntroSetLookupHandler result =
|
|
|
|
service::IntroSetLookupHandler()) = 0;
|
2019-01-24 01:31:02 +00:00
|
|
|
|
|
|
|
virtual void
|
2019-01-28 19:39:17 +00:00
|
|
|
LookupIntroSetIterative(const service::Address& target,
|
|
|
|
const Key_t& whoasked, uint64_t whoaskedTX,
|
|
|
|
const Key_t& askpeer,
|
|
|
|
service::IntroSetLookupHandler result =
|
|
|
|
service::IntroSetLookupHandler()) = 0;
|
2019-01-24 01:31:02 +00:00
|
|
|
|
|
|
|
virtual std::set< service::IntroSet >
|
|
|
|
FindRandomIntroSetsWithTagExcluding(
|
|
|
|
const service::Tag& tag, size_t max = 2,
|
|
|
|
const std::set< service::IntroSet >& excludes = {}) = 0;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
DHTSendTo(const RouterID& peer, IMessage* msg, bool keepalive = true) = 0;
|
|
|
|
|
|
|
|
virtual llarp_time_t
|
|
|
|
Now() const = 0;
|
|
|
|
|
|
|
|
virtual llarp::Crypto*
|
|
|
|
Crypto() const = 0;
|
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
virtual llarp::AbstractRouter*
|
2019-01-24 01:31:02 +00:00
|
|
|
GetRouter() const = 0;
|
|
|
|
|
|
|
|
virtual const Key_t&
|
|
|
|
OurKey() const = 0;
|
|
|
|
|
2019-02-05 00:41:33 +00:00
|
|
|
virtual Bucket< RCNode >*
|
|
|
|
Nodes() const = 0;
|
2019-01-24 01:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Context final : public AbstractContext
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
Context();
|
|
|
|
|
2019-01-24 01:31:02 +00:00
|
|
|
~Context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
llarp::Crypto*
|
2019-01-24 01:31:02 +00:00
|
|
|
Crypto() const override;
|
2018-09-09 11:23:21 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// on behalf of whoasked request introset for target from dht router with
|
|
|
|
/// key askpeer
|
2018-07-11 13:20:14 +00:00
|
|
|
void
|
2019-02-05 00:41:33 +00:00
|
|
|
LookupIntroSetRecursive(
|
|
|
|
const service::Address& target, const Key_t& whoasked,
|
|
|
|
uint64_t whoaskedTX, const Key_t& askpeer, uint64_t R,
|
|
|
|
service::IntroSetLookupHandler result = nullptr) override;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-07-12 18:21:44 +00:00
|
|
|
void
|
2019-02-05 00:41:33 +00:00
|
|
|
LookupIntroSetIterative(
|
|
|
|
const service::Address& target, const Key_t& whoasked,
|
|
|
|
uint64_t whoaskedTX, const Key_t& askpeer,
|
|
|
|
service::IntroSetLookupHandler result = nullptr) override;
|
2018-07-12 18:21:44 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// on behalf of whoasked request router with public key target from dht
|
|
|
|
/// router with key askpeer
|
2018-07-11 13:20:14 +00:00
|
|
|
void
|
2018-08-29 20:40:26 +00:00
|
|
|
LookupRouterRecursive(const RouterID& target, const Key_t& whoasked,
|
|
|
|
uint64_t whoaskedTX, const Key_t& askpeer,
|
2018-08-30 18:48:43 +00:00
|
|
|
RouterLookupHandler result = nullptr);
|
|
|
|
|
|
|
|
bool
|
2019-01-24 01:31:02 +00:00
|
|
|
LookupRouter(const RouterID& target, RouterLookupHandler result) override
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
Key_t askpeer;
|
2019-01-02 01:04:08 +00:00
|
|
|
if(!nodes->FindClosest(Key_t(target), askpeer))
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
return false;
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
2018-11-28 16:38:20 +00:00
|
|
|
LookupRouterRecursive(target, OurKey(), 0, askpeer, result);
|
2018-08-30 18:48:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-09-14 13:43:42 +00:00
|
|
|
bool
|
|
|
|
HasRouterLookup(const RouterID& target) const
|
|
|
|
{
|
|
|
|
return pendingRouterLookups.HasLookupFor(target);
|
|
|
|
}
|
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// on behalf of whoasked request introsets with tag from dht router with
|
|
|
|
/// key askpeer with Recursion depth R
|
2018-07-17 04:37:50 +00:00
|
|
|
void
|
2018-08-29 20:40:26 +00:00
|
|
|
LookupTagRecursive(const service::Tag& tag, const Key_t& whoasked,
|
|
|
|
uint64_t whoaskedTX, const Key_t& askpeer, uint64_t R);
|
2018-07-17 04:37:50 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// issue dht lookup for tag via askpeer and send reply to local path
|
2018-07-18 20:58:16 +00:00
|
|
|
void
|
|
|
|
LookupTagForPath(const service::Tag& tag, uint64_t txid,
|
|
|
|
const llarp::PathID_t& path, const Key_t& askpeer);
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/// issue dht lookup for router via askpeer and send reply to local path
|
|
|
|
void
|
|
|
|
LookupRouterForPath(const RouterID& target, uint64_t txid,
|
|
|
|
const llarp::PathID_t& path, const Key_t& askpeer);
|
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// issue dht lookup for introset for addr via askpeer and send reply to
|
|
|
|
/// local path
|
2018-08-02 01:41:40 +00:00
|
|
|
void
|
|
|
|
LookupIntroSetForPath(const service::Address& addr, uint64_t txid,
|
2018-08-29 20:40:26 +00:00
|
|
|
const llarp::PathID_t& path, const Key_t& askpeer);
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// send a dht message to peer, if keepalive is true then keep the session
|
|
|
|
/// with that peer alive for 10 seconds
|
2018-08-10 21:34:11 +00:00
|
|
|
void
|
2019-02-05 00:41:33 +00:00
|
|
|
DHTSendTo(const RouterID& peer, IMessage* msg,
|
|
|
|
bool keepalive = true) override;
|
2018-08-02 01:41:40 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// get routers closest to target excluding requester
|
2018-08-27 13:44:16 +00:00
|
|
|
bool
|
2018-09-02 18:25:42 +00:00
|
|
|
HandleExploritoryRouterLookup(
|
|
|
|
const Key_t& requester, uint64_t txid, const RouterID& target,
|
|
|
|
std::vector< std::unique_ptr< IMessage > >& reply);
|
2018-08-02 04:34:46 +00:00
|
|
|
|
2018-07-18 20:58:16 +00:00
|
|
|
std::set< service::IntroSet >
|
2018-08-29 20:40:26 +00:00
|
|
|
FindRandomIntroSetsWithTagExcluding(
|
|
|
|
const service::Tag& tag, size_t max = 2,
|
2019-01-24 01:31:02 +00:00
|
|
|
const std::set< service::IntroSet >& excludes = {}) override;
|
2018-07-18 20:58:16 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// handle rc lookup from requester for target
|
2018-07-11 13:20:14 +00:00
|
|
|
void
|
|
|
|
LookupRouterRelayed(const Key_t& requester, uint64_t txid,
|
|
|
|
const Key_t& target, bool recursive,
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > >& replies);
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2019-01-19 18:16:40 +00:00
|
|
|
/// relay a dht message from a local path to the main network
|
2018-07-11 13:20:14 +00:00
|
|
|
bool
|
|
|
|
RelayRequestForPath(const llarp::PathID_t& localPath,
|
|
|
|
const IMessage* msg);
|
2018-07-18 22:50:16 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// send introset to peer from source with S counter and excluding peers
|
2018-07-18 22:50:16 +00:00
|
|
|
void
|
2018-08-29 20:40:26 +00:00
|
|
|
PropagateIntroSetTo(const Key_t& source, uint64_t sourceTX,
|
2018-07-19 04:58:39 +00:00
|
|
|
const service::IntroSet& introset, const Key_t& peer,
|
2018-08-01 22:10:38 +00:00
|
|
|
uint64_t S, const std::set< Key_t >& exclude);
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// initialize dht context and explore every exploreInterval milliseconds
|
2018-07-11 13:20:14 +00:00
|
|
|
void
|
2018-12-13 16:14:44 +00:00
|
|
|
Init(const Key_t& us, llarp::Router* router,
|
|
|
|
llarp_time_t exploreInterval);
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
/// get localally stored introset by service address
|
2018-07-16 21:22:25 +00:00
|
|
|
const llarp::service::IntroSet*
|
|
|
|
GetIntroSetByServiceAddress(const llarp::service::Address& addr) const;
|
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
static void
|
|
|
|
handle_cleaner_timer(void* user, uint64_t orig, uint64_t left);
|
|
|
|
|
2018-08-27 13:44:16 +00:00
|
|
|
static void
|
|
|
|
handle_explore_timer(void* user, uint64_t orig, uint64_t left);
|
|
|
|
|
|
|
|
/// explore dht for new routers
|
|
|
|
void
|
2018-09-06 20:31:58 +00:00
|
|
|
Explore(size_t N = 3);
|
2018-08-27 13:44:16 +00:00
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp::AbstractRouter* router;
|
2018-07-11 13:20:14 +00:00
|
|
|
// for router contacts
|
2019-01-22 01:14:02 +00:00
|
|
|
std::unique_ptr< Bucket< RCNode > > nodes;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
// for introduction sets
|
2019-01-22 01:14:02 +00:00
|
|
|
std::unique_ptr< Bucket< ISNode > > services;
|
|
|
|
bool allowTransit;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2019-02-05 00:41:33 +00:00
|
|
|
Bucket< RCNode >*
|
|
|
|
Nodes() const override
|
|
|
|
{
|
|
|
|
return nodes.get();
|
|
|
|
}
|
2019-01-24 01:31:02 +00:00
|
|
|
|
2019-01-02 01:03:53 +00:00
|
|
|
const Key_t&
|
2019-01-24 01:31:02 +00:00
|
|
|
OurKey() const override
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
return ourKey;
|
|
|
|
}
|
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp::AbstractRouter*
|
2019-02-05 00:41:33 +00:00
|
|
|
GetRouter() const override
|
|
|
|
{
|
|
|
|
return router;
|
|
|
|
}
|
2019-01-24 01:31:02 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
TXHolder< service::Address, service::IntroSet, service::Address::Hash >
|
|
|
|
pendingIntrosetLookups;
|
|
|
|
|
|
|
|
TXHolder< service::Tag, service::IntroSet, service::Tag::Hash >
|
|
|
|
pendingTagLookups;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
TXHolder< RouterID, RouterContact, RouterID::Hash > pendingRouterLookups;
|
2018-08-29 20:40:26 +00:00
|
|
|
|
|
|
|
TXHolder< RouterID, RouterID, RouterID::Hash > pendingExploreLookups;
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
NextID()
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
return ++ids;
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
2019-01-24 01:31:02 +00:00
|
|
|
Now() const override;
|
2018-10-29 16:48:36 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
void
|
|
|
|
ExploreNetworkVia(const Key_t& peer);
|
2018-11-28 15:27:36 +00:00
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
private:
|
2018-08-29 20:40:26 +00:00
|
|
|
void
|
|
|
|
ScheduleCleanupTimer();
|
|
|
|
|
|
|
|
void
|
|
|
|
CleanupTX();
|
|
|
|
|
|
|
|
uint64_t ids;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
Key_t ourKey;
|
2018-07-17 04:37:50 +00:00
|
|
|
}; // namespace llarp
|
|
|
|
} // namespace dht
|
2018-07-12 13:43:37 +00:00
|
|
|
} // namespace llarp
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
struct llarp_dht_context
|
|
|
|
{
|
|
|
|
llarp::dht::Context impl;
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router* parent;
|
|
|
|
llarp_dht_context(llarp::Router* router);
|
2018-07-11 13:20:14 +00:00
|
|
|
};
|
|
|
|
|
2018-08-27 13:44:16 +00:00
|
|
|
#endif
|