2019-01-14 21:46:07 +00:00
|
|
|
#include <messages/link_message_parser.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <messages/dht_immediate.hpp>
|
|
|
|
#include <messages/discard.hpp>
|
|
|
|
#include <messages/link_intro.hpp>
|
|
|
|
#include <messages/link_message.hpp>
|
|
|
|
#include <messages/relay_commit.hpp>
|
|
|
|
#include <messages/relay.hpp>
|
2018-12-12 01:55:30 +00:00
|
|
|
#include <router_contact.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/logger.hpp>
|
2019-03-26 19:30:10 +00:00
|
|
|
#include <util/metrics.hpp>
|
2018-05-25 17:52:10 +00:00
|
|
|
|
2019-04-19 18:24:33 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
struct InboundMessageParser::msg_holder_t
|
|
|
|
{
|
|
|
|
LinkIntroMessage i;
|
|
|
|
RelayDownstreamMessage d;
|
|
|
|
RelayUpstreamMessage u;
|
|
|
|
DHTImmediateMessage m;
|
|
|
|
LR_CommitMessage c;
|
|
|
|
DiscardMessage x;
|
2019-04-19 18:24:33 +00:00
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
msg_holder_t() = default;
|
2019-02-11 19:45:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
InboundMessageParser::InboundMessageParser(AbstractRouter* _router)
|
2019-04-22 12:25:25 +00:00
|
|
|
: router(_router), from(nullptr), msg(nullptr), holder(new msg_holder_t())
|
2019-02-11 19:45:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InboundMessageParser::~InboundMessageParser()
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2019-04-19 18:24:33 +00:00
|
|
|
delete holder;
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-06-01 14:08:54 +00:00
|
|
|
InboundMessageParser::OnKey(dict_reader* r, llarp_buffer_t* key)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2018-06-01 14:08:54 +00:00
|
|
|
InboundMessageParser* handler =
|
|
|
|
static_cast< InboundMessageParser* >(r->user);
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
// we are reading the first key
|
|
|
|
if(handler->firstkey)
|
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
llarp_buffer_t strbuf;
|
2018-05-25 17:52:10 +00:00
|
|
|
// check for empty dict
|
|
|
|
if(!key)
|
|
|
|
return false;
|
|
|
|
// we are expecting the first key to be 'a'
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!(*key == "a"))
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("message has no message type");
|
2018-05-25 17:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("could not read value of message type");
|
2018-05-25 17:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// bad key size
|
|
|
|
if(strbuf.sz != 1)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("bad mesage type size: ", strbuf.sz);
|
2018-05-25 17:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-08 13:12:17 +00:00
|
|
|
// create the message to parse based off message type
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("inbound message ", *strbuf.cur);
|
2019-03-26 20:04:41 +00:00
|
|
|
bool isLIM = false;
|
2018-06-01 14:08:54 +00:00
|
|
|
switch(*strbuf.cur)
|
|
|
|
{
|
|
|
|
case 'i':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->i;
|
2019-03-26 20:04:41 +00:00
|
|
|
isLIM = true;
|
2018-06-01 14:08:54 +00:00
|
|
|
break;
|
2018-06-08 13:12:17 +00:00
|
|
|
case 'd':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->d;
|
2018-06-08 13:12:17 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->u;
|
2018-06-08 13:12:17 +00:00
|
|
|
break;
|
2018-06-01 14:08:54 +00:00
|
|
|
case 'm':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->m;
|
2018-06-01 14:08:54 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->c;
|
2018-06-01 17:47:37 +00:00
|
|
|
break;
|
2018-09-06 20:31:58 +00:00
|
|
|
case 'x':
|
2019-02-11 19:45:42 +00:00
|
|
|
handler->msg = &handler->holder->x;
|
2018-09-06 20:31:58 +00:00
|
|
|
break;
|
2018-06-01 17:47:37 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
2019-03-26 20:04:41 +00:00
|
|
|
|
|
|
|
if(!isLIM)
|
2019-03-26 19:30:10 +00:00
|
|
|
{
|
|
|
|
const std::string host =
|
2019-03-26 20:04:41 +00:00
|
|
|
"RX_" + RouterID(handler->from->GetPubKey()).ToString();
|
|
|
|
METRICS_DYNAMIC_INCREMENT(handler->msg->Name(), host.c_str());
|
2019-03-26 19:30:10 +00:00
|
|
|
}
|
2019-03-26 20:04:41 +00:00
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
handler->msg->session = handler->from;
|
|
|
|
handler->firstkey = false;
|
2018-09-06 11:46:19 +00:00
|
|
|
return true;
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
2018-05-26 18:31:45 +00:00
|
|
|
// check for last element
|
2018-05-25 17:52:10 +00:00
|
|
|
if(!key)
|
2018-05-26 18:31:45 +00:00
|
|
|
return handler->MessageDone();
|
2018-05-25 17:52:10 +00:00
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
return handler->msg->DecodeKey(*key, r->buffer);
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
bool
|
2018-06-01 14:08:54 +00:00
|
|
|
InboundMessageParser::MessageDone()
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2018-06-01 14:08:54 +00:00
|
|
|
bool result = false;
|
|
|
|
if(msg)
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2018-06-01 14:08:54 +00:00
|
|
|
result = msg->HandleMessage(router);
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
2018-10-21 14:57:36 +00:00
|
|
|
Reset();
|
2018-06-01 14:08:54 +00:00
|
|
|
return result;
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
InboundMessageParser::ProcessFrom(ILinkSession* src,
|
|
|
|
const llarp_buffer_t& buf)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("no link session");
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
reader.user = this;
|
|
|
|
reader.on_key = &OnKey;
|
|
|
|
from = src;
|
|
|
|
firstkey = true;
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer copy(buf);
|
2019-02-02 23:12:42 +00:00
|
|
|
return bencode_read_dict(©.underlying, &reader);
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
InboundMessageParser::Reset()
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
if(msg)
|
|
|
|
msg->Clear();
|
2018-12-20 16:49:05 +00:00
|
|
|
msg = nullptr;
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-06-20 12:34:48 +00:00
|
|
|
} // namespace llarp
|