lokinet/llarp/service/endpoint.hpp

480 lines
13 KiB
C++
Raw Normal View History

#pragma once
#include <llarp.h>
#include <llarp/dht/messages/gotrouter.hpp>
#include <llarp/ev/ev.hpp>
#include <llarp/exit/session.hpp>
#include <llarp/net/ip_range_map.hpp>
#include <llarp/net/net.hpp>
#include <llarp/path/path.hpp>
#include <llarp/path/pathbuilder.hpp>
#include "address.hpp"
#include "handler.hpp"
#include "identity.hpp"
#include "pendingbuffer.hpp"
#include "protocol.hpp"
#include "sendcontext.hpp"
#include "session.hpp"
#include "lookup.hpp"
#include <llarp/hook/ihook.hpp>
#include <llarp/util/compare_ptr.hpp>
#include "endpoint_types.hpp"
#include "auth.hpp"
2020-05-28 11:07:32 +00:00
2019-03-29 01:02:41 +00:00
// minimum time between introset shifts
2018-09-18 17:48:26 +00:00
#ifndef MIN_SHIFT_INTERVAL
2020-02-24 19:40:45 +00:00
#define MIN_SHIFT_INTERVAL 5s
2018-09-18 17:48:26 +00:00
#endif
2018-07-09 17:32:11 +00:00
namespace llarp
{
namespace service
{
struct AsyncKeyExchange;
struct Context;
struct EndpointState;
struct OutboundContext;
struct IConvoEventListener
{
~IConvoEventListener() = default;
/// called when we have obtained the introset
/// called with nullptr on not found or when we
/// talking to a snode
virtual void
FoundIntroSet(const IntroSet*) = 0;
/// called when we found the RC we need for alignment
virtual void
FoundRC(const RouterContact) = 0;
/// called when we have successfully built an aligned path
virtual void GotAlignedPath(path::Path_ptr) = 0;
/// called when we have established a session or conversation
virtual void
MadeConvo(const ConvoTag) = 0;
};
using ConvoEventListener_ptr = std::shared_ptr<IConvoEventListener>;
2020-02-24 19:40:45 +00:00
/// minimum interval for publishing introsets
static constexpr auto INTROSET_PUBLISH_INTERVAL =
std::chrono::milliseconds(path::default_lifetime) / 4;
2020-02-24 19:40:45 +00:00
static constexpr auto INTROSET_PUBLISH_RETRY_INTERVAL = 5s;
static constexpr auto INTROSET_LOOKUP_RETRY_COOLDOWN = 3s;
struct Endpoint : public path::Builder, public ILookupHolder, public IDataHandler
2018-07-09 17:32:11 +00:00
{
static const size_t MAX_OUTBOUND_CONTEXT_COUNT = 4;
Endpoint(AbstractRouter* r, Context* parent);
2019-07-30 23:42:13 +00:00
~Endpoint() override;
2018-07-09 17:32:11 +00:00
/// return true if we are ready to recv packets from the void
bool
IsReady() const;
2019-11-28 23:08:02 +00:00
void
QueueRecvData(RecvDataEvent ev) override;
/// return true if our introset has expired intros
bool
IntrosetIsStale() const;
/// construct parameters for notify hooks
virtual std::unordered_map<std::string, std::string>
NotifyParams() const;
virtual util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const;
2019-02-08 19:43:25 +00:00
void
SetHandler(IDataHandler* h);
2018-08-16 14:34:15 +00:00
virtual bool
Configure(const NetworkConfig& conf, const DnsConfig& dnsConf);
2018-07-09 17:32:11 +00:00
2019-07-30 23:42:13 +00:00
void
2019-04-23 16:13:22 +00:00
Tick(llarp_time_t now) override;
/// return true if we have a resolvable ip address
virtual bool
HasIfAddr() const
{
return false;
}
2020-08-21 15:07:37 +00:00
virtual std::string
GetIfName() const = 0;
2021-02-17 19:26:39 +00:00
std::optional<ConvoTag>
GetBestConvoTagForService(Address addr) const;
/// inject vpn io
/// return false if not supported
virtual bool
InjectVPN(llarp_vpn_io*, llarp_vpn_ifaddr_info)
{
return false;
}
/// get our ifaddr if it is set
2019-06-11 16:44:05 +00:00
virtual huint128_t
GetIfAddr() const
{
2019-07-01 13:44:25 +00:00
return {0};
}
virtual void
Thaw(){};
2019-07-30 23:42:13 +00:00
void
2019-05-07 17:46:38 +00:00
ResetInternalState() override;
/// loop (via router)
/// use when sending any data on a path
const EventLoop_ptr&
Loop();
AbstractRouter*
Router();
2018-07-22 23:14:29 +00:00
virtual bool
LoadKeyFile();
2018-08-16 14:34:15 +00:00
virtual bool
2018-07-09 17:32:11 +00:00
Start();
2019-07-30 23:42:13 +00:00
std::string
2019-03-25 01:54:37 +00:00
Name() const override;
2018-07-16 03:32:13 +00:00
2018-07-18 03:10:21 +00:00
bool
2018-12-24 21:10:35 +00:00
ShouldPublishDescriptors(llarp_time_t now) const override;
2018-07-18 03:10:21 +00:00
2019-03-30 13:02:10 +00:00
void
HandlePathDied(path::Path_ptr p) override;
2019-03-30 13:02:10 +00:00
2018-07-18 03:10:21 +00:00
bool
2020-01-27 21:30:41 +00:00
PublishIntroSet(const EncryptedIntroSet& i, AbstractRouter* r) override;
2018-07-18 03:10:21 +00:00
2018-09-18 14:48:06 +00:00
bool
PublishIntroSetVia(
const EncryptedIntroSet& i, AbstractRouter* r, path::Path_ptr p, uint64_t relayOrder);
2018-09-18 14:48:06 +00:00
bool
HandleGotIntroMessage(std::shared_ptr<const dht::GotIntroMessage> msg) override;
2018-08-10 21:34:11 +00:00
bool
HandleGotRouterMessage(std::shared_ptr<const dht::GotRouterMessage> msg) override;
2018-08-10 21:34:11 +00:00
bool
HandleGotNameMessage(std::shared_ptr<const dht::GotNameMessage> msg) override;
2018-07-12 18:21:44 +00:00
bool
HandleHiddenServiceFrame(path::Path_ptr p, const service::ProtocolFrame& msg);
2018-07-12 18:21:44 +00:00
2020-05-28 11:07:32 +00:00
void
SetEndpointAuth(std::shared_ptr<IAuthPolicy> policy);
/// sets how we authenticate with remote address
void
SetAuthInfoForEndpoint(Address remote, AuthInfo info);
virtual huint128_t
ObtainIPForAddr(const AlignedBuffer<32>& addr, bool serviceNode) = 0;
2018-10-23 18:06:55 +00:00
2019-07-01 13:44:25 +00:00
// virtual bool
// HasServiceAddress(const AlignedBuffer< 32 >& addr) const = 0;
2018-08-10 03:51:38 +00:00
/// return true if we have a pending job to build to a hidden service but
/// it's not done yet
bool
HasPendingPathToService(const Address& remote) const;
2018-09-18 17:48:26 +00:00
bool
HandleDataMessage(
path::Path_ptr path, const PathID_t from, std::shared_ptr<ProtocolMessage> msg) override;
2018-08-09 19:02:17 +00:00
/// handle packet io from service node or hidden service to frontend
virtual bool
HandleInboundPacket(
const ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t, uint64_t seqno) = 0;
2019-07-01 13:44:25 +00:00
// virtual bool
// HandleWriteIPPacket(const llarp_buffer_t& pkt,
// std::function< huint128_t(void) > getFromIP) = 0;
2018-11-29 13:12:35 +00:00
bool
ProcessDataMessage(std::shared_ptr<ProtocolMessage> msg);
2018-09-18 17:48:26 +00:00
2018-08-10 21:34:11 +00:00
/// ensure that we know a router, looks up if it doesn't
void
EnsureRouterIsKnown(const RouterID& router);
/// lookup a router via closest path
bool
2019-05-03 13:15:03 +00:00
LookupRouterAnon(RouterID router, RouterLookupHandler handler);
bool
LookupNameAsync(std::string name, std::function<void(std::optional<Address>)> resultHandler);
2019-04-25 17:15:56 +00:00
/// called on event loop pump
virtual void
Pump(llarp_time_t now);
/// stop this endpoint
bool
Stop() override;
const Identity&
2018-11-21 14:30:14 +00:00
GetIdentity() const
2018-07-22 23:14:29 +00:00
{
return m_Identity;
2018-07-22 23:14:29 +00:00
}
2018-07-19 04:58:39 +00:00
void
MapExitRange(IPRange range, service::Address exit);
void
UnmapExitRange(IPRange range);
2018-08-04 02:59:32 +00:00
void
2018-12-24 21:10:35 +00:00
PutLookup(IServiceLookup* lookup, uint64_t txid) override;
2018-08-04 02:59:32 +00:00
void
HandlePathBuilt(path::Path_ptr path) override;
2018-08-22 15:52:10 +00:00
bool
EnsureConvo(const AlignedBuffer<32> addr, bool snode, ConvoEventListener_ptr ev);
2018-11-29 13:12:35 +00:00
bool
2021-01-01 18:55:31 +00:00
SendTo(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t);
bool
HandleDataDrop(path::Path_ptr p, const PathID_t& dst, uint64_t s);
bool
CheckPathIsDead(path::Path_ptr p, llarp_time_t latency);
using PendingBufferQueue = std::deque<PendingBuffer>;
2018-08-22 15:52:10 +00:00
size_t
RemoveAllConvoTagsFor(service::Address remote);
2020-02-18 16:00:45 +00:00
bool
WantsOutboundSession(const Address&) const override;
void
MarkAddressOutbound(const Address&) override;
bool
2020-05-17 17:44:00 +00:00
ShouldBundleRC() const override
{
return false;
}
2020-05-21 14:18:23 +00:00
void
BlacklistSNode(const RouterID snode) override;
/// return true if we have a convotag as an exit session
/// or as a hidden service session
/// set addr and issnode
///
/// return false if we don't have either
bool
GetEndpointWithConvoTag(const ConvoTag t, AlignedBuffer<32>& addr, bool& issnode) const;
2019-03-08 17:00:13 +00:00
bool
HasConvoTag(const ConvoTag& t) const override;
2019-03-08 14:36:24 +00:00
bool
ShouldBuildMore(llarp_time_t now) const override;
2018-07-12 18:21:44 +00:00
// passed a sendto context when we have a path established otherwise
// nullptr if the path was not made before the timeout
using PathEnsureHook = std::function<void(Address, OutboundContext*)>;
2018-07-12 18:21:44 +00:00
/// return false if we have already called this function before for this
/// address
bool
EnsurePathToService(const Address remote, PathEnsureHook h, llarp_time_t timeoutMS);
2018-07-12 18:21:44 +00:00
using SNodeEnsureHook = std::function<void(const RouterID, exit::BaseSession_ptr)>;
2018-11-29 13:12:35 +00:00
/// ensure a path to a service node by public key
bool
2019-07-01 13:44:25 +00:00
EnsurePathToSNode(const RouterID remote, SNodeEnsureHook h);
2018-11-29 13:12:35 +00:00
/// return true if this endpoint is trying to lookup this router right now
bool
HasPendingRouterLookup(const RouterID remote) const;
2018-11-29 13:12:35 +00:00
bool
2019-07-01 13:44:25 +00:00
HasPathToSNode(const RouterID remote) const;
2018-11-29 13:12:35 +00:00
2018-08-09 19:02:17 +00:00
void
PutSenderFor(const ConvoTag& tag, const ServiceInfo& info, bool inbound) override;
bool
HasInboundConvo(const Address& addr) const override;
2018-08-09 19:02:17 +00:00
bool
GetCachedSessionKeyFor(const ConvoTag& remote, SharedSecret& secret) const override;
2018-08-09 19:02:17 +00:00
void
PutCachedSessionKeyFor(const ConvoTag& remote, const SharedSecret& secret) override;
2018-08-09 19:02:17 +00:00
bool
2018-12-24 21:10:35 +00:00
GetSenderFor(const ConvoTag& remote, ServiceInfo& si) const override;
2018-08-09 19:02:17 +00:00
void
2018-12-24 21:10:35 +00:00
PutIntroFor(const ConvoTag& remote, const Introduction& intro) override;
2018-08-09 19:02:17 +00:00
bool
2018-12-24 21:10:35 +00:00
GetIntroFor(const ConvoTag& remote, Introduction& intro) const override;
2018-08-09 19:02:17 +00:00
2019-03-08 16:00:45 +00:00
void
RemoveConvoTag(const ConvoTag& remote) override;
2019-09-19 20:28:12 +00:00
void
MarkConvoTagActive(const ConvoTag& remote) override;
2019-02-21 16:45:33 +00:00
void
PutReplyIntroFor(const ConvoTag& remote, const Introduction& intro) override;
2019-02-21 16:45:33 +00:00
bool
GetReplyIntroFor(const ConvoTag& remote, Introduction& intro) const override;
2019-02-21 16:45:33 +00:00
2018-08-09 19:02:17 +00:00
bool
GetConvoTagsForService(const Address& si, std::set<ConvoTag>& tag) const override;
2018-08-09 19:02:17 +00:00
void
PutNewOutboundContext(const IntroSet& introset);
2019-04-19 16:02:32 +00:00
uint64_t
GetSeqNoForConvo(const ConvoTag& tag);
bool
HasExit() const;
std::optional<std::vector<RouterContact>>
GetHopsForBuild() override;
std::optional<std::vector<RouterContact>>
GetHopsForBuildWithEndpoint(RouterID endpoint);
2019-05-10 16:19:33 +00:00
virtual void
PathBuildStarted(path::Path_ptr path) override;
2018-07-18 03:10:21 +00:00
virtual void
IntroSetPublishFail();
virtual void
IntroSetPublished();
2020-05-28 11:07:32 +00:00
void
2020-06-17 13:07:05 +00:00
AsyncProcessAuthMessage(
std::shared_ptr<ProtocolMessage> msg, std::function<void(AuthResult)> hook);
2020-05-28 11:07:32 +00:00
void
2021-01-01 18:55:31 +00:00
SendAuthResult(path::Path_ptr path, PathID_t replyPath, ConvoTag tag, AuthResult st);
2020-05-28 11:07:32 +00:00
uint64_t
GenTXID();
const std::set<RouterID>&
SnodeBlacklist() const;
bool
SendToServiceOrQueue(
const service::Address& addr, const llarp_buffer_t& payload, ProtocolType t);
bool
SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& payload);
2020-03-07 01:20:11 +00:00
std::optional<AuthInfo>
MaybeGetAuthInfoForEndpoint(service::Address addr);
2020-03-07 01:20:11 +00:00
protected:
/// parent context that owns this endpoint
Context* const context;
2019-06-11 16:44:05 +00:00
virtual bool
SupportsV6() const = 0;
void
2019-11-05 16:58:53 +00:00
RegenAndPublishIntroSet(bool forceRebuild = false);
2018-07-18 03:10:21 +00:00
IServiceLookup*
GenerateLookupByTag(const Tag& tag);
void
PrefetchServicesByTag(const Tag& tag);
private:
void
HandleVerifyGotRouter(dht::GotRouterMessage_constptr msg, RouterID id, bool valid);
2018-08-10 21:34:11 +00:00
bool
2020-05-01 22:25:18 +00:00
OnLookup(const service::Address& addr, std::optional<IntroSet> i, const RouterID& endpoint);
2018-08-10 21:34:11 +00:00
2018-08-09 19:02:17 +00:00
bool
2018-08-18 14:01:21 +00:00
DoNetworkIsolation(bool failed);
2018-08-09 19:02:17 +00:00
2018-08-16 14:34:15 +00:00
virtual bool
SetupNetworking()
{
// XXX: override me
return true;
}
2018-08-18 14:01:21 +00:00
virtual bool
IsolationFailed()
{
// XXX: override me
return false;
}
protected:
IDataHandler* m_DataHandler = nullptr;
2018-08-09 19:02:17 +00:00
Identity m_Identity;
net::IPRangeMap<service::Address> m_ExitMap;
hooks::Backend_ptr m_OnUp;
hooks::Backend_ptr m_OnDown;
hooks::Backend_ptr m_OnReady;
bool m_PublishIntroSet = true;
2020-06-01 17:58:45 +00:00
std::unique_ptr<EndpointState> m_state;
2020-06-02 21:10:42 +00:00
std::shared_ptr<IAuthPolicy> m_AuthPolicy;
std::unordered_map<Address, AuthInfo, Address::Hash> m_RemoteAuthInfos;
/// (lns name, optional exit range, optional auth info) for looking up on startup
std::unordered_map<std::string, std::pair<std::optional<IPRange>, std::optional<AuthInfo>>>
m_StartupLNSMappings;
RecvPacketQueue_t m_InboundTrafficQueue;
2021-01-01 18:55:31 +00:00
public:
SendMessageQueue_t m_SendQueue;
2021-01-01 18:55:31 +00:00
protected:
2019-11-28 23:08:02 +00:00
void
FlushRecvData();
friend struct EndpointUtil;
// clang-format off
const IntroSet& introSet() const;
IntroSet& introSet();
2019-05-07 08:29:47 +00:00
using ConvoMap = std::unordered_map< ConvoTag, Session, ConvoTag::Hash >;
const ConvoMap& Sessions() const;
ConvoMap& Sessions();
// clang-format on
thread::Queue<RecvDataEvent> m_RecvQueue;
2018-07-09 17:32:11 +00:00
};
using Endpoint_ptr = std::shared_ptr<Endpoint>;
2018-07-09 17:32:11 +00:00
} // namespace service
} // namespace llarp