2021-03-09 22:24:35 +00:00
|
|
|
#include "gotname.hpp"
|
2022-02-17 18:44:31 +00:00
|
|
|
#include <oxenc/bt_serialize.h>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/path/path_context.hpp>
|
2020-09-17 19:18:08 +00:00
|
|
|
|
|
|
|
namespace llarp::dht
|
|
|
|
{
|
|
|
|
constexpr size_t NameSizeLimit = 128;
|
|
|
|
|
|
|
|
GotNameMessage::GotNameMessage(const Key_t& from, uint64_t txid, service::EncryptedName data)
|
2023-08-29 14:26:59 +00:00
|
|
|
: AbstractDHTMessage(from), result(std::move(data)), TxID(txid)
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
|
|
|
if (result.ciphertext.size() > NameSizeLimit)
|
|
|
|
throw std::invalid_argument("name data too big");
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
void
|
|
|
|
GotNameMessage::bt_encode(oxenc::bt_dict_producer& btdp) const
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
btdp.append("A", "M");
|
|
|
|
btdp.append("D", result.ciphertext);
|
|
|
|
btdp.append("N", result.nonce.ToView());
|
|
|
|
btdp.append("T", TxID);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
log::error(dht_cat, "Error: GotNameMessage failed to bt encode contents!");
|
|
|
|
}
|
2020-09-17 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
GotNameMessage::decode_key(const llarp_buffer_t& key, llarp_buffer_t* val)
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("D"))
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t str{};
|
|
|
|
if (not bencode_read_string(val, &str))
|
|
|
|
return false;
|
|
|
|
if (str.sz > NameSizeLimit)
|
|
|
|
return false;
|
|
|
|
result.ciphertext.resize(str.sz);
|
|
|
|
std::copy_n(str.cur, str.sz, result.ciphertext.data());
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("N"))
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
|
|
|
return result.nonce.BDecode(val);
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("T"))
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &TxID);
|
|
|
|
}
|
|
|
|
return bencode_discard(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
GotNameMessage::handle_message(struct llarp_dht_context* ctx, std::vector<Ptr_t>&) const
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
2020-09-19 13:29:36 +00:00
|
|
|
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
|
|
|
|
if (pathset == nullptr)
|
|
|
|
return false;
|
|
|
|
auto copy = std::make_shared<const GotNameMessage>(*this);
|
|
|
|
return pathset->HandleGotNameMessage(copy);
|
2020-09-17 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::dht
|