2019-01-10 19:41:51 +00:00
|
|
|
#include <service/endpoint.hpp>
|
|
|
|
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/messages/findintro.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
#include <dht/messages/findrouter.hpp>
|
|
|
|
#include <dht/messages/gotintro.hpp>
|
|
|
|
#include <dht/messages/gotrouter.hpp>
|
|
|
|
#include <dht/messages/pubintro.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <nodedb.hpp>
|
|
|
|
#include <profiling.hpp>
|
|
|
|
#include <router/abstractrouter.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
#include <routing/dht_message.hpp>
|
2019-06-19 22:30:07 +00:00
|
|
|
#include <routing/path_transfer_message.hpp>
|
2019-07-15 09:15:51 +00:00
|
|
|
#include <service/endpoint_state.hpp>
|
2019-04-30 01:06:20 +00:00
|
|
|
#include <service/endpoint_util.hpp>
|
2019-04-21 16:44:27 +00:00
|
|
|
#include <service/hidden_service_address_lookup.hpp>
|
|
|
|
#include <service/outbound_context.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/protocol.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-04-18 11:49:54 +00:00
|
|
|
#include <util/str.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-09-01 12:38:03 +00:00
|
|
|
#include <util/meta/memfn.hpp>
|
2019-04-22 12:25:25 +00:00
|
|
|
#include <hook/shell.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
Endpoint::Endpoint(const std::string& name, AbstractRouter* r,
|
2019-01-28 15:26:35 +00:00
|
|
|
Context* parent)
|
2019-07-15 09:15:51 +00:00
|
|
|
: path::Builder(r, 3, path::default_len), context(parent)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state = std::make_unique< EndpointState >();
|
|
|
|
m_state->m_Router = r;
|
|
|
|
m_state->m_Name = name;
|
|
|
|
m_state->m_Tag.Zero();
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::SetOption(const std::string& k, const std::string& v)
|
|
|
|
{
|
2019-07-18 16:28:17 +00:00
|
|
|
return m_state->SetOption(k, v, *this);
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:01:52 +00:00
|
|
|
llarp_ev_loop_ptr
|
2018-08-17 19:49:58 +00:00
|
|
|
Endpoint::EndpointNetLoop()
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(m_state->m_IsolatedNetLoop)
|
|
|
|
return m_state->m_IsolatedNetLoop;
|
2019-07-06 17:03:40 +00:00
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
return Router()->netloop();
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
|
|
|
Endpoint::NetworkIsIsolated() const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->m_IsolatedLogic.get() != nullptr
|
|
|
|
&& m_state->m_IsolatedNetLoop != nullptr;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HasPendingPathToService(const Address& addr) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->m_PendingServiceLookups.find(addr)
|
|
|
|
!= m_state->m_PendingServiceLookups.end();
|
2018-08-10 03:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:21:35 +00:00
|
|
|
void
|
2019-11-05 16:58:53 +00:00
|
|
|
Endpoint::RegenAndPublishIntroSet(bool forceRebuild)
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
const auto now = llarp::time_now_ms();
|
2018-09-11 13:21:35 +00:00
|
|
|
std::set< Introduction > I;
|
2018-10-01 14:18:17 +00:00
|
|
|
if(!GetCurrentIntroductionsWithFilter(
|
|
|
|
I, [now](const service::Introduction& intro) -> bool {
|
2019-11-05 16:58:53 +00:00
|
|
|
return not intro.ExpiresSoon(now, 2 * 60 * 1000);
|
2018-10-01 14:18:17 +00:00
|
|
|
}))
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("could not publish descriptors for endpoint ", Name(),
|
|
|
|
" because we couldn't get enough valid introductions");
|
2018-10-29 16:48:36 +00:00
|
|
|
if(ShouldBuildMore(now) || forceRebuild)
|
2018-11-22 15:52:04 +00:00
|
|
|
ManualRebuild(1);
|
2018-09-11 13:21:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
introSet().I.clear();
|
2019-05-02 13:47:22 +00:00
|
|
|
for(auto& intro : I)
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
introSet().I.emplace_back(std::move(intro));
|
2018-09-11 13:21:35 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
if(introSet().I.size() == 0)
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("not enough intros to publish introset for ", Name());
|
2019-05-02 13:47:22 +00:00
|
|
|
if(ShouldBuildMore(now) || forceRebuild)
|
|
|
|
ManualRebuild(1);
|
2018-09-11 13:21:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
introSet().topic = m_state->m_Tag;
|
|
|
|
if(!m_Identity.SignIntroSet(introSet(), now))
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("failed to sign introset for endpoint ", Name());
|
2018-09-11 13:21:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
if(PublishIntroSet(Router()))
|
2018-09-11 13:21:35 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo("(re)publishing introset for endpoint ", Name());
|
2018-09-11 13:21:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("failed to publish intro set for endpoint ", Name());
|
2018-09-11 13:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
bool
|
|
|
|
Endpoint::IsReady() const
|
|
|
|
{
|
|
|
|
const auto now = Now();
|
2019-07-15 09:15:51 +00:00
|
|
|
if(introSet().I.size() == 0)
|
2019-04-22 12:25:25 +00:00
|
|
|
return false;
|
2019-07-15 09:15:51 +00:00
|
|
|
if(introSet().IsExpired(now))
|
2019-04-22 12:25:25 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-10 12:47:21 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HasPendingRouterLookup(const RouterID remote) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
const auto& routers = m_state->m_PendingRouters;
|
|
|
|
return routers.find(remote) != routers.end();
|
2019-06-10 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 14:56:56 +00:00
|
|
|
bool
|
|
|
|
Endpoint::GetEndpointWithConvoTag(const ConvoTag tag,
|
|
|
|
llarp::AlignedBuffer< 32 >& addr,
|
|
|
|
bool& snode) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr != Sessions().end())
|
2019-07-01 14:56:56 +00:00
|
|
|
{
|
|
|
|
snode = false;
|
|
|
|
addr = itr->second.remote.Addr();
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
for(const auto& item : m_state->m_SNodeSessions)
|
2019-07-01 14:56:56 +00:00
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
if(item.second.second == tag)
|
2019-07-01 14:56:56 +00:00
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
snode = true;
|
|
|
|
addr = item.first;
|
|
|
|
return true;
|
2019-07-01 14:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
|
2019-07-01 14:56:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
bool
|
|
|
|
Endpoint::IntrosetIsStale() const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return introSet().HasExpiredIntros(Now());
|
2019-04-22 12:25:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
Endpoint::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-08-19 09:33:26 +00:00
|
|
|
auto obj = path::Builder::ExtractStatus();
|
|
|
|
obj["identity"] = m_Identity.pub.Addr().ToString();
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->ExtractStatus(obj);
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 17:01:34 +00:00
|
|
|
void Endpoint::Tick(llarp_time_t)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
const auto now = llarp::time_now_ms();
|
2019-04-23 16:13:22 +00:00
|
|
|
path::Builder::Tick(now);
|
2018-07-19 04:58:39 +00:00
|
|
|
// publish descriptors
|
2018-07-18 22:50:05 +00:00
|
|
|
if(ShouldPublishDescriptors(now))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
RegenAndPublishIntroSet();
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2019-02-21 19:26:59 +00:00
|
|
|
|
2018-12-13 12:27:14 +00:00
|
|
|
// expire snode sessions
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpireSNodeSessions(now, m_state->m_SNodeSessions);
|
2018-07-23 07:38:29 +00:00
|
|
|
// expire pending tx
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpirePendingTx(now, m_state->m_PendingLookups);
|
2018-08-14 21:17:18 +00:00
|
|
|
// expire pending router lookups
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpirePendingRouterLookups(now, m_state->m_PendingRouters);
|
2018-08-14 21:17:18 +00:00
|
|
|
|
|
|
|
// prefetch addrs
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& addr : m_state->m_PrefetchAddrs)
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(!EndpointUtil::HasPathToService(addr, m_state->m_RemoteSessions))
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
if(!EnsurePathToService(
|
2018-11-07 15:30:22 +00:00
|
|
|
addr,
|
2019-08-02 09:27:27 +00:00
|
|
|
[](ABSL_ATTRIBUTE_UNUSED Address _addr,
|
|
|
|
ABSL_ATTRIBUTE_UNUSED OutboundContext* _ctx) {},
|
2018-11-07 15:30:22 +00:00
|
|
|
10000))
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("failed to ensure path to ", addr);
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 14:19:49 +00:00
|
|
|
#ifdef TESTNET
|
2018-07-19 04:58:39 +00:00
|
|
|
// prefetch tags
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& tag : m_state->m_PrefetchTags)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = m_state->m_PrefetchedTags.find(tag);
|
|
|
|
if(itr == m_state->m_PrefetchedTags.end())
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
itr =
|
|
|
|
m_state->m_PrefetchedTags.emplace(tag, CachedTagResult(tag, this))
|
|
|
|
.first;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
for(const auto& introset : itr->second.result)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
if(HasPendingPathToService(introset.A.Addr()))
|
|
|
|
continue;
|
2019-02-09 15:26:20 +00:00
|
|
|
std::array< byte_t, 128 > tmp = {0};
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2019-07-08 14:22:47 +00:00
|
|
|
if(SendToServiceOrQueue(introset.A.Addr(), buf, eProtocolControl))
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo(Name(), " send message to ", introset.A.Addr(), " for tag ",
|
|
|
|
tag.ToString());
|
2019-02-08 19:43:25 +00:00
|
|
|
else
|
|
|
|
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn(Name(), " failed to send/queue data to ", introset.A.Addr(),
|
|
|
|
" for tag ", tag.ToString());
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
2018-07-18 22:50:05 +00:00
|
|
|
itr->second.Expire(now);
|
|
|
|
if(itr->second.ShouldRefresh(now))
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
auto path = PickRandomEstablishedPath();
|
|
|
|
if(path)
|
|
|
|
{
|
2018-08-14 21:17:18 +00:00
|
|
|
auto job = new TagLookupJob(this, &itr->second);
|
2018-10-21 14:19:49 +00:00
|
|
|
if(!job->SendRequestViaPath(path, Router()))
|
2019-04-21 15:40:32 +00:00
|
|
|
LogError(Name(), " failed to send tag lookup");
|
2018-10-21 14:19:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogError(Name(), " has no paths for tag lookup");
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 14:19:49 +00:00
|
|
|
#endif
|
2018-07-23 07:38:29 +00:00
|
|
|
|
2019-02-05 14:50:33 +00:00
|
|
|
// deregister dead sessions
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::DeregisterDeadSessions(now, m_state->m_DeadSessions);
|
2018-07-23 07:38:29 +00:00
|
|
|
// tick remote sessions
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::TickRemoteSessions(now, m_state->m_RemoteSessions,
|
|
|
|
m_state->m_DeadSessions);
|
2019-02-09 14:37:24 +00:00
|
|
|
// expire convotags
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpireConvoSessions(now, Sessions());
|
2018-09-24 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
Endpoint::Stop()
|
|
|
|
{
|
|
|
|
// stop remote sessions
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::StopRemoteSessions(m_state->m_RemoteSessions);
|
2018-12-24 16:09:05 +00:00
|
|
|
// stop snode sessions
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::StopSnodeSessions(m_state->m_SNodeSessions);
|
2019-04-22 12:25:25 +00:00
|
|
|
if(m_OnDown)
|
|
|
|
m_OnDown->NotifyAsync(NotifyParams());
|
2019-04-21 15:40:32 +00:00
|
|
|
return path::Builder::Stop();
|
2018-12-24 16:09:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
uint64_t
|
|
|
|
Endpoint::GenTXID()
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
uint64_t txid = randint();
|
|
|
|
const auto& lookups = m_state->m_PendingLookups;
|
|
|
|
while(lookups.find(txid) != lookups.end())
|
2018-07-18 03:10:21 +00:00
|
|
|
++txid;
|
|
|
|
return txid;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
std::string
|
|
|
|
Endpoint::Name() const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->m_Name + ":" + m_Identity.pub.Name();
|
2018-07-16 03:32:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
void
|
|
|
|
Endpoint::PutLookup(IServiceLookup* lookup, uint64_t txid)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_PendingLookups.emplace(
|
|
|
|
txid, std::unique_ptr< IServiceLookup >(lookup));
|
2018-08-04 02:59:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
2019-05-03 13:15:03 +00:00
|
|
|
Endpoint::HandleGotIntroMessage(dht::GotIntroMessage_constptr msg)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-18 03:10:21 +00:00
|
|
|
std::set< IntroSet > remote;
|
2019-07-15 09:15:51 +00:00
|
|
|
auto currentPub = m_state->m_CurrentPublishTX;
|
2018-07-11 16:11:19 +00:00
|
|
|
for(const auto& introset : msg->I)
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!introset.Verify(Now()))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(m_Identity.pub == introset.A && currentPub == msg->T)
|
2018-07-18 20:58:16 +00:00
|
|
|
IntroSetPublishFail();
|
2018-09-21 16:17:26 +00:00
|
|
|
return true;
|
2018-07-18 22:50:05 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
if(m_Identity.pub == introset.A && currentPub == msg->T)
|
2018-07-18 22:50:05 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo(
|
2018-07-11 16:11:19 +00:00
|
|
|
"got introset publish confirmation for hidden service endpoint ",
|
2018-07-16 03:32:13 +00:00
|
|
|
Name());
|
2018-07-17 06:17:13 +00:00
|
|
|
IntroSetPublished();
|
2018-07-18 03:10:21 +00:00
|
|
|
return true;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
remote.insert(introset);
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& lookups = m_state->m_PendingLookups;
|
|
|
|
auto itr = lookups.find(msg->T);
|
|
|
|
if(itr == lookups.end())
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("invalid lookup response for hidden service endpoint ", Name(),
|
|
|
|
" txid=", msg->T);
|
2018-07-20 04:50:28 +00:00
|
|
|
return true;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
std::unique_ptr< IServiceLookup > lookup = std::move(itr->second);
|
2019-07-15 09:15:51 +00:00
|
|
|
lookups.erase(itr);
|
2018-08-14 21:17:18 +00:00
|
|
|
lookup->HandleResponse(remote);
|
|
|
|
return true;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 12:49:45 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HasInboundConvo(const Address& addr) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& item : Sessions())
|
2019-06-14 12:49:45 +00:00
|
|
|
{
|
|
|
|
if(item.second.remote.Addr() == addr && item.second.inbound)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
void
|
2019-06-14 12:49:45 +00:00
|
|
|
Endpoint::PutSenderFor(const ConvoTag& tag, const ServiceInfo& info,
|
|
|
|
bool inbound)
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
itr = Sessions().emplace(tag, Session{}).first;
|
2019-07-01 13:44:25 +00:00
|
|
|
itr->second.inbound = inbound;
|
|
|
|
itr->second.remote = info;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2018-10-29 16:48:36 +00:00
|
|
|
itr->second.lastUsed = Now();
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::GetSenderFor(const ConvoTag& tag, ServiceInfo& si) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
return false;
|
|
|
|
si = itr->second.remote;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::PutIntroFor(const ConvoTag& tag, const Introduction& intro)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-06-14 12:49:45 +00:00
|
|
|
return;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
itr->second.intro = intro;
|
2018-10-29 16:48:36 +00:00
|
|
|
itr->second.lastUsed = Now();
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::GetIntroFor(const ConvoTag& tag, Introduction& intro) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
return false;
|
|
|
|
intro = itr->second.intro;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:45:33 +00:00
|
|
|
void
|
|
|
|
Endpoint::PutReplyIntroFor(const ConvoTag& tag, const Introduction& intro)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2019-02-21 16:45:33 +00:00
|
|
|
{
|
2019-06-14 12:49:45 +00:00
|
|
|
return;
|
2019-02-21 16:45:33 +00:00
|
|
|
}
|
|
|
|
itr->second.replyIntro = intro;
|
|
|
|
itr->second.lastUsed = Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::GetReplyIntroFor(const ConvoTag& tag, Introduction& intro) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2019-02-21 16:45:33 +00:00
|
|
|
return false;
|
|
|
|
intro = itr->second.replyIntro;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
bool
|
2019-06-14 12:49:45 +00:00
|
|
|
Endpoint::GetConvoTagsForService(const Address& addr,
|
2018-08-09 19:02:17 +00:00
|
|
|
std::set< ConvoTag >& tags) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return EndpointUtil::GetConvoTagsForService(Sessions(), addr, tags);
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::GetCachedSessionKeyFor(const ConvoTag& tag,
|
2019-01-02 01:04:04 +00:00
|
|
|
SharedSecret& secret) const
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
return false;
|
2019-01-02 01:04:04 +00:00
|
|
|
secret = itr->second.sharedKey;
|
2018-08-09 19:02:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::PutCachedSessionKeyFor(const ConvoTag& tag, const SharedSecret& k)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
itr = Sessions().emplace(tag, Session{}).first;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
itr->second.sharedKey = k;
|
2018-10-29 16:48:36 +00:00
|
|
|
itr->second.lastUsed = Now();
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:28:12 +00:00
|
|
|
void
|
|
|
|
Endpoint::MarkConvoTagActive(const ConvoTag& tag)
|
|
|
|
{
|
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr != Sessions().end())
|
|
|
|
{
|
|
|
|
itr->second.lastUsed = Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
2019-01-16 21:12:24 +00:00
|
|
|
Endpoint::LoadKeyFile()
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
const auto& keyfile = m_state->m_Keyfile;
|
|
|
|
if(!keyfile.empty())
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(!m_Identity.EnsureKeys(keyfile))
|
2019-01-16 21:12:24 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
LogError("Can't ensure keyfile [", keyfile, "]");
|
2018-07-11 16:11:19 +00:00
|
|
|
return false;
|
2019-01-16 21:12:24 +00:00
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
m_Identity.RegenerateKeys();
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2019-01-16 21:12:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::Start()
|
|
|
|
{
|
2019-01-18 02:28:30 +00:00
|
|
|
// how can I tell if a m_Identity isn't loaded?
|
2018-08-09 19:02:17 +00:00
|
|
|
if(!m_DataHandler)
|
|
|
|
{
|
|
|
|
m_DataHandler = this;
|
|
|
|
}
|
2018-08-16 14:34:15 +00:00
|
|
|
// this does network isolation
|
2019-07-15 09:15:51 +00:00
|
|
|
while(m_state->m_OnInit.size())
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(m_state->m_OnInit.front()())
|
|
|
|
m_state->m_OnInit.pop_front();
|
2018-08-09 19:02:17 +00:00
|
|
|
else
|
2019-01-16 21:12:24 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("Can't call init of network isolation");
|
2018-08-09 19:02:17 +00:00
|
|
|
return false;
|
2019-01-16 21:12:24 +00:00
|
|
|
}
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Endpoint::~Endpoint()
|
|
|
|
{
|
2019-04-22 12:25:25 +00:00
|
|
|
if(m_OnUp)
|
|
|
|
m_OnUp->Stop();
|
|
|
|
if(m_OnDown)
|
|
|
|
m_OnDown->Stop();
|
|
|
|
if(m_OnReady)
|
|
|
|
m_OnReady->Stop();
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
bool
|
2019-02-11 19:45:42 +00:00
|
|
|
Endpoint::PublishIntroSet(AbstractRouter* r)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2018-09-24 10:23:11 +00:00
|
|
|
// publish via near router
|
2019-01-02 01:03:53 +00:00
|
|
|
RouterID location = m_Identity.pub.Addr().as_array();
|
2018-09-24 14:31:58 +00:00
|
|
|
auto path = GetEstablishedPathClosestTo(location);
|
2018-10-23 17:15:22 +00:00
|
|
|
return path && PublishIntroSetVia(r, path);
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 14:48:06 +00:00
|
|
|
struct PublishIntroSetJob : public IServiceLookup
|
|
|
|
{
|
|
|
|
IntroSet m_IntroSet;
|
|
|
|
Endpoint* m_Endpoint;
|
2019-07-30 23:42:13 +00:00
|
|
|
PublishIntroSetJob(Endpoint* parent, uint64_t id, IntroSet introset)
|
2018-09-18 14:48:06 +00:00
|
|
|
: IServiceLookup(parent, id, "PublishIntroSet")
|
2019-07-30 23:42:13 +00:00
|
|
|
, m_IntroSet(std::move(introset))
|
2018-09-18 14:48:06 +00:00
|
|
|
, m_Endpoint(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
std::shared_ptr< routing::IMessage >
|
2019-07-30 23:42:13 +00:00
|
|
|
BuildRequestMessage() override
|
2018-09-18 14:48:06 +00:00
|
|
|
{
|
2019-04-23 14:28:59 +00:00
|
|
|
auto msg = std::make_shared< routing::DHTMessage >();
|
2019-04-22 17:38:29 +00:00
|
|
|
msg->M.emplace_back(
|
2019-08-07 16:33:29 +00:00
|
|
|
std::make_unique< dht::PublishIntroMessage >(m_IntroSet, txid, 5));
|
2018-09-18 14:48:06 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-30 23:42:13 +00:00
|
|
|
HandleResponse(const std::set< IntroSet >& response) override
|
2018-09-18 14:48:06 +00:00
|
|
|
{
|
|
|
|
if(response.size())
|
|
|
|
m_Endpoint->IntroSetPublished();
|
|
|
|
else
|
|
|
|
m_Endpoint->IntroSetPublishFail();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
void
|
|
|
|
Endpoint::IntroSetPublishFail()
|
|
|
|
{
|
2019-02-21 19:55:31 +00:00
|
|
|
auto now = Now();
|
|
|
|
if(ShouldPublishDescriptors(now))
|
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
RegenAndPublishIntroSet();
|
2019-02-21 19:55:31 +00:00
|
|
|
}
|
2019-04-21 15:40:32 +00:00
|
|
|
else if(NumInStatus(path::ePathEstablished) < 3)
|
2019-02-21 19:55:31 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
if(introSet().HasExpiredIntros(now))
|
2019-02-21 19:55:31 +00:00
|
|
|
ManualRebuild(1);
|
|
|
|
}
|
2018-09-18 14:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-04-23 14:28:59 +00:00
|
|
|
Endpoint::PublishIntroSetVia(AbstractRouter* r, path::Path_ptr path)
|
2018-09-18 14:48:06 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto job = new PublishIntroSetJob(this, GenTXID(), introSet());
|
2018-09-18 14:48:06 +00:00
|
|
|
if(job->SendRequestViaPath(path, r))
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_LastPublishAttempt = Now();
|
2018-09-18 14:48:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 17:46:38 +00:00
|
|
|
void
|
|
|
|
Endpoint::ResetInternalState()
|
|
|
|
{
|
|
|
|
path::Builder::ResetInternalState();
|
2019-07-01 14:56:56 +00:00
|
|
|
static auto resetState = [](auto& container, auto getter) {
|
|
|
|
std::for_each(container.begin(), container.end(), [getter](auto& item) {
|
|
|
|
getter(item)->ResetInternalState();
|
|
|
|
});
|
2019-05-07 17:46:38 +00:00
|
|
|
};
|
2019-07-15 09:15:51 +00:00
|
|
|
resetState(m_state->m_RemoteSessions,
|
2019-07-01 14:56:56 +00:00
|
|
|
[](const auto& item) { return item.second; });
|
2019-07-15 09:15:51 +00:00
|
|
|
resetState(m_state->m_SNodeSessions,
|
2019-07-01 14:56:56 +00:00
|
|
|
[](const auto& item) { return item.second.first; });
|
2019-05-07 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
bool
|
2018-07-18 22:50:05 +00:00
|
|
|
Endpoint::ShouldPublishDescriptors(llarp_time_t now) const
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2019-04-01 19:56:11 +00:00
|
|
|
// make sure we have all paths that are established
|
|
|
|
// in our introset
|
2019-06-17 13:05:37 +00:00
|
|
|
size_t numNotInIntroset = 0;
|
2019-04-23 16:13:22 +00:00
|
|
|
ForEachPath([&](const path::Path_ptr& p) {
|
2019-04-05 14:58:22 +00:00
|
|
|
if(!p->IsReady())
|
|
|
|
return;
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& i : introSet().I)
|
2019-04-05 14:58:22 +00:00
|
|
|
{
|
|
|
|
if(i == p->intro)
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 12:43:16 +00:00
|
|
|
++numNotInIntroset;
|
2019-04-05 14:58:22 +00:00
|
|
|
});
|
2019-07-15 09:15:51 +00:00
|
|
|
|
2019-11-05 16:58:53 +00:00
|
|
|
const auto lastpub = m_state->m_LastPublishAttempt;
|
2019-07-15 09:15:51 +00:00
|
|
|
if(m_state->m_IntroSet.HasExpiredIntros(now) || numNotInIntroset > 1)
|
|
|
|
{
|
|
|
|
return now - lastpub >= INTROSET_PUBLISH_RETRY_INTERVAL;
|
|
|
|
}
|
|
|
|
return now - lastpub >= INTROSET_PUBLISH_INTERVAL;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::IntroSetPublished()
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_LastPublish = Now();
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo(Name(), " IntroSet publish confirmed");
|
2019-04-22 12:25:25 +00:00
|
|
|
if(m_OnReady)
|
|
|
|
m_OnReady->NotifyAsync(NotifyParams());
|
|
|
|
m_OnReady = nullptr;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
2019-05-22 16:20:03 +00:00
|
|
|
Endpoint::IsolatedNetworkMainLoop()
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_IsolatedNetLoop = llarp_make_ev_loop();
|
|
|
|
m_state->m_IsolatedLogic = std::make_shared< llarp::Logic >();
|
2019-05-22 16:20:03 +00:00
|
|
|
if(SetupNetworking())
|
2019-07-15 09:15:51 +00:00
|
|
|
llarp_ev_loop_run_single_process(m_state->m_IsolatedNetLoop,
|
|
|
|
m_state->m_IsolatedLogic);
|
2019-05-22 16:20:03 +00:00
|
|
|
else
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_IsolatedNetLoop.reset();
|
|
|
|
m_state->m_IsolatedLogic.reset();
|
2019-05-22 16:20:03 +00:00
|
|
|
}
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:19:33 +00:00
|
|
|
bool
|
|
|
|
Endpoint::SelectHop(llarp_nodedb* db, const std::set< RouterID >& prev,
|
|
|
|
RouterContact& cur, size_t hop, path::PathRole roles)
|
|
|
|
|
|
|
|
{
|
|
|
|
std::set< RouterID > exclude = prev;
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& snode : SnodeBlacklist())
|
2019-05-10 16:19:33 +00:00
|
|
|
exclude.insert(snode);
|
2019-09-03 15:56:56 +00:00
|
|
|
if(hop == 0)
|
|
|
|
{
|
|
|
|
const auto exits = GetExitRouters();
|
|
|
|
// exclude exit node as first hop in any paths
|
|
|
|
exclude.insert(exits.begin(), exits.end());
|
|
|
|
}
|
2019-05-10 16:19:33 +00:00
|
|
|
return path::Builder::SelectHop(db, exclude, cur, hop, roles);
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:56:56 +00:00
|
|
|
std::set< RouterID >
|
|
|
|
Endpoint::GetExitRouters() const
|
|
|
|
{
|
|
|
|
return m_ExitMap.TransformValues< RouterID >(
|
|
|
|
[](const exit::BaseSession_ptr& ptr) -> RouterID {
|
|
|
|
return ptr->Endpoint();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-18 11:49:54 +00:00
|
|
|
bool
|
|
|
|
Endpoint::ShouldBundleRC() const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->m_BundleRC;
|
2019-04-18 11:49:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
void
|
2019-04-21 15:40:32 +00:00
|
|
|
Endpoint::PutNewOutboundContext(const service::IntroSet& introset)
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
|
|
|
Address addr;
|
2019-01-02 01:03:53 +00:00
|
|
|
introset.A.CalculateAddress(addr.as_array());
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& remoteSessions = m_state->m_RemoteSessions;
|
|
|
|
auto& serviceLookups = m_state->m_PendingServiceLookups;
|
|
|
|
|
|
|
|
if(remoteSessions.count(addr) >= MAX_OUTBOUND_CONTEXT_COUNT)
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = remoteSessions.find(addr);
|
2018-09-28 12:22:50 +00:00
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
auto range = serviceLookups.equal_range(addr);
|
2019-02-06 15:05:25 +00:00
|
|
|
auto i = range.first;
|
|
|
|
if(i != range.second)
|
2018-09-28 12:22:50 +00:00
|
|
|
{
|
2019-02-06 15:05:25 +00:00
|
|
|
i->second(addr, itr->second.get());
|
|
|
|
++i;
|
2018-09-28 12:22:50 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
serviceLookups.erase(addr);
|
2018-09-28 12:22:50 +00:00
|
|
|
return;
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
auto it = remoteSessions.emplace(
|
2019-04-23 16:13:22 +00:00
|
|
|
addr, std::make_shared< OutboundContext >(introset, this));
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo("Created New outbound context for ", addr.ToString());
|
2018-09-28 12:22:50 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
// inform pending
|
2019-07-15 09:15:51 +00:00
|
|
|
auto range = serviceLookups.equal_range(addr);
|
2019-02-06 15:05:25 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
if(itr != range.second)
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2019-04-22 17:38:29 +00:00
|
|
|
itr->second(addr, it->second.get());
|
2019-02-06 15:05:25 +00:00
|
|
|
++itr;
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
serviceLookups.erase(addr);
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 20:28:56 +00:00
|
|
|
void
|
|
|
|
Endpoint::HandleVerifyGotRouter(dht::GotRouterMessage_constptr msg,
|
|
|
|
llarp_async_verify_rc* j)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& pendingRouters = m_state->m_PendingRouters;
|
|
|
|
auto itr = pendingRouters.find(msg->R[0].pubkey);
|
|
|
|
if(itr != pendingRouters.end())
|
2019-05-09 20:28:56 +00:00
|
|
|
{
|
|
|
|
if(j->valid)
|
|
|
|
itr->second.InformResult(msg->R);
|
|
|
|
else
|
|
|
|
itr->second.InformResult({});
|
2019-07-15 09:15:51 +00:00
|
|
|
pendingRouters.erase(itr);
|
2019-05-09 20:28:56 +00:00
|
|
|
}
|
|
|
|
delete j;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
bool
|
2019-05-03 13:15:03 +00:00
|
|
|
Endpoint::HandleGotRouterMessage(dht::GotRouterMessage_constptr msg)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2019-05-09 15:51:21 +00:00
|
|
|
if(msg->R.size())
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* job = new llarp_async_verify_rc;
|
|
|
|
job->nodedb = Router()->nodedb();
|
|
|
|
job->cryptoworker = Router()->threadpool();
|
|
|
|
job->diskworker = Router()->diskworker();
|
|
|
|
job->logic = Router()->logic();
|
2019-05-09 20:28:56 +00:00
|
|
|
job->hook = std::bind(&Endpoint::HandleVerifyGotRouter, this, msg,
|
|
|
|
std::placeholders::_1);
|
|
|
|
job->rc = msg->R[0];
|
2018-08-10 21:34:11 +00:00
|
|
|
llarp_nodedb_async_verify(job);
|
2019-05-03 13:15:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& routers = m_state->m_PendingRouters;
|
|
|
|
auto itr = routers.begin();
|
|
|
|
while(itr != routers.end())
|
2019-05-09 15:51:21 +00:00
|
|
|
{
|
|
|
|
if(itr->second.txid == msg->txid)
|
|
|
|
{
|
|
|
|
itr->second.InformResult({});
|
2019-07-15 09:15:51 +00:00
|
|
|
itr = routers.erase(itr);
|
2019-05-09 15:51:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2019-04-17 14:46:00 +00:00
|
|
|
return true;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::EnsureRouterIsKnown(const RouterID& router)
|
|
|
|
{
|
2018-08-14 22:07:58 +00:00
|
|
|
if(router.IsZero())
|
|
|
|
return;
|
2019-07-15 09:15:51 +00:00
|
|
|
if(!Router()->nodedb()->Has(router))
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2019-05-03 13:15:03 +00:00
|
|
|
LookupRouterAnon(router, nullptr);
|
2018-12-19 17:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2018-12-19 17:48:29 +00:00
|
|
|
bool
|
2019-05-03 13:15:03 +00:00
|
|
|
Endpoint::LookupRouterAnon(RouterID router, RouterLookupHandler handler)
|
2018-12-19 17:48:29 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& routers = m_state->m_PendingRouters;
|
|
|
|
if(routers.find(router) == routers.end())
|
2018-12-19 17:48:29 +00:00
|
|
|
{
|
|
|
|
auto path = GetEstablishedPathClosestTo(router);
|
|
|
|
routing::DHTMessage msg;
|
|
|
|
auto txid = GenTXID();
|
2019-04-22 17:38:29 +00:00
|
|
|
msg.M.emplace_back(
|
|
|
|
std::make_unique< dht::FindRouterMessage >(txid, router));
|
2018-12-19 17:48:29 +00:00
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
if(path && path->SendRoutingMessage(msg, Router()))
|
2018-12-19 17:48:29 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
routers.emplace(router, RouterLookupJob(this, handler));
|
2018-12-19 17:48:29 +00:00
|
|
|
return true;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 17:48:29 +00:00
|
|
|
return false;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
void
|
2019-04-23 14:28:59 +00:00
|
|
|
Endpoint::HandlePathBuilt(path::Path_ptr p)
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2019-06-02 21:19:10 +00:00
|
|
|
p->SetDataHandler(util::memFn(&Endpoint::HandleHiddenServiceFrame, this));
|
|
|
|
p->SetDropHandler(util::memFn(&Endpoint::HandleDataDrop, this));
|
|
|
|
p->SetDeadChecker(util::memFn(&Endpoint::CheckPathIsDead, this));
|
2018-09-26 13:04:25 +00:00
|
|
|
path::Builder::HandlePathBuilt(p);
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 15:28:36 +00:00
|
|
|
bool
|
2019-04-23 16:13:22 +00:00
|
|
|
Endpoint::HandleDataDrop(path::Path_ptr p, const PathID_t& dst,
|
|
|
|
uint64_t seq)
|
2018-09-11 15:28:36 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn(Name(), " message ", seq, " dropped by endpoint ", p->Endpoint(),
|
|
|
|
" via ", dst);
|
2018-09-11 15:28:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
std::unordered_map< std::string, std::string >
|
|
|
|
Endpoint::NotifyParams() const
|
|
|
|
{
|
|
|
|
return {{"LOKINET_ADDR", m_Identity.pub.Addr().ToString()}};
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:08:47 +00:00
|
|
|
bool
|
2019-06-28 14:12:20 +00:00
|
|
|
Endpoint::HandleDataMessage(path::Path_ptr path, const PathID_t from,
|
2019-05-03 13:15:03 +00:00
|
|
|
std::shared_ptr< ProtocolMessage > msg)
|
2018-09-18 11:08:47 +00:00
|
|
|
{
|
2019-06-06 10:52:27 +00:00
|
|
|
msg->sender.UpdateAddr();
|
2019-06-14 12:49:45 +00:00
|
|
|
PutSenderFor(msg->tag, msg->sender, true);
|
2019-06-28 14:12:20 +00:00
|
|
|
PutReplyIntroFor(msg->tag, path->intro);
|
|
|
|
Introduction intro;
|
2019-07-01 13:44:25 +00:00
|
|
|
intro.pathID = from;
|
|
|
|
intro.router = PubKey(path->Endpoint());
|
2019-06-28 14:48:00 +00:00
|
|
|
intro.expiresAt = std::min(path->ExpireTime(), msg->introReply.expiresAt);
|
2019-06-28 14:12:20 +00:00
|
|
|
PutIntroFor(msg->tag, intro);
|
2018-09-18 17:48:26 +00:00
|
|
|
return ProcessDataMessage(msg);
|
2018-09-18 11:08:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 14:01:13 +00:00
|
|
|
bool
|
2019-07-01 13:44:25 +00:00
|
|
|
Endpoint::HasPathToSNode(const RouterID ident) const
|
2018-11-29 14:01:13 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto range = m_state->m_SNodeSessions.equal_range(ident);
|
2018-12-02 18:07:07 +00:00
|
|
|
auto itr = range.first;
|
2018-11-29 14:01:13 +00:00
|
|
|
while(itr != range.second)
|
2018-12-02 18:07:07 +00:00
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
if(itr->second.first->IsReady())
|
2018-11-29 14:01:13 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-29 13:12:35 +00:00
|
|
|
bool
|
2019-05-03 13:15:03 +00:00
|
|
|
Endpoint::ProcessDataMessage(std::shared_ptr< ProtocolMessage > msg)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
if(msg->proto == eProtocolTrafficV4 || msg->proto == eProtocolTrafficV6)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
util::Lock l(&m_state->m_InboundTrafficQueueMutex);
|
|
|
|
m_state->m_InboundTrafficQueue.emplace(msg);
|
2019-05-22 17:47:33 +00:00
|
|
|
return true;
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
if(msg->proto == eProtocolControl)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
|
|
|
// TODO: implement me (?)
|
2019-02-09 15:26:20 +00:00
|
|
|
// right now it's just random noise
|
2018-11-29 13:12:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:00:45 +00:00
|
|
|
void
|
|
|
|
Endpoint::RemoveConvoTag(const ConvoTag& t)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
Sessions().erase(t);
|
2019-03-08 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
2019-04-23 14:28:59 +00:00
|
|
|
Endpoint::HandleHiddenServiceFrame(path::Path_ptr p,
|
2019-04-22 17:38:29 +00:00
|
|
|
const ProtocolFrame& frame)
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2019-04-22 17:38:29 +00:00
|
|
|
if(frame.R)
|
2019-03-08 16:00:45 +00:00
|
|
|
{
|
|
|
|
// handle discard
|
|
|
|
ServiceInfo si;
|
2019-04-22 17:38:29 +00:00
|
|
|
if(!GetSenderFor(frame.T, si))
|
2019-03-08 16:00:45 +00:00
|
|
|
return false;
|
|
|
|
// verify source
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!frame.Verify(si))
|
2019-03-08 16:00:45 +00:00
|
|
|
return false;
|
|
|
|
// remove convotag it doesn't exist
|
2019-04-22 17:38:29 +00:00
|
|
|
LogWarn("remove convotag T=", frame.T);
|
|
|
|
RemoveConvoTag(frame.T);
|
2019-03-08 16:00:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!frame.AsyncDecryptAndVerify(EndpointLogic(), p, CryptoWorker(),
|
|
|
|
m_Identity, m_DataHandler))
|
2019-03-08 16:00:45 +00:00
|
|
|
{
|
|
|
|
// send discard
|
|
|
|
ProtocolFrame f;
|
|
|
|
f.R = 1;
|
2019-04-22 17:38:29 +00:00
|
|
|
f.T = frame.T;
|
2019-03-08 16:00:45 +00:00
|
|
|
f.F = p->intro.pathID;
|
2019-04-28 17:33:27 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!f.Sign(m_Identity))
|
2019-03-08 16:00:45 +00:00
|
|
|
return false;
|
2019-05-02 16:23:31 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
util::Lock lock(&m_state->m_SendQueueMutex);
|
|
|
|
m_state->m_SendQueue.emplace_back(
|
2019-05-02 16:23:31 +00:00
|
|
|
std::make_shared< const routing::PathTransferMessage >(f,
|
|
|
|
frame.F),
|
|
|
|
p);
|
|
|
|
}
|
2019-04-23 14:28:59 +00:00
|
|
|
return true;
|
2019-03-08 16:00:45 +00:00
|
|
|
}
|
|
|
|
return true;
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
void Endpoint::HandlePathDied(path::Path_ptr)
|
2018-09-17 15:32:37 +00:00
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
RegenAndPublishIntroSet(true);
|
2019-03-30 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 12:27:28 +00:00
|
|
|
bool
|
2019-04-23 14:28:59 +00:00
|
|
|
Endpoint::CheckPathIsDead(path::Path_ptr, llarp_time_t dlt)
|
2018-09-13 12:27:28 +00:00
|
|
|
{
|
2019-04-05 14:58:22 +00:00
|
|
|
return dlt > path::alive_timeout;
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
bool
|
2018-10-15 15:43:41 +00:00
|
|
|
Endpoint::OnLookup(const Address& addr, const IntroSet* introset,
|
|
|
|
const RouterID& endpoint)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2019-08-02 09:27:27 +00:00
|
|
|
const auto now = Router()->Now();
|
2019-07-29 15:10:20 +00:00
|
|
|
auto& fails = m_state->m_ServiceLookupFails;
|
|
|
|
auto& lookups = m_state->m_PendingServiceLookups;
|
2018-10-10 21:31:03 +00:00
|
|
|
if(introset == nullptr || introset->IsExpired(now))
|
2018-09-10 16:36:36 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogError(Name(), " failed to lookup ", addr.ToString(), " from ",
|
|
|
|
endpoint);
|
2019-07-15 09:15:51 +00:00
|
|
|
fails[endpoint] = fails[endpoint] + 1;
|
2019-07-29 15:10:20 +00:00
|
|
|
// inform all
|
|
|
|
auto range = lookups.equal_range(addr);
|
|
|
|
auto itr = range.first;
|
|
|
|
if(itr != range.second)
|
2018-09-10 16:36:36 +00:00
|
|
|
{
|
2019-02-06 15:05:25 +00:00
|
|
|
itr->second(addr, nullptr);
|
2019-07-29 15:10:20 +00:00
|
|
|
itr = lookups.erase(itr);
|
2018-09-10 16:36:36 +00:00
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
return false;
|
2018-09-10 16:36:36 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
PutNewOutboundContext(*introset);
|
2018-08-10 21:34:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2019-07-01 13:44:25 +00:00
|
|
|
Endpoint::EnsurePathToService(const Address remote, PathEnsureHook hook,
|
2019-04-30 01:06:20 +00:00
|
|
|
ABSL_ATTRIBUTE_UNUSED llarp_time_t timeoutMS,
|
2018-11-07 15:30:22 +00:00
|
|
|
bool randomPath)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2019-04-23 14:28:59 +00:00
|
|
|
path::Path_ptr path = nullptr;
|
2018-10-15 21:20:05 +00:00
|
|
|
if(randomPath)
|
|
|
|
path = PickRandomEstablishedPath();
|
|
|
|
else
|
|
|
|
path = GetEstablishedPathClosestTo(remote.ToRouter());
|
2018-07-23 07:38:29 +00:00
|
|
|
if(!path)
|
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn("No outbound path for lookup yet");
|
2018-12-02 15:26:26 +00:00
|
|
|
BuildOne();
|
2018-07-23 07:38:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo(Name(), " Ensure Path to ", remote.ToString());
|
2019-07-15 09:15:51 +00:00
|
|
|
|
|
|
|
auto& sessions = m_state->m_RemoteSessions;
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = sessions.find(remote);
|
|
|
|
if(itr != sessions.end())
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
hook(itr->first, itr->second.get());
|
2018-07-22 23:14:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 15:52:00 +00:00
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& lookups = m_state->m_PendingServiceLookups;
|
|
|
|
|
|
|
|
if(lookups.count(remote) >= MaxConcurrentLookups)
|
2019-02-06 15:52:00 +00:00
|
|
|
{
|
2019-04-21 15:40:32 +00:00
|
|
|
LogWarn(Name(), " has too many pending service lookups for ",
|
|
|
|
remote.ToString());
|
2019-02-06 15:52:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-22 17:38:29 +00:00
|
|
|
using namespace std::placeholders;
|
2018-08-10 21:34:11 +00:00
|
|
|
HiddenServiceAddressLookup* job = new HiddenServiceAddressLookup(
|
2019-06-02 21:19:10 +00:00
|
|
|
this, util::memFn(&Endpoint::OnLookup, this), remote, GenTXID());
|
2019-04-21 15:40:32 +00:00
|
|
|
LogInfo("doing lookup for ", remote, " via ", path->Endpoint());
|
2018-08-10 21:34:11 +00:00
|
|
|
if(job->SendRequestViaPath(path, Router()))
|
2019-02-06 15:05:25 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
lookups.emplace(remote, hook);
|
2018-08-10 21:34:11 +00:00
|
|
|
return true;
|
2019-02-06 15:05:25 +00:00
|
|
|
}
|
2019-04-21 15:40:32 +00:00
|
|
|
LogError("send via path failed");
|
2018-08-10 21:34:11 +00:00
|
|
|
return false;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 13:12:35 +00:00
|
|
|
void
|
2019-07-01 13:44:25 +00:00
|
|
|
Endpoint::EnsurePathToSNode(const RouterID snode, SNodeEnsureHook h)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& nodeSessions = m_state->m_SNodeSessions;
|
2019-04-22 17:38:29 +00:00
|
|
|
using namespace std::placeholders;
|
2019-07-15 09:15:51 +00:00
|
|
|
if(nodeSessions.count(snode) == 0)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
ConvoTag tag;
|
|
|
|
// TODO: check for collision lol no we don't but maybe we will...
|
|
|
|
// some day :DDDDD
|
|
|
|
tag.Randomize();
|
2019-04-23 16:13:22 +00:00
|
|
|
auto session = std::make_shared< exit::SNodeSession >(
|
2018-12-02 18:07:07 +00:00
|
|
|
snode,
|
2019-07-01 13:44:25 +00:00
|
|
|
[=](const llarp_buffer_t& pkt) -> bool {
|
2019-07-01 14:56:56 +00:00
|
|
|
/// TODO: V6
|
|
|
|
return HandleInboundPacket(tag, pkt, eProtocolTrafficV4);
|
2019-07-01 13:44:25 +00:00
|
|
|
},
|
2019-07-18 16:28:17 +00:00
|
|
|
Router(), numPaths, numHops, false, ShouldBundleRC());
|
2019-07-15 09:15:51 +00:00
|
|
|
|
|
|
|
m_state->m_SNodeSessions.emplace(snode, std::make_pair(session, tag));
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
2019-04-30 21:36:27 +00:00
|
|
|
EnsureRouterIsKnown(snode);
|
2019-07-15 09:15:51 +00:00
|
|
|
auto range = nodeSessions.equal_range(snode);
|
2019-03-01 19:10:42 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while(itr != range.second)
|
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
if(itr->second.first->IsReady())
|
|
|
|
h(snode, itr->second.first);
|
2019-03-07 15:17:29 +00:00
|
|
|
else
|
2019-04-30 21:36:27 +00:00
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
itr->second.first->AddReadyHook(std::bind(h, snode, _1));
|
|
|
|
itr->second.first->BuildOne();
|
2019-04-30 21:36:27 +00:00
|
|
|
}
|
2019-03-01 19:10:42 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-02 23:12:42 +00:00
|
|
|
Endpoint::SendToSNodeOrQueue(const RouterID& addr,
|
|
|
|
const llarp_buffer_t& buf)
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
auto pkt = std::make_shared< net::IPPacket >();
|
2019-04-30 21:36:27 +00:00
|
|
|
if(!pkt->Load(buf))
|
2018-11-29 13:12:35 +00:00
|
|
|
return false;
|
2019-04-30 21:36:27 +00:00
|
|
|
EnsurePathToSNode(addr, [pkt](RouterID, exit::BaseSession_ptr s) {
|
|
|
|
if(s)
|
|
|
|
s->QueueUpstreamTraffic(*pkt, routing::ExitPadSize);
|
|
|
|
});
|
|
|
|
return true;
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:07:17 +00:00
|
|
|
void Endpoint::Pump(llarp_time_t)
|
2019-04-25 17:15:56 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
const auto& sessions = m_state->m_SNodeSessions;
|
|
|
|
auto& queue = m_state->m_InboundTrafficQueue;
|
2019-11-14 21:56:01 +00:00
|
|
|
|
|
|
|
LogicCall(EndpointLogic(), [&]() {
|
2019-05-22 16:20:50 +00:00
|
|
|
// send downstream packets to user for snode
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& item : sessions)
|
2019-07-01 14:56:56 +00:00
|
|
|
item.second.first->FlushDownstream();
|
2019-05-28 19:45:08 +00:00
|
|
|
// send downstream traffic to user for hidden service
|
2019-07-15 09:15:51 +00:00
|
|
|
util::Lock lock(&m_state->m_InboundTrafficQueueMutex);
|
|
|
|
while(queue.size())
|
2019-05-22 16:20:03 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
const auto& msg = queue.top();
|
2019-07-18 16:28:17 +00:00
|
|
|
const llarp_buffer_t buf(msg->payload);
|
2019-07-01 14:56:56 +00:00
|
|
|
HandleInboundPacket(msg->tag, buf, msg->proto);
|
2019-07-15 09:15:51 +00:00
|
|
|
queue.pop();
|
2019-05-22 16:20:03 +00:00
|
|
|
}
|
2019-04-30 13:56:39 +00:00
|
|
|
});
|
2019-04-30 16:07:17 +00:00
|
|
|
|
|
|
|
auto router = Router();
|
2019-05-22 16:20:03 +00:00
|
|
|
// TODO: locking on this container
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& item : m_state->m_RemoteSessions)
|
2019-04-30 16:07:17 +00:00
|
|
|
item.second->FlushUpstream();
|
2019-05-22 16:20:03 +00:00
|
|
|
// TODO: locking on this container
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& item : sessions)
|
2019-07-01 14:56:56 +00:00
|
|
|
item.second.first->FlushUpstream();
|
2019-07-15 09:15:51 +00:00
|
|
|
util::Lock lock(&m_state->m_SendQueueMutex);
|
2019-05-22 16:20:03 +00:00
|
|
|
// send outbound traffic
|
2019-07-15 09:15:51 +00:00
|
|
|
for(const auto& item : m_state->m_SendQueue)
|
2019-09-19 20:28:12 +00:00
|
|
|
{
|
2019-04-30 16:07:17 +00:00
|
|
|
item.second->SendRoutingMessage(*item.first, router);
|
2019-09-19 20:28:12 +00:00
|
|
|
MarkConvoTagActive(item.first->T.T);
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
m_state->m_SendQueue.clear();
|
2019-09-16 16:12:05 +00:00
|
|
|
router->PumpLL();
|
2019-04-25 17:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 14:56:56 +00:00
|
|
|
bool
|
2019-07-15 09:15:51 +00:00
|
|
|
Endpoint::EnsureConvo(ABSL_ATTRIBUTE_UNUSED const AlignedBuffer< 32 > addr,
|
|
|
|
bool snode,
|
|
|
|
ABSL_ATTRIBUTE_UNUSED ConvoEventListener_ptr ev)
|
2019-07-01 14:56:56 +00:00
|
|
|
{
|
|
|
|
if(snode)
|
|
|
|
{
|
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
|
|
|
|
// TODO: something meaningful
|
|
|
|
return false;
|
2019-07-01 14:56:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
bool
|
2019-06-06 10:52:27 +00:00
|
|
|
Endpoint::SendToServiceOrQueue(const service::Address& remote,
|
2019-02-01 01:58:06 +00:00
|
|
|
const llarp_buffer_t& data, ProtocolType t)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2018-09-28 12:22:50 +00:00
|
|
|
// inbound converstation
|
2019-07-18 16:28:17 +00:00
|
|
|
const auto now = Now();
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2019-06-14 12:49:45 +00:00
|
|
|
if(HasInboundConvo(remote))
|
2018-09-14 13:43:42 +00:00
|
|
|
{
|
2019-06-14 12:49:45 +00:00
|
|
|
auto transfer = std::make_shared< routing::PathTransferMessage >();
|
|
|
|
ProtocolFrame& f = transfer->T;
|
|
|
|
std::shared_ptr< path::Path > p;
|
|
|
|
std::set< ConvoTag > tags;
|
|
|
|
if(GetConvoTagsForService(remote, tags))
|
2018-09-14 13:43:42 +00:00
|
|
|
{
|
2019-06-28 14:12:20 +00:00
|
|
|
// the remote guy's intro
|
2019-06-14 12:49:45 +00:00
|
|
|
Introduction remoteIntro;
|
2019-06-28 14:12:20 +00:00
|
|
|
Introduction replyPath;
|
2019-06-14 12:49:45 +00:00
|
|
|
SharedSecret K;
|
|
|
|
// pick tag
|
|
|
|
for(const auto& tag : tags)
|
2018-09-14 13:43:42 +00:00
|
|
|
{
|
2019-06-14 12:49:45 +00:00
|
|
|
if(tag.IsZero())
|
|
|
|
continue;
|
|
|
|
if(!GetCachedSessionKeyFor(tag, K))
|
|
|
|
continue;
|
2019-06-28 14:12:20 +00:00
|
|
|
if(!GetReplyIntroFor(tag, replyPath))
|
|
|
|
continue;
|
|
|
|
if(!GetIntroFor(tag, remoteIntro))
|
|
|
|
continue;
|
|
|
|
// get path for intro
|
|
|
|
ForEachPath([&](path::Path_ptr path) {
|
2019-07-01 21:35:49 +00:00
|
|
|
if(path->intro == replyPath)
|
|
|
|
{
|
|
|
|
p = path;
|
2019-07-01 20:45:00 +00:00
|
|
|
return;
|
2019-07-01 21:35:49 +00:00
|
|
|
}
|
|
|
|
if(p && p->ExpiresSoon(now) && path->IsReady()
|
|
|
|
&& path->intro.router == replyPath.router)
|
|
|
|
{
|
2019-06-28 14:12:20 +00:00
|
|
|
p = path;
|
2019-07-01 21:35:49 +00:00
|
|
|
}
|
2019-06-28 14:12:20 +00:00
|
|
|
});
|
|
|
|
if(p)
|
2018-09-14 13:43:42 +00:00
|
|
|
{
|
2019-06-28 14:12:20 +00:00
|
|
|
f.T = tag;
|
2018-09-14 13:43:42 +00:00
|
|
|
}
|
2019-06-14 12:49:45 +00:00
|
|
|
}
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
// TODO: check expiration of our end
|
2019-09-19 14:41:31 +00:00
|
|
|
auto m = std::make_shared< ProtocolMessage >(f.T);
|
|
|
|
m->PutBuffer(data);
|
2019-06-14 12:49:45 +00:00
|
|
|
f.N.Randomize();
|
|
|
|
f.C.Zero();
|
|
|
|
transfer->Y.Randomize();
|
2019-09-19 14:41:31 +00:00
|
|
|
m->proto = t;
|
|
|
|
m->introReply = p->intro;
|
|
|
|
PutReplyIntroFor(f.T, m->introReply);
|
|
|
|
m->sender = m_Identity.pub;
|
|
|
|
m->seqno = GetSeqNoForConvo(f.T);
|
2019-06-14 12:49:45 +00:00
|
|
|
f.S = 1;
|
2019-09-19 14:41:31 +00:00
|
|
|
f.F = m->introReply.pathID;
|
2019-06-14 12:49:45 +00:00
|
|
|
transfer->P = remoteIntro.pathID;
|
2019-09-19 14:41:31 +00:00
|
|
|
auto self = this;
|
|
|
|
return CryptoWorker()->addJob([transfer, p, m, K, self]() {
|
|
|
|
if(not transfer->T.EncryptAndSign(*m, K, self->m_Identity))
|
|
|
|
{
|
|
|
|
LogError("failed to encrypt and sign");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
util::Lock lock(&self->m_state->m_SendQueueMutex);
|
|
|
|
self->m_state->m_SendQueue.emplace_back(transfer, p);
|
|
|
|
});
|
2018-09-14 13:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 12:49:45 +00:00
|
|
|
|
2018-09-28 12:22:50 +00:00
|
|
|
// outbound converstation
|
2019-07-15 09:15:51 +00:00
|
|
|
auto& sessions = m_state->m_RemoteSessions;
|
|
|
|
if(EndpointUtil::HasPathToService(remote, sessions))
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto range = sessions.equal_range(remote);
|
2018-09-28 12:22:50 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while(itr != range.second)
|
|
|
|
{
|
|
|
|
if(itr->second->ReadyToSend())
|
|
|
|
{
|
|
|
|
itr->second->AsyncEncryptAndSendTo(data, t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2019-07-15 09:15:51 +00:00
|
|
|
|
|
|
|
auto& traffic = m_state->m_PendingTraffic;
|
|
|
|
traffic[remote].emplace_back(data, t);
|
2018-09-28 12:22:50 +00:00
|
|
|
// no converstation
|
2019-06-19 22:30:07 +00:00
|
|
|
return EnsurePathToService(
|
|
|
|
remote,
|
|
|
|
[&](Address r, OutboundContext* c) {
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
c->UpdateIntroSet(true);
|
2019-07-29 15:10:20 +00:00
|
|
|
for(auto& pending : m_state->m_PendingTraffic[r])
|
2019-07-15 09:15:51 +00:00
|
|
|
{
|
2019-06-19 22:30:07 +00:00
|
|
|
c->AsyncEncryptAndSendTo(pending.Buffer(), pending.protocol);
|
2019-07-15 09:15:51 +00:00
|
|
|
}
|
2019-06-19 22:30:07 +00:00
|
|
|
}
|
2019-07-29 15:10:20 +00:00
|
|
|
m_state->m_PendingTraffic.erase(r);
|
2019-06-19 22:30:07 +00:00
|
|
|
},
|
2019-09-19 14:41:31 +00:00
|
|
|
5000);
|
2018-09-24 19:50:52 +00:00
|
|
|
}
|
2018-08-22 15:52:10 +00:00
|
|
|
|
2019-03-08 17:00:13 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HasConvoTag(const ConvoTag& t) const
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return Sessions().find(t) != Sessions().end();
|
2019-03-08 17:00:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
uint64_t
|
|
|
|
Endpoint::GetSeqNoForConvo(const ConvoTag& tag)
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
auto itr = Sessions().find(tag);
|
|
|
|
if(itr == Sessions().end())
|
2018-08-09 19:02:17 +00:00
|
|
|
return 0;
|
|
|
|
return ++(itr->second.seqno);
|
|
|
|
}
|
|
|
|
|
2019-03-08 14:36:24 +00:00
|
|
|
bool
|
|
|
|
Endpoint::ShouldBuildMore(llarp_time_t now) const
|
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
if(path::Builder::BuildCooldownHit(now))
|
|
|
|
return false;
|
2019-05-02 16:23:31 +00:00
|
|
|
const bool should = path::Builder::ShouldBuildMore(now);
|
2019-01-16 00:24:16 +00:00
|
|
|
// determine newest intro
|
2018-09-27 10:51:30 +00:00
|
|
|
Introduction intro;
|
|
|
|
if(!GetNewestIntro(intro))
|
|
|
|
return should;
|
|
|
|
// time from now that the newest intro expires at
|
2019-05-02 16:23:31 +00:00
|
|
|
if(intro.ExpiresSoon(now))
|
2018-09-27 11:07:20 +00:00
|
|
|
return should;
|
2019-11-05 16:58:53 +00:00
|
|
|
|
2019-08-27 20:07:09 +00:00
|
|
|
const auto dlt = now - (intro.expiresAt - path::default_lifetime);
|
2019-11-05 16:58:53 +00:00
|
|
|
|
2018-09-27 11:09:00 +00:00
|
|
|
return should
|
|
|
|
|| ( // try spacing tunnel builds out evenly in time
|
2019-08-27 20:07:09 +00:00
|
|
|
(dlt >= (path::default_lifetime / 4))
|
2019-11-05 16:58:53 +00:00
|
|
|
&& (NumInStatus(path::ePathBuilding) < numPaths));
|
2018-09-24 19:50:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 16:20:50 +00:00
|
|
|
std::shared_ptr< Logic >
|
2018-08-09 19:02:17 +00:00
|
|
|
Endpoint::RouterLogic()
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return Router()->logic();
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 16:20:50 +00:00
|
|
|
std::shared_ptr< Logic >
|
2018-08-09 19:02:17 +00:00
|
|
|
Endpoint::EndpointLogic()
|
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return m_state->m_IsolatedLogic ? m_state->m_IsolatedLogic
|
|
|
|
: Router()->logic();
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:47:24 +00:00
|
|
|
std::shared_ptr< llarp::thread::ThreadPool >
|
2019-04-23 14:28:59 +00:00
|
|
|
Endpoint::CryptoWorker()
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2019-07-15 09:15:51 +00:00
|
|
|
return Router()->threadpool();
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
AbstractRouter*
|
|
|
|
Endpoint::Router()
|
|
|
|
{
|
|
|
|
return m_state->m_Router;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set< RouterID >&
|
|
|
|
Endpoint::SnodeBlacklist() const
|
|
|
|
{
|
|
|
|
return m_state->m_SnodeBlacklist;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IntroSet&
|
|
|
|
Endpoint::introSet() const
|
|
|
|
{
|
|
|
|
return m_state->m_IntroSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntroSet&
|
|
|
|
Endpoint::introSet()
|
|
|
|
{
|
|
|
|
return m_state->m_IntroSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ConvoMap&
|
|
|
|
Endpoint::Sessions() const
|
|
|
|
{
|
|
|
|
return m_state->m_Sessions;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConvoMap&
|
|
|
|
Endpoint::Sessions()
|
|
|
|
{
|
|
|
|
return m_state->m_Sessions;
|
|
|
|
}
|
2018-07-12 18:21:44 +00:00
|
|
|
} // namespace service
|
2018-07-16 03:32:13 +00:00
|
|
|
} // namespace llarp
|