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>
|
2018-07-09 17:32:11 +00:00
|
|
|
#include <llarp/pathbuilder.hpp>
|
2018-06-22 00:25:30 +00:00
|
|
|
#include "buffer.hpp"
|
2018-06-10 14:05:48 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace path
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
PathContext::PathContext(llarp_router* router)
|
|
|
|
: m_Router(router), m_AllowTransit(false)
|
|
|
|
{
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
PathContext::~PathContext()
|
|
|
|
{
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
PathContext::AllowTransit()
|
|
|
|
{
|
|
|
|
m_AllowTransit = true;
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
PathContext::AllowingTransit() const
|
|
|
|
{
|
|
|
|
return m_AllowTransit;
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_threadpool*
|
|
|
|
PathContext::Worker()
|
|
|
|
{
|
|
|
|
return m_Router->tp;
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_crypto*
|
|
|
|
PathContext::Crypto()
|
|
|
|
{
|
|
|
|
return &m_Router->crypto;
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_logic*
|
|
|
|
PathContext::Logic()
|
|
|
|
{
|
|
|
|
return m_Router->logic;
|
|
|
|
}
|
2018-06-15 14:33:38 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
byte_t*
|
|
|
|
PathContext::EncryptionSecretKey()
|
|
|
|
{
|
|
|
|
return m_Router->encryption;
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
PathContext::HopIsUs(const PubKey& k) const
|
|
|
|
{
|
|
|
|
return memcmp(k, m_Router->pubkey(), PUBKEYSIZE) == 0;
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
PathContext::ForwardLRCM(const RouterID& nextHop,
|
|
|
|
std::deque< EncryptedFrame >& frames)
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("fowarding LRCM to ", nextHop);
|
2018-06-25 15:12:08 +00:00
|
|
|
LR_CommitMessage* msg = new LR_CommitMessage;
|
|
|
|
while(frames.size())
|
|
|
|
{
|
|
|
|
msg->frames.push_back(frames.front());
|
|
|
|
frames.pop_front();
|
|
|
|
}
|
|
|
|
return m_Router->SendToOrQueue(nextHop, msg);
|
|
|
|
}
|
|
|
|
template < typename Map_t, typename Key_t, typename CheckValue_t,
|
|
|
|
typename GetFunc_t >
|
|
|
|
IHopHandler*
|
|
|
|
MapGet(Map_t& map, const Key_t& k, CheckValue_t check, GetFunc_t get)
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-06-25 15:12:08 +00:00
|
|
|
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;
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
template < typename Map_t, typename Key_t, typename CheckValue_t >
|
|
|
|
bool
|
|
|
|
MapHas(Map_t& map, const Key_t& k, CheckValue_t check)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-06-25 15:12:08 +00:00
|
|
|
auto range = map.second.equal_range(k);
|
|
|
|
for(auto i = range.first; i != range.second; ++i)
|
|
|
|
{
|
|
|
|
if(check(i->second))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +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)
|
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-07-24 06:25:13 +00:00
|
|
|
map.second.insert(std::make_pair(k, v));
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
template < typename Map_t, typename Visit_t >
|
|
|
|
void
|
|
|
|
MapIter(Map_t& map, Visit_t v)
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-06-25 15:12:08 +00:00
|
|
|
for(const auto& item : map.second)
|
|
|
|
v(item);
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
template < typename Map_t, typename Key_t, typename Check_t >
|
|
|
|
void
|
|
|
|
MapDel(Map_t& map, const Key_t& k, Check_t check)
|
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-06-25 15:12:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
PathContext::AddOwnPath(PathSet* set, Path* path)
|
|
|
|
{
|
|
|
|
set->AddPath(path);
|
|
|
|
MapPut(m_OurPaths, path->TXID(), set);
|
|
|
|
MapPut(m_OurPaths, path->RXID(), set);
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
PathContext::HasTransitHop(const TransitHopInfo& info)
|
|
|
|
{
|
|
|
|
return MapHas(m_TransitPaths, info.txID, [info](TransitHop* hop) -> bool {
|
|
|
|
return info == hop->info;
|
|
|
|
});
|
|
|
|
}
|
2018-06-22 12:45:46 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
IHopHandler*
|
|
|
|
PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id)
|
|
|
|
{
|
|
|
|
auto own = MapGet(m_OurPaths, id,
|
|
|
|
[](const PathSet* s) -> bool {
|
|
|
|
// TODO: is this right?
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[remote, id](PathSet* p) -> IHopHandler* {
|
|
|
|
return p->GetByUpstream(remote, id);
|
|
|
|
});
|
|
|
|
if(own)
|
|
|
|
return own;
|
|
|
|
|
|
|
|
return MapGet(m_TransitPaths, id,
|
|
|
|
[remote](const TransitHop* hop) -> bool {
|
|
|
|
return hop->info.upstream == remote;
|
|
|
|
},
|
|
|
|
[](TransitHop* h) -> IHopHandler* { return h; });
|
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
IHopHandler*
|
|
|
|
PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id)
|
|
|
|
{
|
|
|
|
return MapGet(m_TransitPaths, id,
|
|
|
|
[remote](const TransitHop* hop) -> bool {
|
|
|
|
return hop->info.downstream == remote;
|
|
|
|
},
|
|
|
|
[](TransitHop* h) -> IHopHandler* { return h; });
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
PathSet*
|
|
|
|
PathContext::GetLocalPathSet(const PathID_t& id)
|
|
|
|
{
|
|
|
|
auto& map = m_OurPaths;
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(map.first);
|
2018-07-09 17:32:11 +00:00
|
|
|
auto itr = map.second.find(id);
|
|
|
|
if(itr != map.second.end())
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
const byte_t*
|
|
|
|
PathContext::OurRouterID() const
|
|
|
|
{
|
|
|
|
return m_Router->pubkey();
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_router*
|
|
|
|
PathContext::Router()
|
|
|
|
{
|
|
|
|
return m_Router;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::PutTransitHop(TransitHop* hop)
|
|
|
|
{
|
|
|
|
MapPut(m_TransitPaths, hop->info.txID, hop);
|
|
|
|
MapPut(m_TransitPaths, hop->info.rxID, hop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::ExpirePaths()
|
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock lock(m_TransitPaths.first);
|
2018-06-25 15:12:08 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
auto& map = m_TransitPaths.second;
|
|
|
|
auto itr = map.begin();
|
|
|
|
std::set< TransitHop* > removePaths;
|
|
|
|
while(itr != map.end())
|
|
|
|
{
|
|
|
|
if(itr->second->Expired(now))
|
|
|
|
{
|
|
|
|
TransitHop* path = itr->second;
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("transit path expired ", path->info);
|
2018-06-25 15:12:08 +00:00
|
|
|
removePaths.insert(path);
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
for(auto& p : removePaths)
|
|
|
|
{
|
|
|
|
map.erase(p->info.txID);
|
|
|
|
map.erase(p->info.rxID);
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
for(auto& builder : m_PathBuilders)
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
if(builder)
|
|
|
|
builder->ExpirePaths(now);
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::BuildPaths()
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
for(auto& builder : m_PathBuilders)
|
|
|
|
{
|
|
|
|
if(builder->ShouldBuildMore())
|
|
|
|
{
|
|
|
|
builder->BuildOne();
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
2018-06-12 16:45:12 +00:00
|
|
|
|
2018-06-29 16:02:39 +00:00
|
|
|
void
|
|
|
|
PathContext::TickPaths()
|
|
|
|
{
|
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
for(auto& builder : m_PathBuilders)
|
|
|
|
builder->Tick(now, m_Router);
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
routing::IMessageHandler*
|
|
|
|
PathContext::GetHandler(const PathID_t& id)
|
|
|
|
{
|
|
|
|
routing::IMessageHandler* h = nullptr;
|
|
|
|
auto pathset = GetLocalPathSet(id);
|
|
|
|
if(pathset)
|
|
|
|
{
|
|
|
|
h = pathset->GetPathByID(id);
|
|
|
|
}
|
|
|
|
if(h)
|
|
|
|
return h;
|
|
|
|
RouterID us(OurRouterID());
|
|
|
|
auto& map = m_TransitPaths;
|
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
util::Lock 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;
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
PathContext::AddPathBuilder(llarp_pathbuilder_context* ctx)
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
m_PathBuilders.push_back(ctx);
|
2018-06-22 12:45:46 +00:00
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-08-18 14:01:21 +00:00
|
|
|
void
|
|
|
|
PathContext::RemovePathSet(PathSet* set)
|
|
|
|
{
|
|
|
|
util::Lock lock(m_OurPaths.first);
|
|
|
|
auto& map = m_OurPaths.second;
|
|
|
|
auto itr = map.begin();
|
|
|
|
while(itr != map.end())
|
|
|
|
{
|
|
|
|
if(itr->second == set)
|
|
|
|
itr = map.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathContext::RemovePathBuilder(llarp_pathbuilder_context* ctx)
|
|
|
|
{
|
|
|
|
m_PathBuilders.remove(ctx);
|
|
|
|
RemovePathSet(ctx);
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
PathHopConfig::PathHopConfig()
|
2018-06-22 12:45:46 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_rc_clear(&router);
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
PathHopConfig::~PathHopConfig()
|
|
|
|
{
|
|
|
|
llarp_rc_free(&router);
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +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);
|
|
|
|
hops[idx].txID.Randomize();
|
|
|
|
hops[idx].rxID.Randomize();
|
|
|
|
}
|
2018-06-29 16:02:39 +00:00
|
|
|
|
2018-06-26 14:52:19 +00:00
|
|
|
for(size_t idx = 0; idx < h->numHops - 1; ++idx)
|
|
|
|
{
|
|
|
|
hops[idx].txID = hops[idx + 1].rxID;
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
// initialize parts of the introduction
|
|
|
|
intro.router = hops[h->numHops - 1].router.pubkey;
|
|
|
|
// TODO: or is it rxid ?
|
|
|
|
intro.pathID = hops[h->numHops - 1].txID;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
Path::SetBuildResultHook(BuildResultHookFunc func)
|
|
|
|
{
|
|
|
|
m_BuiltHook = func;
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-07-23 07:38:29 +00:00
|
|
|
RouterID
|
|
|
|
Path::Endpoint() const
|
|
|
|
{
|
|
|
|
return hops[hops.size() - 1].router.pubkey;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
const PathID_t&
|
|
|
|
Path::TXID() const
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
return hops[0].txID;
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
const PathID_t&
|
|
|
|
Path::RXID() const
|
|
|
|
{
|
|
|
|
return hops[0].rxID;
|
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
|
|
|
Path::IsReady() const
|
|
|
|
{
|
|
|
|
return intro.latency > 0 && status == ePathEstablished;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
RouterID
|
|
|
|
Path::Upstream() const
|
|
|
|
{
|
|
|
|
return hops[0].router.pubkey;
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:02:39 +00:00
|
|
|
void
|
|
|
|
Path::Tick(llarp_time_t now, llarp_router* r)
|
|
|
|
{
|
2018-07-23 07:38:29 +00:00
|
|
|
if(Expired(now))
|
|
|
|
return;
|
2018-07-23 23:08:08 +00:00
|
|
|
if(now < m_LastLatencyTestTime)
|
|
|
|
return;
|
2018-06-29 16:02:39 +00:00
|
|
|
auto dlt = now - m_LastLatencyTestTime;
|
2018-07-03 13:54:43 +00:00
|
|
|
if(dlt > 5000 && m_LastLatencyTestID == 0)
|
2018-06-29 16:02:39 +00:00
|
|
|
{
|
|
|
|
llarp::routing::PathLatencyMessage latency;
|
2018-07-20 04:50:28 +00:00
|
|
|
latency.T = llarp_randint();
|
2018-06-29 16:02:39 +00:00
|
|
|
m_LastLatencyTestID = latency.T;
|
|
|
|
m_LastLatencyTestTime = now;
|
|
|
|
SendRoutingMessage(&latency, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
|
2018-06-22 00:25:30 +00:00
|
|
|
llarp_router* r)
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
2018-07-28 22:20:32 +00:00
|
|
|
TunnelNonce n = Y;
|
2018-06-25 15:12:08 +00:00
|
|
|
for(const auto& hop : hops)
|
|
|
|
{
|
2018-07-28 22:20:32 +00:00
|
|
|
r->crypto.xchacha20(buf, hop.shared, n);
|
|
|
|
n ^= hop.nonceXOR;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
RelayUpstreamMessage* msg = new RelayUpstreamMessage;
|
|
|
|
msg->X = buf;
|
|
|
|
msg->Y = Y;
|
|
|
|
msg->pathid = TXID();
|
2018-08-10 21:34:11 +00:00
|
|
|
if(r->SendToOrQueue(Upstream(), msg))
|
|
|
|
return true;
|
|
|
|
llarp::LogError("send to ", Upstream(), " failed");
|
|
|
|
return false;
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::Expired(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
if(status == ePathEstablished)
|
|
|
|
return now - buildStarted > hops[0].lifetime;
|
|
|
|
else if(status == ePathBuilding)
|
|
|
|
return now - buildStarted > PATH_BUILD_TIMEOUT;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-23 00:00:44 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
|
|
|
|
llarp_router* r)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2018-07-28 22:20:32 +00:00
|
|
|
TunnelNonce n = Y;
|
2018-06-25 15:12:08 +00:00
|
|
|
for(const auto& hop : hops)
|
|
|
|
{
|
2018-07-28 22:20:32 +00:00
|
|
|
n ^= hop.nonceXOR;
|
|
|
|
r->crypto.xchacha20(buf, hop.shared, n);
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
return HandleRoutingMessage(buf, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r)
|
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
if(!m_InboundMessageParser.ParseMessageBuffer(buf, this, RXID(), r))
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("Failed to parse inbound routing message");
|
2018-06-25 15:12:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-07-24 22:34:46 +00:00
|
|
|
Path::SendRoutingMessage(llarp::routing::IMessage* msg, llarp_router* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-07-24 22:34:46 +00:00
|
|
|
msg->S = m_SequenceNum++;
|
2018-06-25 15:12:08 +00:00
|
|
|
byte_t tmp[MAX_LINK_MSG_SIZE / 2];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!msg->BEncode(&buf))
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Bencode failed");
|
|
|
|
llarp::DumpBuffer(buf);
|
2018-06-25 15:12:08 +00:00
|
|
|
return false;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
// make nonce
|
|
|
|
TunnelNonce N;
|
|
|
|
N.Randomize();
|
2018-07-23 22:36:46 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
// pad smaller messages
|
|
|
|
if(buf.sz < MESSAGE_PAD_SIZE)
|
|
|
|
{
|
|
|
|
// randomize padding
|
|
|
|
r->crypto.randbytes(buf.cur, MESSAGE_PAD_SIZE - buf.sz);
|
|
|
|
buf.sz = MESSAGE_PAD_SIZE;
|
|
|
|
}
|
|
|
|
buf.cur = buf.base;
|
2018-06-25 15:12:08 +00:00
|
|
|
return HandleUpstream(buf, N, r);
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:23:43 +00:00
|
|
|
bool
|
|
|
|
Path::HandlePathTransferMessage(
|
|
|
|
const llarp::routing::PathTransferMessage* msg, llarp_router* r)
|
|
|
|
{
|
2018-07-17 04:37:50 +00:00
|
|
|
llarp::LogWarn("unwarrented path transfer message on tx=", TXID(),
|
|
|
|
" rx=", RXID());
|
2018-06-26 16:23:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::HandlePathConfirmMessage(
|
2018-06-26 16:23:43 +00:00
|
|
|
const llarp::routing::PathConfirmMessage* msg, llarp_router* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
|
|
|
if(status == ePathBuilding)
|
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
// finish initializing introduction
|
|
|
|
intro.expiresAt = buildStarted + hops[0].lifetime;
|
2018-06-25 15:12:08 +00:00
|
|
|
// confirm that we build the path
|
|
|
|
status = ePathEstablished;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("path is confirmed tx=", TXID(), " rx=", RXID());
|
2018-06-25 15:12:08 +00:00
|
|
|
if(m_BuiltHook)
|
|
|
|
m_BuiltHook(this);
|
|
|
|
m_BuiltHook = nullptr;
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2018-06-26 16:23:43 +00:00
|
|
|
llarp::routing::PathLatencyMessage latency;
|
2018-07-20 04:50:28 +00:00
|
|
|
latency.T = llarp_randint();
|
2018-06-26 16:23:43 +00:00
|
|
|
m_LastLatencyTestID = latency.T;
|
|
|
|
m_LastLatencyTestTime = llarp_time_now_ms();
|
|
|
|
return SendRoutingMessage(&latency, r);
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("got unwarrented path confirm message on tx=", RXID(),
|
|
|
|
" rx=", RXID());
|
2018-06-22 00:25:30 +00:00
|
|
|
return false;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
|
|
|
Path::HandleHiddenServiceFrame(const llarp::service::ProtocolFrame* frame)
|
|
|
|
{
|
|
|
|
if(m_DataHandler)
|
|
|
|
return m_DataHandler(frame);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::HandlePathLatencyMessage(
|
2018-06-26 16:23:43 +00:00
|
|
|
const llarp::routing::PathLatencyMessage* msg, llarp_router* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-07-23 07:38:29 +00:00
|
|
|
if(msg->L == m_LastLatencyTestID && status == ePathEstablished)
|
2018-06-26 16:23:43 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
intro.latency = llarp_time_now_ms() - m_LastLatencyTestTime;
|
|
|
|
llarp::LogInfo("path latency is ", intro.latency, " ms for tx=", TXID(),
|
2018-07-05 15:44:06 +00:00
|
|
|
" rx=", RXID());
|
2018-07-03 13:54:43 +00:00
|
|
|
m_LastLatencyTestID = 0;
|
2018-07-23 07:38:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("unwarrented path latency message via ", Upstream());
|
|
|
|
return false;
|
2018-06-26 16:23:43 +00:00
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2018-06-26 16:23:43 +00:00
|
|
|
Path::HandleDHTMessage(const llarp::dht::IMessage* msg, llarp_router* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
std::vector< llarp::dht::IMessage* > discard;
|
|
|
|
auto result = msg->HandleMessage(r->dht, discard);
|
|
|
|
for(auto& msg : discard)
|
|
|
|
delete msg;
|
|
|
|
return result;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
} // namespace path
|
2018-06-21 09:31:53 +00:00
|
|
|
} // namespace llarp
|