mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
5.9 KiB
C++
228 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include <llarp/constants/path.hpp>
|
|
#include <llarp/path/abstracthophandler.hpp>
|
|
#include <llarp/path/path_types.hpp>
|
|
#include <llarp/routing/handler.hpp>
|
|
#include <llarp/router_id.hpp>
|
|
#include <llarp/util/compare_ptr.hpp>
|
|
#include <llarp/util/thread/queue.hpp>
|
|
|
|
namespace llarp
|
|
{
|
|
struct LR_CommitRecord;
|
|
|
|
namespace dht
|
|
{
|
|
struct GotIntroMessage;
|
|
}
|
|
|
|
namespace path
|
|
{
|
|
struct TransitHopInfo
|
|
{
|
|
TransitHopInfo() = default;
|
|
TransitHopInfo(const RouterID& down, const LR_CommitRecord& record);
|
|
|
|
PathID_t txID, rxID;
|
|
RouterID upstream;
|
|
RouterID downstream;
|
|
|
|
std::string
|
|
ToString() const;
|
|
};
|
|
|
|
inline bool
|
|
operator==(const TransitHopInfo& lhs, const TransitHopInfo& rhs)
|
|
{
|
|
return std::tie(lhs.txID, lhs.rxID, lhs.upstream, lhs.downstream)
|
|
== std::tie(rhs.txID, rhs.rxID, rhs.upstream, rhs.downstream);
|
|
}
|
|
|
|
inline bool
|
|
operator!=(const TransitHopInfo& lhs, const TransitHopInfo& rhs)
|
|
{
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
inline bool
|
|
operator<(const TransitHopInfo& lhs, const TransitHopInfo& rhs)
|
|
{
|
|
return std::tie(lhs.txID, lhs.rxID, lhs.upstream, lhs.downstream)
|
|
< std::tie(rhs.txID, rhs.rxID, rhs.upstream, rhs.downstream);
|
|
}
|
|
|
|
struct TransitHop : public AbstractHopHandler,
|
|
public routing::AbstractRoutingMessageHandler,
|
|
std::enable_shared_from_this<TransitHop>
|
|
{
|
|
TransitHop();
|
|
|
|
TransitHopInfo info;
|
|
SharedSecret pathKey;
|
|
ShortHash nonceXOR;
|
|
llarp_time_t started = 0s;
|
|
// 10 minutes default
|
|
llarp_time_t lifetime = DEFAULT_LIFETIME;
|
|
llarp_proto_version_t version;
|
|
llarp_time_t m_LastActivity = 0s;
|
|
|
|
PathID_t
|
|
RXID() const override
|
|
{
|
|
return info.rxID;
|
|
}
|
|
|
|
void
|
|
Stop();
|
|
|
|
bool destroy = false;
|
|
|
|
bool
|
|
operator<(const TransitHop& other) const
|
|
{
|
|
return info < other.info;
|
|
}
|
|
|
|
bool
|
|
IsEndpoint(const RouterID& us) const
|
|
{
|
|
return info.upstream == us;
|
|
}
|
|
|
|
llarp_time_t
|
|
ExpireTime() const;
|
|
|
|
llarp_time_t
|
|
LastRemoteActivityAt() const override
|
|
{
|
|
return m_LastActivity;
|
|
}
|
|
|
|
bool
|
|
HandleLRSM(uint64_t status, std::array<EncryptedFrame, 8>& frames, Router* r) override;
|
|
|
|
std::string
|
|
ToString() const;
|
|
|
|
bool
|
|
Expired(llarp_time_t now) const override;
|
|
|
|
bool
|
|
ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const override
|
|
{
|
|
return now >= ExpireTime() - dlt;
|
|
}
|
|
|
|
// send routing message when end of path
|
|
bool
|
|
SendRoutingMessage(const routing::AbstractRoutingMessage& msg, Router* r) override;
|
|
|
|
// handle routing message when end of path
|
|
bool
|
|
HandleRoutingMessage(const routing::AbstractRoutingMessage& msg, Router* r);
|
|
|
|
bool
|
|
HandleDataDiscardMessage(const routing::DataDiscardMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandlePathConfirmMessage(Router* r);
|
|
|
|
bool
|
|
HandlePathConfirmMessage(const routing::PathConfirmMessage& msg, Router* r) override;
|
|
bool
|
|
HandlePathTransferMessage(const routing::PathTransferMessage& msg, Router* r) override;
|
|
bool
|
|
HandlePathLatencyMessage(const routing::PathLatencyMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleObtainExitMessage(const routing::ObtainExitMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleUpdateExitVerifyMessage(
|
|
const routing::UpdateExitVerifyMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleTransferTrafficMessage(const routing::TransferTrafficMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleUpdateExitMessage(const routing::UpdateExitMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleGrantExitMessage(const routing::GrantExitMessage& msg, Router* r) override;
|
|
bool
|
|
HandleRejectExitMessage(const routing::RejectExitMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleCloseExitMessage(const routing::CloseExitMessage& msg, Router* r) override;
|
|
|
|
bool
|
|
HandleHiddenServiceFrame(const service::ProtocolFrameMessage& /*frame*/) override
|
|
{
|
|
/// TODO: implement me
|
|
LogWarn("Got hidden service data on transit hop");
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
HandleGotIntroMessage(const dht::GotIntroMessage& msg);
|
|
|
|
bool
|
|
HandleDHTMessage(const dht::AbstractDHTMessage& msg, Router* r) override;
|
|
|
|
void
|
|
FlushUpstream(Router* r) override;
|
|
|
|
void
|
|
FlushDownstream(Router* r) override;
|
|
|
|
void
|
|
QueueDestroySelf(Router* r);
|
|
|
|
protected:
|
|
void
|
|
UpstreamWork(TrafficQueue_t queue, Router* r) override;
|
|
|
|
void
|
|
DownstreamWork(TrafficQueue_t queue, Router* r) override;
|
|
|
|
void
|
|
HandleAllUpstream(std::vector<RelayUpstreamMessage> msgs, Router* r) override;
|
|
|
|
void
|
|
HandleAllDownstream(std::vector<RelayDownstreamMessage> msgs, Router* r) override;
|
|
|
|
private:
|
|
void
|
|
SetSelfDestruct();
|
|
|
|
std::set<std::shared_ptr<TransitHop>, ComparePtr<std::shared_ptr<TransitHop>>> m_FlushOthers;
|
|
thread::Queue<RelayUpstreamMessage> m_UpstreamGather;
|
|
thread::Queue<RelayDownstreamMessage> m_DownstreamGather;
|
|
std::atomic<uint32_t> m_UpstreamWorkCounter;
|
|
std::atomic<uint32_t> m_DownstreamWorkCounter;
|
|
};
|
|
} // namespace path
|
|
|
|
template <>
|
|
constexpr inline bool IsToStringFormattable<path::TransitHop> = true;
|
|
template <>
|
|
constexpr inline bool IsToStringFormattable<path::TransitHopInfo> = true;
|
|
|
|
} // namespace llarp
|
|
|
|
namespace std
|
|
{
|
|
template <>
|
|
struct hash<llarp::path::TransitHopInfo>
|
|
{
|
|
std::size_t
|
|
operator()(llarp::path::TransitHopInfo const& a) const
|
|
{
|
|
hash<llarp::RouterID> RHash{};
|
|
hash<llarp::PathID_t> PHash{};
|
|
return RHash(a.upstream) ^ RHash(a.downstream) ^ PHash(a.txID) ^ PHash(a.rxID);
|
|
}
|
|
};
|
|
} // namespace std
|