lokinet/llarp/exit/endpoint.cpp

127 lines
2.9 KiB
C++
Raw Normal View History

#include <llarp/exit/endpoint.hpp>
#include "router.hpp"
namespace llarp
{
namespace exit
{
2018-11-14 12:23:08 +00:00
Endpoint::Endpoint(const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath, bool rewriteIP,
huint32_t ip, llarp::handlers::ExitEndpoint* parent)
2018-11-14 12:23:08 +00:00
: m_Parent(parent)
, m_remoteSignKey(remoteIdent)
, m_CurrentPath(beginPath)
, m_IP(ip)
2018-11-14 12:23:08 +00:00
, m_RewriteSource(rewriteIP)
{
}
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);
}
2018-11-14 12:23:08 +00:00
bool
Endpoint::UpdateLocalPath(const llarp::PathID_t& nextPath)
{
if(!m_Parent->UpdateEndpointPath(m_remoteSignKey, nextPath))
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)
return path->ExpiresSoon(now, dlt);
return true;
}
2018-11-14 12:23:08 +00:00
bool
Endpoint::SendOutboundTraffic(llarp_buffer_t buf)
{
llarp::net::IPv4Packet pkt;
if(!pkt.Load(buf))
return false;
huint32_t dst;
if(m_RewriteSource)
dst = m_Parent->GetIfAddr();
else
dst = pkt.dst();
pkt.UpdateIPv4PacketOnDst(m_IP, dst);
if(!m_Parent->QueueOutboundTraffic(pkt.Buffer()))
2018-11-14 21:52:31 +00:00
{
llarp::LogError("failed to queue outbound traffic");
2018-11-14 18:02:27 +00:00
return false;
2018-11-14 21:52:31 +00:00
}
2018-11-14 18:02:27 +00:00
m_TxRate += buf.sz;
return true;
2018-11-14 12:23:08 +00:00
}
bool
Endpoint::SendInboundTraffic(llarp_buffer_t buf)
{
auto path = GetCurrentPath();
if(path)
{
llarp::net::IPv4Packet pkt;
if(!pkt.Load(buf))
return false;
huint32_t src;
if(m_RewriteSource)
src = m_Parent->GetIfAddr();
else
src = pkt.src();
pkt.UpdateIPv4PacketOnDst(src, m_IP);
llarp::routing::TransferTrafficMessage msg;
if(!msg.PutBuffer(pkt.Buffer()))
return false;
msg.S = path->NextSeqNo();
if(!msg.Sign(m_Parent->Crypto(), m_Parent->Router()->identity))
return false;
2018-11-14 18:02:27 +00:00
if(!path->SendRoutingMessage(&msg, m_Parent->Router()))
return false;
m_RxRate += buf.sz;
return true;
}
return false;
}
llarp::path::IHopHandler*
Endpoint::GetCurrentPath() const
{
auto router = m_Parent->Router();
return router->paths.GetByUpstream(router->pubkey(), m_CurrentPath);
}
} // namespace exit
} // namespace llarp