2018-05-25 17:52:10 +00:00
|
|
|
#include <llarp/router_contact.h>
|
|
|
|
#include <llarp/link_message.hpp>
|
|
|
|
#include "buffer.hpp"
|
2018-05-27 13:42:55 +00:00
|
|
|
#include "logger.hpp"
|
2018-05-26 18:31:45 +00:00
|
|
|
#include "router.hpp"
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
InboundMessageHandler::InboundMessageHandler(llarp_router* _router)
|
|
|
|
: router(_router)
|
|
|
|
{
|
|
|
|
reader.user = this;
|
|
|
|
reader.on_key = &OnKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageHandler::OnKey(dict_reader* r, llarp_buffer_t* key)
|
|
|
|
{
|
|
|
|
InboundMessageHandler* handler =
|
|
|
|
static_cast< InboundMessageHandler* >(r->user);
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
|
|
|
|
// we are reading the first key
|
|
|
|
if(handler->firstkey)
|
|
|
|
{
|
|
|
|
// check for empty dict
|
|
|
|
if(!key)
|
|
|
|
return false;
|
|
|
|
// we are expecting the first key to be 'a'
|
|
|
|
if(!llarp_buffer_eq(*key, "a"))
|
|
|
|
{
|
2018-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "message has no message type");
|
2018-05-25 17:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!bdecode_read_string(r->buffer, &strbuf))
|
|
|
|
{
|
2018-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "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-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "bad mesage type size: ", strbuf.sz);
|
2018-05-25 17:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
handler->msgtype = *strbuf.cur;
|
|
|
|
handler->firstkey = false;
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
switch(handler->msgtype)
|
|
|
|
{
|
2018-05-26 18:31:45 +00:00
|
|
|
// link introduce
|
2018-05-25 17:52:10 +00:00
|
|
|
case 'i':
|
2018-05-26 18:31:45 +00:00
|
|
|
return handler->DecodeLIM(*key, r->buffer);
|
|
|
|
// immidate dht
|
|
|
|
case 'd':
|
|
|
|
return handler->DecodeDHT(*key, r->buffer);
|
|
|
|
// relay commit
|
|
|
|
case 'c':
|
|
|
|
return handler->DecodeLRCM(*key, r->buffer);
|
|
|
|
// unknown message type
|
2018-05-25 17:52:10 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
bool
|
|
|
|
InboundMessageHandler::DecodeLIM(llarp_buffer_t key, llarp_buffer_t* buff)
|
|
|
|
{
|
|
|
|
if(llarp_buffer_eq(key, "r"))
|
|
|
|
{
|
|
|
|
if(!llarp_rc_bdecode(from->get_remote_router(from), buff))
|
|
|
|
{
|
2018-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "failed to decode RC");
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(llarp_buffer_eq(key, "v"))
|
|
|
|
{
|
|
|
|
if(!bdecode_read_integer(buff, &proto))
|
|
|
|
return false;
|
|
|
|
if(proto != LLARP_PROTO_VERSION)
|
|
|
|
{
|
2018-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "llarp protocol version missmatch ", proto);
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-27 13:42:55 +00:00
|
|
|
llarp::Warn(__FILE__, "invalid LIM key: ", *key.cur);
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageHandler::DecodeDHT(llarp_buffer_t key, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageHandler::DecodeLRCM(llarp_buffer_t key, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageHandler::MessageDone()
|
|
|
|
{
|
|
|
|
switch(msgtype)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
return router->ProcessLRCM(lrcm);
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
bool
|
|
|
|
InboundMessageHandler::ProcessFrom(llarp_link_session* src,
|
|
|
|
llarp_buffer_t buf)
|
|
|
|
{
|
|
|
|
from = src;
|
|
|
|
msgtype = 0;
|
|
|
|
firstkey = true;
|
|
|
|
return bdecode_read_dict(&buf, &reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageHandler::FlushReplies()
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
while(sendq.size())
|
|
|
|
{
|
|
|
|
auto& msg = sendq.front();
|
|
|
|
auto buf = llarp::Buffer< decltype(msg) >(msg);
|
|
|
|
success &= from->sendto(from, buf);
|
|
|
|
sendq.pop();
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|