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>
|
2018-06-22 00:25:30 +00:00
|
|
|
#include "buffer.hpp"
|
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace path
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-06-26 16:23:43 +00:00
|
|
|
TransitHop::TransitHop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
TransitHop::Expired(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
return now - started > lifetime;
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
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
|
|
|
|
2018-06-25 15:12:08 +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
|
|
|
|
2018-06-25 15:12:08 +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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2018-07-24 22:34:46 +00:00
|
|
|
TransitHop::SendRoutingMessage(llarp::routing::IMessage* msg,
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_router* r)
|
|
|
|
{
|
2018-07-24 22:34:46 +00:00
|
|
|
msg->S = m_SequenceNum++;
|
2018-08-14 21:17:18 +00:00
|
|
|
byte_t tmp[MAX_LINK_MSG_SIZE - 1024];
|
2018-06-25 15:12:08 +00:00
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!msg->BEncode(&buf))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("failed to encode routing message");
|
2018-06-25 15:12:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TunnelNonce N;
|
|
|
|
N.Randomize();
|
2018-07-23 22:36:46 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
// pad smaller messages
|
|
|
|
if(buf.sz < MESSAGE_PAD_SIZE)
|
|
|
|
{
|
|
|
|
// randomize padding
|
|
|
|
r->crypto.randbytes(buf.cur, MESSAGE_PAD_SIZE - buf.sz);
|
|
|
|
buf.sz = MESSAGE_PAD_SIZE;
|
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
return HandleDownstream(buf, N, r);
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
TransitHop::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
|
|
|
|
llarp_router* r)
|
|
|
|
{
|
|
|
|
RelayDownstreamMessage* msg = new RelayDownstreamMessage;
|
2018-06-26 14:52:19 +00:00
|
|
|
msg->pathid = info.rxID;
|
2018-07-28 22:20:32 +00:00
|
|
|
msg->Y = Y ^ nonceXOR;
|
2018-06-25 15:12:08 +00:00
|
|
|
r->crypto.xchacha20(buf, pathKey, Y);
|
|
|
|
msg->X = buf;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("relay ", msg->X.size(), " bytes downstream from ",
|
|
|
|
info.upstream, " to ", info.downstream);
|
2018-06-25 15:12:08 +00:00
|
|
|
return r->SendToOrQueue(info.downstream, msg);
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
TransitHop::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
|
|
|
|
llarp_router* r)
|
|
|
|
{
|
|
|
|
r->crypto.xchacha20(buf, pathKey, Y);
|
2018-06-26 16:23:43 +00:00
|
|
|
if(info.upstream == RouterID(r->pubkey()))
|
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
return m_MessageParser.ParseMessageBuffer(buf, this, info.rxID, r);
|
2018-06-26 16:23:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RelayUpstreamMessage* msg = new RelayUpstreamMessage;
|
|
|
|
msg->pathid = info.txID;
|
2018-07-28 22:20:32 +00:00
|
|
|
msg->Y = Y ^ nonceXOR;
|
2018-06-26 16:23:43 +00:00
|
|
|
|
|
|
|
msg->X = buf;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("relay ", msg->X.size(), " bytes upstream from ",
|
|
|
|
info.downstream, " to ", info.upstream);
|
2018-06-26 16:23:43 +00:00
|
|
|
return r->SendToOrQueue(info.upstream, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TransitHop::HandleDHTMessage(const llarp::dht::IMessage* msg,
|
|
|
|
llarp_router* r)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
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-25 15:12:08 +00:00
|
|
|
}
|
2018-06-26 16:23:43 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
TransitHop::HandlePathConfirmMessage(
|
|
|
|
const llarp::routing::PathConfirmMessage* msg, llarp_router* r)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("unwarrented path confirm message on ", info);
|
2018-06-26 16:23:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TransitHop::HandlePathTransferMessage(
|
|
|
|
const llarp::routing::PathTransferMessage* msg, llarp_router* r)
|
|
|
|
{
|
2018-08-12 17:22:29 +00:00
|
|
|
auto path = r->paths.GetByUpstream(r->pubkey(), msg->P);
|
|
|
|
if(!path)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("No such path for path transfer pathid=", msg->P);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
llarp::LogInfo("Transfer ", buf.sz, " bytes", " to ", msg->P);
|
|
|
|
return path->HandleDownstream(buf, msg->Y, r);
|
2018-06-26 16:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
} // namespace path
|
2018-06-22 05:44:19 +00:00
|
|
|
} // namespace llarp
|