2021-03-09 22:24:35 +00:00
|
|
|
#include "path.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/exit/exit_messages.hpp>
|
|
|
|
#include <llarp/link/i_link_manager.hpp>
|
|
|
|
#include <llarp/messages/discard.hpp>
|
|
|
|
#include <llarp/messages/relay_commit.hpp>
|
|
|
|
#include <llarp/messages/relay_status.hpp>
|
|
|
|
#include "pathbuilder.hpp"
|
|
|
|
#include "transit_hop.hpp"
|
|
|
|
#include <llarp/profiling.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/routing/dht_message.hpp>
|
|
|
|
#include <llarp/routing/path_latency_message.hpp>
|
|
|
|
#include <llarp/routing/transfer_traffic_message.hpp>
|
|
|
|
#include <llarp/util/buffer.hpp>
|
|
|
|
#include <llarp/util/endian.hpp>
|
|
|
|
#include <llarp/tooling/path_event.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-06-12 16:45:12 +00:00
|
|
|
#include <deque>
|
2021-03-31 16:06:50 +00:00
|
|
|
#include <queue>
|
2018-06-10 14:05:48 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace path
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::Path(
|
|
|
|
const std::vector<RouterContact>& h,
|
|
|
|
PathSet* parent,
|
|
|
|
PathRole startingRoles,
|
|
|
|
std::string shortName)
|
|
|
|
: m_PathSet(parent), _role(startingRoles), m_shortName(std::move(shortName))
|
2020-01-03 11:04:47 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
hops.resize(h.size());
|
2018-08-30 18:48:43 +00:00
|
|
|
size_t hsz = h.size();
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t idx = 0; idx < hsz; ++idx)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
hops[idx].rc = h[idx];
|
2019-10-30 14:18:38 +00:00
|
|
|
do
|
2019-10-29 02:28:59 +00:00
|
|
|
{
|
|
|
|
hops[idx].txID.Randomize();
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (hops[idx].txID.IsZero());
|
2019-10-30 14:18:38 +00:00
|
|
|
|
|
|
|
do
|
2019-10-29 02:28:59 +00:00
|
|
|
{
|
|
|
|
hops[idx].rxID.Randomize();
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (hops[idx].rxID.IsZero());
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-29 16:02:39 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t idx = 0; idx < hsz - 1; ++idx)
|
2018-06-26 14:52:19 +00:00
|
|
|
{
|
|
|
|
hops[idx].txID = hops[idx + 1].rxID;
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
// initialize parts of the introduction
|
2018-08-30 18:48:43 +00:00
|
|
|
intro.router = hops[hsz - 1].rc.pubkey;
|
2018-10-06 16:37:54 +00:00
|
|
|
intro.pathID = hops[hsz - 1].txID;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (parent)
|
2020-02-10 17:12:20 +00:00
|
|
|
EnterState(ePathBuilding, parent->Now());
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
Path::SetBuildResultHook(BuildResultHookFunc func)
|
|
|
|
{
|
|
|
|
m_BuiltHook = func;
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2021-04-12 17:14:59 +00:00
|
|
|
bool
|
|
|
|
Path::HandleUpstream(const llarp_buffer_t& X, const TunnelNonce& Y, AbstractRouter* r)
|
|
|
|
{
|
|
|
|
if (not m_UpstreamReplayFilter.Insert(Y))
|
|
|
|
return false;
|
|
|
|
return IHopHandler::HandleUpstream(X, Y, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::HandleDownstream(const llarp_buffer_t& X, const TunnelNonce& Y, AbstractRouter* r)
|
|
|
|
{
|
|
|
|
if (not m_DownstreamReplayFilter.Insert(Y))
|
|
|
|
return false;
|
|
|
|
return IHopHandler::HandleDownstream(X, Y, r);
|
|
|
|
}
|
|
|
|
|
2018-07-23 07:38:29 +00:00
|
|
|
RouterID
|
|
|
|
Path::Endpoint() const
|
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
return hops[hops.size() - 1].rc.pubkey;
|
2018-07-23 07:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
PubKey
|
|
|
|
Path::EndpointPubKey() const
|
|
|
|
{
|
|
|
|
return hops[hops.size() - 1].rc.pubkey;
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:03:50 +00:00
|
|
|
PathID_t
|
2018-06-25 15:12:08 +00:00
|
|
|
Path::TXID() const
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
return hops[0].txID;
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-12-18 19:03:50 +00:00
|
|
|
PathID_t
|
2018-06-25 15:12:08 +00:00
|
|
|
Path::RXID() const
|
|
|
|
{
|
|
|
|
return hops[0].rxID;
|
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
|
|
|
Path::IsReady() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (Expired(llarp::time_now_ms()))
|
2020-02-08 16:21:18 +00:00
|
|
|
return false;
|
2020-02-24 19:40:45 +00:00
|
|
|
return intro.latency > 0s && _status == ePathEstablished;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:26:29 +00:00
|
|
|
bool
|
|
|
|
Path::IsEndpoint(const RouterID& r, const PathID_t& id) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return hops[hops.size() - 1].rc.pubkey == r && hops[hops.size() - 1].txID == id;
|
2019-03-08 17:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
RouterID
|
|
|
|
Path::Upstream() const
|
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
return hops[0].rc.pubkey;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:46:07 +00:00
|
|
|
const std::string&
|
2020-02-20 21:37:39 +00:00
|
|
|
Path::ShortName() const
|
|
|
|
{
|
|
|
|
return m_shortName;
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:58:31 +00:00
|
|
|
std::string
|
|
|
|
Path::HopsString() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& hop : hops)
|
2019-03-11 13:58:31 +00:00
|
|
|
ss << RouterID(hop.rc.pubkey) << " -> ";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:31:17 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleLRSM(uint64_t status, std::array<EncryptedFrame, 8>& frames, AbstractRouter* r)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2019-07-31 12:51:24 +00:00
|
|
|
uint64_t currentStatus = status;
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
size_t index = 0;
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<RouterID> failedAt;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (index < hops.size())
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!frames[index].DoDecrypt(hops[index].shared))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
currentStatus = LR_StatusRecord::FAIL_DECRYPT_ERROR;
|
2020-04-07 18:38:56 +00:00
|
|
|
failedAt = hops[index].rc.pubkey;
|
2019-06-04 18:31:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
llarp::LogDebug("decrypted LRSM frame from ", hops[index].rc.pubkey);
|
|
|
|
|
|
|
|
llarp_buffer_t* buf = frames[index].Buffer();
|
2020-04-07 18:38:56 +00:00
|
|
|
buf->cur = buf->base + EncryptedFrameOverheadSize;
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
LR_StatusRecord record;
|
|
|
|
// successful decrypt
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!record.BDecode(buf))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn("malformed frame inside LRCM from ", hops[index].rc.pubkey);
|
2019-06-04 18:31:17 +00:00
|
|
|
currentStatus = LR_StatusRecord::FAIL_MALFORMED_RECORD;
|
2020-04-07 18:38:56 +00:00
|
|
|
failedAt = hops[index].rc.pubkey;
|
2019-06-04 18:31:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogDebug("Decoded LR Status Record from ", hops[index].rc.pubkey);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
currentStatus = record.status;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((record.status & LR_StatusRecord::SUCCESS) != LR_StatusRecord::SUCCESS)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2021-03-31 10:54:28 +00:00
|
|
|
if (record.status & LR_StatusRecord::FAIL_CONGESTION and index == 0)
|
|
|
|
{
|
|
|
|
// first hop building too fast
|
|
|
|
failedAt = hops[index].rc.pubkey;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-03 12:52:19 +00:00
|
|
|
// failed at next hop
|
2020-04-07 18:38:56 +00:00
|
|
|
if (index + 1 < hops.size())
|
2020-01-03 12:52:19 +00:00
|
|
|
{
|
|
|
|
failedAt = hops[index + 1].rc.pubkey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failedAt = hops[index].rc.pubkey;
|
|
|
|
}
|
2019-06-04 18:31:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((currentStatus & LR_StatusRecord::SUCCESS) == LR_StatusRecord::SUCCESS)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("LR_Status message processed, path build successful");
|
2021-03-02 07:02:59 +00:00
|
|
|
r->loop()->call([r, self = shared_from_this()] { self->HandlePathConfirmMessage(r); });
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-20 19:46:08 +00:00
|
|
|
if (failedAt)
|
2020-01-03 12:52:19 +00:00
|
|
|
{
|
2020-05-20 22:48:13 +00:00
|
|
|
r->NotifyRouterEvent<tooling::PathBuildRejectedEvent>(Endpoint(), RXID(), *failedAt);
|
2020-05-20 19:46:08 +00:00
|
|
|
LogWarn(Name(), " build failed at ", *failedAt);
|
|
|
|
r->routerProfiling().MarkHopFail(*failedAt);
|
2020-01-03 12:52:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
r->routerProfiling().MarkPathFail(this);
|
2019-07-31 12:51:24 +00:00
|
|
|
llarp::LogDebug("LR_Status message processed, path build failed");
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (currentStatus & LR_StatusRecord::FAIL_TIMEOUT)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("Path build failed due to timeout");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_CONGESTION)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("Path build failed due to congestion");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_DEST_UNKNOWN)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug(
|
|
|
|
"Path build failed due to one or more nodes giving destination "
|
|
|
|
"unknown");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_DEST_INVALID)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug(
|
|
|
|
"Path build failed due to one or more nodes considered an "
|
|
|
|
"invalid destination");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_CANNOT_CONNECT)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug(
|
|
|
|
"Path build failed due to a node being unable to connect to the "
|
|
|
|
"next hop");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_MALFORMED_RECORD)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug(
|
|
|
|
"Path build failed due to a malformed record in the build status "
|
|
|
|
"message");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (currentStatus & LR_StatusRecord::FAIL_DECRYPT_ERROR)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug(
|
|
|
|
"Path build failed due to a decrypt error in the build status "
|
|
|
|
"message");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogDebug("Path build failed for an unspecified reason");
|
|
|
|
}
|
2021-03-31 10:54:28 +00:00
|
|
|
RouterID edge{};
|
|
|
|
if (failedAt)
|
|
|
|
edge = *failedAt;
|
|
|
|
r->loop()->call([r, self = shared_from_this(), edge]() {
|
|
|
|
self->EnterState(ePathFailed, r->Now());
|
|
|
|
self->m_PathSet->HandlePathBuildFailedAt(self, edge);
|
|
|
|
});
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: meaningful return value?
|
|
|
|
return true;
|
2020-01-03 12:52:19 +00:00
|
|
|
} // namespace path
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2018-09-14 14:50:37 +00:00
|
|
|
void
|
2018-10-29 16:48:36 +00:00
|
|
|
Path::EnterState(PathStatus st, llarp_time_t now)
|
2018-09-14 14:50:37 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (st == ePathFailed)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
_status = st;
|
2019-07-31 12:51:24 +00:00
|
|
|
return;
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (st == ePathExpired && _status == ePathBuilding)
|
2018-09-14 14:50:37 +00:00
|
|
|
{
|
2019-04-13 14:32:07 +00:00
|
|
|
_status = st;
|
2019-04-23 14:28:59 +00:00
|
|
|
m_PathSet->HandlePathBuildTimeout(shared_from_this());
|
2018-09-14 14:50:37 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (st == ePathBuilding)
|
2018-09-14 14:50:37 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo("path ", Name(), " is building");
|
2018-10-29 16:48:36 +00:00
|
|
|
buildStarted = now;
|
2018-09-14 14:50:37 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (st == ePathEstablished && _status == ePathBuilding)
|
2019-02-05 17:55:23 +00:00
|
|
|
{
|
2020-02-25 17:07:30 +00:00
|
|
|
LogInfo("path ", Name(), " is built, took ", now - buildStarted);
|
2019-02-05 17:55:23 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (st == ePathTimeout && _status == ePathEstablished)
|
2019-03-30 13:02:10 +00:00
|
|
|
{
|
2019-03-30 13:12:48 +00:00
|
|
|
LogInfo("path ", Name(), " died");
|
|
|
|
_status = st;
|
2019-04-23 14:28:59 +00:00
|
|
|
m_PathSet->HandlePathDied(shared_from_this());
|
2019-03-30 13:02:10 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (st == ePathEstablished && _status == ePathTimeout)
|
2019-04-13 14:32:07 +00:00
|
|
|
{
|
|
|
|
LogInfo("path ", Name(), " reanimated");
|
|
|
|
}
|
2018-09-14 14:50:37 +00:00
|
|
|
_status = st;
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
PathHopConfig::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2021-03-05 17:31:52 +00:00
|
|
|
util::StatusObject obj{
|
|
|
|
{"lifetime", to_json(lifetime)},
|
|
|
|
{"router", rc.pubkey.ToHex()},
|
|
|
|
{"txid", txID.ToHex()},
|
|
|
|
{"rxid", rxID.ToHex()}};
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
|
|
|
}
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
Path::ExtractStatus() const
|
|
|
|
{
|
|
|
|
auto now = llarp::time_now_ms();
|
|
|
|
|
2021-03-05 17:31:52 +00:00
|
|
|
util::StatusObject obj{
|
|
|
|
{"intro", intro.ExtractStatus()},
|
|
|
|
{"lastRecvMsg", to_json(m_LastRecvMessage)},
|
|
|
|
{"lastLatencyTest", to_json(m_LastLatencyTestTime)},
|
|
|
|
{"buildStarted", to_json(buildStarted)},
|
|
|
|
{"expired", Expired(now)},
|
|
|
|
{"expiresSoon", ExpiresSoon(now)},
|
|
|
|
{"expiresAt", to_json(ExpireTime())},
|
|
|
|
{"ready", IsReady()},
|
|
|
|
{"txRateCurrent", m_LastTXRate},
|
|
|
|
{"rxRateCurrent", m_LastRXRate},
|
2021-04-12 17:14:59 +00:00
|
|
|
{"replayTX", m_UpstreamReplayFilter.Size()},
|
|
|
|
{"replayRX", m_DownstreamReplayFilter.Size()},
|
2021-03-05 17:31:52 +00:00
|
|
|
{"hasExit", SupportsAnyRoles(ePathRoleExit)}};
|
2020-04-07 18:38:56 +00:00
|
|
|
|
|
|
|
std::vector<util::StatusObject> hopsObj;
|
|
|
|
std::transform(
|
|
|
|
hops.begin(),
|
|
|
|
hops.end(),
|
|
|
|
std::back_inserter(hopsObj),
|
|
|
|
[](const auto& hop) -> util::StatusObject { return hop.ExtractStatus(); });
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["hops"] = hopsObj;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (_status)
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
|
|
|
case ePathBuilding:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "building";
|
2019-02-08 19:43:25 +00:00
|
|
|
break;
|
|
|
|
case ePathEstablished:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "established";
|
2019-02-08 19:43:25 +00:00
|
|
|
break;
|
|
|
|
case ePathTimeout:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "timeout";
|
2019-02-08 19:43:25 +00:00
|
|
|
break;
|
|
|
|
case ePathExpired:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "expired";
|
2019-02-08 19:43:25 +00:00
|
|
|
break;
|
2019-06-04 18:31:17 +00:00
|
|
|
case ePathFailed:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "failed";
|
2019-06-04 18:31:17 +00:00
|
|
|
break;
|
2019-05-31 10:57:41 +00:00
|
|
|
case ePathIgnore:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "ignored";
|
2019-07-12 06:27:21 +00:00
|
|
|
break;
|
2019-02-08 19:43:25 +00:00
|
|
|
default:
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["status"] = "unknown";
|
2019-02-08 19:43:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 14:54:05 +00:00
|
|
|
void
|
|
|
|
Path::Rebuild()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<RouterContact> newHops;
|
|
|
|
for (const auto& hop : hops)
|
2019-05-06 14:54:05 +00:00
|
|
|
newHops.emplace_back(hop.rc);
|
2020-02-20 21:37:39 +00:00
|
|
|
LogInfo(Name(), " rebuilding on ", ShortName());
|
2019-05-06 14:54:05 +00:00
|
|
|
m_PathSet->Build(newHops);
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:02:39 +00:00
|
|
|
void
|
2019-02-11 19:45:42 +00:00
|
|
|
Path::Tick(llarp_time_t now, AbstractRouter* r)
|
2018-06-29 16:02:39 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (Expired(now))
|
2018-07-23 07:38:29 +00:00
|
|
|
return;
|
2018-09-13 12:27:28 +00:00
|
|
|
|
2020-02-08 16:21:18 +00:00
|
|
|
m_LastRXRate = m_RXRate;
|
|
|
|
m_LastTXRate = m_TXRate;
|
|
|
|
|
|
|
|
m_RXRate = 0;
|
|
|
|
m_TXRate = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathBuilding)
|
2018-09-13 16:41:53 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buildStarted == 0s)
|
2019-11-05 16:58:53 +00:00
|
|
|
return;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (now >= buildStarted)
|
2018-09-13 16:41:53 +00:00
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
const auto dlt = now - buildStarted;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dlt >= path::build_timeout)
|
2019-02-05 17:55:23 +00:00
|
|
|
{
|
2020-02-25 17:05:13 +00:00
|
|
|
LogWarn(Name(), " waited for ", dlt, " and no path was built");
|
2019-02-11 19:45:42 +00:00
|
|
|
r->routerProfiling().MarkPathFail(this);
|
2019-04-13 14:32:07 +00:00
|
|
|
EnterState(ePathExpired, now);
|
2019-02-05 17:55:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 12:27:28 +00:00
|
|
|
// check to see if this path is dead
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathEstablished)
|
2018-09-13 12:27:28 +00:00
|
|
|
{
|
2020-05-21 14:24:59 +00:00
|
|
|
auto dlt = now - m_LastLatencyTestTime;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dlt > path::latency_interval && m_LastLatencyTestID == 0)
|
2019-02-06 13:51:05 +00:00
|
|
|
{
|
2020-05-21 14:24:59 +00:00
|
|
|
// bail doing test if we are active
|
|
|
|
if (now - m_LastRecvMessage < path::latency_interval)
|
|
|
|
return;
|
2019-02-11 19:45:42 +00:00
|
|
|
routing::PathLatencyMessage latency;
|
2020-04-07 18:38:56 +00:00
|
|
|
latency.T = randint();
|
|
|
|
m_LastLatencyTestID = latency.T;
|
2019-02-06 13:51:05 +00:00
|
|
|
m_LastLatencyTestTime = now;
|
2019-04-22 17:38:29 +00:00
|
|
|
SendRoutingMessage(latency, r);
|
2019-09-16 16:12:05 +00:00
|
|
|
FlushUpstream(r);
|
2019-03-25 15:41:37 +00:00
|
|
|
return;
|
2019-02-06 13:51:05 +00:00
|
|
|
}
|
2020-05-21 14:24:59 +00:00
|
|
|
dlt = now - m_LastRecvMessage;
|
|
|
|
if (dlt >= path::alive_timeout)
|
2018-09-14 14:50:37 +00:00
|
|
|
{
|
2020-05-21 14:24:59 +00:00
|
|
|
LogWarn(Name(), " waited for ", dlt, " and path looks dead");
|
|
|
|
r->routerProfiling().MarkPathFail(this);
|
|
|
|
EnterState(ePathTimeout, now);
|
2018-09-14 14:50:37 +00:00
|
|
|
}
|
2018-09-13 12:27:28 +00:00
|
|
|
}
|
2018-06-29 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleAllUpstream(std::vector<RelayUpstreamMessage> msgs, AbstractRouter* r)
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& msg : msgs)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (r->SendToOrQueue(Upstream(), &msg))
|
2020-02-08 16:21:18 +00:00
|
|
|
{
|
|
|
|
m_TXRate += msg.X.size();
|
|
|
|
}
|
|
|
|
else
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
LogDebug("failed to send upstream to ", Upstream());
|
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2019-09-16 16:12:05 +00:00
|
|
|
r->linkManager().PumpLinks();
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-16 10:21:12 +00:00
|
|
|
Path::UpstreamWork(TrafficQueue_ptr msgs, AbstractRouter* r)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<RelayUpstreamMessage> sendmsgs(msgs->size());
|
2019-09-05 17:39:09 +00:00
|
|
|
size_t idx = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& ev : *msgs)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
const llarp_buffer_t buf(ev.first);
|
|
|
|
TunnelNonce n = ev.second;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& hop : hops)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
CryptoManager::instance()->xchacha20(buf, hop.shared, n);
|
|
|
|
n ^= hop.nonceXOR;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
auto& msg = sendmsgs[idx];
|
|
|
|
msg.X = buf;
|
|
|
|
msg.Y = ev.second;
|
2019-09-05 17:39:09 +00:00
|
|
|
msg.pathid = TXID();
|
|
|
|
++idx;
|
|
|
|
}
|
2021-03-02 15:23:38 +00:00
|
|
|
r->loop()->call([self = shared_from_this(), data = std::move(sendmsgs), r]() mutable {
|
2020-05-23 20:05:26 +00:00
|
|
|
self->HandleAllUpstream(std::move(data), r);
|
|
|
|
});
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-16 10:21:12 +00:00
|
|
|
Path::FlushUpstream(AbstractRouter* r)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2020-05-23 20:05:26 +00:00
|
|
|
if (m_UpstreamQueue && not m_UpstreamQueue->empty())
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
2020-05-23 20:05:26 +00:00
|
|
|
TrafficQueue_ptr data = nullptr;
|
|
|
|
std::swap(m_UpstreamQueue, data);
|
2020-06-11 11:44:02 +00:00
|
|
|
r->QueueWork(
|
2020-05-23 20:05:26 +00:00
|
|
|
[self = shared_from_this(), data, r]() { self->UpstreamWork(std::move(data), r); });
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Path::FlushDownstream(AbstractRouter* r)
|
|
|
|
{
|
2020-05-23 20:05:26 +00:00
|
|
|
if (m_DownstreamQueue && not m_DownstreamQueue->empty())
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
2020-05-23 20:05:26 +00:00
|
|
|
TrafficQueue_ptr data = nullptr;
|
|
|
|
std::swap(m_DownstreamQueue, data);
|
2020-06-11 11:44:02 +00:00
|
|
|
r->QueueWork(
|
2020-05-23 20:05:26 +00:00
|
|
|
[self = shared_from_this(), data, r]() { self->DownstreamWork(std::move(data), r); });
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
|
|
|
Path::Expired(llarp_time_t now) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathFailed)
|
2019-06-04 18:31:17 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathBuilding)
|
2018-09-13 13:07:00 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathEstablished || _status == ePathTimeout)
|
2019-11-05 16:58:53 +00:00
|
|
|
{
|
|
|
|
return now >= ExpireTime();
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
return true;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-23 00:00:44 +00:00
|
|
|
|
2018-09-13 13:07:00 +00:00
|
|
|
std::string
|
|
|
|
Path::Name() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "TX=" << TXID() << " RX=" << RXID();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_PathSet)
|
2019-03-22 14:10:30 +00:00
|
|
|
ss << " on " << m_PathSet->Name();
|
2018-09-13 13:07:00 +00:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
void
|
2019-09-16 10:21:12 +00:00
|
|
|
Path::DownstreamWork(TrafficQueue_ptr msgs, AbstractRouter* r)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<RelayDownstreamMessage> sendMsgs(msgs->size());
|
2019-09-05 17:39:09 +00:00
|
|
|
size_t idx = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& ev : *msgs)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2019-09-05 17:39:09 +00:00
|
|
|
const llarp_buffer_t buf(ev.first);
|
|
|
|
sendMsgs[idx].Y = ev.second;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& hop : hops)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
sendMsgs[idx].Y ^= hop.nonceXOR;
|
2020-04-07 18:38:56 +00:00
|
|
|
CryptoManager::instance()->xchacha20(buf, hop.shared, sendMsgs[idx].Y);
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
sendMsgs[idx].X = buf;
|
|
|
|
++idx;
|
|
|
|
}
|
2021-03-02 15:23:38 +00:00
|
|
|
r->loop()->call([self = shared_from_this(), msgs = std::move(sendMsgs), r]() mutable {
|
2020-05-23 20:05:26 +00:00
|
|
|
self->HandleAllDownstream(std::move(msgs), r);
|
|
|
|
});
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleAllDownstream(std::vector<RelayDownstreamMessage> msgs, AbstractRouter* r)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& msg : msgs)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2021-03-31 16:06:50 +00:00
|
|
|
const llarp_buffer_t buf{msg.X};
|
2020-02-08 16:21:18 +00:00
|
|
|
m_RXRate += buf.sz;
|
2021-03-31 16:06:50 +00:00
|
|
|
if (HandleRoutingMessage(buf, r))
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2021-03-31 16:06:50 +00:00
|
|
|
r->loop()->wakeup();
|
|
|
|
m_LastRecvMessage = r->Now();
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-29 02:16:31 +00:00
|
|
|
Path::HandleRoutingMessage(const llarp_buffer_t& buf, AbstractRouter* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!r->ParseRoutingMessageBuffer(buf, this, RXID()))
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogWarn("Failed to parse inbound routing message");
|
2018-06-25 15:12:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
|
|
|
Path::HandleUpdateExitVerifyMessage(
|
2019-04-22 17:38:29 +00:00
|
|
|
const routing::UpdateExitVerifyMessage& msg, AbstractRouter* r)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
(void)r;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_UpdateExitTX && msg.T == m_UpdateExitTX)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ExitUpdated)
|
2019-04-23 14:28:59 +00:00
|
|
|
return m_ExitUpdated(shared_from_this());
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_CloseExitTX && msg.T == m_CloseExitTX)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ExitClosed)
|
2019-04-23 14:28:59 +00:00
|
|
|
return m_ExitClosed(shared_from_this());
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2019-04-22 17:38:29 +00:00
|
|
|
Path::SendRoutingMessage(const routing::IMessage& msg, AbstractRouter* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<byte_t, MAX_LINK_MSG_SIZE / 2> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2019-02-11 19:45:42 +00:00
|
|
|
// should help prevent bad paths with uninitialized members
|
|
|
|
// FIXME: Why would we get uninitialized IMessages?
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.version != LLARP_PROTO_VERSION)
|
2019-01-02 04:33:03 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!msg.BEncode(&buf))
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("Bencode failed");
|
|
|
|
DumpBuffer(buf);
|
2018-06-25 15:12:08 +00:00
|
|
|
return false;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
// make nonce
|
|
|
|
TunnelNonce N;
|
|
|
|
N.Randomize();
|
2018-07-23 22:36:46 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
// pad smaller messages
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf.sz < pad_size)
|
2018-07-23 22:36:46 +00:00
|
|
|
{
|
|
|
|
// randomize padding
|
2019-05-28 19:45:08 +00:00
|
|
|
CryptoManager::instance()->randbytes(buf.cur, pad_size - buf.sz);
|
2019-04-05 14:58:22 +00:00
|
|
|
buf.sz = pad_size;
|
2018-07-23 22:36:46 +00:00
|
|
|
}
|
|
|
|
buf.cur = buf.base;
|
2021-03-31 16:06:50 +00:00
|
|
|
LogDebug("send routing message ", msg.S, " with ", buf.sz, " bytes to endpoint ", Endpoint());
|
2018-06-25 15:12:08 +00:00
|
|
|
return HandleUpstream(buf, N, r);
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:23:43 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandlePathTransferMessage(
|
|
|
|
const routing::PathTransferMessage& /*msg*/, AbstractRouter* /*r*/)
|
2018-06-26 16:23:43 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn("unwarranted path transfer message on tx=", TXID(), " rx=", RXID());
|
2018-06-26 16:23:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-11 15:28:36 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleDataDiscardMessage(const routing::DataDiscardMessage& msg, AbstractRouter* r)
|
2018-09-11 15:28:36 +00:00
|
|
|
{
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(r->Now());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_DropHandler)
|
2019-04-23 14:28:59 +00:00
|
|
|
return m_DropHandler(shared_from_this(), msg.P, msg.S);
|
2018-09-11 15:28:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2019-06-04 18:31:17 +00:00
|
|
|
Path::HandlePathConfirmMessage(AbstractRouter* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2020-02-20 21:37:39 +00:00
|
|
|
LogDebug("Path Build Confirm, path: ", ShortName());
|
2019-11-05 16:58:53 +00:00
|
|
|
const auto now = llarp::time_now_ms();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_status == ePathBuilding)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
// finish initializing introduction
|
|
|
|
intro.expiresAt = buildStarted + hops[0].lifetime;
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
r->routerProfiling().MarkPathSuccess(this);
|
2018-09-14 14:50:37 +00:00
|
|
|
|
2018-08-23 14:35:29 +00:00
|
|
|
// persist session with upstream router until the path is done
|
|
|
|
r->PersistSessionUntil(Upstream(), intro.expiresAt);
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(now);
|
2018-08-23 14:35:29 +00:00
|
|
|
// send path latency test
|
2019-02-11 19:45:42 +00:00
|
|
|
routing::PathLatencyMessage latency;
|
2020-04-07 18:38:56 +00:00
|
|
|
latency.T = randint();
|
2021-03-31 16:06:50 +00:00
|
|
|
latency.S = NextSeqNo();
|
2020-04-07 18:38:56 +00:00
|
|
|
m_LastLatencyTestID = latency.T;
|
2018-10-29 16:48:36 +00:00
|
|
|
m_LastLatencyTestTime = now;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!SendRoutingMessage(latency, r))
|
2019-09-16 16:12:05 +00:00
|
|
|
return false;
|
|
|
|
FlushUpstream(r);
|
|
|
|
return true;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn("got unwarranted path confirm message on tx=", RXID(), " rx=", RXID());
|
2018-06-22 00:25:30 +00:00
|
|
|
return false;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandlePathConfirmMessage(const routing::PathConfirmMessage& /*msg*/, AbstractRouter* r)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
return HandlePathConfirmMessage(r);
|
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
2019-04-22 17:38:29 +00:00
|
|
|
Path::HandleHiddenServiceFrame(const service::ProtocolFrame& frame)
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(m_PathSet->Now());
|
2019-04-23 14:28:59 +00:00
|
|
|
return m_DataHandler && m_DataHandler(shared_from_this(), frame);
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandlePathLatencyMessage(const routing::PathLatencyMessage& msg, AbstractRouter* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2019-11-03 20:52:00 +00:00
|
|
|
const auto now = r->Now();
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(now);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.L == m_LastLatencyTestID)
|
2018-06-26 16:23:43 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
intro.latency = now - m_LastLatencyTestTime;
|
2018-07-03 13:54:43 +00:00
|
|
|
m_LastLatencyTestID = 0;
|
2019-11-04 13:39:29 +00:00
|
|
|
EnterState(ePathEstablished, now);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_BuiltHook)
|
2019-11-04 13:39:29 +00:00
|
|
|
m_BuiltHook(shared_from_this());
|
|
|
|
m_BuiltHook = nullptr;
|
2020-02-25 17:05:13 +00:00
|
|
|
LogDebug("path latency is now ", intro.latency, " for ", Name());
|
2018-07-23 07:38:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
LogWarn("unwarranted path latency message via ", Upstream());
|
|
|
|
return false;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2020-02-28 16:46:50 +00:00
|
|
|
/// this is the Client's side of handling a DHT message. it's handled
|
|
|
|
/// in-place.
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2019-04-22 17:38:29 +00:00
|
|
|
Path::HandleDHTMessage(const dht::IMessage& msg, AbstractRouter* r)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2019-03-25 15:41:37 +00:00
|
|
|
MarkActive(r->Now());
|
2019-02-11 19:45:42 +00:00
|
|
|
routing::DHTMessage reply;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!msg.HandleMessage(r->dht(), reply.M))
|
2018-09-02 18:25:42 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (reply.M.size())
|
2019-04-22 17:38:29 +00:00
|
|
|
return SendRoutingMessage(reply, r);
|
2018-09-02 18:25:42 +00:00
|
|
|
return true;
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleCloseExitMessage(const routing::CloseExitMessage& msg, AbstractRouter* /*r*/)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
/// allows exits to close from their end
|
2020-04-07 18:38:56 +00:00
|
|
|
if (SupportsAnyRoles(ePathRoleExit | ePathRoleSVC))
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.Verify(EndpointPubKey()))
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo(Name(), " had its exit closed");
|
2018-11-14 18:02:27 +00:00
|
|
|
_role &= ~ePathRoleExit;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
LogError(Name(), " CXM from exit with bad signature");
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
|
|
|
else
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " unwarranted CXM");
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-14 19:34:17 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::SendExitRequest(const routing::ObtainExitMessage& msg, AbstractRouter* r)
|
2018-11-14 19:34:17 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo(Name(), " sending exit request to ", Endpoint());
|
2019-04-22 17:38:29 +00:00
|
|
|
m_ExitObtainTX = msg.T;
|
2018-11-14 19:34:17 +00:00
|
|
|
return SendRoutingMessage(msg, r);
|
|
|
|
}
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
2019-04-22 17:38:29 +00:00
|
|
|
Path::SendExitClose(const routing::CloseExitMessage& msg, AbstractRouter* r)
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo(Name(), " closing exit to ", Endpoint());
|
2018-12-24 16:09:05 +00:00
|
|
|
// mark as not exit anymore
|
|
|
|
_role &= ~ePathRoleExit;
|
|
|
|
return SendRoutingMessage(msg, r);
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleObtainExitMessage(const routing::ObtainExitMessage& msg, AbstractRouter* r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
|
|
|
(void)msg;
|
|
|
|
(void)r;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " got unwarranted OXM");
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleUpdateExitMessage(const routing::UpdateExitMessage& msg, AbstractRouter* r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
|
|
|
(void)msg;
|
|
|
|
(void)r;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " got unwarranted UXM");
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleRejectExitMessage(const routing::RejectExitMessage& msg, AbstractRouter* r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ExitObtainTX && msg.T == m_ExitObtainTX)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!msg.Verify(EndpointPubKey()))
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), "RXM invalid signature");
|
2018-11-14 18:02:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo(Name(), " ", Endpoint(), " Rejected exit");
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(r->Now());
|
2020-02-25 17:05:13 +00:00
|
|
|
return InformExitResult(llarp_time_t(msg.B));
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " got unwarranted RXM");
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Path::HandleGrantExitMessage(const routing::GrantExitMessage& msg, AbstractRouter* r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ExitObtainTX && msg.T == m_ExitObtainTX)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!msg.Verify(EndpointPubKey()))
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " GXM signature failed");
|
2018-11-14 18:02:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// we now can send exit traffic
|
|
|
|
_role |= ePathRoleExit;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogInfo(Name(), " ", Endpoint(), " Granted exit");
|
2018-11-16 14:21:23 +00:00
|
|
|
MarkActive(r->Now());
|
2020-02-24 19:40:45 +00:00
|
|
|
return InformExitResult(0s);
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError(Name(), " got unwarranted GXM");
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
bool
|
|
|
|
Path::InformExitResult(llarp_time_t B)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto self = shared_from_this();
|
2018-11-14 18:02:27 +00:00
|
|
|
bool result = true;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& hook : m_ObtainedExitHooks)
|
2019-04-23 14:28:59 +00:00
|
|
|
result &= hook(self, B);
|
2018-11-14 18:02:27 +00:00
|
|
|
m_ObtainedExitHooks.clear();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
bool
|
|
|
|
Path::HandleTransferTrafficMessage(
|
2019-04-22 17:38:29 +00:00
|
|
|
const routing::TransferTrafficMessage& msg, AbstractRouter* r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
// check if we can handle exit data
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!SupportsAnyRoles(ePathRoleExit | ePathRoleSVC))
|
2018-11-14 18:02:27 +00:00
|
|
|
return false;
|
|
|
|
// handle traffic if we have a handler
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!m_ExitTrafficHandler)
|
2018-11-28 17:58:46 +00:00
|
|
|
return false;
|
2019-04-22 17:38:29 +00:00
|
|
|
bool sent = msg.X.size() > 0;
|
2019-04-23 14:28:59 +00:00
|
|
|
auto self = shared_from_this();
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& pkt : msg.X)
|
2018-11-29 21:19:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pkt.size() <= 8)
|
2018-11-29 21:19:20 +00:00
|
|
|
return false;
|
|
|
|
uint64_t counter = bufbe64toh(pkt.data());
|
2021-03-26 13:16:43 +00:00
|
|
|
if (m_ExitTrafficHandler(
|
|
|
|
self, llarp_buffer_t(pkt.data() + 8, pkt.size() - 8), counter, msg.protocol))
|
2019-04-22 17:55:07 +00:00
|
|
|
{
|
|
|
|
MarkActive(r->Now());
|
|
|
|
EnterState(ePathEstablished, r->Now());
|
|
|
|
}
|
2018-11-29 21:19:20 +00:00
|
|
|
}
|
2018-11-28 17:58:46 +00:00
|
|
|
return sent;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
} // namespace path
|
2018-06-21 09:31:53 +00:00
|
|
|
} // namespace llarp
|