lokinet/llarp/dht/message.cpp

134 lines
3.3 KiB
C++
Raw Normal View History

2019-01-19 13:49:15 +00:00
#include <dht/context.hpp>
#include <util/bencode.hpp>
2019-01-19 13:49:15 +00:00
#include <dht/messages/findintro.hpp>
#include <dht/messages/findrouter.hpp>
#include <dht/messages/gotintro.hpp>
#include <dht/messages/gotrouter.hpp>
#include <dht/messages/pubintro.hpp>
namespace llarp
{
namespace dht
{
struct MessageDecoder
{
const Key_t &From;
2019-05-03 13:15:03 +00:00
IMessage::Ptr_t msg;
2019-01-19 13:49:15 +00:00
bool firstKey = true;
bool relayed = false;
MessageDecoder(const Key_t &from, bool wasRelayed)
: From(from), relayed(wasRelayed)
2019-01-19 13:49:15 +00:00
{
}
bool
operator()(llarp_buffer_t *buffer, llarp_buffer_t *key)
2019-01-19 13:49:15 +00:00
{
llarp_buffer_t strbuf;
// check for empty dict
if(!key)
return !firstKey;
2019-01-19 13:49:15 +00:00
// first key
if(firstKey)
2019-01-19 13:49:15 +00:00
{
if(!(*key == "A"))
2019-01-19 13:49:15 +00:00
return false;
if(!bencode_read_string(buffer, &strbuf))
2019-01-19 13:49:15 +00:00
return false;
// bad msg size?
if(strbuf.sz != 1)
return false;
2019-07-17 12:25:51 +00:00
llarp::LogDebug("Handle DHT message ", *strbuf.base,
" relayed=", relayed);
2019-01-19 13:49:15 +00:00
switch(*strbuf.base)
{
case 'F':
msg.reset(new FindIntroMessage(From, relayed));
2019-01-19 13:49:15 +00:00
break;
case 'R':
if(relayed)
msg.reset(new RelayedFindRouterMessage(From));
2019-01-19 13:49:15 +00:00
else
msg.reset(new FindRouterMessage(From));
2019-01-19 13:49:15 +00:00
break;
case 'S':
msg.reset(new GotRouterMessage(From, relayed));
2019-01-19 13:49:15 +00:00
break;
case 'I':
msg.reset(new PublishIntroMessage());
2019-01-19 13:49:15 +00:00
break;
case 'G':
if(relayed)
2019-01-19 13:49:15 +00:00
{
msg.reset(new RelayedGotIntroMessage());
2019-01-19 13:49:15 +00:00
break;
}
else
{
msg.reset(new GotIntroMessage(From));
2019-01-19 13:49:15 +00:00
break;
}
default:
llarp::LogWarn("unknown dht message type: ", (char)*strbuf.base);
// bad msg type
return false;
}
firstKey = false;
return msg != nullptr;
2019-01-19 13:49:15 +00:00
}
2019-07-06 17:03:40 +00:00
return msg->DecodeKey(*key, buffer);
2019-01-19 13:49:15 +00:00
}
};
2019-05-03 13:15:03 +00:00
IMessage::Ptr_t
2019-01-19 13:49:15 +00:00
DecodeMesssage(const Key_t &from, llarp_buffer_t *buf, bool relayed)
{
MessageDecoder dec(from, relayed);
if(!bencode_read_dict(dec, buf))
2019-01-19 13:49:15 +00:00
return nullptr;
2019-05-03 13:15:03 +00:00
return std::move(dec.msg);
2019-01-19 13:49:15 +00:00
}
struct ListDecoder
{
ListDecoder(bool hasRelayed, const Key_t &from,
std::vector< IMessage::Ptr_t > &list)
: relayed(hasRelayed), From(from), l(list)
{
}
2019-01-19 13:49:15 +00:00
bool relayed;
2019-01-19 13:49:15 +00:00
const Key_t &From;
2019-05-03 13:15:03 +00:00
std::vector< IMessage::Ptr_t > &l;
2019-01-19 13:49:15 +00:00
bool
operator()(llarp_buffer_t *buffer, bool has)
2019-01-19 13:49:15 +00:00
{
if(!has)
return true;
auto msg = DecodeMesssage(From, buffer, relayed);
2019-01-19 13:49:15 +00:00
if(msg)
{
l.emplace_back(std::move(msg));
2019-01-19 13:49:15 +00:00
return true;
}
2019-07-06 17:03:40 +00:00
return false;
2019-01-19 13:49:15 +00:00
}
};
bool
DecodeMesssageList(Key_t from, llarp_buffer_t *buf,
2019-05-03 13:15:03 +00:00
std::vector< IMessage::Ptr_t > &list, bool relayed)
2019-01-19 13:49:15 +00:00
{
ListDecoder dec(relayed, from, list);
return bencode_read_list(dec, buf);
2019-01-19 13:49:15 +00:00
}
} // namespace dht
} // namespace llarp