2018-07-11 13:20:14 +00:00
|
|
|
#include <llarp/messages/dht_immediate.hpp>
|
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
DHTImmeidateMessage::~DHTImmeidateMessage()
|
|
|
|
{
|
|
|
|
for(auto &msg : msgs)
|
|
|
|
delete msg;
|
|
|
|
msgs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
|
|
|
{
|
|
|
|
if(llarp_buffer_eq(key, "m"))
|
|
|
|
return llarp::dht::DecodeMesssageList(remote.data(), buf, msgs);
|
|
|
|
if(llarp_buffer_eq(key, "v"))
|
|
|
|
{
|
|
|
|
if(!bencode_read_integer(buf, &version))
|
|
|
|
return false;
|
|
|
|
return version == LLARP_PROTO_VERSION;
|
|
|
|
}
|
|
|
|
// bad key
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTImmeidateMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// message type
|
|
|
|
if(!bencode_write_bytestring(buf, "a", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buf, "m", 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// dht messages
|
|
|
|
if(!bencode_write_bytestring(buf, "m", 1))
|
|
|
|
return false;
|
|
|
|
// begin list
|
|
|
|
if(!bencode_start_list(buf))
|
|
|
|
return false;
|
|
|
|
for(const auto &msg : msgs)
|
|
|
|
{
|
|
|
|
if(!msg->BEncode(buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// end list
|
|
|
|
if(!bencode_end(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// protocol version
|
|
|
|
if(!bencode_write_version_entry(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTImmeidateMessage::HandleMessage(llarp_router *router) const
|
|
|
|
{
|
|
|
|
DHTImmeidateMessage *reply = new DHTImmeidateMessage(remote);
|
|
|
|
bool result = true;
|
|
|
|
for(auto &msg : msgs)
|
|
|
|
{
|
|
|
|
result &= msg->HandleMessage(router->dht, reply->msgs);
|
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
if(reply->msgs.size())
|
|
|
|
{
|
|
|
|
return result && router->SendToOrQueue(remote.data(), reply);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete reply;
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
} // namespace llarp
|