lokinet/llarp/transit_hop.cpp

178 lines
4.7 KiB
C++
Raw Normal View History

2018-06-22 00:25:30 +00:00
#include <llarp/path.hpp>
2018-06-26 16:23:43 +00:00
#include <llarp/routing/handler.hpp>
#include <llarp/messages/discard.hpp>
2018-06-22 00:25:30 +00:00
#include "buffer.hpp"
#include "router.hpp"
namespace llarp
{
namespace path
2018-06-22 00:25:30 +00:00
{
2018-06-26 16:23:43 +00:00
TransitHop::TransitHop()
{
}
bool
TransitHop::Expired(llarp_time_t now) const
{
return now > ExpireTime();
}
2018-06-22 00:25:30 +00:00
2018-08-22 16:19:51 +00:00
llarp_time_t
TransitHop::ExpireTime() const
{
return started + lifetime;
}
TransitHopInfo::TransitHopInfo(const TransitHopInfo& other)
: txID(other.txID)
, rxID(other.rxID)
, upstream(other.upstream)
, downstream(other.downstream)
{
}
2018-06-22 00:25:30 +00:00
TransitHopInfo::TransitHopInfo(const RouterID& down,
const LR_CommitRecord& record)
: txID(record.txid)
, rxID(record.rxid)
, upstream(record.nextHop)
, downstream(down)
{
}
2018-06-22 00:25:30 +00:00
TransitHop::TransitHop(const TransitHop& other)
: info(other.info)
, pathKey(other.pathKey)
, started(other.started)
, lifetime(other.lifetime)
, version(other.version)
2018-06-22 00:25:30 +00:00
{
}
bool
TransitHop::SendRoutingMessage(llarp::routing::IMessage* msg,
llarp_router* r)
{
if(!IsEndpoint(r->pubkey()))
return false;
byte_t tmp[MAX_LINK_MSG_SIZE - 128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!msg->BEncode(&buf))
{
llarp::LogError("failed to encode routing message");
return false;
}
TunnelNonce N;
N.Randomize();
buf.sz = buf.cur - buf.base;
// pad to nearest MESSAGE_PAD_SIZE bytes
auto dlt = buf.sz % MESSAGE_PAD_SIZE;
if(dlt)
{
dlt = MESSAGE_PAD_SIZE - dlt;
// randomize padding
r->crypto.randbytes(buf.cur, dlt);
buf.sz += dlt;
}
buf.cur = buf.base;
return HandleDownstream(buf, N, r);
}
2018-06-22 00:25:30 +00:00
bool
TransitHop::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
llarp_router* r)
{
RelayDownstreamMessage msg;
msg.pathid = info.rxID;
msg.Y = Y ^ nonceXOR;
r->crypto.xchacha20(buf, pathKey, Y);
msg.X = buf;
llarp::LogDebug("relay ", msg.X.size(), " bytes downstream from ",
info.upstream, " to ", info.downstream);
return r->SendToOrQueue(info.downstream, &msg);
}
2018-06-22 00:25:30 +00:00
bool
TransitHop::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
llarp_router* r)
{
r->crypto.xchacha20(buf, pathKey, Y);
if(IsEndpoint(r->pubkey()))
2018-06-26 16:23:43 +00:00
{
return m_MessageParser.ParseMessageBuffer(buf, this, info.rxID, r);
2018-06-26 16:23:43 +00:00
}
else
{
RelayUpstreamMessage msg;
msg.pathid = info.txID;
msg.Y = Y ^ nonceXOR;
2018-06-26 16:23:43 +00:00
msg.X = buf;
llarp::LogDebug("relay ", msg.X.size(), " bytes upstream from ",
info.downstream, " to ", info.upstream);
return r->SendToOrQueue(info.upstream, &msg);
2018-06-26 16:23:43 +00:00
}
}
bool
TransitHop::HandleDHTMessage(const llarp::dht::IMessage* msg,
llarp_router* r)
{
return r->dht->impl.RelayRequestForPath(info.rxID, msg);
2018-06-26 16:23:43 +00:00
}
bool
TransitHop::HandlePathLatencyMessage(
const llarp::routing::PathLatencyMessage* msg, llarp_router* r)
{
llarp::routing::PathLatencyMessage reply;
reply.L = msg->T;
return SendRoutingMessage(&reply, r);
}
2018-06-26 16:23:43 +00:00
bool
TransitHop::HandlePathConfirmMessage(
const llarp::routing::PathConfirmMessage* msg, llarp_router* r)
{
llarp::LogWarn("unwarrented path confirm message on ", info);
2018-06-26 16:23:43 +00:00
return false;
}
bool
TransitHop::HandleDataDiscardMessage(
const llarp::routing::DataDiscardMessage* msg, llarp_router* r)
{
llarp::LogWarn("unwarranted path data discard message on ", info);
return false;
}
2018-06-26 16:23:43 +00:00
bool
TransitHop::HandlePathTransferMessage(
const llarp::routing::PathTransferMessage* msg, llarp_router* r)
{
2018-10-06 16:37:54 +00:00
auto path = r->paths.GetPathForTransfer(msg->P);
2018-08-12 17:22:29 +00:00
if(!path)
{
2018-09-11 15:34:12 +00:00
llarp::routing::DataDiscardMessage discarded(msg->P, msg->S);
2018-10-06 16:37:54 +00:00
path = r->paths.GetPathForTransfer(msg->from);
2018-09-11 15:34:12 +00:00
return path && path->SendRoutingMessage(&discarded, r);
2018-08-12 17:22:29 +00:00
}
byte_t tmp[service::MAX_PROTOCOL_MESSAGE_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!msg->T.BEncode(&buf))
{
llarp::LogWarn("failed to transfer data message, encode failed");
return false;
}
// rewind0
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
// send
return path->HandleDownstream(buf, msg->Y, r);
2018-06-26 16:23:43 +00:00
}
} // namespace path
2018-06-22 05:44:19 +00:00
} // namespace llarp