2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/dht.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
#include <routing/handler.hpp>
|
2018-06-29 14:25:09 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace routing
|
|
|
|
{
|
|
|
|
DHTMessage::~DHTMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-05 00:41:33 +00:00
|
|
|
DHTMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
2018-06-29 14:25:09 +00:00
|
|
|
{
|
|
|
|
llarp::dht::Key_t from;
|
|
|
|
from.Zero();
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "M")
|
2018-06-29 14:25:09 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
return llarp::dht::DecodeMesssageList(from, val, M, true);
|
2018-06-29 14:25:09 +00:00
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
else if(key == "S")
|
2018-07-23 21:59:43 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &S);
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
else if(key == "V")
|
2018-06-29 14:25:09 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &V);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "M"))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictBEncodeList("M", M, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("S", S, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
|
2018-06-29 14:25:09 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-11 19:45:42 +00:00
|
|
|
DHTMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
|
2018-06-29 14:25:09 +00:00
|
|
|
{
|
|
|
|
// set source as us
|
2019-01-02 01:04:08 +00:00
|
|
|
llarp::dht::Key_t us{r->pubkey()};
|
2018-09-02 18:25:42 +00:00
|
|
|
for(const auto& msg : M)
|
2018-06-29 14:25:09 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
msg->From = us;
|
|
|
|
msg->pathID = from;
|
2019-04-22 17:38:29 +00:00
|
|
|
if(!h->HandleDHTMessage(*msg, r))
|
2018-06-29 14:25:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace routing
|
2018-09-02 18:25:42 +00:00
|
|
|
} // namespace llarp
|