lokinet/include/llarp/service/endpoint.hpp

223 lines
5.7 KiB
C++
Raw Normal View History

2018-07-09 17:32:11 +00:00
#ifndef LLARP_SERVICE_ENDPOINT_HPP
#define LLARP_SERVICE_ENDPOINT_HPP
2018-07-19 04:58:39 +00:00
#include <llarp/codel.hpp>
2018-07-09 17:32:11 +00:00
#include <llarp/pathbuilder.hpp>
#include <llarp/service/Identity.hpp>
2018-07-19 04:58:39 +00:00
#include <llarp/service/protocol.hpp>
2018-07-09 17:32:11 +00:00
namespace llarp
{
namespace service
{
struct Endpoint : public llarp_pathbuilder_context
2018-07-09 17:32:11 +00:00
{
2018-07-18 03:10:21 +00:00
/// minimum interval for publishing introsets
static const llarp_time_t INTROSET_PUBLISH_INTERVAL =
DEFAULT_PATH_LIFETIME / 4;
static const llarp_time_t INTROSET_PUBLISH_RETRY_INTERVAL = 5000;
2018-07-09 17:32:11 +00:00
Endpoint(const std::string& nickname, llarp_router* r);
~Endpoint();
bool
SetOption(const std::string& k, const std::string& v);
void
2018-07-18 22:50:05 +00:00
Tick(llarp_time_t now);
2018-07-19 04:58:39 +00:00
llarp_logic*
Logic();
llarp_crypto*
Crypto();
llarp_threadpool*
Worker();
2018-07-22 23:14:29 +00:00
llarp_router*
Router()
{
return m_Router;
}
2018-07-09 17:32:11 +00:00
bool
Start();
2018-07-16 03:32:13 +00:00
std::string
Name() const;
2018-07-18 03:10:21 +00:00
bool
2018-07-18 22:50:05 +00:00
ShouldPublishDescriptors(llarp_time_t now) const;
2018-07-18 03:10:21 +00:00
bool
PublishIntroSet(llarp_router* r);
bool
HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg);
2018-07-12 18:21:44 +00:00
bool
HandleHiddenServiceFrame(const llarp::service::ProtocolFrame* msg);
2018-07-12 18:21:44 +00:00
/// return true if we have an established path to a hidden service
bool
HasPathToService(const Address& remote) const;
/// return false if we don't have a path to the service
/// return true if we did and we removed it
bool
ForgetPathToService(const Address& remote);
2018-07-22 23:14:29 +00:00
Identity*
GetIdentity()
{
return &m_Identity;
}
2018-07-19 04:58:39 +00:00
2018-07-12 18:21:44 +00:00
/// context needed to initiate an outbound hidden service session
struct OutboundContext : public llarp_pathbuilder_context
{
OutboundContext(const IntroSet& introSet, Endpoint* parent);
2018-07-12 18:21:44 +00:00
~OutboundContext();
/// the remote hidden service's curren intro set
IntroSet currentIntroSet;
2018-07-22 23:14:29 +00:00
/// the current selected intro
Introduction selectedIntro;
/// update the current selected intro to be a new best introduction
void
ShiftIntroduction();
2018-07-12 18:21:44 +00:00
/// tick internal state
/// return true to remove otherwise don't remove
bool
Tick(llarp_time_t now);
2018-07-12 18:21:44 +00:00
/// encrypt asynchronously and send to remote endpoint from us
2018-07-19 04:58:39 +00:00
void
AsyncEncryptAndSendTo(llarp_buffer_t D, ProtocolType protocol);
2018-07-12 18:21:44 +00:00
/// issues a lookup to find the current intro set of the remote service
void
UpdateIntroSet();
bool
SelectHop(llarp_nodedb* db, llarp_rc* prev, llarp_rc* cur, size_t hop);
2018-07-12 18:21:44 +00:00
bool
HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg);
private:
2018-07-19 04:58:39 +00:00
void
2018-07-22 23:14:29 +00:00
AsyncEncrypt(llarp_buffer_t payload);
2018-07-19 04:58:39 +00:00
void
2018-07-22 23:14:29 +00:00
AsyncGenIntro(llarp_buffer_t payload);
/// send a fully encrypted hidden service frame
2018-07-19 04:58:39 +00:00
void
2018-07-22 23:14:29 +00:00
Send(ProtocolFrame& f);
2018-07-19 04:58:39 +00:00
uint64_t sequenceNo = 0;
2018-07-12 18:21:44 +00:00
llarp::SharedSecret sharedKey;
Endpoint* m_Parent;
};
// passed a sendto context when we have a path established otherwise
// nullptr if the path was not made before the timeout
typedef std::function< void(OutboundContext*) > PathEnsureHook;
/// return false if we have already called this function before for this
/// address
bool
EnsurePathToService(const Address& remote, PathEnsureHook h,
uint64_t timeoutMS);
virtual bool
HandleAuthenticatedDataFrom(const Address& remote, llarp_buffer_t data)
{
/// TODO: imlement me
return true;
}
void
PutNewOutboundContext(const IntroSet& introset);
2018-07-18 03:10:21 +00:00
protected:
virtual void
IntroSetPublishFail();
virtual void
IntroSetPublished();
IServiceLookup*
GenerateLookupByTag(const Tag& tag);
void
PrefetchServicesByTag(const Tag& tag);
private:
uint64_t
GenTXID();
2018-07-09 17:32:11 +00:00
private:
llarp_router* m_Router;
std::string m_Keyfile;
std::string m_Name;
2018-07-09 17:32:11 +00:00
Identity m_Identity;
2018-07-12 18:21:44 +00:00
std::unordered_map< Address, OutboundContext*, Address::Hash >
m_RemoteSessions;
2018-07-22 23:14:29 +00:00
std::unordered_map< Address, PathEnsureHook, Address::Hash >
m_PendingServiceLookups;
2018-07-18 03:10:21 +00:00
uint64_t m_CurrentPublishTX = 0;
llarp_time_t m_LastPublish = 0;
llarp_time_t m_LastPublishAttempt = 0;
/// our introset
service::IntroSet m_IntroSet;
/// pending remote service lookups by id
2018-07-18 03:10:21 +00:00
std::unordered_map< uint64_t, service::IServiceLookup* > m_PendingLookups;
/// hidden service tag
Tag m_Tag;
/// prefetch descriptors for these hidden service tags
std::set< Tag > m_PrefetchTags;
struct CachedTagResult : public IServiceLookup
{
2018-07-19 04:58:39 +00:00
const static llarp_time_t TTL = 10000;
llarp_time_t lastRequest = 0;
llarp_time_t lastModified;
uint64_t pendingTX = 0;
2018-07-18 03:10:21 +00:00
std::set< IntroSet > result;
Tag tag;
2018-07-19 04:58:39 +00:00
CachedTagResult(const Tag& t, llarp_time_t now)
: lastModified(now), tag(t)
2018-07-18 03:10:21 +00:00
{
}
~CachedTagResult();
2018-07-18 22:50:05 +00:00
void
Expire(llarp_time_t now);
2018-07-18 03:10:21 +00:00
bool
ShouldRefresh(llarp_time_t now) const
{
2018-07-19 04:58:39 +00:00
if(now <= lastRequest)
return false;
return (now - lastRequest) > TTL && pendingTX == 0;
2018-07-18 03:10:21 +00:00
}
llarp::routing::IMessage*
BuildRequestMessage();
bool
HandleResponse(const std::set< IntroSet >& results);
};
std::unordered_map< Tag, CachedTagResult, Tag::Hash > m_PrefetchedTags;
2018-07-09 17:32:11 +00:00
};
} // namespace service
} // namespace llarp
#endif