lokinet/llarp/dht/got_intro.cpp

103 lines
2.7 KiB
C++
Raw Normal View History

#include <llarp/dht/context.hpp>
#include <llarp/dht/messages/gotintro.hpp>
#include <llarp/messages/dht.hpp>
#include "router.hpp"
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)
{
}
GotIntroMessage::~GotIntroMessage()
{
}
bool
GotIntroMessage::HandleMessage(llarp_dht_context *ctx,
std::vector< IMessage * > &replies) const
{
auto &dht = ctx->impl;
auto crypto = &dht.router->crypto;
for(const auto &introset : I)
{
if(!introset.VerifySignature(crypto))
{
llarp::LogWarn(
"Invalid introset signature while handling direct GotIntro "
"from ",
From);
return false;
}
}
2018-08-29 20:40:26 +00:00
TXOwner owner(From, T);
auto tagLookup = dht.pendingTagLookups.GetPendingLookupFrom(owner);
if(tagLookup)
{
2018-08-29 20:40:26 +00:00
dht.pendingTagLookups.Inform(owner, tagLookup->target, I);
return true;
}
2018-08-29 20:40:26 +00:00
auto serviceLookup =
dht.pendingIntrosetLookups.GetPendingLookupFrom(owner);
if(serviceLookup)
{
2018-08-29 20:40:26 +00:00
dht.pendingIntrosetLookups.Inform(owner, serviceLookup->target, I);
return true;
}
2018-08-29 20:40:26 +00:00
llarp::LogError("no pending TX for GIM from ", From, " txid=", T);
return false;
}
bool
RelayedGotIntroMessage::HandleMessage(
llarp_dht_context *ctx, std::vector< IMessage * > &replies) const
{
// TODO: implement me better?
auto pathset = ctx->impl.router->paths.GetLocalPathSet(pathID);
if(pathset)
{
return pathset->HandleGotIntroMessage(this);
}
llarp::LogWarn("No path for got intro message pathid=", pathID);
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);
}
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;
if(!BEncodeWriteDictInt("T", T, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
2018-08-29 20:40:26 +00:00
} // namespace llarp