mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-29 11:05:43 +00:00
be234e4b6e
* snode to snode direct traffic * wire up dns on service node tun
113 lines
2.7 KiB
C++
113 lines
2.7 KiB
C++
#include <buffer.hpp>
|
|
#include <logger.hpp>
|
|
#include <messages.hpp>
|
|
#include <router_contact.hpp>
|
|
|
|
namespace llarp
|
|
{
|
|
InboundMessageParser::InboundMessageParser(Router* _router) : router(_router)
|
|
{
|
|
}
|
|
|
|
bool
|
|
InboundMessageParser::OnKey(dict_reader* r, llarp_buffer_t* key)
|
|
{
|
|
InboundMessageParser* handler =
|
|
static_cast< InboundMessageParser* >(r->user);
|
|
|
|
// we are reading the first key
|
|
if(handler->firstkey)
|
|
{
|
|
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;
|
|
}
|
|
// create the message to parse based off message type
|
|
llarp::LogDebug("inbound message ", *strbuf.cur);
|
|
switch(*strbuf.cur)
|
|
{
|
|
case 'i':
|
|
handler->msg = std::make_unique< LinkIntroMessage >(handler->from);
|
|
break;
|
|
case 'd':
|
|
handler->msg =
|
|
std::make_unique< RelayDownstreamMessage >(handler->from);
|
|
break;
|
|
case 'u':
|
|
handler->msg =
|
|
std::make_unique< RelayUpstreamMessage >(handler->from);
|
|
break;
|
|
case 'm':
|
|
handler->msg = std::make_unique< DHTImmeidateMessage >(handler->from);
|
|
break;
|
|
case 'c':
|
|
handler->msg = std::make_unique< LR_CommitMessage >(handler->from);
|
|
break;
|
|
case 'x':
|
|
handler->msg = std::make_unique< DiscardMessage >(handler->from);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
handler->firstkey = false;
|
|
return true;
|
|
}
|
|
// check for last element
|
|
if(!key)
|
|
return handler->MessageDone();
|
|
|
|
return handler->msg->DecodeKey(*key, r->buffer);
|
|
}
|
|
|
|
bool
|
|
InboundMessageParser::MessageDone()
|
|
{
|
|
bool result = false;
|
|
if(msg)
|
|
{
|
|
result = msg->HandleMessage(router);
|
|
}
|
|
Reset();
|
|
return result;
|
|
}
|
|
|
|
bool
|
|
InboundMessageParser::ProcessFrom(ILinkSession* src, llarp_buffer_t buf)
|
|
{
|
|
if(!src)
|
|
{
|
|
llarp::LogWarn("no link session");
|
|
return false;
|
|
}
|
|
reader.user = this;
|
|
reader.on_key = &OnKey;
|
|
from = src;
|
|
firstkey = true;
|
|
return bencode_read_dict(&buf, &reader);
|
|
}
|
|
|
|
void
|
|
InboundMessageParser::Reset()
|
|
{
|
|
msg.reset(nullptr);
|
|
}
|
|
} // namespace llarp
|