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)
|
|
|
|
{
|
2019-08-02 09:27:27 +00:00
|
|
|
llarp::dht::Key_t fromKey;
|
|
|
|
fromKey.Zero();
|
2019-06-15 14:55:13 +00:00
|
|
|
if(key == "M")
|
|
|
|
{
|
2019-08-02 09:27:27 +00:00
|
|
|
return llarp::dht::DecodeMesssageList(fromKey, val, M, true);
|
2019-06-15 14:55:13 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
if(key == "S")
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &S);
|
|
|
|
}
|
2019-07-06 17:03:40 +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
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "M"))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictBEncodeList("M", M, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("S", S, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DHTMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
|
|
|
|
{
|
|
|
|
// set source as us
|
|
|
|
llarp::dht::Key_t us{r->pubkey()};
|
|
|
|
for(const auto& msg : M)
|
|
|
|
{
|
|
|
|
msg->From = us;
|
|
|
|
msg->pathID = from;
|
|
|
|
if(!h->HandleDHTMessage(*msg, r))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace routing
|
|
|
|
} // namespace llarp
|