lokinet/llarp/routing/dht_message.cpp

69 lines
1.8 KiB
C++
Raw Normal View History

#include "dht_message.hpp"
2019-06-15 14:55:13 +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)
{
if (key.startswith("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
}
if (key.startswith("S"))
2019-06-15 14:55:13 +00:00
{
return bencode_read_integer(val, &S);
}
if (key.startswith("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
{
if (!bencode_start_dict(buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictMsgType(buf, "A", "M"))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictBEncodeList("M", M, buf))
2019-06-15 14:55:13 +00:00
return false;
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);
}
/// '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());
for (const auto& msg : M)
2019-06-15 14:55:13 +00:00
{
msg->From = us;
2019-06-15 14:55:13 +00:00
msg->pathID = from;
if (!h->HandleDHTMessage(*msg, r))
2019-06-15 14:55:13 +00:00
return false;
}
return true;
}
} // namespace routing
} // namespace llarp