2018-06-29 14:25:09 +00:00
|
|
|
#include <llarp/messages/dht.hpp>
|
2018-06-22 13:59:28 +00:00
|
|
|
#include <llarp/messages/path_confirm.hpp>
|
2018-06-26 16:23:43 +00:00
|
|
|
#include <llarp/messages/path_latency.hpp>
|
2018-06-29 14:25:09 +00:00
|
|
|
#include <llarp/messages/path_transfer.hpp>
|
2018-09-11 15:28:36 +00:00
|
|
|
#include <llarp/messages/discard.hpp>
|
2018-06-22 13:59:28 +00:00
|
|
|
#include <llarp/routing/message.hpp>
|
2018-07-18 20:58:16 +00:00
|
|
|
#include "mem.hpp"
|
2018-06-22 13:59:28 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace routing
|
|
|
|
{
|
|
|
|
InboundMessageParser::InboundMessageParser()
|
|
|
|
{
|
|
|
|
reader.user = this;
|
|
|
|
reader.on_key = &OnKey;
|
|
|
|
firstKey = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageParser::OnKey(dict_reader* r, llarp_buffer_t* key)
|
|
|
|
{
|
|
|
|
InboundMessageParser* self =
|
|
|
|
static_cast< InboundMessageParser* >(r->user);
|
|
|
|
|
|
|
|
if(key == nullptr && self->firstKey)
|
|
|
|
{
|
|
|
|
// empty dict
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!key)
|
|
|
|
return true;
|
|
|
|
if(self->firstKey)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!llarp_buffer_eq(*key, "A"))
|
|
|
|
return false;
|
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
|
|
|
return false;
|
|
|
|
if(strbuf.sz != 1)
|
|
|
|
return false;
|
2018-07-17 06:17:13 +00:00
|
|
|
self->key = *strbuf.cur;
|
|
|
|
switch(self->key)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2018-09-11 15:28:36 +00:00
|
|
|
case 'D':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new DataDiscardMessage());
|
2018-09-11 15:28:36 +00:00
|
|
|
break;
|
2018-06-26 16:23:43 +00:00
|
|
|
case 'L':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new PathLatencyMessage());
|
2018-06-26 16:23:43 +00:00
|
|
|
break;
|
2018-06-29 14:25:09 +00:00
|
|
|
case 'M':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new DHTMessage());
|
2018-06-29 14:25:09 +00:00
|
|
|
break;
|
2018-06-22 13:59:28 +00:00
|
|
|
case 'P':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new PathConfirmMessage());
|
2018-06-22 13:59:28 +00:00
|
|
|
break;
|
2018-06-29 14:25:09 +00:00
|
|
|
case 'T':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new PathTransferMessage());
|
2018-06-29 14:25:09 +00:00
|
|
|
break;
|
2018-08-12 17:22:29 +00:00
|
|
|
case 'H':
|
2018-10-19 11:33:49 +00:00
|
|
|
self->msg.reset(new service::ProtocolFrame());
|
2018-08-12 17:22:29 +00:00
|
|
|
break;
|
2018-11-12 16:43:40 +00:00
|
|
|
case 'I':
|
|
|
|
self->msg.reset(new TransferTrafficMessage());
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
self->msg.reset(new GrantExitMessage());
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
self->msg.reset(new RejectExitMessage());
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
self->msg.reset(new ObtainExitMessage());
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
self->msg.reset(new UpdateExitMessage());
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
self->msg.reset(new CloseExitMessage());
|
|
|
|
break;
|
2018-06-22 13:59:28 +00:00
|
|
|
default:
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("invalid routing message id: ", *strbuf.cur);
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
|
|
|
self->firstKey = false;
|
|
|
|
return self->msg != nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return self->msg->DecodeKey(*key, r->buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
InboundMessageParser::ParseMessageBuffer(llarp_buffer_t buf,
|
2018-06-26 16:23:43 +00:00
|
|
|
IMessageHandler* h,
|
2018-07-18 20:58:16 +00:00
|
|
|
const PathID_t& from,
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router* r)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
msg = nullptr;
|
|
|
|
firstKey = true;
|
|
|
|
if(bencode_read_dict(&buf, &reader))
|
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
msg->from = from;
|
|
|
|
result = msg->HandleMessage(h, r);
|
|
|
|
if(!result)
|
2018-07-17 06:17:13 +00:00
|
|
|
llarp::LogWarn("Failed to handle inbound routing message ", key);
|
2018-06-22 13:59:28 +00:00
|
|
|
}
|
2018-06-26 14:52:19 +00:00
|
|
|
else
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("read dict failed in routing layer");
|
|
|
|
llarp::DumpBuffer< llarp_buffer_t, 128 >(buf);
|
|
|
|
}
|
2018-10-19 11:33:49 +00:00
|
|
|
msg.reset();
|
2018-06-22 13:59:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // namespace routing
|
2018-09-11 15:28:36 +00:00
|
|
|
} // namespace llarp
|