2021-03-09 22:24:35 +00:00
|
|
|
#include "gotname.hpp"
|
2021-02-02 14:35:40 +00:00
|
|
|
#include <oxenmq/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)
|
|
|
|
: IMessage(from), result(std::move(data)), TxID(txid)
|
|
|
|
{
|
|
|
|
if (result.ciphertext.size() > NameSizeLimit)
|
|
|
|
throw std::invalid_argument("name data too big");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotNameMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
const std::string nonce((const char*)result.nonce.data(), result.nonce.size());
|
2021-02-03 18:12:21 +00:00
|
|
|
const auto data = oxenmq::bt_serialize(
|
|
|
|
oxenmq::bt_dict{{"A", "M"sv}, {"D", result.ciphertext}, {"N", nonce}, {"T", TxID}});
|
2020-09-17 19:18:08 +00:00
|
|
|
return buf->write(data.begin(), data.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotNameMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
|
|
|
{
|
|
|
|
if (key == "D")
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (key == "N")
|
|
|
|
{
|
|
|
|
return result.nonce.BDecode(val);
|
|
|
|
}
|
|
|
|
if (key == "T")
|
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &TxID);
|
|
|
|
}
|
|
|
|
return bencode_discard(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-09-19 13:29:36 +00:00
|
|
|
GotNameMessage::HandleMessage(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
|