2018-06-12 16:45:12 +00:00
|
|
|
#include <deque>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/encrypted_frame.hpp>
|
|
|
|
#include <llarp/path.hpp>
|
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
PathContext::PathContext(llarp_router* router)
|
|
|
|
: m_Router(router), m_AllowTransit(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PathContext::~PathContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::AllowTransit()
|
|
|
|
{
|
|
|
|
m_AllowTransit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-06-18 22:03:50 +00:00
|
|
|
PathContext::AllowingTransit() const
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-06-18 22:03:50 +00:00
|
|
|
return m_AllowTransit;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 16:45:12 +00:00
|
|
|
llarp_threadpool*
|
|
|
|
PathContext::Worker()
|
|
|
|
{
|
|
|
|
return m_Router->tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_crypto*
|
|
|
|
PathContext::Crypto()
|
|
|
|
{
|
|
|
|
return &m_Router->crypto;
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:33:38 +00:00
|
|
|
llarp_logic*
|
|
|
|
PathContext::Logic()
|
|
|
|
{
|
|
|
|
return m_Router->logic;
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:45:12 +00:00
|
|
|
byte_t*
|
|
|
|
PathContext::EncryptionSecretKey()
|
|
|
|
{
|
|
|
|
return m_Router->encryption;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PathContext::HopIsUs(const PubKey& k) const
|
|
|
|
{
|
|
|
|
return memcmp(k, m_Router->pubkey(), PUBKEYSIZE) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PathContext::ForwardLRCM(const RouterID& nextHop,
|
2018-06-18 22:03:50 +00:00
|
|
|
std::deque< EncryptedFrame >& frames)
|
2018-06-12 16:45:12 +00:00
|
|
|
{
|
2018-06-20 12:34:48 +00:00
|
|
|
llarp::Info("fowarding LRCM to ", nextHop);
|
2018-06-12 16:45:12 +00:00
|
|
|
LR_CommitMessage* msg = new LR_CommitMessage;
|
|
|
|
while(frames.size())
|
|
|
|
{
|
2018-06-15 14:33:38 +00:00
|
|
|
msg->frames.push_back(frames.back());
|
|
|
|
frames.pop_back();
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
2018-06-21 09:31:53 +00:00
|
|
|
return m_Router->SendToOrQueue(nextHop, msg);
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
template < typename Map_t, typename Key_t, typename CheckValue_t >
|
2018-06-12 16:45:12 +00:00
|
|
|
bool
|
2018-06-18 22:03:50 +00:00
|
|
|
MapHas(Map_t& map, const Key_t& k, CheckValue_t check)
|
2018-06-12 16:45:12 +00:00
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(map.first);
|
2018-06-18 22:03:50 +00:00
|
|
|
auto itr = map.second.find(k);
|
|
|
|
while(itr != map.second.end())
|
|
|
|
{
|
|
|
|
if(check(itr->second))
|
|
|
|
return true;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template < typename Map_t, typename Key_t, typename Value_t >
|
|
|
|
void
|
|
|
|
MapPut(Map_t& map, const Key_t& k, const Value_t& v)
|
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(map.first);
|
2018-06-18 22:03:50 +00:00
|
|
|
map.second.emplace(k, v);
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
void
|
|
|
|
PathContext::AddOwnPath(Path* path)
|
|
|
|
{
|
|
|
|
MapPut(m_OurPaths, path->PathID(), path);
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
bool
|
|
|
|
PathContext::HasTransitHop(const TransitHopInfo& info)
|
|
|
|
{
|
2018-06-18 22:03:50 +00:00
|
|
|
return MapHas(
|
|
|
|
m_TransitPaths, info.pathID,
|
|
|
|
[info](const TransitHop& hop) -> bool { return info == hop.info; });
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t*
|
|
|
|
PathContext::OurRouterID() const
|
|
|
|
{
|
|
|
|
return m_Router->pubkey();
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-06-18 22:03:50 +00:00
|
|
|
PathContext::PutTransitHop(const TransitHop& hop)
|
2018-06-12 16:45:12 +00:00
|
|
|
{
|
2018-06-18 22:03:50 +00:00
|
|
|
MapPut(m_TransitPaths, hop.info.pathID, hop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::ExpirePaths()
|
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(m_TransitPaths.first);
|
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
auto& map = m_TransitPaths.second;
|
|
|
|
auto itr = map.begin();
|
|
|
|
while(itr != map.end())
|
|
|
|
{
|
|
|
|
if(itr->second.Expired(now))
|
|
|
|
itr = map.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-06-18 22:03:50 +00:00
|
|
|
TransitHop::Expired(llarp_time_t now) const
|
2018-06-12 16:45:12 +00:00
|
|
|
{
|
2018-06-18 22:03:50 +00:00
|
|
|
return now - started > lifetime;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TransitHopInfo::TransitHopInfo(const RouterID& down,
|
|
|
|
const LR_CommitRecord& record)
|
2018-06-18 22:03:50 +00:00
|
|
|
: pathID(record.pathid), upstream(record.nextHop), downstream(down)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-06-20 12:34:48 +00:00
|
|
|
Path::Path(llarp_path_hops* h) : hops(h->numHops)
|
|
|
|
{
|
|
|
|
for(size_t idx = 0; idx < h->numHops; ++idx)
|
|
|
|
{
|
|
|
|
llarp_rc_copy(&hops[idx].router, &h->hops[idx].router);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
const PathID_t&
|
|
|
|
Path::PathID() const
|
|
|
|
{
|
|
|
|
return hops[0].pathID;
|
|
|
|
}
|
|
|
|
|
|
|
|
RouterID
|
|
|
|
Path::Upstream()
|
|
|
|
{
|
|
|
|
return hops[0].router.pubkey;
|
|
|
|
}
|
|
|
|
|
2018-06-21 09:31:53 +00:00
|
|
|
} // namespace llarp
|