2021-03-09 22:24:35 +00:00
|
|
|
#include "dht_message.hpp"
|
2019-06-15 14:55:13 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include "handler.hpp"
|
2019-06-15 14:55:13 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace routing
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
DHTMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "M")
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
2020-01-21 17:31:48 +00:00
|
|
|
llarp::dht::Key_t fromKey;
|
|
|
|
fromKey.Zero();
|
2019-08-02 09:27:27 +00:00
|
|
|
return llarp::dht::DecodeMesssageList(fromKey, val, M, true);
|
2019-06-15 14:55:13 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "S")
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &S);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "V")
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &V);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "A", "M"))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictBEncodeList("M", M, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("S", S, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2022-05-26 15:59:44 +00:00
|
|
|
if (!BEncodeWriteDictInt("V", llarp::constants::proto_version, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:46:50 +00:00
|
|
|
/// 'h' here is either TransitHop or Path.
|
|
|
|
/// TransitHop chains to dht::Context::RelayRequestForPath and is where the
|
|
|
|
/// end of a path handles a client's DHT message Path handles the message
|
|
|
|
/// (e.g. dht::IMessage::HandleMessage()) in-place and is the case where a
|
|
|
|
/// client receives a DHT message
|
2019-06-15 14:55:13 +00:00
|
|
|
bool
|
|
|
|
DHTMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
|
|
|
|
{
|
|
|
|
// set source as us
|
2020-01-21 17:31:48 +00:00
|
|
|
const llarp::dht::Key_t us(r->pubkey());
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& msg : M)
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
msg->From = us;
|
2019-06-15 14:55:13 +00:00
|
|
|
msg->pathID = from;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!h->HandleDHTMessage(*msg, r))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace routing
|
|
|
|
} // namespace llarp
|