2019-06-15 14:55:13 +00:00
|
|
|
#include <routing/dht_message.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
|
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
#include <routing/handler.hpp>
|
|
|
|
|
|
|
|
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;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("V", LLARP_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
|