lokinet/llarp/exit/endpoint.cpp

229 lines
6.1 KiB
C++
Raw Normal View History

2018-12-12 01:06:46 +00:00
#include <exit/endpoint.hpp>
2019-06-17 23:19:39 +00:00
2018-12-12 01:12:59 +00:00
#include <handlers/exit.hpp>
2019-06-17 23:19:39 +00:00
#include <path/path_context.hpp>
#include <router/abstractrouter.hpp>
namespace llarp
{
namespace exit
{
Endpoint::Endpoint(
const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath,
bool rewriteIP,
huint128_t ip,
llarp::handlers::ExitEndpoint* parent)
2018-12-23 13:29:11 +00:00
: createdAt(parent->Now())
, m_Parent(parent)
2018-11-14 12:23:08 +00:00
, m_remoteSignKey(remoteIdent)
, m_CurrentPath(beginPath)
, m_IP(ip)
2018-11-14 12:23:08 +00:00
, m_RewriteSource(rewriteIP)
2018-11-29 21:19:20 +00:00
, m_Counter(0)
2018-11-14 12:23:08 +00:00
{
2018-11-28 12:32:38 +00:00
m_LastActive = parent->Now();
2018-11-14 12:23:08 +00:00
}
Endpoint::~Endpoint()
{
m_Parent->DelEndpointInfo(m_CurrentPath);
2018-11-14 12:23:08 +00:00
}
2018-11-14 18:02:27 +00:00
void
Endpoint::Close()
{
m_Parent->RemoveExit(this);
}
2019-02-11 17:14:43 +00:00
util::StatusObject
Endpoint::ExtractStatus() const
2019-02-08 19:43:25 +00:00
{
auto now = m_Parent->Now();
2019-02-11 17:14:43 +00:00
util::StatusObject obj{{"identity", m_remoteSignKey.ToString()},
{"ip", m_IP.ToString()},
{"txRate", m_TxRate},
{"rxRate", m_RxRate},
{"createdAt", to_json(createdAt)},
2019-02-11 17:14:43 +00:00
{"exiting", !m_RewriteSource},
{"looksDead", LooksDead(now)},
{"expiresSoon", ExpiresSoon(now)},
{"expired", IsExpired(now)}};
return obj;
2019-02-08 19:43:25 +00:00
}
2018-11-14 12:23:08 +00:00
bool
Endpoint::UpdateLocalPath(const llarp::PathID_t& nextPath)
{
if (!m_Parent->UpdateEndpointPath(m_remoteSignKey, nextPath))
2018-11-14 12:23:08 +00:00
return false;
m_CurrentPath = nextPath;
return true;
}
2018-11-14 18:02:27 +00:00
void
Endpoint::Tick(llarp_time_t now)
{
(void)now;
m_RxRate = 0;
m_TxRate = 0;
}
bool
Endpoint::IsExpired(llarp_time_t now) const
{
auto path = GetCurrentPath();
if (path)
{
return path->Expired(now);
}
// if we don't have an underlying path we are considered expired
return true;
}
2018-11-14 18:02:27 +00:00
bool
Endpoint::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
{
auto path = GetCurrentPath();
if (path)
2018-11-14 18:02:27 +00:00
return path->ExpiresSoon(now, dlt);
return true;
}
bool
Endpoint::LooksDead(llarp_time_t now, llarp_time_t timeout) const
2018-11-28 12:32:38 +00:00
{
if (ExpiresSoon(now, timeout))
2018-11-28 12:32:38 +00:00
return true;
auto path = GetCurrentPath();
if (!path)
return true;
auto lastPing = path->LastRemoteActivityAt();
if (lastPing == 0s || (now > lastPing && now - lastPing > timeout))
return now > m_LastActive && now - m_LastActive > timeout;
else if (lastPing > 0s) // NOLINT
2018-12-27 15:31:24 +00:00
return now > lastPing && now - lastPing > timeout;
2020-02-24 19:40:45 +00:00
return lastPing > 0s;
2018-11-28 12:32:38 +00:00
}
2018-11-14 12:23:08 +00:00
bool
2019-02-03 00:48:10 +00:00
Endpoint::QueueOutboundTraffic(ManagedBuffer buf, uint64_t counter)
2018-11-14 12:23:08 +00:00
{
2018-11-29 21:19:20 +00:00
// queue overflow
if (m_UpstreamQueue.size() > MaxUpstreamQueueSize)
2018-11-29 21:19:20 +00:00
return false;
2019-06-11 16:44:05 +00:00
llarp::net::IPPacket pkt;
if (!pkt.Load(buf.underlying))
2018-11-14 12:23:08 +00:00
return false;
if (pkt.IsV6() && m_Parent->SupportsV6())
2019-06-11 19:57:52 +00:00
{
huint128_t dst;
if (m_RewriteSource)
2019-06-11 19:57:52 +00:00
dst = m_Parent->GetIfAddr();
else
dst = pkt.dstv6();
2019-06-11 22:29:45 +00:00
pkt.UpdateIPv6Address(m_IP, dst);
2019-06-11 19:57:52 +00:00
}
else if (pkt.IsV4() && !m_Parent->SupportsV6())
2019-06-11 19:57:52 +00:00
{
huint32_t dst;
if (m_RewriteSource)
2019-06-11 19:57:52 +00:00
dst = net::IPPacket::TruncateV6(m_Parent->GetIfAddr());
else
dst = pkt.dstv4();
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(m_IP)), xhtonl(dst));
2019-06-11 19:57:52 +00:00
}
2018-11-14 12:23:08 +00:00
else
2019-06-11 19:57:52 +00:00
{
return false;
}
2018-11-29 21:19:20 +00:00
m_UpstreamQueue.emplace(pkt, counter);
2019-02-02 23:12:42 +00:00
m_TxRate += buf.underlying.sz;
2018-11-28 12:32:38 +00:00
m_LastActive = m_Parent->Now();
2018-11-14 18:02:27 +00:00
return true;
2018-11-14 12:23:08 +00:00
}
bool
2019-02-03 00:48:10 +00:00
Endpoint::QueueInboundTraffic(ManagedBuffer buf)
{
2019-06-11 16:44:05 +00:00
llarp::net::IPPacket pkt;
if (!pkt.Load(buf.underlying))
return false;
2019-06-11 16:44:05 +00:00
huint128_t src;
if (m_RewriteSource)
2018-11-29 13:12:35 +00:00
src = m_Parent->GetIfAddr();
else
2019-06-11 16:44:05 +00:00
src = pkt.srcv6();
if (pkt.IsV6())
2019-12-06 20:33:43 +00:00
pkt.UpdateIPv6Address(src, m_IP);
else
pkt.UpdateIPv4Address(
xhtonl(net::IPPacket::TruncateV6(src)), xhtonl(net::IPPacket::TruncateV6(m_IP)));
2019-12-06 20:33:43 +00:00
const auto _pktbuf = pkt.Buffer();
const llarp_buffer_t& pktbuf = _pktbuf.underlying;
const uint8_t queue_idx = pktbuf.sz / llarp::routing::ExitPadSize;
if (m_DownstreamQueues.find(queue_idx) == m_DownstreamQueues.end())
2019-05-07 15:08:49 +00:00
m_DownstreamQueues.emplace(queue_idx, InboundTrafficQueue_t{});
2019-05-08 13:08:21 +00:00
auto& queue = m_DownstreamQueues.at(queue_idx);
if (queue.size() == 0)
{
queue.emplace_back();
return queue.back().PutBuffer(pktbuf, m_Counter++);
}
auto& msg = queue.back();
if (msg.Size() + pktbuf.sz > llarp::routing::ExitPadSize)
2018-11-29 13:12:35 +00:00
{
queue.emplace_back();
2018-11-29 21:19:20 +00:00
return queue.back().PutBuffer(pktbuf, m_Counter++);
2018-11-29 13:12:35 +00:00
}
2019-07-06 17:03:40 +00:00
return msg.PutBuffer(pktbuf, m_Counter++);
2018-11-28 16:38:20 +00:00
}
bool
2018-11-29 21:19:20 +00:00
Endpoint::Flush()
2018-11-28 16:38:20 +00:00
{
2018-11-29 21:19:20 +00:00
// flush upstream queue
while (m_UpstreamQueue.size())
2018-11-29 21:19:20 +00:00
{
m_Parent->QueueOutboundTraffic(m_UpstreamQueue.top().pkt.ConstBuffer());
m_UpstreamQueue.pop();
}
// flush downstream queue
2018-11-28 16:38:20 +00:00
auto path = GetCurrentPath();
bool sent = path != nullptr;
if (path)
2018-11-28 16:38:20 +00:00
{
for (auto& item : m_DownstreamQueues)
2018-11-28 16:38:20 +00:00
{
auto& queue = item.second;
while (queue.size())
2018-11-28 16:38:20 +00:00
{
auto& msg = queue.front();
msg.S = path->NextSeqNo();
if (path->SendRoutingMessage(msg, m_Parent->GetRouter()))
{
m_RxRate += msg.Size();
sent = true;
}
queue.pop_front();
2018-11-28 16:38:20 +00:00
}
}
}
for (auto& item : m_DownstreamQueues)
item.second.clear();
2018-11-28 16:38:20 +00:00
return sent;
}
llarp::path::HopHandler_ptr
Endpoint::GetCurrentPath() const
{
auto router = m_Parent->GetRouter();
return router->pathContext().GetByUpstream(router->pubkey(), m_CurrentPath);
}
} // namespace exit
} // namespace llarp