2019-06-04 18:31:17 +00:00
|
|
|
#include <messages/relay_status.hpp>
|
|
|
|
|
|
|
|
#include <crypto/crypto.hpp>
|
|
|
|
#include <path/path_context.hpp>
|
|
|
|
#include <path/ihophandler.hpp>
|
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
#include <routing/path_confirm_message.hpp>
|
|
|
|
#include <util/bencode.hpp>
|
|
|
|
#include <util/buffer.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-09-01 12:38:03 +00:00
|
|
|
#include <util/meta/memfn.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2020-03-03 19:56:33 +00:00
|
|
|
#include <tooling/path_event.hpp>
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
#include <functional>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
struct LRSM_AsyncHandler : public std::enable_shared_from_this<LRSM_AsyncHandler>
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
using HopHandler_ptr = std::shared_ptr<llarp::path::IHopHandler>;
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<EncryptedFrame, 8> frames;
|
2019-06-04 18:31:17 +00:00
|
|
|
uint64_t status = 0;
|
|
|
|
HopHandler_ptr path;
|
|
|
|
AbstractRouter* router;
|
2020-03-03 05:26:25 +00:00
|
|
|
PathID_t pathid;
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
LRSM_AsyncHandler(
|
|
|
|
std::array<EncryptedFrame, 8> _frames,
|
|
|
|
uint64_t _status,
|
|
|
|
HopHandler_ptr _path,
|
|
|
|
AbstractRouter* _router,
|
|
|
|
const PathID_t& pathid)
|
2019-07-30 23:42:13 +00:00
|
|
|
: frames(std::move(_frames))
|
|
|
|
, status(_status)
|
|
|
|
, path(std::move(_path))
|
|
|
|
, router(_router)
|
2020-03-03 05:26:25 +00:00
|
|
|
, pathid(pathid)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~LRSM_AsyncHandler() = default;
|
|
|
|
|
|
|
|
void
|
|
|
|
handle()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
router->NotifyRouterEvent<tooling::PathStatusReceivedEvent>(router->pubkey(), pathid, status);
|
2020-03-03 05:26:25 +00:00
|
|
|
|
2019-06-04 18:31:17 +00:00
|
|
|
path->HandleLRSM(status, frames, router);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
queue_handle()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto func = std::bind(&llarp::LRSM_AsyncHandler::handle, shared_from_this());
|
2019-06-04 18:31:17 +00:00
|
|
|
router->threadpool()->addJob(func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "c")
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return BEncodeReadArray(frames, buf);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "p")
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (key == "s")
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("s", status, read, key, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (key == "v")
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("v", version, LLARP_PROTO_VERSION, read, key, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LR_StatusMessage::Clear()
|
|
|
|
{
|
|
|
|
std::for_each(frames.begin(), frames.end(), [](auto& f) { f.Clear(); });
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
status = 0;
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
// msg type
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "a", "s"))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
// frames
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictArray("c", frames, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
// path id
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("p", pathid, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
// status (for now, only success bit is relevant)
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("s", status, buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
// version
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64_entry(buf, "v", 1, LLARP_PROTO_VERSION))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusMessage::HandleMessage(AbstractRouter* router) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogDebug("Received LR_Status message from (", session->GetPubKey(), ")");
|
|
|
|
if (frames.size() != path::max_len)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("LRSM invalid number of records, ", frames.size(), "!=", path::max_len);
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto path = router->pathContext().GetByUpstream(session->GetPubKey(), pathid);
|
|
|
|
if (!path)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn("unhandled LR_Status message: no associated path found pathid=", pathid);
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto handler = std::make_shared<LRSM_AsyncHandler>(frames, status, path, router, pathid);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
handler->queue_handle();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LR_StatusMessage::SetDummyFrames()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call this from a worker thread
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
LR_StatusMessage::CreateAndSend(
|
|
|
|
AbstractRouter* router,
|
|
|
|
const PathID_t pathid,
|
|
|
|
const RouterID nextHop,
|
|
|
|
const SharedSecret pathKey,
|
|
|
|
uint64_t status)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto message = std::make_shared<LR_StatusMessage>();
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2020-01-21 17:31:48 +00:00
|
|
|
message->status = status;
|
2019-06-04 18:31:17 +00:00
|
|
|
message->pathid = pathid;
|
|
|
|
|
|
|
|
message->SetDummyFrames();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!message->AddFrame(pathKey, status))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueueSendMessage(router, nextHop, message);
|
|
|
|
return true; // can't guarantee delivery here, as far as we know it's fine
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-01 07:44:26 +00:00
|
|
|
LR_StatusMessage::AddFrame(const SharedSecret& pathKey, uint64_t newStatus)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
frames[7] = frames[6];
|
|
|
|
frames[6] = frames[5];
|
|
|
|
frames[5] = frames[4];
|
|
|
|
frames[4] = frames[3];
|
|
|
|
frames[3] = frames[2];
|
|
|
|
frames[2] = frames[1];
|
|
|
|
frames[1] = frames[0];
|
|
|
|
|
|
|
|
auto& frame = frames[0];
|
|
|
|
|
|
|
|
frame.Randomize();
|
|
|
|
|
|
|
|
LR_StatusRecord record;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
record.status = newStatus;
|
2019-06-04 18:31:17 +00:00
|
|
|
record.version = LLARP_PROTO_VERSION;
|
|
|
|
|
|
|
|
llarp_buffer_t buf(frame.data(), frame.size());
|
|
|
|
buf.cur = buf.base + EncryptedFrameOverheadSize;
|
|
|
|
// encode record
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!record.BEncode(&buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
// failed to encode?
|
|
|
|
LogError(Name(), " Failed to generate Status Record");
|
|
|
|
DumpBuffer(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// use ephemeral keypair for frame
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!frame.DoEncrypt(pathKey, true))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
LogError(Name(), " Failed to encrypt LRSR");
|
|
|
|
DumpBuffer(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LR_StatusMessage::QueueSendMessage(
|
|
|
|
AbstractRouter* router, const RouterID nextHop, std::shared_ptr<LR_StatusMessage> msg)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
auto func = std::bind(&LR_StatusMessage::SendMessage, router, nextHop, msg);
|
2019-11-14 21:56:01 +00:00
|
|
|
LogicCall(router->logic(), func);
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LR_StatusMessage::SendMessage(
|
|
|
|
AbstractRouter* router, const RouterID nextHop, std::shared_ptr<LR_StatusMessage> msg)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("Attempting to send LR_Status message to (", nextHop, ")");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not router->SendToOrQueue(nextHop, msg.get()))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("Sending LR_Status message, SendToOrQueue to ", nextHop, " failed");
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusRecord::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
return bencode_start_dict(buf) && BEncodeWriteDictInt("s", status, buf)
|
2020-04-07 18:38:56 +00:00
|
|
|
&& bencode_write_uint64_entry(buf, "v", 1, LLARP_PROTO_VERSION) && bencode_end(buf);
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusRecord::OnKey(llarp_buffer_t* buffer, llarp_buffer_t* key)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!key)
|
2019-06-04 18:31:17 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool read = false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("s", status, read, *key, buffer))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("v", version, LLARP_PROTO_VERSION, read, *key, buffer))
|
2019-06-04 18:31:17 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusRecord::BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_read_dict(util::memFn(&LR_StatusRecord::OnKey, this), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_StatusRecord::operator==(const LR_StatusRecord& other) const
|
|
|
|
{
|
|
|
|
return status == other.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|