2021-03-09 22:24:35 +00:00
|
|
|
#include "path_context.hpp"
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "path.hpp"
|
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
#include <llarp/router/router.hpp>
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
namespace llarp::path
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
static constexpr auto DefaultPathBuildLimit = 500ms;
|
2019-12-30 22:03:34 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
PathContext::PathContext(Router* router)
|
2023-09-29 21:00:13 +00:00
|
|
|
: _router(router), m_AllowTransit(false), path_limits(DefaultPathBuildLimit)
|
2023-09-13 19:57:18 +00:00
|
|
|
{}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
void
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::allow_transit()
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
|
|
|
m_AllowTransit = true;
|
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
bool
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::is_transit_allowed() const
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
|
|
|
return m_AllowTransit;
|
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
bool
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::check_path_limit_hit_by_ip(const IpAddress& ip)
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
2020-01-14 01:01:33 +00:00
|
|
|
#ifdef TESTNET
|
2023-09-13 19:57:18 +00:00
|
|
|
return false;
|
2020-01-14 01:01:33 +00:00
|
|
|
#else
|
2023-09-13 19:57:18 +00:00
|
|
|
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
|
2023-09-29 21:00:13 +00:00
|
|
|
return not path_limits.Insert(remote);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
const EventLoop_ptr&
|
|
|
|
PathContext::loop()
|
|
|
|
{
|
2023-09-21 14:20:49 +00:00
|
|
|
return _router->loop();
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
const SecretKey&
|
|
|
|
PathContext::EncryptionSecretKey()
|
|
|
|
{
|
2023-09-21 14:20:49 +00:00
|
|
|
return _router->encryption();
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
bool
|
|
|
|
PathContext::HopIsUs(const RouterID& k) const
|
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
return _router->pubkey() == k;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-11-06 21:02:20 +00:00
|
|
|
std::vector<std::shared_ptr<Path>>
|
2023-09-13 19:57:18 +00:00
|
|
|
PathContext::FindOwnedPathsWithEndpoint(const RouterID& r)
|
|
|
|
{
|
2023-11-06 21:02:20 +00:00
|
|
|
std::vector<std::shared_ptr<Path>> found;
|
2023-10-21 03:30:10 +00:00
|
|
|
for (const auto& [pathid, path] : own_paths)
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-11-06 21:02:20 +00:00
|
|
|
// each path is stored in this map twice, once for each pathid at the first hop
|
|
|
|
// This will make the output deduplicated without needing a std::set
|
|
|
|
// TODO: we should only need to map one pathid; as the path owner we only send/receive
|
|
|
|
// packets with the first hop's RXID; its TXID is for packets between it and hop 2.
|
|
|
|
// TODO: Also, perhaps we want a bit of data duplication here, e.g. a map from
|
|
|
|
// RouterID (terminal hop) to shared_ptr<Path>.
|
|
|
|
if (path->TXID() == pathid)
|
|
|
|
continue;
|
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
if (path->Endpoint() == r && path->IsReady())
|
2023-11-06 21:02:20 +00:00
|
|
|
found.push_back(path);
|
2019-06-17 23:19:39 +00:00
|
|
|
}
|
2023-10-21 03:30:10 +00:00
|
|
|
return found;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
void
|
|
|
|
PathContext::AddOwnPath(PathSet_ptr set, Path_ptr path)
|
|
|
|
{
|
|
|
|
set->AddPath(path);
|
2023-10-21 03:30:10 +00:00
|
|
|
own_paths[path->TXID()] = path;
|
|
|
|
own_paths[path->RXID()] = path;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2021-06-07 12:39:38 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
bool
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::has_transit_hop(const TransitHopInfo& info)
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
TransitHopID downstream{info.downstream, info.rxID};
|
|
|
|
if (transit_hops.count(downstream))
|
|
|
|
return true;
|
2023-09-13 19:57:18 +00:00
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
TransitHopID upstream{info.upstream, info.txID};
|
|
|
|
if (transit_hops.count(upstream))
|
|
|
|
return true;
|
2023-09-13 19:57:18 +00:00
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
return false;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
std::shared_ptr<TransitHop>
|
|
|
|
PathContext::GetTransitHop(const RouterID& rid, const PathID_t& path_id)
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
if (auto itr = transit_hops.find({rid, path_id}); itr != transit_hops.end())
|
|
|
|
return itr->second;
|
2023-09-13 19:57:18 +00:00
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
return nullptr;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
Path_ptr
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::get_path(const PathID_t& path_id)
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
if (auto itr = own_paths.find(path_id); itr != own_paths.end())
|
|
|
|
return itr->second;
|
|
|
|
|
|
|
|
return nullptr;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 15:34:50 +00:00
|
|
|
bool
|
2023-10-21 03:30:10 +00:00
|
|
|
PathContext::TransitHopPreviousIsRouter(const PathID_t& path_id, const RouterID& otherRouter)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
return transit_hops.count({otherRouter, path_id});
|
2023-10-06 15:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
PathSet_ptr
|
|
|
|
PathContext::GetLocalPathSet(const PathID_t& id)
|
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
if (auto itr = own_paths.find(id); itr != own_paths.end())
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
if (auto parent = itr->second->m_PathSet.lock())
|
|
|
|
return parent;
|
2019-06-17 23:19:39 +00:00
|
|
|
}
|
2023-09-13 19:57:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
const byte_t*
|
|
|
|
PathContext::OurRouterID() const
|
|
|
|
{
|
2023-09-21 14:20:49 +00:00
|
|
|
return _router->pubkey();
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-11-06 21:02:20 +00:00
|
|
|
std::shared_ptr<TransitHop>
|
2023-09-13 19:57:18 +00:00
|
|
|
PathContext::GetPathForTransfer(const PathID_t& id)
|
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
if (auto itr = transit_hops.find({OurRouterID(), id}); itr != transit_hops.end())
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
return itr->second;
|
2019-06-17 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 03:30:10 +00:00
|
|
|
return nullptr;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
uint64_t
|
|
|
|
PathContext::CurrentTransitPaths()
|
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
return transit_hops.size() / 2;
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
uint64_t
|
|
|
|
PathContext::CurrentOwnedPaths(path::PathStatus st)
|
|
|
|
{
|
|
|
|
uint64_t num{};
|
2023-10-21 03:30:10 +00:00
|
|
|
for (auto& own_path : own_paths)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
if (own_path.second->Status() == st)
|
2023-09-13 19:57:18 +00:00
|
|
|
num++;
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
2023-09-13 19:57:18 +00:00
|
|
|
return num / 2;
|
|
|
|
}
|
2019-09-16 10:21:12 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
void
|
2023-12-06 21:54:51 +00:00
|
|
|
PathContext::put_transit_hop(std::shared_ptr<TransitHop> hop)
|
2023-09-13 19:57:18 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
TransitHopID downstream{hop->info.downstream, hop->info.rxID};
|
|
|
|
TransitHopID upstream{hop->info.upstream, hop->info.txID};
|
|
|
|
transit_hops.emplace(std::move(downstream), hop);
|
|
|
|
transit_hops.emplace(std::move(upstream), hop);
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-09-05 17:39:09 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
void
|
|
|
|
PathContext::ExpirePaths(llarp_time_t now)
|
|
|
|
{
|
|
|
|
// decay limits
|
2023-09-29 21:00:13 +00:00
|
|
|
path_limits.Decay(now);
|
2020-03-03 23:04:09 +00:00
|
|
|
|
2022-01-13 21:24:08 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
auto itr = transit_hops.begin();
|
|
|
|
while (itr != transit_hops.end())
|
2022-01-13 21:24:08 +00:00
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
if (itr->second->Expired(now))
|
|
|
|
{
|
2023-09-29 21:00:13 +00:00
|
|
|
// TODO: this
|
|
|
|
// _router->outboundMessageHandler().RemovePath(itr->first);
|
2023-10-21 03:30:10 +00:00
|
|
|
itr = transit_hops.erase(itr);
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
2022-01-13 21:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
for (auto itr = own_paths.begin(); itr != own_paths.end();)
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
if (itr->second->Expired(now))
|
2019-06-17 23:19:39 +00:00
|
|
|
{
|
2023-10-21 03:30:10 +00:00
|
|
|
itr = own_paths.erase(itr);
|
2019-06-17 23:19:39 +00:00
|
|
|
}
|
2023-09-13 19:57:18 +00:00
|
|
|
else
|
2019-12-03 15:52:06 +00:00
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
++itr;
|
2019-12-03 15:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:19:39 +00:00
|
|
|
}
|
2023-09-13 19:57:18 +00:00
|
|
|
}
|
2019-11-05 16:58:53 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
void
|
|
|
|
PathContext::RemovePathSet(PathSet_ptr)
|
|
|
|
{}
|
|
|
|
} // namespace llarp::path
|