2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/dht_immediate.hpp>
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
#include <router.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
DHTImmeidateMessage::~DHTImmeidateMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
void
|
|
|
|
DHTImmeidateMessage::Clear()
|
|
|
|
{
|
|
|
|
msgs.clear();
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
bool
|
|
|
|
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
|
|
|
{
|
|
|
|
if(llarp_buffer_eq(key, "m"))
|
2018-12-10 17:22:59 +00:00
|
|
|
return llarp::dht::DecodeMesssageList(session->GetPubKey(), buf, msgs);
|
2018-07-11 13:20:14 +00:00
|
|
|
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
|
2018-12-10 16:26:46 +00:00
|
|
|
DHTImmeidateMessage::HandleMessage(llarp::Router *router) const
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-12-20 16:49:05 +00:00
|
|
|
DHTImmeidateMessage reply;
|
|
|
|
reply.session = session;
|
|
|
|
bool result = true;
|
2018-07-11 13:20:14 +00:00
|
|
|
for(auto &msg : msgs)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
result &= msg->HandleMessage(router->dht, reply.msgs);
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
if(reply.msgs.size())
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-08-23 14:35:29 +00:00
|
|
|
if(result)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
result = router->SendToOrQueue(session->GetPubKey(), &reply);
|
2018-08-23 14:35:29 +00:00
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
return result;
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-08-23 14:35:29 +00:00
|
|
|
} // namespace llarp
|