2021-03-09 22:24:35 +00:00
|
|
|
#include "message_parser.hpp"
|
2018-06-22 13:59:28 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/exit/exit_messages.hpp>
|
|
|
|
#include <llarp/messages/discard.hpp>
|
|
|
|
#include <llarp/path/path_types.hpp>
|
2023-08-31 16:28:02 +00:00
|
|
|
#include "path_dht_message.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "path_confirm_message.hpp"
|
|
|
|
#include "path_latency_message.hpp"
|
|
|
|
#include "path_transfer_message.hpp"
|
|
|
|
#include "transfer_traffic_message.hpp"
|
|
|
|
#include <llarp/util/mem.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
namespace llarp::routing
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
struct InboundMessageParser::MessageHolder
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
DataDiscardMessage D;
|
|
|
|
PathLatencyMessage L;
|
2023-08-31 16:28:02 +00:00
|
|
|
PathDHTMessage M;
|
2023-08-29 14:26:59 +00:00
|
|
|
PathConfirmMessage P;
|
|
|
|
PathTransferMessage T;
|
2023-08-31 16:28:02 +00:00
|
|
|
service::ProtocolFrameMessage H;
|
2023-08-29 14:26:59 +00:00
|
|
|
TransferTrafficMessage I;
|
|
|
|
GrantExitMessage G;
|
|
|
|
RejectExitMessage J;
|
|
|
|
ObtainExitMessage O;
|
|
|
|
UpdateExitMessage U;
|
|
|
|
CloseExitMessage C;
|
|
|
|
};
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
InboundMessageParser::InboundMessageParser() : m_Holder(std::make_unique<MessageHolder>())
|
|
|
|
{}
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
InboundMessageParser::~InboundMessageParser() = default;
|
2018-06-22 13:59:28 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
bool
|
|
|
|
InboundMessageParser::operator()(llarp_buffer_t* buffer, llarp_buffer_t* key)
|
|
|
|
{
|
|
|
|
if (key == nullptr && firstKey)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
// empty dict
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!key)
|
|
|
|
return true;
|
|
|
|
if (firstKey)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if (!(key->startswith("A")))
|
2018-06-22 13:59:28 +00:00
|
|
|
return false;
|
2023-08-29 14:26:59 +00:00
|
|
|
if (!bencode_read_string(buffer, &strbuf))
|
|
|
|
return false;
|
|
|
|
if (strbuf.sz != 1)
|
|
|
|
return false;
|
|
|
|
ourKey = *strbuf.cur;
|
|
|
|
LogDebug("routing message '", std::string{ourKey, 1}, "'");
|
|
|
|
switch (ourKey)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
case 'D':
|
|
|
|
msg = &m_Holder->D;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
msg = &m_Holder->L;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
msg = &m_Holder->M;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
msg = &m_Holder->P;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
msg = &m_Holder->T;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
msg = &m_Holder->H;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
msg = &m_Holder->I;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
msg = &m_Holder->G;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
msg = &m_Holder->J;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
msg = &m_Holder->O;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
msg = &m_Holder->U;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
msg = &m_Holder->C;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llarp::LogError("invalid routing message id: ", *strbuf.cur);
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2023-08-29 14:26:59 +00:00
|
|
|
if (msg)
|
|
|
|
msg->version = version;
|
|
|
|
firstKey = false;
|
|
|
|
return msg != nullptr;
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
return msg->decode_key(*key, buffer);
|
2023-08-29 14:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageParser::ParseMessageBuffer(
|
2023-09-15 14:55:32 +00:00
|
|
|
const llarp_buffer_t& buf, AbstractRoutingMessageHandler* h, const PathID_t& from, Router* r)
|
2023-08-29 14:26:59 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
msg = nullptr;
|
|
|
|
firstKey = true;
|
|
|
|
ManagedBuffer copiedBuf(buf);
|
|
|
|
auto& copy = copiedBuf.underlying;
|
|
|
|
uint64_t v = 0;
|
|
|
|
if (BEncodeSeekDictVersion(v, ©, 'V'))
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
version = v;
|
|
|
|
}
|
|
|
|
if (bencode_read_dict(*this, ©))
|
|
|
|
{
|
|
|
|
msg->from = from;
|
2023-08-31 16:28:02 +00:00
|
|
|
LogDebug("handle routing message ", msg->sequence_number, " from ", from);
|
|
|
|
result = msg->handle_message(h, r);
|
2023-08-29 14:26:59 +00:00
|
|
|
if (!result)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
llarp::LogWarn("Failed to handle inbound routing message ", ourKey);
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2023-08-29 14:26:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("read dict failed in routing layer");
|
|
|
|
llarp::DumpBuffer<llarp_buffer_t, 128>(buf);
|
|
|
|
}
|
|
|
|
if (msg)
|
2023-08-31 16:28:02 +00:00
|
|
|
msg->clear();
|
2023-08-29 14:26:59 +00:00
|
|
|
msg = nullptr;
|
|
|
|
version = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // namespace llarp::routing
|