2018-12-27 14:32:37 +00:00
|
|
|
#include <routing/message_parser.hpp>
|
2018-06-22 13:59:28 +00:00
|
|
|
|
2019-06-15 14:55:14 +00:00
|
|
|
#include <exit/exit_messages.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <messages/discard.hpp>
|
|
|
|
#include <path/path_types.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
#include <routing/dht_message.hpp>
|
2019-06-19 22:30:07 +00:00
|
|
|
#include <routing/path_confirm_message.hpp>
|
|
|
|
#include <routing/path_latency_message.hpp>
|
|
|
|
#include <routing/path_transfer_message.hpp>
|
|
|
|
#include <routing/transfer_traffic_message.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.hpp>
|
|
|
|
|
2018-06-22 13:59:28 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace routing
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
struct InboundMessageParser::MessageHolder
|
|
|
|
{
|
|
|
|
DataDiscardMessage D;
|
|
|
|
PathLatencyMessage L;
|
|
|
|
DHTMessage M;
|
|
|
|
PathConfirmMessage P;
|
|
|
|
PathTransferMessage T;
|
|
|
|
service::ProtocolFrame H;
|
|
|
|
TransferTrafficMessage I;
|
|
|
|
GrantExitMessage G;
|
|
|
|
RejectExitMessage J;
|
|
|
|
ObtainExitMessage O;
|
|
|
|
UpdateExitMessage U;
|
|
|
|
CloseExitMessage C;
|
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
InboundMessageParser::InboundMessageParser() : m_Holder(std::make_unique<MessageHolder>())
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
InboundMessageParser::~InboundMessageParser() = default;
|
2018-06-22 13:59:28 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
InboundMessageParser::operator()(llarp_buffer_t* buffer, llarp_buffer_t* key)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == nullptr && firstKey)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
|
|
|
// empty dict
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!key)
|
2018-06-22 13:59:28 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (firstKey)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!(*key == "A"))
|
2018-06-22 13:59:28 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buffer, &strbuf))
|
2018-06-22 13:59:28 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz != 1)
|
2018-06-22 13:59:28 +00:00
|
|
|
return false;
|
2019-05-22 00:36:03 +00:00
|
|
|
ourKey = *strbuf.cur;
|
|
|
|
LogDebug("routing message '", key, "'");
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (ourKey)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2018-09-11 15:28:36 +00:00
|
|
|
case 'D':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->D;
|
2018-09-11 15:28:36 +00:00
|
|
|
break;
|
2018-06-26 16:23:43 +00:00
|
|
|
case 'L':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->L;
|
2018-06-26 16:23:43 +00:00
|
|
|
break;
|
2018-06-29 14:25:09 +00:00
|
|
|
case 'M':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->M;
|
2018-06-29 14:25:09 +00:00
|
|
|
break;
|
2018-06-22 13:59:28 +00:00
|
|
|
case 'P':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->P;
|
2018-06-22 13:59:28 +00:00
|
|
|
break;
|
2018-06-29 14:25:09 +00:00
|
|
|
case 'T':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->T;
|
2018-06-29 14:25:09 +00:00
|
|
|
break;
|
2018-08-12 17:22:29 +00:00
|
|
|
case 'H':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->H;
|
2018-08-12 17:22:29 +00:00
|
|
|
break;
|
2018-11-12 16:43:40 +00:00
|
|
|
case 'I':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->I;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
|
|
|
case 'G':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->G;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
|
|
|
case 'J':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->J;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
|
|
|
case 'O':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->O;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
|
|
|
case 'U':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->U;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
2019-05-22 00:36:03 +00:00
|
|
|
msg = &m_Holder->C;
|
2018-11-12 16:43:40 +00:00
|
|
|
break;
|
2018-06-22 13:59:28 +00:00
|
|
|
default:
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("invalid routing message id: ", *strbuf.cur);
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg)
|
2019-11-03 15:31:01 +00:00
|
|
|
msg->version = version;
|
2019-05-22 00:36:03 +00:00
|
|
|
firstKey = false;
|
|
|
|
return msg != nullptr;
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return msg->DecodeKey(*key, buffer);
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
InboundMessageParser::ParseMessageBuffer(
|
|
|
|
const llarp_buffer_t& buf, IMessageHandler* h, const PathID_t& from, AbstractRouter* r)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = nullptr;
|
|
|
|
firstKey = true;
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer copiedBuf(buf);
|
2019-02-02 23:12:42 +00:00
|
|
|
auto& copy = copiedBuf.underlying;
|
2019-11-03 15:31:01 +00:00
|
|
|
uint64_t v = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (BEncodeSeekDictVersion(v, ©, 'V'))
|
2019-11-03 15:31:01 +00:00
|
|
|
{
|
|
|
|
version = v;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (bencode_read_dict(*this, ©))
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
msg->from = from;
|
2020-04-07 18:38:56 +00:00
|
|
|
result = msg->HandleMessage(h, r);
|
|
|
|
if (!result)
|
2019-04-08 18:21:01 +00:00
|
|
|
{
|
2019-05-22 00:36:03 +00:00
|
|
|
llarp::LogWarn("Failed to handle inbound routing message ", ourKey);
|
2019-04-08 18:21:01 +00:00
|
|
|
}
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2018-06-26 14:52:19 +00:00
|
|
|
else
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("read dict failed in routing layer");
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::DumpBuffer<llarp_buffer_t, 128>(buf);
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg)
|
2018-12-27 15:02:19 +00:00
|
|
|
msg->Clear();
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = nullptr;
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2018-06-22 13:59:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // namespace routing
|
2018-09-11 15:28:36 +00:00
|
|
|
} // namespace llarp
|