mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-15 12:13:24 +00:00
273270916e
This commit reflects changes to clang-format rules. Unfortunately, these rule changes create a massive change to the codebase, which causes an apparent rewrite of git history. Git blame's --ignore-rev flag can be used to ignore this commit when attempting to `git blame` some code.
83 lines
1.7 KiB
C++
83 lines
1.7 KiB
C++
#include <messages/dht_immediate.hpp>
|
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
namespace llarp
|
|
{
|
|
void
|
|
DHTImmediateMessage::Clear()
|
|
{
|
|
msgs.clear();
|
|
version = 0;
|
|
}
|
|
|
|
bool
|
|
DHTImmediateMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
|
{
|
|
if (key == "m")
|
|
return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs);
|
|
if (key == "v")
|
|
{
|
|
if (!bencode_read_integer(buf, &version))
|
|
return false;
|
|
return version == LLARP_PROTO_VERSION;
|
|
}
|
|
// bad key
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
DHTImmediateMessage::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_uint64_entry(buf, "v", 1, LLARP_PROTO_VERSION))
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
}
|
|
|
|
bool
|
|
DHTImmediateMessage::HandleMessage(AbstractRouter* router) const
|
|
{
|
|
DHTImmediateMessage reply;
|
|
reply.session = session;
|
|
bool result = true;
|
|
for (auto& msg : msgs)
|
|
{
|
|
result &= msg->HandleMessage(router->dht(), reply.msgs);
|
|
}
|
|
if (reply.msgs.size())
|
|
{
|
|
if (result)
|
|
{
|
|
result = router->SendToOrQueue(session->GetPubKey(), &reply);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} // namespace llarp
|