2019-01-19 13:49:15 +00:00
|
|
|
#include <dht/context.hpp>
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <memory>
|
2019-05-22 00:36:03 +00:00
|
|
|
#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
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
const Key_t& From;
|
2019-05-03 13:15:03 +00:00
|
|
|
IMessage::Ptr_t msg;
|
2020-02-14 17:43:44 +00:00
|
|
|
bool firstKey = true;
|
2020-04-07 18:38:56 +00:00
|
|
|
bool relayed = false;
|
2019-01-19 13:49:15 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
MessageDecoder(const Key_t& from, bool wasRelayed) : From(from), relayed(wasRelayed)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:36:03 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!key)
|
2019-05-22 00:36:03 +00:00
|
|
|
return !firstKey;
|
2019-01-19 13:49:15 +00:00
|
|
|
|
|
|
|
// first key
|
2020-04-07 18:38:56 +00:00
|
|
|
if (firstKey)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!(*key == "A"))
|
2019-01-19 13:49:15 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buffer, &strbuf))
|
2019-01-19 13:49:15 +00:00
|
|
|
return false;
|
|
|
|
// bad msg size?
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz != 1)
|
2019-01-19 13:49:15 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogDebug("Handle DHT message ", *strbuf.base, " relayed=", relayed);
|
|
|
|
switch (*strbuf.base)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
|
|
|
case 'F':
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<FindIntroMessage>(From, relayed, 0);
|
2019-01-19 13:49:15 +00:00
|
|
|
break;
|
|
|
|
case 'R':
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayed)
|
|
|
|
msg = std::make_unique<RelayedFindRouterMessage>(From);
|
2019-01-19 13:49:15 +00:00
|
|
|
else
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<FindRouterMessage>(From);
|
2019-01-19 13:49:15 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<GotRouterMessage>(From, relayed);
|
2019-01-19 13:49:15 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<PublishIntroMessage>(From, relayed);
|
2019-01-19 13:49:15 +00:00
|
|
|
break;
|
|
|
|
case 'G':
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayed)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<RelayedGotIntroMessage>();
|
2019-01-19 13:49:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
msg = std::make_unique<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;
|
|
|
|
}
|
2019-05-22 00:36:03 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
DecodeMesssage(const Key_t& from, llarp_buffer_t* buf, bool relayed)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2020-02-14 16:54:33 +00:00
|
|
|
MessageDecoder dec(from, relayed);
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
ListDecoder(bool hasRelayed, const Key_t& from, std::vector<IMessage::Ptr_t>& list)
|
2020-02-14 16:54:33 +00:00
|
|
|
: relayed(hasRelayed), From(from), l(list)
|
2019-04-24 23:27:31 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-19 13:49:15 +00:00
|
|
|
|
2019-05-22 00:36:03 +00:00
|
|
|
bool relayed;
|
2020-04-07 18:38:56 +00:00
|
|
|
const Key_t& From;
|
|
|
|
std::vector<IMessage::Ptr_t>& l;
|
2019-01-19 13:49:15 +00:00
|
|
|
|
2019-05-22 00:36:03 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
operator()(llarp_buffer_t* buffer, bool has)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!has)
|
2019-01-19 13:49:15 +00:00
|
|
|
return true;
|
2020-02-14 16:54:33 +00:00
|
|
|
auto msg = DecodeMesssage(From, buffer, relayed);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2019-05-22 00:36:03 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
DecodeMesssageList(
|
|
|
|
Key_t from, llarp_buffer_t* buf, std::vector<IMessage::Ptr_t>& list, bool relayed)
|
2019-01-19 13:49:15 +00:00
|
|
|
{
|
2020-02-14 16:54:33 +00:00
|
|
|
ListDecoder dec(relayed, from, list);
|
2019-05-22 00:36:03 +00:00
|
|
|
return bencode_read_list(dec, buf);
|
2019-01-19 13:49:15 +00:00
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|