mirror of https://github.com/oxen-io/lokinet
Nuked superfluous interface classes
RIP: - i_link_manager - i_outbound_message_handler - i_gossiper - i_outbound_session_maker - i_rc_lookup_handlerpull/2204/head
parent
821cbeaacf
commit
7f8207d5d3
@ -1,109 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "endpoint.hpp"
|
||||
#include "server.hpp"
|
||||
|
||||
#include <llarp/util/types.hpp>
|
||||
#include <llarp/peerstats/peer_db.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
struct llarp_buffer_t;
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
|
||||
// TODO: do we still want this?
|
||||
enum class SessionResult
|
||||
{
|
||||
Establish,
|
||||
Timeout,
|
||||
RouterNotFound,
|
||||
InvalidRouter,
|
||||
NoLink,
|
||||
EstablishFail
|
||||
};
|
||||
|
||||
constexpr std::string_view
|
||||
ToString(SessionResult sr)
|
||||
{
|
||||
return sr == llarp::SessionResult::Establish ? "success"sv
|
||||
: sr == llarp::SessionResult::Timeout ? "timeout"sv
|
||||
: sr == llarp::SessionResult::NoLink ? "no link"sv
|
||||
: sr == llarp::SessionResult::InvalidRouter ? "invalid router"sv
|
||||
: sr == llarp::SessionResult::RouterNotFound ? "not found"sv
|
||||
: sr == llarp::SessionResult::EstablishFail ? "establish failed"sv
|
||||
: "???"sv;
|
||||
}
|
||||
template <>
|
||||
constexpr inline bool IsToStringFormattable<SessionResult> = true;
|
||||
|
||||
struct RouterContact;
|
||||
struct AbstractLinkSession;
|
||||
struct IOutboundSessionMaker;
|
||||
struct RouterID;
|
||||
|
||||
struct ILinkManager
|
||||
{
|
||||
virtual ~ILinkManager() = default;
|
||||
|
||||
virtual bool
|
||||
SendTo(
|
||||
const RouterID& remote,
|
||||
const llarp_buffer_t& buf,
|
||||
AbstractLinkSession::CompletionHandler completed,
|
||||
uint16_t priority = 0) = 0;
|
||||
|
||||
virtual bool
|
||||
HaveConnection(const RouterID& remote, bool client_only = false) const = 0;
|
||||
|
||||
/// return true if we have a connection to the remote and it is not a relay,
|
||||
/// else return false
|
||||
virtual bool
|
||||
HaveClientConnection(const RouterID& remote) const = 0;
|
||||
|
||||
virtual void
|
||||
Stop() = 0;
|
||||
|
||||
virtual void
|
||||
PersistSessionUntil(const RouterID& remote, llarp_time_t until) = 0;
|
||||
|
||||
/// close all connections to this peer
|
||||
/// remove all link layer commits
|
||||
virtual void
|
||||
DeregisterPeer(RouterID remote) = 0;
|
||||
|
||||
virtual size_t
|
||||
NumberOfConnectedRouters(bool clients_only = false) const = 0;
|
||||
|
||||
virtual size_t
|
||||
NumberOfConnectedClients() const = 0;
|
||||
|
||||
virtual bool
|
||||
GetRandomConnectedRouter(RouterContact& router) const = 0;
|
||||
|
||||
virtual void
|
||||
CheckPersistingSessions(llarp_time_t now) = 0;
|
||||
|
||||
virtual void
|
||||
updatePeerDb(std::shared_ptr<PeerDb> peerDb) = 0;
|
||||
|
||||
virtual util::StatusObject
|
||||
ExtractStatus() const = 0;
|
||||
|
||||
// Do an RC lookup for the given RouterID; the result will trigger
|
||||
// Connect(RouterContact) on success (or if we already have it), and will
|
||||
// trigger connection failure callback on lookup failure.
|
||||
virtual void
|
||||
Connect(RouterID router) = 0;
|
||||
|
||||
// Establish a connection to the remote `rc`.
|
||||
//
|
||||
// Connection established/failed callbacks should be invoked when either happens,
|
||||
// but this function should do nothing if already connected.
|
||||
virtual void
|
||||
Connect(RouterContact rc) = 0;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
@ -1,419 +1,417 @@
|
||||
#include "path.hpp"
|
||||
#include "path_context.hpp"
|
||||
|
||||
#include <llarp/router/outbound_message_handler.hpp>
|
||||
#include <llarp/messages/relay_commit.hpp>
|
||||
#include "path.hpp"
|
||||
#include <llarp/router/abstractrouter.hpp>
|
||||
#include <llarp/router/i_outbound_message_handler.hpp>
|
||||
#include <llarp/router/outbound_message_handler.hpp>
|
||||
|
||||
namespace llarp
|
||||
namespace llarp::path
|
||||
{
|
||||
namespace path
|
||||
{
|
||||
static constexpr auto DefaultPathBuildLimit = 500ms;
|
||||
static constexpr auto DefaultPathBuildLimit = 500ms;
|
||||
|
||||
PathContext::PathContext(AbstractRouter* router)
|
||||
: m_Router(router), m_AllowTransit(false), m_PathLimits(DefaultPathBuildLimit)
|
||||
{}
|
||||
PathContext::PathContext(AbstractRouter* router)
|
||||
: m_Router(router), m_AllowTransit(false), m_PathLimits(DefaultPathBuildLimit)
|
||||
{}
|
||||
|
||||
void
|
||||
PathContext::AllowTransit()
|
||||
{
|
||||
m_AllowTransit = true;
|
||||
}
|
||||
void
|
||||
PathContext::AllowTransit()
|
||||
{
|
||||
m_AllowTransit = true;
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::AllowingTransit() const
|
||||
{
|
||||
return m_AllowTransit;
|
||||
}
|
||||
bool
|
||||
PathContext::AllowingTransit() const
|
||||
{
|
||||
return m_AllowTransit;
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::CheckPathLimitHitByIP(const IpAddress& ip)
|
||||
{
|
||||
bool
|
||||
PathContext::CheckPathLimitHitByIP(const IpAddress& ip)
|
||||
{
|
||||
#ifdef TESTNET
|
||||
return false;
|
||||
return false;
|
||||
#else
|
||||
IpAddress remote = ip;
|
||||
// null out the port -- we don't care about it for path limiting purposes
|
||||
remote.setPort(0);
|
||||
// try inserting remote address by ip into decaying hash set
|
||||
// if it cannot insert it has hit a limit
|
||||
return not m_PathLimits.Insert(remote);
|
||||
IpAddress remote = ip;
|
||||
// null out the port -- we don't care about it for path limiting purposes
|
||||
remote.setPort(0);
|
||||
// try inserting remote address by ip into decaying hash set
|
||||
// if it cannot insert it has hit a limit
|
||||
return not m_PathLimits.Insert(remote);
|
||||
#endif
|
||||
}
|
||||
|
||||
const EventLoop_ptr&
|
||||
PathContext::loop()
|
||||
{
|
||||
return m_Router->loop();
|
||||
}
|
||||
}
|
||||
|
||||
const SecretKey&
|
||||
PathContext::EncryptionSecretKey()
|
||||
{
|
||||
return m_Router->encryption();
|
||||
}
|
||||
const EventLoop_ptr&
|
||||
PathContext::loop()
|
||||
{
|
||||
return m_Router->loop();
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::HopIsUs(const RouterID& k) const
|
||||
{
|
||||
return std::equal(m_Router->pubkey(), m_Router->pubkey() + PUBKEYSIZE, k.begin());
|
||||
}
|
||||
const SecretKey&
|
||||
PathContext::EncryptionSecretKey()
|
||||
{
|
||||
return m_Router->encryption();
|
||||
}
|
||||
|
||||
PathContext::EndpointPathPtrSet
|
||||
PathContext::FindOwnedPathsWithEndpoint(const RouterID& r)
|
||||
{
|
||||
EndpointPathPtrSet found;
|
||||
m_OurPaths.ForEach([&](const Path_ptr& p) {
|
||||
if (p->Endpoint() == r && p->IsReady())
|
||||
found.insert(p);
|
||||
});
|
||||
return found;
|
||||
}
|
||||
bool
|
||||
PathContext::HopIsUs(const RouterID& k) const
|
||||
{
|
||||
return std::equal(m_Router->pubkey(), m_Router->pubkey() + PUBKEYSIZE, k.begin());
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::ForwardLRCM(
|
||||
const RouterID& nextHop,
|
||||
const std::array<EncryptedFrame, 8>& frames,
|
||||
SendStatusHandler handler)
|
||||
PathContext::EndpointPathPtrSet
|
||||
PathContext::FindOwnedPathsWithEndpoint(const RouterID& r)
|
||||
{
|
||||
EndpointPathPtrSet found;
|
||||
m_OurPaths.ForEach([&](const Path_ptr& p) {
|
||||
if (p->Endpoint() == r && p->IsReady())
|
||||
found.insert(p);
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::ForwardLRCM(
|
||||
const RouterID& nextHop,
|
||||
const std::array<EncryptedFrame, 8>& frames,
|
||||
SendStatusHandler handler)
|
||||
{
|
||||
if (handler == nullptr)
|
||||
{
|
||||
if (handler == nullptr)
|
||||
{
|
||||
LogError("Calling ForwardLRCM without passing result handler");
|
||||
return false;
|
||||
}
|
||||
|
||||
const LR_CommitMessage msg{frames};
|
||||
|
||||
LogDebug("forwarding LRCM to ", nextHop);
|
||||
|
||||
return m_Router->SendToOrQueue(nextHop, msg, handler);
|
||||
LogError("Calling ForwardLRCM without passing result handler");
|
||||
return false;
|
||||
}
|
||||
|
||||
template <
|
||||
typename Lock_t,
|
||||
typename Map_t,
|
||||
typename Key_t,
|
||||
typename CheckValue_t,
|
||||
typename GetFunc_t,
|
||||
typename Return_ptr = HopHandler_ptr>
|
||||
Return_ptr
|
||||
MapGet(Map_t& map, const Key_t& k, CheckValue_t check, GetFunc_t get)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
if (check(i->second))
|
||||
return get(i->second);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
const LR_CommitMessage msg{frames};
|
||||
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename CheckValue_t>
|
||||
bool
|
||||
MapHas(Map_t& map, const Key_t& k, CheckValue_t check)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
if (check(i->second))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
LogDebug("forwarding LRCM to ", nextHop);
|
||||
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename Value_t>
|
||||
void
|
||||
MapPut(Map_t& map, const Key_t& k, const Value_t& v)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
map.second.emplace(k, v);
|
||||
}
|
||||
return m_Router->SendToOrQueue(nextHop, msg, handler);
|
||||
}
|
||||
|
||||
template <typename Lock_t, typename Map_t, typename Visit_t>
|
||||
void
|
||||
MapIter(Map_t& map, Visit_t v)
|
||||
template <
|
||||
typename Lock_t,
|
||||
typename Map_t,
|
||||
typename Key_t,
|
||||
typename CheckValue_t,
|
||||
typename GetFunc_t,
|
||||
typename Return_ptr = HopHandler_ptr>
|
||||
Return_ptr
|
||||
MapGet(Map_t& map, const Key_t& k, CheckValue_t check, GetFunc_t get)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
for (const auto& item : map.second)
|
||||
v(item);
|
||||
if (check(i->second))
|
||||
return get(i->second);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename Check_t>
|
||||
void
|
||||
MapDel(Map_t& map, const Key_t& k, Check_t check)
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename CheckValue_t>
|
||||
bool
|
||||
MapHas(Map_t& map, const Key_t& k, CheckValue_t check)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second;)
|
||||
{
|
||||
if (check(i->second))
|
||||
i = map.second.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
if (check(i->second))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::AddOwnPath(PathSet_ptr set, Path_ptr path)
|
||||
{
|
||||
set->AddPath(path);
|
||||
MapPut<util::Lock>(m_OurPaths, path->TXID(), path);
|
||||
MapPut<util::Lock>(m_OurPaths, path->RXID(), path);
|
||||
}
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename Value_t>
|
||||
void
|
||||
MapPut(Map_t& map, const Key_t& k, const Value_t& v)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
map.second.emplace(k, v);
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::HasTransitHop(const TransitHopInfo& info)
|
||||
template <typename Lock_t, typename Map_t, typename Visit_t>
|
||||
void
|
||||
MapIter(Map_t& map, Visit_t v)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
for (const auto& item : map.second)
|
||||
v(item);
|
||||
}
|
||||
|
||||
template <typename Lock_t, typename Map_t, typename Key_t, typename Check_t>
|
||||
void
|
||||
MapDel(Map_t& map, const Key_t& k, Check_t check)
|
||||
{
|
||||
Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(k);
|
||||
for (auto i = range.first; i != range.second;)
|
||||
{
|
||||
return MapHas<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths, info.txID, [info](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return info == hop->info;
|
||||
});
|
||||
if (check(i->second))
|
||||
i = map.second.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::weak_ptr<TransitHop>>
|
||||
PathContext::TransitHopByInfo(const TransitHopInfo& info)
|
||||
{
|
||||
// this is ugly as sin
|
||||
auto own = MapGet<
|
||||
SyncTransitMap_t::Lock_t,
|
||||
decltype(m_TransitPaths),
|
||||
PathID_t,
|
||||
std::function<bool(const std::shared_ptr<TransitHop>&)>,
|
||||
std::function<TransitHop*(const std::shared_ptr<TransitHop>&)>,
|
||||
TransitHop*>(
|
||||
m_TransitPaths,
|
||||
info.txID,
|
||||
[info](const auto& hop) -> bool { return hop->info == info; },
|
||||
[](const auto& hop) -> TransitHop* { return hop.get(); });
|
||||
if (own)
|
||||
return own->weak_from_this();
|
||||
return std::nullopt;
|
||||
}
|
||||
void
|
||||
PathContext::AddOwnPath(PathSet_ptr set, Path_ptr path)
|
||||
{
|
||||
set->AddPath(path);
|
||||
MapPut<util::Lock>(m_OurPaths, path->TXID(), path);
|
||||
MapPut<util::Lock>(m_OurPaths, path->RXID(), path);
|
||||
}
|
||||
|
||||
std::optional<std::weak_ptr<TransitHop>>
|
||||
PathContext::TransitHopByUpstream(const RouterID& upstream, const PathID_t& id)
|
||||
{
|
||||
// this is ugly as sin as well
|
||||
auto own = MapGet<
|
||||
SyncTransitMap_t::Lock_t,
|
||||
decltype(m_TransitPaths),
|
||||
PathID_t,
|
||||
std::function<bool(const std::shared_ptr<TransitHop>&)>,
|
||||
std::function<TransitHop*(const std::shared_ptr<TransitHop>&)>,
|
||||
TransitHop*>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[upstream](const auto& hop) -> bool { return hop->info.upstream == upstream; },
|
||||
[](const auto& hop) -> TransitHop* { return hop.get(); });
|
||||
if (own)
|
||||
return own->weak_from_this();
|
||||
return std::nullopt;
|
||||
}
|
||||
bool
|
||||
PathContext::HasTransitHop(const TransitHopInfo& info)
|
||||
{
|
||||
return MapHas<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths, info.txID, [info](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return info == hop->info;
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<std::weak_ptr<TransitHop>>
|
||||
PathContext::TransitHopByInfo(const TransitHopInfo& info)
|
||||
{
|
||||
// this is ugly as sin
|
||||
auto own = MapGet<
|
||||
SyncTransitMap_t::Lock_t,
|
||||
decltype(m_TransitPaths),
|
||||
PathID_t,
|
||||
std::function<bool(const std::shared_ptr<TransitHop>&)>,
|
||||
std::function<TransitHop*(const std::shared_ptr<TransitHop>&)>,
|
||||
TransitHop*>(
|
||||
m_TransitPaths,
|
||||
info.txID,
|
||||
[info](const auto& hop) -> bool { return hop->info == info; },
|
||||
[](const auto& hop) -> TransitHop* { return hop.get(); });
|
||||
if (own)
|
||||
return own->weak_from_this();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::weak_ptr<TransitHop>>
|
||||
PathContext::TransitHopByUpstream(const RouterID& upstream, const PathID_t& id)
|
||||
{
|
||||
// this is ugly as sin as well
|
||||
auto own = MapGet<
|
||||
SyncTransitMap_t::Lock_t,
|
||||
decltype(m_TransitPaths),
|
||||
PathID_t,
|
||||
std::function<bool(const std::shared_ptr<TransitHop>&)>,
|
||||
std::function<TransitHop*(const std::shared_ptr<TransitHop>&)>,
|
||||
TransitHop*>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[upstream](const auto& hop) -> bool { return hop->info.upstream == upstream; },
|
||||
[](const auto& hop) -> TransitHop* { return hop.get(); });
|
||||
if (own)
|
||||
return own->weak_from_this();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
HopHandler_ptr
|
||||
PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id)
|
||||
{
|
||||
auto own = MapGet<util::Lock>(
|
||||
m_OurPaths,
|
||||
id,
|
||||
[](const Path_ptr) -> bool {
|
||||
// TODO: is this right?
|
||||
return true;
|
||||
},
|
||||
[](Path_ptr p) -> HopHandler_ptr { return p; });
|
||||
if (own)
|
||||
return own;
|
||||
|
||||
return MapGet<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[remote](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return hop->info.upstream == remote;
|
||||
},
|
||||
[](const std::shared_ptr<TransitHop>& h) -> HopHandler_ptr { return h; });
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::TransitHopPreviousIsRouter(const PathID_t& path, const RouterID& otherRouter)
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
auto itr = m_TransitPaths.second.find(path);
|
||||
if (itr == m_TransitPaths.second.end())
|
||||
return false;
|
||||
return itr->second->info.downstream == otherRouter;
|
||||
}
|
||||
|
||||
HopHandler_ptr
|
||||
PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id)
|
||||
HopHandler_ptr
|
||||
PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id)
|
||||
{
|
||||
return MapGet<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[remote](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return hop->info.downstream == remote;
|
||||
},
|
||||
[](const std::shared_ptr<TransitHop>& h) -> HopHandler_ptr { return h; });
|
||||
}
|
||||
|
||||
PathSet_ptr
|
||||
PathContext::GetLocalPathSet(const PathID_t& id)
|
||||
{
|
||||
auto& map = m_OurPaths;
|
||||
util::Lock lock(map.first);
|
||||
auto itr = map.second.find(id);
|
||||
if (itr != map.second.end())
|
||||
{
|
||||
auto own = MapGet<util::Lock>(
|
||||
m_OurPaths,
|
||||
id,
|
||||
[](const Path_ptr) -> bool {
|
||||
// TODO: is this right?
|
||||
return true;
|
||||
},
|
||||
[](Path_ptr p) -> HopHandler_ptr { return p; });
|
||||
if (own)
|
||||
return own;
|
||||
|
||||
return MapGet<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[remote](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return hop->info.upstream == remote;
|
||||
},
|
||||
[](const std::shared_ptr<TransitHop>& h) -> HopHandler_ptr { return h; });
|
||||
if (auto parent = itr->second->m_PathSet.lock())
|
||||
return parent;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool
|
||||
PathContext::TransitHopPreviousIsRouter(const PathID_t& path, const RouterID& otherRouter)
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
auto itr = m_TransitPaths.second.find(path);
|
||||
if (itr == m_TransitPaths.second.end())
|
||||
return false;
|
||||
return itr->second->info.downstream == otherRouter;
|
||||
}
|
||||
const byte_t*
|
||||
PathContext::OurRouterID() const
|
||||
{
|
||||
return m_Router->pubkey();
|
||||
}
|
||||
|
||||
HopHandler_ptr
|
||||
PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id)
|
||||
{
|
||||
return MapGet<SyncTransitMap_t::Lock_t>(
|
||||
m_TransitPaths,
|
||||
id,
|
||||
[remote](const std::shared_ptr<TransitHop>& hop) -> bool {
|
||||
return hop->info.downstream == remote;
|
||||
},
|
||||
[](const std::shared_ptr<TransitHop>& h) -> HopHandler_ptr { return h; });
|
||||
}
|
||||
AbstractRouter*
|
||||
PathContext::Router()
|
||||
{
|
||||
return m_Router;
|
||||
}
|
||||
|
||||
PathSet_ptr
|
||||
PathContext::GetLocalPathSet(const PathID_t& id)
|
||||
TransitHop_ptr
|
||||
PathContext::GetPathForTransfer(const PathID_t& id)
|
||||
{
|
||||
const RouterID us(OurRouterID());
|
||||
auto& map = m_TransitPaths;
|
||||
{
|
||||
auto& map = m_OurPaths;
|
||||
util::Lock lock(map.first);
|
||||
auto itr = map.second.find(id);
|
||||
if (itr != map.second.end())
|
||||
SyncTransitMap_t::Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(id);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
if (auto parent = itr->second->m_PathSet.lock())
|
||||
return parent;
|
||||
if (i->second->info.upstream == us)
|
||||
return i->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const byte_t*
|
||||
PathContext::OurRouterID() const
|
||||
{
|
||||
return m_Router->pubkey();
|
||||
}
|
||||
void
|
||||
PathContext::PumpUpstream()
|
||||
{
|
||||
m_TransitPaths.ForEach([&](auto& ptr) { ptr->FlushUpstream(m_Router); });
|
||||
m_OurPaths.ForEach([&](auto& ptr) { ptr->FlushUpstream(m_Router); });
|
||||
}
|
||||
|
||||
AbstractRouter*
|
||||
PathContext::Router()
|
||||
{
|
||||
return m_Router;
|
||||
}
|
||||
void
|
||||
PathContext::PumpDownstream()
|
||||
{
|
||||
m_TransitPaths.ForEach([&](auto& ptr) { ptr->FlushDownstream(m_Router); });
|
||||
m_OurPaths.ForEach([&](auto& ptr) { ptr->FlushDownstream(m_Router); });
|
||||
}
|
||||
|
||||
TransitHop_ptr
|
||||
PathContext::GetPathForTransfer(const PathID_t& id)
|
||||
{
|
||||
const RouterID us(OurRouterID());
|
||||
auto& map = m_TransitPaths;
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(id);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
if (i->second->info.upstream == us)
|
||||
return i->second;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
uint64_t
|
||||
PathContext::CurrentTransitPaths()
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
const auto& map = m_TransitPaths.second;
|
||||
return map.size() / 2;
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::PumpUpstream()
|
||||
uint64_t
|
||||
PathContext::CurrentOwnedPaths(path::PathStatus st)
|
||||
{
|
||||
uint64_t num{};
|
||||
util::Lock lock{m_OurPaths.first};
|
||||
auto& map = m_OurPaths.second;
|
||||
for (auto itr = map.begin(); itr != map.end(); ++itr)
|
||||
{
|
||||
m_TransitPaths.ForEach([&](auto& ptr) { ptr->FlushUpstream(m_Router); });
|
||||
m_OurPaths.ForEach([&](auto& ptr) { ptr->FlushUpstream(m_Router); });
|
||||
if (itr->second->Status() == st)
|
||||
num++;
|
||||
}
|
||||
return num / 2;
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::PumpDownstream()
|
||||
{
|
||||
m_TransitPaths.ForEach([&](auto& ptr) { ptr->FlushDownstream(m_Router); });
|
||||
m_OurPaths.ForEach([&](auto& ptr) { ptr->FlushDownstream(m_Router); });
|
||||
}
|
||||
void
|
||||
PathContext::PutTransitHop(std::shared_ptr<TransitHop> hop)
|
||||
{
|
||||
MapPut<SyncTransitMap_t::Lock_t>(m_TransitPaths, hop->info.txID, hop);
|
||||
MapPut<SyncTransitMap_t::Lock_t>(m_TransitPaths, hop->info.rxID, hop);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
PathContext::CurrentTransitPaths()
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
const auto& map = m_TransitPaths.second;
|
||||
return map.size() / 2;
|
||||
}
|
||||
void
|
||||
PathContext::ExpirePaths(llarp_time_t now)
|
||||
{
|
||||
// decay limits
|
||||
m_PathLimits.Decay(now);
|
||||
|
||||
uint64_t
|
||||
PathContext::CurrentOwnedPaths(path::PathStatus st)
|
||||
{
|
||||
uint64_t num{};
|
||||
util::Lock lock{m_OurPaths.first};
|
||||
auto& map = m_OurPaths.second;
|
||||
for (auto itr = map.begin(); itr != map.end(); ++itr)
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
auto& map = m_TransitPaths.second;
|
||||
auto itr = map.begin();
|
||||
while (itr != map.end())
|
||||
{
|
||||
if (itr->second->Status() == st)
|
||||
num++;
|
||||
if (itr->second->Expired(now))
|
||||
{
|
||||
m_Router->outboundMessageHandler().RemovePath(itr->first);
|
||||
itr = map.erase(itr);
|
||||
}
|
||||
else
|
||||
{
|
||||
itr->second->DecayFilters(now);
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
return num / 2;
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::PutTransitHop(std::shared_ptr<TransitHop> hop)
|
||||
{
|
||||
MapPut<SyncTransitMap_t::Lock_t>(m_TransitPaths, hop->info.txID, hop);
|
||||
MapPut<SyncTransitMap_t::Lock_t>(m_TransitPaths, hop->info.rxID, hop);
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::ExpirePaths(llarp_time_t now)
|
||||
{
|
||||
// decay limits
|
||||
m_PathLimits.Decay(now);
|
||||
|
||||
util::Lock lock(m_OurPaths.first);
|
||||
auto& map = m_OurPaths.second;
|
||||
auto itr = map.begin();
|
||||
while (itr != map.end())
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);
|
||||
auto& map = m_TransitPaths.second;
|
||||
auto itr = map.begin();
|
||||
while (itr != map.end())
|
||||
if (itr->second->Expired(now))
|
||||
{
|
||||
if (itr->second->Expired(now))
|
||||
{
|
||||
m_Router->outboundMessageHandler().RemovePath(itr->first);
|
||||
itr = map.erase(itr);
|
||||
}
|
||||
else
|
||||
{
|
||||
itr->second->DecayFilters(now);
|
||||
++itr;
|
||||
}
|
||||
itr = map.erase(itr);
|
||||
}
|
||||
}
|
||||
{
|
||||
util::Lock lock(m_OurPaths.first);
|
||||
auto& map = m_OurPaths.second;
|
||||
auto itr = map.begin();
|
||||
while (itr != map.end())
|
||||
else
|
||||
{
|
||||
if (itr->second->Expired(now))
|
||||
{
|
||||
itr = map.erase(itr);
|
||||
}
|
||||
else
|
||||
{
|
||||
itr->second->DecayFilters(now);
|
||||
++itr;
|
||||
}
|
||||
itr->second->DecayFilters(now);
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routing::MessageHandler_ptr
|
||||
PathContext::GetHandler(const PathID_t& id)
|
||||
routing::MessageHandler_ptr
|
||||
PathContext::GetHandler(const PathID_t& id)
|
||||
{
|
||||
routing::MessageHandler_ptr h = nullptr;
|
||||
auto pathset = GetLocalPathSet(id);
|
||||
if (pathset)
|
||||
{
|
||||
routing::MessageHandler_ptr h = nullptr;
|
||||
auto pathset = GetLocalPathSet(id);
|
||||
if (pathset)
|
||||
{
|
||||
h = pathset->GetPathByID(id);
|
||||
}
|
||||
if (h)
|
||||
return h;
|
||||
const RouterID us(OurRouterID());
|
||||
auto& map = m_TransitPaths;
|
||||
h = pathset->GetPathByID(id);
|
||||
}
|
||||
if (h)
|
||||
return h;
|
||||
const RouterID us(OurRouterID());
|
||||
auto& map = m_TransitPaths;
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(id);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
SyncTransitMap_t::Lock_t lock(map.first);
|
||||
auto range = map.second.equal_range(id);
|
||||
for (auto i = range.first; i != range.second; ++i)
|
||||
{
|
||||
if (i->second->info.upstream == us)
|
||||
return i->second;
|
||||
}
|
||||
if (i->second->info.upstream == us)
|
||||
return i->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
PathContext::RemovePathSet(PathSet_ptr)
|
||||
{}
|
||||
} // namespace path
|
||||
} // namespace llarp
|
||||
void
|
||||
PathContext::RemovePathSet(PathSet_ptr)
|
||||
{}
|
||||
} // namespace llarp::path
|
||||
|
@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
#include <llarp/router_contact.hpp>
|
||||
#include <llarp/util/time.hpp>
|
||||
#include <optional>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
/// The maximum number of peers we will flood a gossiped RC to when propagating an RC
|
||||
constexpr size_t MaxGossipPeers = 20;
|
||||
|
||||
struct I_RCGossiper
|
||||
{
|
||||
virtual ~I_RCGossiper() = default;
|
||||
/// try goissping RC
|
||||
/// return false if we hit a cooldown for this rc
|
||||
/// return true if we gossiped this rc to at least 1 peer
|
||||
virtual bool
|
||||
GossipRC(const RouterContact& rc) = 0;
|
||||
|
||||
using Time_t = Duration_t;
|
||||
|
||||
virtual void
|
||||
Decay(Time_t now) = 0;
|
||||
|
||||
/// return true if we should gossip our RC now
|
||||
virtual bool
|
||||
ShouldGossipOurRC(Time_t now) const = 0;
|
||||
|
||||
/// return true if that rc is owned by us
|
||||
virtual bool
|
||||
IsOurRC(const RouterContact& rc) const = 0;
|
||||
|
||||
/// forget the replay filter entry given pubkey
|
||||
virtual void
|
||||
Forget(const RouterID& router) = 0;
|
||||
|
||||
/// returns the time point when we will send our next gossip at
|
||||
virtual TimePoint_t
|
||||
NextGossipAt() const = 0;
|
||||
|
||||
/// returns the time point when we sent our last gossip at or nullopt if we never did
|
||||
virtual std::optional<TimePoint_t>
|
||||
LastGossipAt() const = 0;
|
||||
};
|
||||
} // namespace llarp
|
@ -1,48 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <llarp/util/status.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
enum class SendStatus
|
||||
{
|
||||
Success,
|
||||
Timeout,
|
||||
NoLink,
|
||||
InvalidRouter,
|
||||
RouterNotFound,
|
||||
Congestion
|
||||
};
|
||||
|
||||
struct AbstractLinkMessage;
|
||||
struct RouterID;
|
||||
struct PathID_t;
|
||||
|
||||
using SendStatusHandler = std::function<void(SendStatus)>;
|
||||
|
||||
static const size_t MAX_PATH_QUEUE_SIZE = 100;
|
||||
static const size_t MAX_OUTBOUND_QUEUE_SIZE = 1000;
|
||||
static const size_t MAX_OUTBOUND_MESSAGES_PER_TICK = 500;
|
||||
|
||||
struct IOutboundMessageHandler
|
||||
{
|
||||
virtual ~IOutboundMessageHandler() = default;
|
||||
|
||||
virtual bool
|
||||
QueueMessage(
|
||||
const RouterID& remote, const AbstractLinkMessage& msg, SendStatusHandler callback) = 0;
|
||||
|
||||
virtual void
|
||||
Pump() = 0;
|
||||
|
||||
virtual void
|
||||
RemovePath(const PathID_t& pathid) = 0;
|
||||
|
||||
virtual util::StatusObject
|
||||
ExtractStatus() const = 0;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
@ -1,72 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <llarp/util/status.hpp>
|
||||
#include <llarp/util/types.hpp>
|
||||
#include <llarp/util/formattable.hpp>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct AbstractLinkSession;
|
||||
struct RouterID;
|
||||
struct RouterContact;
|
||||
|
||||
enum class SessionResult
|
||||
{
|
||||
Establish,
|
||||
Timeout,
|
||||
RouterNotFound,
|
||||
InvalidRouter,
|
||||
NoLink,
|
||||
EstablishFail
|
||||
};
|
||||
|
||||
constexpr std::string_view
|
||||
ToString(SessionResult sr)
|
||||
{
|
||||
return sr == llarp::SessionResult::Establish ? "success"sv
|
||||
: sr == llarp::SessionResult::Timeout ? "timeout"sv
|
||||
: sr == llarp::SessionResult::NoLink ? "no link"sv
|
||||
: sr == llarp::SessionResult::InvalidRouter ? "invalid router"sv
|
||||
: sr == llarp::SessionResult::RouterNotFound ? "not found"sv
|
||||
: sr == llarp::SessionResult::EstablishFail ? "establish failed"sv
|
||||
: "???"sv;
|
||||
}
|
||||
template <>
|
||||
constexpr inline bool IsToStringFormattable<SessionResult> = true;
|
||||
|
||||
using RouterCallback = std::function<void(const RouterID&, const SessionResult)>;
|
||||
|
||||
struct IOutboundSessionMaker
|
||||
{
|
||||
virtual ~IOutboundSessionMaker() = default;
|
||||
|
||||
virtual bool
|
||||
OnSessionEstablished(AbstractLinkSession* session) = 0;
|
||||
|
||||
virtual void
|
||||
OnConnectTimeout(AbstractLinkSession* session) = 0;
|
||||
|
||||
virtual void
|
||||
CreateSessionTo(const RouterID& router, RouterCallback on_result) = 0;
|
||||
|
||||
virtual void
|
||||
CreateSessionTo(const RouterContact& rc, RouterCallback on_result) = 0;
|
||||
|
||||
virtual bool
|
||||
HavePendingSessionTo(const RouterID& router) const = 0;
|
||||
|
||||
virtual void
|
||||
ConnectToRandomRouters(int numDesired) = 0;
|
||||
|
||||
virtual util::StatusObject
|
||||
ExtractStatus() const = 0;
|
||||
|
||||
virtual bool
|
||||
ShouldConnectTo(const RouterID& router) const = 0;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
@ -1,81 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <llarp/util/types.hpp>
|
||||
#include <llarp/router_id.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct RouterContact;
|
||||
|
||||
enum class RCRequestResult
|
||||
{
|
||||
Success,
|
||||
InvalidRouter,
|
||||
RouterNotFound,
|
||||
BadRC
|
||||
};
|
||||
|
||||
using RCRequestCallback =
|
||||
std::function<void(const RouterID&, const RouterContact* const, const RCRequestResult)>;
|
||||
|
||||
struct I_RCLookupHandler
|
||||
{
|
||||
virtual ~I_RCLookupHandler() = default;
|
||||
|
||||
virtual void
|
||||
AddValidRouter(const RouterID& router) = 0;
|
||||
|
||||
virtual void
|
||||
RemoveValidRouter(const RouterID& router) = 0;
|
||||
|
||||
virtual void
|
||||
SetRouterWhitelist(
|
||||
const std::vector<RouterID>& whitelist,
|
||||
const std::vector<RouterID>& greylist,
|
||||
const std::vector<RouterID>& greenlist) = 0;
|
||||
|
||||
virtual void
|
||||
GetRC(const RouterID& router, RCRequestCallback callback, bool forceLookup = false) = 0;
|
||||
|
||||
virtual bool
|
||||
PathIsAllowed(const RouterID& remote) const = 0;
|
||||
|
||||
virtual bool
|
||||
SessionIsAllowed(const RouterID& remote) const = 0;
|
||||
|
||||
virtual bool
|
||||
IsGreylisted(const RouterID& remote) const = 0;
|
||||
|
||||
virtual bool
|
||||
IsGreenlisted(const RouterID& remote) const = 0;
|
||||
|
||||
virtual bool
|
||||
IsRegistered(const RouterID& remote) const = 0;
|
||||
|
||||
virtual bool
|
||||
CheckRC(const RouterContact& rc) const = 0;
|
||||
|
||||
virtual bool
|
||||
GetRandomWhitelistRouter(RouterID& router) const = 0;
|
||||
|
||||
virtual bool
|
||||
CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc) = 0;
|
||||
|
||||
virtual void
|
||||
PeriodicUpdate(llarp_time_t now) = 0;
|
||||
|
||||
virtual void
|
||||
ExploreNetwork() = 0;
|
||||
|
||||
virtual size_t
|
||||
NumberOfStrictConnectRouters() const = 0;
|
||||
|
||||
virtual bool
|
||||
HaveReceivedWhitelist() const = 0;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
Loading…
Reference in New Issue