lokinet/llarp/link_message.cpp

115 lines
2.5 KiB
C++
Raw Normal View History

2018-12-12 01:55:30 +00:00
#include <buffer.hpp>
#include <logger.hpp>
#include <messages.hpp>
2018-12-12 01:55:30 +00:00
#include <router_contact.hpp>
2018-12-20 16:49:05 +00:00
#include <link_message_parser.hpp>
namespace llarp
{
InboundMessageParser::InboundMessageParser(Router* _router) : router(_router)
{
}
bool
2018-06-01 14:08:54 +00:00
InboundMessageParser::OnKey(dict_reader* r, llarp_buffer_t* key)
{
2018-06-01 14:08:54 +00:00
InboundMessageParser* handler =
static_cast< InboundMessageParser* >(r->user);
// we are reading the first key
if(handler->firstkey)
{
2018-09-06 11:46:19 +00:00
llarp_buffer_t strbuf;
// check for empty dict
if(!key)
return false;
// we are expecting the first key to be 'a'
if(!llarp_buffer_eq(*key, "a"))
{
llarp::LogWarn("message has no message type");
return false;
}
if(!bencode_read_string(r->buffer, &strbuf))
{
llarp::LogWarn("could not read value of message type");
return false;
}
// bad key size
if(strbuf.sz != 1)
{
llarp::LogWarn("bad mesage type size: ", strbuf.sz);
return false;
}
2018-06-08 13:12:17 +00:00
// create the message to parse based off message type
llarp::LogDebug("inbound message ", *strbuf.cur);
2018-06-01 14:08:54 +00:00
switch(*strbuf.cur)
{
case 'i':
2018-12-20 16:49:05 +00:00
handler->msg = &handler->holder.i;
2018-06-01 14:08:54 +00:00
break;
2018-06-08 13:12:17 +00:00
case 'd':
2018-12-20 16:49:05 +00:00
handler->msg = &handler->holder.d;
2018-06-08 13:12:17 +00:00
break;
case 'u':
2018-12-20 16:49:05 +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':
2018-12-20 16:49:05 +00:00
handler->msg = &handler->holder.m;
2018-06-01 14:08:54 +00:00
break;
case 'c':
2018-12-20 16:49:05 +00:00
handler->msg = &handler->holder.c;
break;
2018-09-06 20:31:58 +00:00
case 'x':
2018-12-20 16:49:05 +00:00
handler->msg = &handler->holder.x;
2018-09-06 20:31:58 +00:00
break;
default:
return false;
2018-06-01 14:08:54 +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;
}
// check for last element
if(!key)
return handler->MessageDone();
2018-06-01 14:08:54 +00:00
return handler->msg->DecodeKey(*key, r->buffer);
}
bool
2018-06-01 14:08:54 +00:00
InboundMessageParser::MessageDone()
{
2018-06-01 14:08:54 +00:00
bool result = false;
if(msg)
{
2018-06-01 14:08:54 +00:00
result = msg->HandleMessage(router);
}
2018-10-21 14:57:36 +00:00
Reset();
2018-06-01 14:08:54 +00:00
return result;
}
bool
InboundMessageParser::ProcessFrom(ILinkSession* src, llarp_buffer_t buf)
{
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;
return bencode_read_dict(&buf, &reader);
}
2018-09-06 11:46:19 +00:00
void
InboundMessageParser::Reset()
{
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