2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/context.hpp>
|
|
|
|
#include <dht/messages/gotintro.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/dht.hpp>
|
2018-12-10 16:26:46 +00:00
|
|
|
#include <router.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2018-07-18 03:10:21 +00:00
|
|
|
GotIntroMessage::GotIntroMessage(
|
2018-07-20 04:50:28 +00:00
|
|
|
const std::vector< llarp::service::IntroSet > &results, uint64_t tx)
|
2018-07-19 04:58:39 +00:00
|
|
|
: IMessage({}), I(results), T(tx)
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GotIntroMessage::~GotIntroMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-02 18:25:42 +00:00
|
|
|
GotIntroMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
2018-11-07 15:30:22 +00:00
|
|
|
__attribute__((unused))
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
|
|
|
auto &dht = ctx->impl;
|
|
|
|
auto crypto = &dht.router->crypto;
|
2018-07-18 20:58:16 +00:00
|
|
|
|
|
|
|
for(const auto &introset : I)
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
if(!introset.Verify(crypto, dht.Now()))
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn(
|
2018-09-20 11:27:18 +00:00
|
|
|
"Invalid introset while handling direct GotIntro "
|
2018-07-18 20:58:16 +00:00
|
|
|
"from ",
|
2018-07-12 13:43:37 +00:00
|
|
|
From);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
TXOwner owner(From, T);
|
|
|
|
auto tagLookup = dht.pendingTagLookups.GetPendingLookupFrom(owner);
|
|
|
|
if(tagLookup)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2018-10-21 14:19:49 +00:00
|
|
|
dht.pendingTagLookups.Found(owner, tagLookup->target, I);
|
2018-07-12 13:43:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
auto serviceLookup =
|
|
|
|
dht.pendingIntrosetLookups.GetPendingLookupFrom(owner);
|
|
|
|
if(serviceLookup)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2018-11-08 15:15:02 +00:00
|
|
|
if(I.size())
|
|
|
|
{
|
|
|
|
dht.pendingIntrosetLookups.Found(owner, serviceLookup->target, I);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dht.pendingIntrosetLookups.NotFound(owner, K);
|
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
return true;
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
llarp::LogError("no pending TX for GIM from ", From, " txid=", T);
|
|
|
|
return false;
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayedGotIntroMessage::HandleMessage(
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_dht_context *ctx,
|
2018-11-07 15:30:22 +00:00
|
|
|
__attribute__((unused))
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
// TODO: implement me better?
|
2018-07-11 16:11:19 +00:00
|
|
|
auto pathset = ctx->impl.router->paths.GetLocalPathSet(pathID);
|
|
|
|
if(pathset)
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
return pathset->HandleGotIntroMessage(this);
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
llarp::LogWarn("No path for got intro message pathid=", pathID);
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
|
|
|
{
|
|
|
|
if(llarp_buffer_eq(key, "I"))
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
return BEncodeReadList(I, buf);
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-11-08 15:15:02 +00:00
|
|
|
if(llarp_buffer_eq(key, "K"))
|
|
|
|
{
|
|
|
|
if(K) // duplicate key?
|
|
|
|
return false;
|
|
|
|
K.reset(new dht::Key_t());
|
|
|
|
return K->BDecode(buf);
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("T", T, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("V", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotIntroMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "G"))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictList("I", I, buf))
|
|
|
|
return false;
|
2018-11-08 15:15:02 +00:00
|
|
|
if(K)
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("K", *K.get(), buf))
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("T", T, buf))
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("V", 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-08-29 20:40:26 +00:00
|
|
|
} // namespace llarp
|