lokinet/include/llarp/path.hpp

349 lines
7.8 KiB
C++
Raw Normal View History

2018-06-10 14:05:48 +00:00
#ifndef LLARP_PATH_HPP
#define LLARP_PATH_HPP
2018-06-18 22:03:50 +00:00
#include <llarp/path.h>
2018-06-10 14:05:48 +00:00
#include <llarp/router.h>
#include <llarp/time.h>
#include <llarp/aligned.hpp>
#include <llarp/crypto.hpp>
#include <llarp/messages/relay_ack.hpp>
#include <llarp/messages/relay_commit.hpp>
#include <llarp/path_types.hpp>
#include <llarp/router_id.hpp>
#include <mutex>
#include <unordered_map>
#include <vector>
namespace llarp
{
struct TransitHopInfo
{
TransitHopInfo() = default;
TransitHopInfo(const RouterID& down, const LR_CommitRecord& record);
2018-06-18 22:03:50 +00:00
PathID_t pathID;
2018-06-10 14:05:48 +00:00
RouterID upstream;
RouterID downstream;
friend std::ostream&
operator<<(std::ostream& out, const TransitHopInfo& info)
{
2018-06-18 22:03:50 +00:00
out << "<Transit Hop id=" << info.pathID;
2018-06-10 14:05:48 +00:00
out << " upstream=" << info.upstream << " downstream=" << info.downstream;
return out << ">";
}
bool
operator==(const TransitHopInfo& other) const
{
2018-06-18 22:03:50 +00:00
return pathID == other.pathID && upstream == other.upstream
&& downstream == other.downstream;
2018-06-10 14:05:48 +00:00
}
bool
operator!=(const TransitHopInfo& other) const
{
return !(*this == other);
}
2018-06-18 22:03:50 +00:00
bool
operator<(const TransitHopInfo& other) const
{
return pathID < other.pathID || upstream < other.upstream
|| downstream < other.downstream;
}
2018-06-10 14:05:48 +00:00
struct Hash
{
std::size_t
operator()(TransitHopInfo const& a) const
{
2018-06-18 22:03:50 +00:00
std::size_t idx0, idx1, idx2;
2018-06-10 14:05:48 +00:00
memcpy(&idx0, a.upstream, sizeof(std::size_t));
memcpy(&idx1, a.downstream, sizeof(std::size_t));
2018-06-18 22:03:50 +00:00
memcpy(&idx2, a.pathID, sizeof(std::size_t));
return idx0 ^ idx1 ^ idx2;
2018-06-10 14:05:48 +00:00
}
};
};
2018-06-18 22:03:50 +00:00
struct PathIDHash
{
std::size_t
operator()(const PathID_t& a) const
{
std::size_t idx0;
memcpy(&idx0, a, sizeof(std::size_t));
return idx0;
}
};
2018-06-10 14:05:48 +00:00
struct TransitHop
{
2018-06-12 16:45:12 +00:00
TransitHop() = default;
2018-06-18 22:03:50 +00:00
TransitHopInfo info;
SharedSecret pathKey;
2018-06-10 14:05:48 +00:00
llarp_time_t started;
2018-06-18 22:03:50 +00:00
// 10 minutes default
llarp_time_t lifetime = 360000;
2018-06-10 14:05:48 +00:00
llarp_proto_version_t version;
2018-06-18 22:03:50 +00:00
bool
Expired(llarp_time_t now) const;
2018-06-10 14:05:48 +00:00
};
2018-06-18 22:03:50 +00:00
/// configuration for a single hop when building a path
2018-06-10 14:05:48 +00:00
struct PathHopConfig
{
/// path id
2018-06-18 22:03:50 +00:00
PathID_t pathID;
// router contact of router
llarp_rc router;
// temp public encryption key
SecretKey commkey;
2018-06-10 14:05:48 +00:00
/// shared secret at this hop
SharedSecret shared;
2018-06-18 22:03:50 +00:00
/// next hop's router id
RouterID upstream;
2018-06-10 14:05:48 +00:00
/// nonce for key exchange
TunnelNonce nonce;
2018-06-18 22:03:50 +00:00
~PathHopConfig();
PathHopConfig();
2018-06-10 14:05:48 +00:00
};
struct Path
{
typedef std::vector< PathHopConfig > HopList;
HopList hops;
llarp_time_t buildStarted;
2018-06-18 22:03:50 +00:00
Path(llarp_path_hops* path);
2018-06-10 14:05:48 +00:00
};
template < typename User >
struct AsyncPathKeyExchangeContext
{
2018-06-18 22:03:50 +00:00
Path* path = nullptr;
typedef void (*Handler)(AsyncPathKeyExchangeContext< User >*);
2018-06-10 14:05:48 +00:00
User* user = nullptr;
Handler result = nullptr;
size_t idx = 0;
llarp_threadpool* worker = nullptr;
2018-06-18 22:03:50 +00:00
llarp_logic* logic = nullptr;
llarp_crypto* crypto = nullptr;
LR_CommitMessage LRCM;
static void
HandleDone(void* user)
{
AsyncPathKeyExchangeContext< User >* ctx =
static_cast< AsyncPathKeyExchangeContext< User >* >(user);
ctx->result(ctx);
delete ctx;
}
2018-06-10 14:05:48 +00:00
static void
GenerateNextKey(void* user)
{
AsyncPathKeyExchangeContext< User >* ctx =
static_cast< AsyncPathKeyExchangeContext< User >* >(user);
2018-06-18 22:03:50 +00:00
auto& hop = ctx->path->hops[ctx->idx];
// generate key
ctx->crypto->encryption_keygen(hop.commkey);
// do key exchange
if(!ctx->crypto->dh_client(hop.shared, hop.router.enckey, hop.nonce,
hop.commkey))
{
llarp::Error("Failed to generate shared key for path build");
delete ctx;
return;
}
// randomize hop's path id
hop.pathID.Randomize();
LR_CommitRecord record;
auto& frame = ctx->LRCM.frames[ctx->idx];
2018-06-10 14:05:48 +00:00
++ctx->idx;
2018-06-18 22:03:50 +00:00
if(ctx->idx < ctx->path->hops.size())
2018-06-10 14:05:48 +00:00
{
2018-06-18 22:03:50 +00:00
hop.upstream = ctx->path->hops[ctx->idx].router.pubkey;
2018-06-10 14:05:48 +00:00
}
else
{
2018-06-18 22:03:50 +00:00
hop.upstream = hop.router.pubkey;
}
// generate record
if(!record.BEncode(frame.Buffer()))
{
// failed to encode?
llarp::Error("Failed to generate Commit Record");
delete ctx;
return;
2018-06-10 14:05:48 +00:00
}
2018-06-18 22:03:50 +00:00
if(ctx->idx < ctx->path->hops.size())
{
// next hop
llarp_threadpool_queue_job(ctx->worker, {ctx, &GenerateNextKey});
}
else
{
// farthest hop
llarp_logic_queue_job(ctx->logic, {ctx, &HandleDone});
}
2018-06-10 14:05:48 +00:00
}
2018-06-18 22:03:50 +00:00
AsyncPathKeyExchangeContext(llarp_crypto* c) : crypto(c)
2018-06-10 14:05:48 +00:00
{
}
/// Generate all keys asynchronously and call hadler when done
void
2018-06-18 22:03:50 +00:00
AsyncGenerateKeys(Path* p, llarp_logic* l, llarp_threadpool* pool, User* u,
Handler func)
2018-06-10 14:05:48 +00:00
{
2018-06-18 22:03:50 +00:00
path = p;
logic = l;
2018-06-10 14:05:48 +00:00
user = u;
result = func;
worker = pool;
2018-06-18 22:03:50 +00:00
for(size_t idx = 0; idx < MAXHOPS; ++idx)
{
LRCM.frames.emplace_back(256);
LRCM.frames.back().Randomize();
}
2018-06-10 14:05:48 +00:00
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
}
};
enum PathBuildStatus
{
ePathBuildSuccess,
ePathBuildTimeout,
ePathBuildReject
};
/// path selection algorithm
struct IPathSelectionAlgorithm
{
virtual ~IPathSelectionAlgorithm(){};
/// select full path given an empty hop list to end at target
virtual bool
SelectFullPathTo(Path::HopList& hops, const RouterID& target) = 0;
/// report to path builder the result of a path build
/// can be used to "improve" path building algoirthm in the
/// future
virtual void
ReportPathBuildStatus(const Path::HopList& hops, const RouterID& target,
PathBuildStatus status){};
};
class PathBuildJob
{
public:
PathBuildJob(llarp_router* router, IPathSelectionAlgorithm* selector);
~PathBuildJob();
void
Start();
private:
typedef AsyncPathKeyExchangeContext< PathBuildJob > KeyExchanger;
LR_CommitMessage*
BuildLRCM();
static void
KeysGenerated(KeyExchanger* ctx);
llarp_router* router;
IPathSelectionAlgorithm* m_HopSelector;
KeyExchanger m_KeyExchanger;
};
/// a pool of paths for a hidden service
struct PathPool
{
PathPool(llarp_router* router);
~PathPool();
/// build a new path to a router by identity key
PathBuildJob*
BuildNewPathTo(const RouterID& router);
};
struct PathContext
{
PathContext(llarp_router* router);
~PathContext();
2018-06-18 22:03:50 +00:00
/// called from router tick function
void
ExpirePaths();
2018-06-10 14:05:48 +00:00
void
AllowTransit();
void
RejectTransit();
bool
2018-06-18 22:03:50 +00:00
AllowingTransit() const;
2018-06-10 14:05:48 +00:00
bool
2018-06-18 22:03:50 +00:00
HasTransitHop(const TransitHopInfo& info);
2018-06-10 14:05:48 +00:00
bool
2018-06-18 22:03:50 +00:00
HandleRelayCommit(const LR_CommitMessage* msg);
2018-06-10 14:05:48 +00:00
2018-06-12 16:45:12 +00:00
void
2018-06-18 22:03:50 +00:00
PutTransitHop(const TransitHop& hop);
2018-06-12 16:45:12 +00:00
bool
2018-06-18 22:03:50 +00:00
ForwardLRCM(const RouterID& nextHop, std::deque< EncryptedFrame >& frames);
2018-06-12 16:45:12 +00:00
2018-06-18 22:03:50 +00:00
void
ForwradLRUM(const PathID_t& id, const RouterID& from, llarp_buffer_t X,
const TunnelNonce& nonce);
void
ForwradLRDM(const PathID_t& id, const RouterID& from, llarp_buffer_t X,
const TunnelNonce& nonce);
2018-06-12 16:45:12 +00:00
bool
HopIsUs(const PubKey& k) const;
2018-06-18 22:03:50 +00:00
typedef std::unordered_multimap< PathID_t, TransitHop, PathIDHash >
2018-06-10 14:05:48 +00:00
TransitHopsMap_t;
2018-06-12 16:45:12 +00:00
typedef std::pair< std::mutex, TransitHopsMap_t > SyncTransitMap_t;
llarp_threadpool*
Worker();
llarp_crypto*
Crypto();
llarp_logic*
Logic();
2018-06-12 16:45:12 +00:00
byte_t*
EncryptionSecretKey();
2018-06-18 22:03:50 +00:00
const byte_t*
OurRouterID() const;
2018-06-10 14:05:48 +00:00
private:
llarp_router* m_Router;
2018-06-12 16:45:12 +00:00
SyncTransitMap_t m_TransitPaths;
2018-06-10 14:05:48 +00:00
bool m_AllowTransit;
};
2018-06-18 22:03:50 +00:00
} // namespace llarp
2018-06-10 14:05:48 +00:00
2018-06-14 20:13:07 +00:00
#endif