2018-07-11 13:20:14 +00:00
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include <llarp/dht/messages/all.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
struct MessageDecoder
|
|
|
|
{
|
|
|
|
const Key_t &From;
|
2018-09-02 18:25:42 +00:00
|
|
|
std::unique_ptr< IMessage > msg;
|
2018-07-11 13:20:14 +00:00
|
|
|
bool firstKey = true;
|
|
|
|
bool relayed = false;
|
|
|
|
|
|
|
|
MessageDecoder(const Key_t &from) : From(from)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
on_key(dict_reader *r, llarp_buffer_t *key)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
MessageDecoder *dec = static_cast< MessageDecoder * >(r->user);
|
|
|
|
// check for empty dict
|
|
|
|
if(!key)
|
|
|
|
return !dec->firstKey;
|
|
|
|
|
|
|
|
// first key
|
|
|
|
if(dec->firstKey)
|
|
|
|
{
|
|
|
|
if(!llarp_buffer_eq(*key, "A"))
|
|
|
|
return false;
|
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
|
|
|
return false;
|
|
|
|
// bad msg size?
|
|
|
|
if(strbuf.sz != 1)
|
|
|
|
return false;
|
2018-08-10 03:51:38 +00:00
|
|
|
llarp::LogInfo("Handle DHT message ", *strbuf.base,
|
|
|
|
" relayed=", dec->relayed);
|
2018-09-02 18:25:42 +00:00
|
|
|
IMessage *msg;
|
2018-07-11 13:20:14 +00:00
|
|
|
switch(*strbuf.base)
|
|
|
|
{
|
2018-07-16 21:22:25 +00:00
|
|
|
case 'F':
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new FindIntroMessage(dec->From, dec->relayed);
|
2018-07-18 20:58:16 +00:00
|
|
|
break;
|
2018-07-11 13:20:14 +00:00
|
|
|
case 'R':
|
|
|
|
if(dec->relayed)
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new RelayedFindRouterMessage(dec->From);
|
2018-07-11 13:20:14 +00:00
|
|
|
else
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new FindRouterMessage(dec->From);
|
2018-07-11 13:20:14 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new GotRouterMessage(dec->From, dec->relayed);
|
2018-07-11 13:20:14 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new PublishIntroMessage();
|
2018-07-11 13:20:14 +00:00
|
|
|
break;
|
2018-07-11 16:11:19 +00:00
|
|
|
case 'G':
|
|
|
|
if(dec->relayed)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new RelayedGotIntroMessage();
|
2018-07-11 16:11:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
msg = new GotIntroMessage(dec->From);
|
2018-07-12 13:43:37 +00:00
|
|
|
break;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
default:
|
|
|
|
llarp::LogWarn("unknown dht message type: ", (char)*strbuf.base);
|
|
|
|
// bad msg type
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
dec->msg = std::unique_ptr< IMessage >(msg);
|
2018-07-11 13:20:14 +00:00
|
|
|
dec->firstKey = false;
|
|
|
|
return dec->msg != nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return dec->msg->DecodeKey(*key, r->buffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
std::unique_ptr< IMessage >
|
2018-07-11 13:20:14 +00:00
|
|
|
DecodeMesssage(const Key_t &from, llarp_buffer_t *buf, bool relayed)
|
|
|
|
{
|
|
|
|
MessageDecoder dec(from);
|
|
|
|
dec.relayed = relayed;
|
|
|
|
dict_reader r;
|
|
|
|
r.user = &dec;
|
|
|
|
r.on_key = &MessageDecoder::on_key;
|
2018-09-02 18:25:42 +00:00
|
|
|
if(!bencode_read_dict(buf, &r))
|
2018-07-11 13:20:14 +00:00
|
|
|
return nullptr;
|
2018-09-02 18:25:42 +00:00
|
|
|
return std::unique_ptr< IMessage >(std::move(dec.msg));
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ListDecoder
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
ListDecoder(const Key_t &from,
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &list)
|
2018-07-11 13:20:14 +00:00
|
|
|
: From(from), l(list){};
|
|
|
|
|
|
|
|
bool relayed = false;
|
|
|
|
const Key_t &From;
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > > &l;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
on_item(list_reader *r, bool has)
|
|
|
|
{
|
|
|
|
ListDecoder *dec = static_cast< ListDecoder * >(r->user);
|
|
|
|
if(!has)
|
|
|
|
return true;
|
|
|
|
auto msg = DecodeMesssage(dec->From, r->buffer, dec->relayed);
|
|
|
|
if(msg)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
dec->l.emplace_back(std::move(msg));
|
2018-07-11 13:20:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2018-09-02 18:25:42 +00:00
|
|
|
DecodeMesssageList(Key_t from, llarp_buffer_t *buf,
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &list,
|
|
|
|
bool relayed)
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
ListDecoder dec(from, list);
|
|
|
|
dec.relayed = relayed;
|
|
|
|
list_reader r;
|
|
|
|
r.user = &dec;
|
|
|
|
r.on_item = &ListDecoder::on_item;
|
|
|
|
return bencode_read_list(buf, &r);
|
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
} // namespace dht
|
2018-09-02 18:25:42 +00:00
|
|
|
} // namespace llarp
|