2021-03-09 22:24:35 +00:00
|
|
|
#include "dht_immediate.hpp"
|
2019-01-19 18:16:10 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
2019-01-19 18:16:10 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
void
|
|
|
|
DHTImmediateMessage::Clear()
|
|
|
|
{
|
|
|
|
msgs.clear();
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2019-01-19 18:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
DHTImmediateMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("m"))
|
2020-04-07 18:38:56 +00:00
|
|
|
return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs);
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("v"))
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_integer(buf, &version))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
2022-05-26 15:59:44 +00:00
|
|
|
return version == llarp::constants::proto_version;
|
2019-01-19 18:16:10 +00:00
|
|
|
}
|
|
|
|
// bad key
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
DHTImmediateMessage::BEncode(llarp_buffer_t* buf) const
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// message type
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "a", 1))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "m", 1))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// dht messages
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "m", 1))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
// begin list
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_list(buf))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& msg : msgs)
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!msg->BEncode(buf))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// end list
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_end(buf))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// protocol version
|
2022-05-26 15:59:44 +00:00
|
|
|
if (!bencode_write_uint64_entry(buf, "v", 1, llarp::constants::proto_version))
|
2019-01-19 18:16:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
DHTImmediateMessage::HandleMessage(AbstractRouter* router) const
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
|
|
|
DHTImmediateMessage reply;
|
|
|
|
reply.session = session;
|
2020-04-07 18:38:56 +00:00
|
|
|
bool result = true;
|
|
|
|
for (auto& msg : msgs)
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
result &= msg->HandleMessage(router->dht(), reply.msgs);
|
2019-01-19 18:16:10 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (reply.msgs.size())
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result)
|
2019-01-19 18:16:10 +00:00
|
|
|
{
|
2021-04-12 11:39:07 +00:00
|
|
|
result = router->SendToOrQueue(session->GetPubKey(), reply);
|
2019-01-19 18:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 18:34:10 +00:00
|
|
|
return true;
|
2019-01-19 18:16:10 +00:00
|
|
|
}
|
|
|
|
} // namespace llarp
|