lokinet/include/llarp/path.hpp

311 lines
6.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>
2018-06-19 17:11:24 +00:00
#include <llarp/endpoint.hpp>
2018-06-22 00:25:30 +00:00
#include <llarp/messages/relay.hpp>
2018-06-10 14:05:48 +00:00
#include <llarp/messages/relay_commit.hpp>
#include <llarp/path_types.hpp>
#include <llarp/router_id.hpp>
#include <llarp/routing/handler.hpp>
#include <llarp/routing/message.hpp>
2018-06-10 14:05:48 +00:00
2018-06-19 17:11:24 +00:00
#include <list>
#include <map>
2018-06-10 14:05:48 +00:00
#include <mutex>
#include <unordered_map>
#include <vector>
2018-06-22 00:25:30 +00:00
#define DEFAULT_PATH_LIFETIME (10 * 60 * 1000)
2018-06-10 14:05:48 +00:00
namespace llarp
{
struct TransitHopInfo
{
TransitHopInfo() = default;
2018-06-22 00:25:30 +00:00
TransitHopInfo(const TransitHopInfo& other);
2018-06-10 14:05:48 +00:00
TransitHopInfo(const RouterID& down, const LR_CommitRecord& record);
2018-06-22 00:25:30 +00:00
PathID_t txID, rxID;
2018-06-10 14:05:48 +00:00
RouterID upstream;
RouterID downstream;
friend std::ostream&
operator<<(std::ostream& out, const TransitHopInfo& info)
{
2018-06-22 00:25:30 +00:00
out << "<tx=" << info.txID << " rx=" << info.rxID;
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-22 00:25:30 +00:00
return txID == other.txID && rxID == other.rxID
&& 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
{
2018-06-22 00:25:30 +00:00
return txID < other.txID || rxID < other.rxID || upstream < other.upstream
2018-06-18 22:03:50 +00:00
|| downstream < other.downstream;
}
2018-06-10 14:05:48 +00:00
struct Hash
{
std::size_t
operator()(TransitHopInfo const& a) const
{
2018-06-22 00:25:30 +00:00
std::size_t idx0, idx1, idx2, idx3;
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-22 00:25:30 +00:00
memcpy(&idx2, a.txID, sizeof(std::size_t));
memcpy(&idx3, a.rxID, sizeof(std::size_t));
2018-06-18 22:03:50 +00:00
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-22 00:25:30 +00:00
struct IHopHandler
{
virtual ~IHopHandler(){};
virtual bool
Expired(llarp_time_t now) const = 0;
virtual bool
SendRoutingMessage(const llarp::routing::IMessage* msg,
llarp_router* r) = 0;
// handle data in upstream direction
virtual bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r) = 0;
// handle data in downstream direction
virtual bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y,
llarp_router* r) = 0;
};
struct TransitHop : public IHopHandler
2018-06-10 14:05:48 +00:00
{
2018-06-12 16:45:12 +00:00
TransitHop() = default;
2018-06-22 00:25:30 +00:00
TransitHop(const TransitHop& other);
2018-06-18 22:03:50 +00:00
TransitHopInfo info;
SharedSecret pathKey;
2018-06-22 00:25:30 +00:00
llarp_time_t started = 0;
2018-06-18 22:03:50 +00:00
// 10 minutes default
2018-06-22 00:25:30 +00:00
llarp_time_t lifetime = DEFAULT_PATH_LIFETIME;
2018-06-10 14:05:48 +00:00
llarp_proto_version_t version;
2018-06-18 22:03:50 +00:00
2018-06-22 00:25:30 +00:00
friend std::ostream&
operator<<(std::ostream& out, const TransitHop& h)
{
return out << "[TransitHop " << h.info << " started=" << h.started
<< " lifetime=" << h.lifetime << "]";
}
2018-06-18 22:03:50 +00:00
bool
Expired(llarp_time_t now) const;
2018-06-19 17:11:24 +00:00
2018-06-22 00:25:30 +00:00
bool
SendRoutingMessage(const llarp::routing::IMessage* msg, llarp_router* r);
2018-06-19 17:11:24 +00:00
2018-06-22 00:25:30 +00:00
// handle data in upstream direction
bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);
// handle data in downstream direction
bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);
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-22 00:25:30 +00:00
PathID_t txID, rxID;
2018-06-18 22:03:50 +00:00
// 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-22 00:25:30 +00:00
// lifetime
llarp_time_t lifetime = DEFAULT_PATH_LIFETIME;
2018-06-18 22:03:50 +00:00
~PathHopConfig();
PathHopConfig();
2018-06-10 14:05:48 +00:00
};
2018-06-19 17:11:24 +00:00
enum PathStatus
{
ePathBuilding,
ePathEstablished,
ePathTimeout,
ePathExpired
};
/// A path we made
struct Path : public IHopHandler, public llarp::routing::IMessageHandler
2018-06-10 14:05:48 +00:00
{
typedef std::vector< PathHopConfig > HopList;
HopList hops;
llarp_time_t buildStarted;
2018-06-19 17:11:24 +00:00
PathStatus status;
2018-06-18 22:03:50 +00:00
Path(llarp_path_hops* path);
2018-06-19 17:11:24 +00:00
2018-06-22 00:25:30 +00:00
bool
Expired(llarp_time_t now) const;
2018-06-19 17:11:24 +00:00
2018-06-22 00:25:30 +00:00
bool
SendRoutingMessage(const llarp::routing::IMessage* msg, llarp_router* r);
bool
HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r);
2018-06-23 00:00:44 +00:00
bool
HandleHiddenServiceData(llarp_buffer_t buf);
2018-06-22 00:25:30 +00:00
// handle data in upstream direction
bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);
// handle data in downstream direction
bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);
2018-06-19 17:11:24 +00:00
2018-06-22 05:44:19 +00:00
// Is this deprecated?
2018-06-19 17:11:24 +00:00
const PathID_t&
TXID() const;
const PathID_t&
RXID() const;
2018-06-19 17:11:24 +00:00
RouterID
2018-06-22 00:25:30 +00:00
Upstream() const;
protected:
llarp::routing::InboundMessageParser m_InboundMessageParser;
2018-06-10 14:05:48 +00:00
};
enum PathBuildStatus
{
ePathBuildSuccess,
ePathBuildTimeout,
ePathBuildReject
};
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-22 00:25:30 +00:00
PutTransitHop(TransitHop* hop);
IHopHandler*
GetByUpstream(const RouterID& id, const PathID_t& path);
IHopHandler*
GetByDownstream(const RouterID& id, const PathID_t& path);
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
bool
HopIsUs(const PubKey& k) const;
2018-06-22 00:25:30 +00:00
bool
HandleLRUM(const RelayUpstreamMessage* msg);
bool
HandleLRDM(const RelayDownstreamMessage* msg);
2018-06-19 17:11:24 +00:00
void
AddOwnPath(Path* p);
2018-06-22 00:25:30 +00:00
typedef std::multimap< PathID_t, TransitHop* > TransitHopsMap_t;
2018-06-10 14:05:48 +00:00
2018-06-12 16:45:12 +00:00
typedef std::pair< std::mutex, TransitHopsMap_t > SyncTransitMap_t;
2018-06-19 17:11:24 +00:00
typedef std::map< PathID_t, Path* > OwnedPathsMap_t;
typedef std::pair< std::mutex, OwnedPathsMap_t > SyncOwnedPathsMap_t;
2018-06-12 16:45:12 +00:00
llarp_threadpool*
Worker();
llarp_crypto*
Crypto();
llarp_logic*
Logic();
2018-06-22 00:25:30 +00:00
llarp_router*
Router();
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-22 00:25:30 +00:00
SyncTransitMap_t m_Paths;
2018-06-19 17:11:24 +00:00
SyncOwnedPathsMap_t m_OurPaths;
2018-06-12 16:45:12 +00:00
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