lokinet/llarp/path/path.hpp

411 lines
11 KiB
C++
Raw Normal View History

2018-06-10 14:05:48 +00:00
#ifndef LLARP_PATH_HPP
#define LLARP_PATH_HPP
2018-12-12 02:04:32 +00:00
2019-06-17 23:19:39 +00:00
#include <constants/path.hpp>
#include <crypto/encrypted_frame.hpp>
#include <crypto/types.hpp>
#include <messages/relay.hpp>
2019-06-17 23:19:39 +00:00
#include <path/ihophandler.hpp>
2019-01-11 01:19:36 +00:00
#include <path/path_types.hpp>
#include <path/pathbuilder.hpp>
#include <path/pathset.hpp>
#include <router_id.hpp>
2018-12-12 02:04:32 +00:00
#include <routing/handler.hpp>
#include <routing/message.hpp>
#include <service/intro.hpp>
#include <util/aligned.hpp>
#include <util/compare_ptr.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/threading.hpp>
#include <util/time.hpp>
2018-06-10 14:05:48 +00:00
#include <algorithm>
#include <functional>
2018-06-19 17:11:24 +00:00
#include <list>
#include <map>
2018-06-10 14:05:48 +00:00
#include <unordered_map>
#include <vector>
namespace llarp
{
class Logic;
struct AbstractRouter;
struct LR_CommitMessage;
namespace path
2018-06-10 14:05:48 +00:00
{
2019-06-17 23:19:39 +00:00
struct TransitHop;
struct TransitHopInfo;
using TransitHop_ptr = std::shared_ptr< TransitHop >;
/// configuration for a single hop when building a path
2019-04-19 15:10:26 +00:00
struct PathHopConfig
2018-06-22 00:25:30 +00:00
{
/// path id
PathID_t txID, rxID;
// router contact of router
2018-08-30 18:48:43 +00:00
RouterContact rc;
// temp public encryption key
SecretKey commkey;
/// shared secret at this hop
SharedSecret shared;
/// hash of shared secret used for nonce mutation
ShortHash nonceXOR;
/// next hop's router id
RouterID upstream;
/// nonce for key exchange
TunnelNonce nonce;
// lifetime
2019-04-05 14:58:22 +00:00
llarp_time_t lifetime = default_lifetime;
2019-02-11 17:14:43 +00:00
util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const;
};
2018-06-22 00:25:30 +00:00
2019-06-17 23:19:39 +00:00
inline bool
operator<(const PathHopConfig& lhs, const PathHopConfig& rhs)
{
return std::tie(lhs.txID, lhs.rxID, lhs.rc, lhs.upstream, lhs.lifetime)
< std::tie(rhs.txID, rhs.rxID, rhs.rc, rhs.upstream, rhs.lifetime);
}
/// A path we made
2019-06-20 16:22:29 +00:00
struct Path final : public IHopHandler,
public routing::IMessageHandler,
public std::enable_shared_from_this< Path >
{
using BuildResultHookFunc = std::function< void(Path_ptr) >;
using CheckForDeadFunc = std::function< bool(Path_ptr, llarp_time_t) >;
using DropHandlerFunc =
std::function< bool(Path_ptr, const PathID_t&, uint64_t) >;
using HopList = std::vector< PathHopConfig >;
using DataHandlerFunc =
std::function< bool(Path_ptr, const service::ProtocolFrame&) >;
using ExitUpdatedFunc = std::function< bool(Path_ptr) >;
using ExitClosedFunc = std::function< bool(Path_ptr) >;
using ExitTrafficHandlerFunc =
std::function< bool(Path_ptr, const llarp_buffer_t&, uint64_t) >;
2018-11-14 18:02:27 +00:00
/// (path, backoff) backoff is 0 on success
using ObtainedExitHandler = std::function< bool(Path_ptr, llarp_time_t) >;
HopList hops;
2019-04-23 16:13:22 +00:00
PathSet* const m_PathSet;
service::Introduction intro;
2019-11-05 16:58:53 +00:00
llarp_time_t buildStarted = 0;
2018-06-19 17:11:24 +00:00
2018-11-14 18:02:27 +00:00
Path(const std::vector< RouterContact >& routers, PathSet* parent,
PathRole startingRoles);
2019-02-11 17:14:43 +00:00
util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const;
2019-02-08 19:43:25 +00:00
2018-11-14 18:02:27 +00:00
PathRole
Role() const
{
return _role;
}
struct Hash
{
size_t
operator()(const Path& p) const
{
const auto& tx = p.hops[0].txID;
const auto& rx = p.hops[0].rxID;
const auto& r = p.hops[0].upstream;
const size_t rhash =
std::accumulate(r.begin(), r.end(), 0, std::bit_xor< size_t >());
return std::accumulate(rx.begin(), rx.begin(),
std::accumulate(tx.begin(), tx.end(), rhash,
std::bit_xor< size_t >()),
std::bit_xor< size_t >());
}
};
struct Ptr_Hash
{
size_t
operator()(const std::shared_ptr< Path >& p) const
{
if(p == nullptr)
return 0;
return Hash{}(*p);
}
};
bool
operator<(const Path& other) const
{
2019-05-08 14:59:28 +00:00
return hops < other.hops;
}
2018-11-16 14:22:13 +00:00
void
2018-11-16 14:21:23 +00:00
MarkActive(llarp_time_t now)
{
2018-12-20 15:03:37 +00:00
m_LastRecvMessage = std::max(now, m_LastRecvMessage);
2018-11-16 14:21:23 +00:00
}
2018-11-21 12:31:36 +00:00
/// return true if ALL of the specified roles are supported
2018-11-14 18:02:27 +00:00
bool
2018-11-21 12:31:36 +00:00
SupportsAllRoles(PathRole roles) const
2018-11-14 18:02:27 +00:00
{
return (_role & roles) == roles;
}
2018-11-21 12:31:36 +00:00
/// return true if ANY of the specified roles are supported
bool
SupportsAnyRoles(PathRole roles) const
{
2019-05-07 18:15:22 +00:00
return roles == ePathRoleAny || (_role | roles) != 0;
2018-11-21 12:31:36 +00:00
}
/// clear role bits
void
ClearRoles(PathRole roles)
{
_role &= ~roles;
}
2018-11-14 18:02:27 +00:00
PathStatus
Status() const
{
return _status;
}
2018-06-19 17:11:24 +00:00
std::string
HopsString() const;
llarp_time_t
LastRemoteActivityAt() const override
{
return m_LastRecvMessage;
}
bool
HandleLRSM(uint64_t status, std::array< EncryptedFrame, 8 >& frames,
AbstractRouter* r) override;
void
SetBuildResultHook(BuildResultHookFunc func);
2018-06-22 00:25:30 +00:00
2018-11-14 18:02:27 +00:00
void
SetExitTrafficHandler(ExitTrafficHandlerFunc handler)
{
m_ExitTrafficHandler = handler;
}
2018-11-14 12:23:08 +00:00
void
SetCloseExitFunc(ExitClosedFunc handler)
{
m_ExitClosed = handler;
}
void
SetUpdateExitFunc(ExitUpdatedFunc handler)
{
m_ExitUpdated = handler;
}
void
SetDataHandler(DataHandlerFunc func)
{
m_DataHandler = func;
}
void
SetDropHandler(DropHandlerFunc func)
{
m_DropHandler = func;
}
void
SetDeadChecker(CheckForDeadFunc func)
{
m_CheckForDead = func;
}
void
2018-10-29 16:48:36 +00:00
EnterState(PathStatus st, llarp_time_t now);
2018-08-14 21:17:18 +00:00
llarp_time_t
ExpireTime() const
{
return buildStarted + hops[0].lifetime;
}
2018-11-14 18:02:27 +00:00
bool
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const override
2018-11-14 18:02:27 +00:00
{
return now >= (ExpireTime() - dlt);
}
bool
Expired(llarp_time_t now) const override;
2018-06-10 14:05:48 +00:00
2019-05-06 14:54:05 +00:00
/// build a new path on the same set of hops as us
/// regenerates keys
void
Rebuild();
2020-01-03 11:04:47 +00:00
bool
HandleUpstream(const llarp_buffer_t& X, const TunnelNonce& Y,
AbstractRouter*) override;
bool
HandleDownstream(const llarp_buffer_t& X, const TunnelNonce& Y,
AbstractRouter*) override;
void
Tick(llarp_time_t now, AbstractRouter* r);
bool
SendRoutingMessage(const routing::IMessage& msg,
AbstractRouter* r) override;
bool
HandleObtainExitMessage(const routing::ObtainExitMessage& msg,
AbstractRouter* r) override;
2018-11-14 12:23:08 +00:00
bool
HandleUpdateExitVerifyMessage(const routing::UpdateExitVerifyMessage& msg,
AbstractRouter* r) override;
2018-11-14 12:23:08 +00:00
bool
HandleTransferTrafficMessage(const routing::TransferTrafficMessage& msg,
AbstractRouter* r) override;
bool
HandleUpdateExitMessage(const routing::UpdateExitMessage& msg,
AbstractRouter* r) override;
bool
HandleCloseExitMessage(const routing::CloseExitMessage& msg,
AbstractRouter* r) override;
bool
HandleGrantExitMessage(const routing::GrantExitMessage& msg,
AbstractRouter* r) override;
bool
HandleRejectExitMessage(const routing::RejectExitMessage& msg,
AbstractRouter* r) override;
2018-06-19 17:11:24 +00:00
bool
HandleDataDiscardMessage(const routing::DataDiscardMessage& msg,
AbstractRouter* r) override;
bool
HandlePathConfirmMessage(AbstractRouter* r);
bool
HandlePathConfirmMessage(const routing::PathConfirmMessage& msg,
AbstractRouter* r) override;
2018-06-19 17:11:24 +00:00
bool
HandlePathLatencyMessage(const routing::PathLatencyMessage& msg,
AbstractRouter* r) override;
2018-06-19 17:11:24 +00:00
bool
HandlePathTransferMessage(const routing::PathTransferMessage& msg,
AbstractRouter* r) override;
2018-06-26 16:23:43 +00:00
bool
HandleHiddenServiceFrame(const service::ProtocolFrame& frame) override;
2018-07-09 17:32:11 +00:00
bool
HandleGotIntroMessage(const dht::GotIntroMessage& msg);
2018-07-09 17:32:11 +00:00
2018-06-26 16:23:43 +00:00
bool
HandleDHTMessage(const dht::IMessage& msg, AbstractRouter* r) override;
2018-06-22 00:25:30 +00:00
bool
HandleRoutingMessage(const llarp_buffer_t& buf, AbstractRouter* r);
2018-06-22 00:25:30 +00:00
bool
IsReady() const;
// Is this deprecated?
// nope not deprecated :^DDDD
2018-12-18 19:03:50 +00:00
PathID_t
TXID() const;
RouterID
Endpoint() const;
PubKey
EndpointPubKey() const;
bool
IsEndpoint(const RouterID& router, const PathID_t& path) const;
2018-12-18 19:03:50 +00:00
PathID_t
RXID() const;
2018-06-19 17:11:24 +00:00
RouterID
Upstream() const;
std::string
Name() const;
2018-11-14 19:34:17 +00:00
void
AddObtainExitHandler(ObtainedExitHandler handler)
{
m_ObtainedExitHooks.push_back(handler);
}
2018-11-14 18:02:27 +00:00
bool
SendExitRequest(const routing::ObtainExitMessage& msg, AbstractRouter* r);
2018-11-14 18:02:27 +00:00
bool
SendExitClose(const routing::CloseExitMessage& msg, AbstractRouter* r);
2019-09-05 17:39:09 +00:00
void
FlushUpstream(AbstractRouter* r) override;
void
FlushDownstream(AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
protected:
void
UpstreamWork(TrafficQueue_ptr queue, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
DownstreamWork(TrafficQueue_ptr queue, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
HandleAllUpstream(std::vector< RelayUpstreamMessage > msgs,
AbstractRouter* r) override;
void
HandleAllDownstream(std::vector< RelayDownstreamMessage > msgs,
AbstractRouter* r) override;
private:
2018-11-14 18:02:27 +00:00
/// call obtained exit hooks
bool
InformExitResult(llarp_time_t b);
BuildResultHookFunc m_BuiltHook;
DataHandlerFunc m_DataHandler;
DropHandlerFunc m_DropHandler;
CheckForDeadFunc m_CheckForDead;
2018-11-14 12:23:08 +00:00
ExitUpdatedFunc m_ExitUpdated;
ExitClosedFunc m_ExitClosed;
2018-11-14 18:02:27 +00:00
ExitTrafficHandlerFunc m_ExitTrafficHandler;
std::vector< ObtainedExitHandler > m_ObtainedExitHooks;
llarp_time_t m_LastRecvMessage = 0;
2018-06-26 16:23:43 +00:00
llarp_time_t m_LastLatencyTestTime = 0;
uint64_t m_LastLatencyTestID = 0;
2018-11-14 12:23:08 +00:00
uint64_t m_UpdateExitTX = 0;
uint64_t m_CloseExitTX = 0;
2018-11-14 18:02:27 +00:00
uint64_t m_ExitObtainTX = 0;
PathStatus _status;
PathRole _role;
2020-01-03 11:04:47 +00:00
util::DecayingHashSet< TunnelNonce > m_UpstreamBloomFilter;
util::DecayingHashSet< TunnelNonce > m_DownstreamBloomFilter;
};
} // namespace path
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