2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include <llarp/dht/messages/pubintro.hpp>
|
|
|
|
#include <llarp/messages/dht.hpp>
|
2018-07-12 13:43:37 +00:00
|
|
|
#include <llarp/messages/dht_immediate.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
PublishIntroMessage::~PublishIntroMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PublishIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
|
|
|
|
{
|
|
|
|
bool read = false;
|
2018-07-19 04:58:39 +00:00
|
|
|
if(llarp_buffer_eq(key, "E"))
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
return BEncodeReadList(E, val);
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("I", I, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("R", R, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(llarp_buffer_eq(key, "S"))
|
|
|
|
{
|
|
|
|
read = true;
|
|
|
|
hasS = true;
|
|
|
|
if(!bencode_read_integer(val, &S))
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("T", txID, read, key, val))
|
|
|
|
return false;
|
2018-07-11 13:20:14 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("V", version, read, key, val))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-02 18:25:42 +00:00
|
|
|
PublishIntroMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
if(S > 5)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("invalid S value ", S, " > 5");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
auto &dht = ctx->impl;
|
2018-09-20 11:27:18 +00:00
|
|
|
if(!I.Verify(&dht.router->crypto))
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-09-20 11:27:18 +00:00
|
|
|
llarp::LogWarn("invalid introset: ", I);
|
2018-09-21 16:37:49 +00:00
|
|
|
// don't propogate or store
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
if(I.W && !I.W->IsValid(dht.router->crypto.shorthash))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("proof of work not good enough for IntroSet");
|
2018-09-21 16:37:49 +00:00
|
|
|
// don't propogate or store
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
llarp::dht::Key_t addr;
|
|
|
|
if(!I.A.CalculateAddress(addr))
|
|
|
|
{
|
|
|
|
llarp::LogWarn(
|
|
|
|
"failed to calculate hidden service address for PubIntro message");
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-19 17:58:02 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
2018-09-21 16:37:49 +00:00
|
|
|
now += llarp::service::MAX_INTROSET_TIME_DELTA;
|
2018-09-19 17:58:02 +00:00
|
|
|
if(I.IsExpired(now))
|
|
|
|
{
|
2018-09-21 16:37:49 +00:00
|
|
|
// don't propogate or store
|
2018-09-19 17:58:02 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
dht.services->PutNode(I);
|
2018-09-02 18:25:42 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({I}, txID));
|
2018-07-12 13:43:37 +00:00
|
|
|
Key_t peer;
|
2018-07-20 04:50:28 +00:00
|
|
|
std::set< Key_t > exclude;
|
|
|
|
for(const auto &e : E)
|
|
|
|
exclude.insert(e);
|
2018-07-19 04:58:39 +00:00
|
|
|
exclude.insert(From);
|
2018-07-20 04:50:28 +00:00
|
|
|
exclude.insert(dht.OurKey());
|
2018-07-18 22:50:05 +00:00
|
|
|
if(S && dht.nodes->FindCloseExcluding(addr, peer, exclude))
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-08-01 22:10:38 +00:00
|
|
|
dht.PropagateIntroSetTo(From, txID, I, peer, S - 1, exclude);
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PublishIntroMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "I"))
|
|
|
|
return false;
|
2018-07-19 04:58:39 +00:00
|
|
|
if(!BEncodeWriteDictList("E", E, buf))
|
|
|
|
return false;
|
2018-07-11 13:20:14 +00:00
|
|
|
if(!BEncodeWriteDictEntry("I", I, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("R", R, buf))
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("S", S, buf))
|
2018-07-18 22:50:05 +00:00
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("T", txID, buf))
|
2018-07-11 16:11:19 +00:00
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
} // namespace dht
|
2018-09-02 18:25:42 +00:00
|
|
|
} // namespace llarp
|