lokinet/llarp/path/transit_hop.hpp

237 lines
6.1 KiB
C++
Raw Normal View History

#pragma once
2019-06-17 23:19:39 +00:00
#include <llarp/constants/path.hpp>
#include "ihophandler.hpp"
#include "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>
2019-06-17 23:19:39 +00:00
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::ostream&
print(std::ostream& stream, int level, int spaces) 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);
}
inline std::ostream&
operator<<(std::ostream& out, const TransitHopInfo& info)
{
return info.print(out, -1, -1);
}
2019-09-05 17:39:09 +00:00
struct TransitHop : public IHopHandler,
public routing::IMessageHandler,
std::enable_shared_from_this<TransitHop>
2019-06-17 23:19:39 +00:00
{
TransitHop();
TransitHopInfo info;
SharedSecret pathKey;
ShortHash nonceXOR;
2020-02-24 19:40:45 +00:00
llarp_time_t started = 0s;
2019-06-17 23:19:39 +00:00
// 10 minutes default
llarp_time_t lifetime = default_lifetime;
2019-06-17 23:19:39 +00:00
llarp_proto_version_t version;
2020-02-24 19:40:45 +00:00
llarp_time_t m_LastActivity = 0s;
2019-06-17 23:19:39 +00:00
2021-04-07 12:52:17 +00:00
PathID_t
RXID() const override
{
return info.rxID;
}
2019-11-29 00:34:31 +00:00
void
Stop();
bool destroy = false;
2019-11-21 14:48:31 +00:00
bool
operator<(const TransitHop& other) const
{
return info < other.info;
}
2019-06-17 23:19:39 +00:00
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, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
std::ostream&
print(std::ostream& stream, int level, int spaces) 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::IMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
// handle routing message when end of path
bool
HandleRoutingMessage(const routing::IMessage& msg, AbstractRouter* r);
bool
HandleDataDiscardMessage(const routing::DataDiscardMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandlePathConfirmMessage(AbstractRouter* r);
2019-06-17 23:19:39 +00:00
bool
HandlePathConfirmMessage(const routing::PathConfirmMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandlePathTransferMessage(
const routing::PathTransferMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandlePathLatencyMessage(const routing::PathLatencyMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleObtainExitMessage(const routing::ObtainExitMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleUpdateExitVerifyMessage(
const routing::UpdateExitVerifyMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleTransferTrafficMessage(
const routing::TransferTrafficMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleUpdateExitMessage(const routing::UpdateExitMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleGrantExitMessage(const routing::GrantExitMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleRejectExitMessage(const routing::RejectExitMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
HandleCloseExitMessage(const routing::CloseExitMessage& msg, AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
bool
2020-02-23 02:21:38 +00:00
HandleHiddenServiceFrame(const service::ProtocolFrame& /*frame*/) override
2019-06-17 23:19:39 +00:00
{
/// TODO: implement me
LogWarn("Got hidden service data on transit hop");
return false;
}
bool
HandleGotIntroMessage(const dht::GotIntroMessage& msg);
bool
HandleDHTMessage(const dht::IMessage& msg, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
FlushUpstream(AbstractRouter* r) override;
void
FlushDownstream(AbstractRouter* r) override;
2019-06-17 23:19:39 +00:00
2021-06-07 12:39:38 +00:00
void
QueueDestroySelf(AbstractRouter* r);
2019-09-05 17:39:09 +00:00
protected:
void
UpstreamWork(TrafficQueue_t queue, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
DownstreamWork(TrafficQueue_t queue, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
HandleAllUpstream(std::vector<RelayUpstreamMessage> msgs, AbstractRouter* r) override;
2019-09-05 17:39:09 +00:00
void
HandleAllDownstream(std::vector<RelayDownstreamMessage> msgs, AbstractRouter* 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;
2019-06-17 23:19:39 +00:00
};
inline std::ostream&
operator<<(std::ostream& out, const TransitHop& h)
{
return h.print(out, -1, -1);
}
} // namespace path
} // 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