lokinet/llarp/messages/dht_immediate.cpp

83 lines
1.7 KiB
C++
Raw Normal View History

#include "dht_immediate.hpp"
2019-01-19 18:16:10 +00:00
#include <llarp/router/abstractrouter.hpp>
2019-01-19 18:16:10 +00:00
namespace llarp
{
void
DHTImmediateMessage::Clear()
{
msgs.clear();
version = 0;
2019-01-19 18:16:10 +00:00
}
bool
DHTImmediateMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
2019-01-19 18:16:10 +00:00
{
if (key == "m")
return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs);
if (key == "v")
2019-01-19 18:16:10 +00:00
{
if (!bencode_read_integer(buf, &version))
2019-01-19 18:16:10 +00:00
return false;
return version == LLARP_PROTO_VERSION;
}
// bad key
return false;
}
bool
DHTImmediateMessage::BEncode(llarp_buffer_t* buf) const
2019-01-19 18:16:10 +00:00
{
if (!bencode_start_dict(buf))
2019-01-19 18:16:10 +00:00
return false;
// message type
if (!bencode_write_bytestring(buf, "a", 1))
2019-01-19 18:16:10 +00:00
return false;
if (!bencode_write_bytestring(buf, "m", 1))
2019-01-19 18:16:10 +00:00
return false;
// dht messages
if (!bencode_write_bytestring(buf, "m", 1))
2019-01-19 18:16:10 +00:00
return false;
// begin list
if (!bencode_start_list(buf))
2019-01-19 18:16:10 +00:00
return false;
for (const auto& msg : msgs)
2019-01-19 18:16:10 +00:00
{
if (!msg->BEncode(buf))
2019-01-19 18:16:10 +00:00
return false;
}
// end list
if (!bencode_end(buf))
2019-01-19 18:16:10 +00:00
return false;
// protocol version
if (!bencode_write_uint64_entry(buf, "v", 1, LLARP_PROTO_VERSION))
2019-01-19 18:16:10 +00:00
return false;
return bencode_end(buf);
}
bool
DHTImmediateMessage::HandleMessage(AbstractRouter* router) const
2019-01-19 18:16:10 +00:00
{
DHTImmediateMessage reply;
reply.session = session;
bool result = true;
for (auto& msg : msgs)
2019-01-19 18:16:10 +00:00
{
result &= msg->HandleMessage(router->dht(), reply.msgs);
2019-01-19 18:16:10 +00:00
}
if (reply.msgs.size())
2019-01-19 18:16:10 +00:00
{
if (result)
2019-01-19 18:16:10 +00:00
{
result = router->SendToOrQueue(session->GetPubKey(), &reply);
}
}
2019-02-21 18:34:10 +00:00
return true;
2019-01-19 18:16:10 +00:00
}
} // namespace llarp