lokinet/llarp/dht/context.hpp

270 lines
8.0 KiB
C++
Raw Normal View History

2019-01-22 01:14:02 +00:00
#ifndef LLARP_DHT_CONTEXT
#define LLARP_DHT_CONTEXT
2018-12-12 00:48:54 +00:00
#include <dht/bucket.hpp>
#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>
#include <util/time.hpp>
2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
#include <set>
namespace llarp
{
struct AbstractRouter;
namespace dht
{
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;
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;
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;
virtual llarp::AbstractRouter*
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-02-08 19:43:25 +00:00
struct Context final : public AbstractContext, public util::IStateful
{
Context();
~Context()
{
}
2019-02-11 17:14:43 +00:00
util::StatusObject
ExtractStatus() const override;
2019-02-08 19:43:25 +00:00
llarp::Crypto*
Crypto() const override;
2018-08-29 20:40:26 +00:00
/// on behalf of whoasked request introset for target from dht router with
/// key askpeer
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-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
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
LookupRouter(const RouterID& target, RouterLookupHandler result) override
2018-08-30 18:48:43 +00:00
{
Key_t askpeer;
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;
}
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
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
bool
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
std::set< service::IntroSet >
2018-08-29 20:40:26 +00:00
FindRandomIntroSetsWithTagExcluding(
const service::Tag& tag, size_t max = 2,
const std::set< service::IntroSet >& excludes = {}) override;
2018-08-29 20:40:26 +00:00
/// handle rc lookup from requester for target
void
LookupRouterRelayed(const Key_t& requester, uint64_t txid,
const Key_t& target, bool recursive,
std::vector< std::unique_ptr< IMessage > >& replies);
2019-01-19 18:16:40 +00:00
/// relay a dht message from a local path to the main network
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-08-29 20:40:26 +00:00
/// initialize dht context and explore every exploreInterval milliseconds
void
Init(const Key_t& us, AbstractRouter* router,
llarp_time_t exploreInterval);
2018-08-29 20:40:26 +00:00
/// get localally stored introset by service address
const llarp::service::IntroSet*
GetIntroSetByServiceAddress(const llarp::service::Address& addr) const;
static void
handle_cleaner_timer(void* user, uint64_t orig, uint64_t left);
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);
llarp::AbstractRouter* router;
// for router contacts
2019-01-22 01:14:02 +00:00
std::unique_ptr< Bucket< RCNode > > nodes;
// for introduction sets
2019-01-22 01:14:02 +00:00
std::unique_ptr< Bucket< ISNode > > services;
bool allowTransit;
2019-02-05 00:41:33 +00:00
Bucket< RCNode >*
Nodes() const override
{
return nodes.get();
}
const Key_t&
OurKey() const override
{
return ourKey;
}
llarp::AbstractRouter*
2019-02-05 00:41:33 +00:00
GetRouter() const override
{
return router;
}
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-08-29 20:40:26 +00:00
return ++ids;
}
2018-10-29 16:48:36 +00:00
llarp_time_t
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
private:
2018-08-29 20:40:26 +00:00
void
ScheduleCleanupTimer();
void
CleanupTX();
uint64_t ids;
Key_t ourKey;
2018-07-17 04:37:50 +00:00
}; // namespace llarp
} // namespace dht
} // namespace llarp
struct llarp_dht_context
{
llarp::dht::Context impl;
llarp::AbstractRouter* parent;
llarp_dht_context(llarp::AbstractRouter* router);
};
#endif