2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/router_contact.hpp>
|
2018-06-08 13:12:17 +00:00
|
|
|
#include <llarp/messages.hpp>
|
2018-05-25 17:52:10 +00:00
|
|
|
#include "buffer.hpp"
|
2018-07-09 04:26:27 +00:00
|
|
|
#include "llarp/iwp/session.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
|
|
|
|
{
|
2018-06-01 18:05:40 +00:00
|
|
|
ILinkMessage::ILinkMessage(const RouterID& id) : remote(id)
|
2018-06-01 17:47:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
InboundMessageParser::InboundMessageParser(llarp_router* _router)
|
2018-05-25 17:52:10 +00:00
|
|
|
: router(_router)
|
|
|
|
{
|
|
|
|
reader.user = this;
|
|
|
|
reader.on_key = &OnKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
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-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);
|
2018-06-01 14:08:54 +00:00
|
|
|
switch(*strbuf.cur)
|
|
|
|
{
|
|
|
|
case 'i':
|
2018-07-09 04:26:27 +00:00
|
|
|
handler->msg =
|
|
|
|
new LinkIntroMessage(handler->from->get_remote_router());
|
2018-06-01 14:08:54 +00:00
|
|
|
break;
|
2018-06-08 13:12:17 +00:00
|
|
|
case 'd':
|
|
|
|
handler->msg = new RelayDownstreamMessage(handler->GetCurrentFrom());
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
handler->msg = new RelayUpstreamMessage(handler->GetCurrentFrom());
|
|
|
|
break;
|
2018-06-01 14:08:54 +00:00
|
|
|
case 'm':
|
|
|
|
handler->msg = new DHTImmeidateMessage(handler->GetCurrentFrom());
|
2018-06-08 13:12:17 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
handler->msg = new LR_AckMessage(handler->GetCurrentFrom());
|
2018-06-01 14:08:54 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
handler->msg = new LR_CommitMessage(handler->GetCurrentFrom());
|
2018-06-01 17:47:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
handler->firstkey = false;
|
2018-06-01 14:08:54 +00:00
|
|
|
return handler->msg != nullptr;
|
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-06-01 14:08:54 +00:00
|
|
|
RouterID
|
|
|
|
InboundMessageParser::GetCurrentFrom()
|
|
|
|
{
|
2018-08-31 14:41:04 +00:00
|
|
|
return from->get_remote_router()->pubkey;
|
2018-06-01 14:08:54 +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);
|
|
|
|
delete msg;
|
|
|
|
msg = nullptr;
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
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
|
2018-06-01 14:08:54 +00:00
|
|
|
InboundMessageParser::ProcessFrom(llarp_link_session* src, llarp_buffer_t buf)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
|
|
|
from = src;
|
|
|
|
firstkey = true;
|
2018-06-01 17:47:37 +00:00
|
|
|
return bencode_read_dict(&buf, &reader);
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
2018-06-20 12:34:48 +00:00
|
|
|
} // namespace llarp
|