lokinet/llarp/exit/session.cpp

195 lines
5.5 KiB
C++
Raw Normal View History

2018-12-12 01:06:46 +00:00
#include <exit/session.hpp>
#include <path.hpp>
#include <router.hpp>
namespace llarp
{
namespace exit
{
2018-11-14 19:34:17 +00:00
BaseSession::BaseSession(const llarp::RouterID& router,
std::function< bool(llarp_buffer_t) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen)
2018-11-14 19:34:17 +00:00
: llarp::path::Builder(r, r->dht, numpaths, hoplen)
, m_ExitRouter(router)
, m_WritePacket(writepkt)
2018-11-29 21:19:20 +00:00
, m_Counter(0)
2018-12-13 12:27:14 +00:00
, m_LastUse(0)
2018-11-14 19:34:17 +00:00
{
r->crypto.identity_keygen(m_ExitIdentity);
}
BaseSession::~BaseSession()
{
}
bool
BaseSession::LoadIdentityFromFile(const char* fname)
{
return m_ExitIdentity.LoadFromFile(fname);
}
bool
BaseSession::ShouldBuildMore(llarp_time_t now) const
{
2018-11-25 16:58:27 +00:00
const size_t expect = (1 + (m_NumPaths / 2));
if(NumPathsExistingAt(now + (10 * 1000)) < expect)
2018-12-10 15:31:58 +00:00
return path::Builder::ShouldBuildMore(now);
2018-11-25 16:58:27 +00:00
if(AvailablePaths(llarp::path::ePathRoleExit) < expect)
2018-12-10 15:31:58 +00:00
return path::Builder::ShouldBuildMore(now);
2018-11-25 16:58:27 +00:00
return false;
}
2018-11-14 19:34:17 +00:00
bool
BaseSession::SelectHop(llarp_nodedb* db, const RouterContact& prev,
RouterContact& cur, size_t hop,
llarp::path::PathRole roles)
{
if(hop == numHops - 1)
2018-12-10 15:44:18 +00:00
{
return db->Get(m_ExitRouter, cur);
2018-12-10 15:44:18 +00:00
}
2018-11-14 19:34:17 +00:00
else
return path::Builder::SelectHop(db, prev, cur, hop, roles);
}
void
BaseSession::HandlePathBuilt(llarp::path::Path* p)
{
2018-12-10 15:31:58 +00:00
path::Builder::HandlePathBuilt(p);
2018-11-14 19:34:17 +00:00
p->SetDropHandler(std::bind(&BaseSession::HandleTrafficDrop, this,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3));
p->SetExitTrafficHandler(std::bind(&BaseSession::HandleTraffic, this,
std::placeholders::_1,
std::placeholders::_2));
p->AddObtainExitHandler(std::bind(&BaseSession::HandleGotExit, this,
std::placeholders::_1,
std::placeholders::_2));
llarp::routing::ObtainExitMessage obtain;
obtain.S = p->NextSeqNo();
obtain.T = llarp::randint();
2018-11-29 13:12:35 +00:00
PopulateRequest(obtain);
2018-11-14 19:34:17 +00:00
if(!obtain.Sign(&router->crypto, m_ExitIdentity))
{
llarp::LogError("Failed to sign exit request");
return;
}
if(p->SendExitRequest(&obtain, router))
llarp::LogInfo("asking ", m_ExitRouter, " for exit");
else
llarp::LogError("faild to send exit request");
}
bool
BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b)
{
2018-12-13 12:27:14 +00:00
m_LastUse = router->Now();
2018-11-14 19:34:17 +00:00
if(b == 0)
llarp::LogInfo("obtained an exit via ", p->Endpoint());
2018-12-13 12:27:14 +00:00
2018-11-14 19:34:17 +00:00
return true;
}
bool
BaseSession::HandleTraffic(llarp::path::Path* p, llarp_buffer_t pkt)
{
(void)p;
if(m_WritePacket)
2018-12-13 12:27:14 +00:00
{
if(!m_WritePacket(pkt))
return false;
m_LastUse = router->Now();
return true;
}
2018-11-14 19:34:17 +00:00
return false;
}
bool
BaseSession::HandleTrafficDrop(llarp::path::Path* p, const PathID_t& path,
uint64_t s)
{
(void)p;
llarp::LogError("dropped traffic on exit ", m_ExitRouter, " S=", s,
" P=", path);
return true;
}
bool
BaseSession::QueueUpstreamTraffic(llarp::net::IPv4Packet pkt,
const size_t N)
2018-11-28 16:38:20 +00:00
{
auto buf = pkt.Buffer();
auto& queue = m_Upstream[buf.sz / N];
2018-11-29 13:12:35 +00:00
// queue overflow
if(queue.size() >= MaxUpstreamQueueLength)
2018-11-29 13:12:35 +00:00
return false;
if(queue.size() == 0)
{
queue.emplace_back();
2018-11-29 21:19:20 +00:00
return queue.back().PutBuffer(buf, m_Counter++);
}
auto& back = queue.back();
2018-11-28 16:38:20 +00:00
// pack to nearest N
if(back.Size() + buf.sz > N)
{
queue.emplace_back();
return queue.back().PutBuffer(buf, m_Counter++);
2018-11-28 16:38:20 +00:00
}
else
2018-11-29 21:19:20 +00:00
return back.PutBuffer(buf, m_Counter++);
2018-11-28 16:38:20 +00:00
}
2018-11-29 13:12:35 +00:00
bool
BaseSession::IsReady() const
2018-11-29 13:12:35 +00:00
{
return AvailablePaths(llarp::path::ePathRoleExit) > 0;
}
2018-12-13 12:27:14 +00:00
bool
BaseSession::IsExpired(llarp_time_t now) const
{
return m_LastUse && now > m_LastUse && now - m_LastUse > LifeSpan;
}
bool
2018-11-28 17:58:46 +00:00
BaseSession::FlushUpstreamTraffic()
2018-11-14 19:34:17 +00:00
{
2018-12-13 12:27:14 +00:00
auto now = router->Now();
2018-11-14 19:34:17 +00:00
auto path = PickRandomEstablishedPath(llarp::path::ePathRoleExit);
if(!path)
2018-11-28 16:38:20 +00:00
{
// discard
for(auto& item : m_Upstream)
item.second.clear();
2018-11-14 19:34:17 +00:00
return false;
2018-11-28 16:38:20 +00:00
}
for(auto& item : m_Upstream)
2018-11-28 16:38:20 +00:00
{
auto& queue = item.second;
while(queue.size())
{
auto& msg = queue.front();
msg.S = path->NextSeqNo();
2018-12-13 12:27:14 +00:00
if(path->SendRoutingMessage(&msg, router))
m_LastUse = now;
queue.pop_front();
}
2018-11-28 16:38:20 +00:00
}
return true;
2018-11-14 19:34:17 +00:00
}
SNodeSession::SNodeSession(const llarp::RouterID& snodeRouter,
std::function< bool(llarp_buffer_t) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen,
bool useRouterSNodeKey)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen)
{
if(useRouterSNodeKey)
{
m_ExitIdentity = r->identity;
}
}
2018-11-14 19:34:17 +00:00
} // namespace exit
2018-12-10 15:31:58 +00:00
} // namespace llarp