lokinet/llarp/routing/message_parser.cpp

142 lines
3.6 KiB
C++
Raw Normal View History

#include <routing/message_parser.hpp>
2019-06-15 14:55:14 +00:00
#include <exit/exit_messages.hpp>
#include <messages/discard.hpp>
#include <path/path_types.hpp>
#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>
#include <util/mem.hpp>
namespace llarp
{
namespace routing
{
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;
};
InboundMessageParser::InboundMessageParser()
2019-07-30 23:42:13 +00:00
: m_Holder(std::make_unique< MessageHolder >())
{
}
2019-07-30 23:42:13 +00:00
InboundMessageParser::~InboundMessageParser() = default;
bool
InboundMessageParser::operator()(llarp_buffer_t* buffer,
llarp_buffer_t* key)
{
if(key == nullptr && firstKey)
{
// empty dict
return false;
}
if(!key)
return true;
if(firstKey)
{
llarp_buffer_t strbuf;
if(!(*key == "A"))
return false;
if(!bencode_read_string(buffer, &strbuf))
return false;
if(strbuf.sz != 1)
return false;
ourKey = *strbuf.cur;
LogDebug("routing message '", key, "'");
switch(ourKey)
{
case 'D':
msg = &m_Holder->D;
break;
2018-06-26 16:23:43 +00:00
case 'L':
msg = &m_Holder->L;
2018-06-26 16:23:43 +00:00
break;
case 'M':
msg = &m_Holder->M;
break;
case 'P':
msg = &m_Holder->P;
break;
case 'T':
msg = &m_Holder->T;
break;
2018-08-12 17:22:29 +00:00
case 'H':
msg = &m_Holder->H;
2018-08-12 17:22:29 +00:00
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);
}
firstKey = false;
return msg != nullptr;
}
2019-07-06 17:03:40 +00:00
return msg->DecodeKey(*key, buffer);
}
bool
InboundMessageParser::ParseMessageBuffer(const llarp_buffer_t& buf,
2018-06-26 16:23:43 +00:00
IMessageHandler* h,
const PathID_t& from,
AbstractRouter* r)
{
bool result = false;
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;
if(bencode_read_dict(*this, &copy))
{
msg->from = from;
result = msg->HandleMessage(h, r);
if(!result)
2019-04-08 18:21:01 +00:00
{
llarp::LogWarn("Failed to handle inbound routing message ", ourKey);
2019-04-08 18:21:01 +00:00
}
}
else
{
llarp::LogError("read dict failed in routing layer");
llarp::DumpBuffer< llarp_buffer_t, 128 >(buf);
}
2018-12-27 15:02:19 +00:00
if(msg)
msg->Clear();
msg = nullptr;
return result;
}
} // namespace routing
} // namespace llarp