You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/exit/endpoint.cpp

200 lines
5.2 KiB
C++

#include <exit/endpoint.hpp>
#include <handlers/exit.hpp>
#include <router/router.hpp>
namespace llarp
{
namespace exit
{
6 years ago
Endpoint::Endpoint(const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath, bool rewriteIP,
huint32_t ip, llarp::handlers::ExitEndpoint* parent)
: createdAt(parent->Now())
, m_Parent(parent)
6 years ago
, m_remoteSignKey(remoteIdent)
, m_CurrentPath(beginPath)
, m_IP(ip)
6 years ago
, m_RewriteSource(rewriteIP)
, m_Counter(0)
6 years ago
{
m_LastActive = parent->Now();
6 years ago
}
Endpoint::~Endpoint()
{
m_Parent->DelEndpointInfo(m_CurrentPath);
6 years ago
}
6 years ago
void
Endpoint::Close()
{
m_Parent->RemoveExit(this);
}
void
Endpoint::ExtractStatus(util::StatusObject& obj) const
{
obj.PutString("identity", m_remoteSignKey.ToHex());
obj.PutString("ip", m_IP.ToString());
obj.PutInt("txRate", m_TxRate);
obj.PutInt("rxRate", m_RxRate);
obj.PutInt("createdAt", createdAt);
auto now = m_Parent->Now();
obj.PutBool("exiting", !m_RewriteSource);
obj.PutBool("looksDead", LooksDead(now));
obj.PutBool("expiresSoon", ExpiresSoon(now));
obj.PutBool("expired", IsExpired(now));
}
6 years ago
bool
Endpoint::UpdateLocalPath(const llarp::PathID_t& nextPath)
{
if(!m_Parent->UpdateEndpointPath(m_remoteSignKey, nextPath))
return false;
m_CurrentPath = nextPath;
return true;
}
6 years ago
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;
}
6 years ago
bool
Endpoint::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
{
auto path = GetCurrentPath();
if(path)
return path->ExpiresSoon(now, dlt);
return true;
}
bool
Endpoint::LooksDead(llarp_time_t now, llarp_time_t timeout) const
{
if(ExpiresSoon(now, timeout))
return true;
auto path = GetCurrentPath();
if(!path)
return true;
auto lastPing = path->LastRemoteActivityAt();
if(lastPing == 0 || (now > lastPing && now - lastPing > timeout))
return now > m_LastActive && now - m_LastActive > timeout;
5 years ago
else if(lastPing)
return now > lastPing && now - lastPing > timeout;
return lastPing > 0;
}
6 years ago
bool
Endpoint::QueueOutboundTraffic(ManagedBuffer buf, uint64_t counter)
6 years ago
{
// queue overflow
if(m_UpstreamQueue.size() > MaxUpstreamQueueSize)
return false;
6 years ago
llarp::net::IPv4Packet pkt;
if(!pkt.Load(buf.underlying))
6 years ago
return false;
6 years ago
huint32_t dst;
if(m_RewriteSource)
dst = m_Parent->GetIfAddr();
else
dst = pkt.dst();
pkt.UpdateIPv4PacketOnDst(m_IP, dst);
m_UpstreamQueue.emplace(pkt, counter);
m_TxRate += buf.underlying.sz;
m_LastActive = m_Parent->Now();
6 years ago
return true;
6 years ago
}
bool
Endpoint::QueueInboundTraffic(ManagedBuffer buf)
{
llarp::net::IPv4Packet pkt;
if(!pkt.Load(buf.underlying))
return false;
huint32_t src;
if(m_RewriteSource)
src = m_Parent->GetIfAddr();
else
src = pkt.src();
pkt.UpdateIPv4PacketOnDst(src, m_IP);
const llarp_buffer_t& pktbuf = pkt.Buffer(); // life time extension
uint8_t queue_idx = pktbuf.sz / llarp::routing::ExitPadSize;
auto& queue = m_DownstreamQueues[queue_idx];
if(queue.size() == 0)
{
queue.emplace_back();
return queue.back().PutBuffer(buf.underlying, m_Counter++);
}
auto& msg = queue.back();
if(msg.Size() + pktbuf.sz > llarp::routing::ExitPadSize)
{
queue.emplace_back();
return queue.back().PutBuffer(pktbuf, m_Counter++);
}
else
return msg.PutBuffer(pktbuf, m_Counter++);
}
bool
Endpoint::Flush()
{
// flush upstream queue
while(m_UpstreamQueue.size())
{
m_Parent->QueueOutboundTraffic(m_UpstreamQueue.top().pkt.ConstBuffer());
m_UpstreamQueue.pop();
}
// flush downstream queue
auto path = GetCurrentPath();
bool sent = path != nullptr;
if(path)
{
for(auto& item : m_DownstreamQueues)
{
auto& queue = item.second;
while(queue.size())
{
auto& msg = queue.front();
msg.S = path->NextSeqNo();
if(path->SendRoutingMessage(&msg, m_Parent->GetRouter()))
{
m_RxRate += msg.Size();
sent = true;
}
queue.pop_front();
}
}
}
for(auto& item : m_DownstreamQueues)
item.second.clear();
return sent;
}
llarp::path::IHopHandler*
Endpoint::GetCurrentPath() const
{
auto router = m_Parent->GetRouter();
return router->paths.GetByUpstream(router->pubkey(), m_CurrentPath);
}
} // namespace exit
} // namespace llarp