2018-12-15 16:21:52 +00:00
|
|
|
#include <dht/messages/pubintro.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
#include <dht/context.hpp>
|
|
|
|
#include <dht/messages/gotintro.hpp>
|
|
|
|
#include <messages/dht_immediate.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
#include <routing/dht_message.hpp>
|
2020-02-20 00:07:46 +00:00
|
|
|
#include <nodedb.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
const uint64_t PublishIntroMessage::MaxPropagationDepth = 5;
|
|
|
|
PublishIntroMessage::~PublishIntroMessage() = default;
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
bool
|
2019-02-05 00:41:33 +00:00
|
|
|
PublishIntroMessage::DecodeKey(const llarp_buffer_t &key,
|
|
|
|
llarp_buffer_t *val)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-01-23 12:11:11 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("I", introset, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-02-20 00:07:46 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("O", relayOrder, read, key, val))
|
|
|
|
return false;
|
|
|
|
uint64_t relayedInt = (relayed ? 1 : 0);
|
|
|
|
if(!BEncodeMaybeReadDictInt("R", relayedInt, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("T", txID, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("V", version, read, key, val))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PublishIntroMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto now = ctx->impl->Now();
|
2020-01-23 12:11:11 +00:00
|
|
|
|
2019-02-22 15:08:00 +00:00
|
|
|
auto &dht = *ctx->impl;
|
2020-01-23 12:11:11 +00:00
|
|
|
if(!introset.Verify(now))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-02-20 00:07:46 +00:00
|
|
|
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ",
|
2020-02-20 15:36:29 +00:00
|
|
|
introset);
|
2019-01-16 00:24:16 +00:00
|
|
|
// don't propogate or store
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2020-02-20 00:07:46 +00:00
|
|
|
if(introset.IsExpired(now + llarp::service::MAX_INTROSET_TIME_DELTA))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
// don't propogate or store
|
2020-02-20 00:07:46 +00:00
|
|
|
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ",
|
2020-02-20 15:36:29 +00:00
|
|
|
introset);
|
2019-01-16 00:24:16 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
|
|
|
|
const llarp::dht::Key_t addr(introset.derivedSigningKey);
|
|
|
|
|
|
|
|
// identify closest 4 routers
|
|
|
|
auto closestRCs = dht.GetRouter()->nodedb()->FindClosestTo(addr, 4);
|
2020-02-20 15:36:29 +00:00
|
|
|
if(closestRCs.size() != 4)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-02-20 15:36:29 +00:00
|
|
|
llarp::LogWarn("Received PublishIntroMessage but only know ",
|
|
|
|
closestRCs.size(), " nodes");
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
const auto &us = dht.OurKey();
|
2020-02-20 00:30:58 +00:00
|
|
|
|
2020-02-20 00:07:46 +00:00
|
|
|
// function to identify the closest 4 routers we know of for this introset
|
2020-02-20 00:30:58 +00:00
|
|
|
auto propagateToClosestFour = [&]() {
|
2020-02-20 00:07:46 +00:00
|
|
|
// grab 1st & 2nd if we are relayOrder == 0, 3rd & 4th otherwise
|
2020-02-20 15:36:29 +00:00
|
|
|
const auto &rc0 = (relayOrder == 0 ? closestRCs[0] : closestRCs[2]);
|
|
|
|
const auto &rc1 = (relayOrder == 0 ? closestRCs[1] : closestRCs[3]);
|
2020-02-20 00:30:58 +00:00
|
|
|
|
|
|
|
const Key_t peer0{rc0.pubkey};
|
|
|
|
const Key_t peer1{rc1.pubkey};
|
|
|
|
|
|
|
|
bool arePeer0 = (peer0 == us);
|
|
|
|
bool arePeer1 = (peer1 == us);
|
2020-02-20 00:07:46 +00:00
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
if(arePeer0 or arePeer1)
|
2020-02-20 00:30:58 +00:00
|
|
|
{
|
|
|
|
dht.services()->PutNode(introset);
|
|
|
|
replies.emplace_back(new GotIntroMessage({introset}, txID));
|
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
if(not arePeer0)
|
2020-02-20 00:30:58 +00:00
|
|
|
dht.PropagateIntroSetTo(From, txID, introset, peer0, false, 0);
|
2020-02-20 00:07:46 +00:00
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
if(not arePeer1)
|
2020-02-20 00:30:58 +00:00
|
|
|
dht.PropagateIntroSetTo(From, txID, introset, peer1, false, 0);
|
2020-02-20 00:07:46 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
if(relayed)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-02-20 15:36:29 +00:00
|
|
|
if(relayOrder > 1)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-02-20 15:36:29 +00:00
|
|
|
llarp::LogWarn(
|
|
|
|
"Received PublishIntroMessage with invalid relayOrder: ",
|
2020-02-20 00:07:46 +00:00
|
|
|
relayOrder);
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
propagateToClosestFour();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool found = false;
|
2020-02-20 15:36:29 +00:00
|
|
|
for(const auto &rc : closestRCs)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-02-20 15:36:29 +00:00
|
|
|
if(rc.pubkey == dht.OurKey())
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:36:29 +00:00
|
|
|
if(found)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
|
|
|
dht.services()->PutNode(introset);
|
|
|
|
replies.emplace_back(new GotIntroMessage({introset}, txID));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: ensure this can't create a loop (reintroduce depth?)
|
|
|
|
propagateToClosestFour();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:24:16 +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;
|
2020-01-23 12:11:11 +00:00
|
|
|
if(!BEncodeWriteDictEntry("I", introset, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-02-20 00:07:46 +00:00
|
|
|
if(!BEncodeWriteDictInt("O", relayOrder, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("R", relayed, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("T", txID, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|