Skip to content

Commit

Permalink
testnet kick
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Oct 24, 2024
1 parent 1b14e79 commit 0805844
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 108 deletions.
2 changes: 1 addition & 1 deletion llarp/contact/client_contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace llarp

void ClientContact::handle_updated_field(intro_set iset)
{
log::trace(logcat, "ClientContact storing updated ClientIntros...");
if (iset.empty())
throw std::invalid_argument{"Cannot publish ClientContact with no ClientIntros!"};
intros = std::move(iset);
log::debug(logcat, "ClientContact stored updated ClientIntros (n={})...", intros.size());
}

void ClientContact::handle_updated_field(std::unordered_set<dns::SRVData> srvs)
Expand Down
2 changes: 1 addition & 1 deletion llarp/contact/client_contact.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace llarp
};

// TESTNET:
inline static constexpr auto CC_PUBLISH_INTERVAL{5min};
inline static constexpr auto CC_PUBLISH_INTERVAL{1min};

/** ClientContact
On the wire we encode the data as a dict containing:
Expand Down
15 changes: 7 additions & 8 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ namespace llarp::handlers
assert(not _is_snode_service);

_exit_policy = net_config.traffic_policy;
// _local_cc.exit_policy = _exit_policy; // TESTNET:
client_contact.exit_policy = _exit_policy;
}

if (not net_config.srv_records.empty()) // TESTNET:
if (not net_config.srv_records.empty())
{
_srv_records.merge(net_config.srv_records);
// _local_cc.SRVs = std::move(net_config.srv_records);
client_contact.SRVs = _srv_records;
}

if (use_tokens = not net_config.auth_static_tokens.empty(); use_tokens)
_static_auth_tokens.merge(net_config.auth_static_tokens);
Expand Down Expand Up @@ -104,11 +106,6 @@ namespace llarp::handlers
_exit_policy);

should_publish_cc = net_config.is_reachable;

if (should_publish_cc)
{
//
}
}

void SessionEndpoint::build_more(size_t n)
Expand Down Expand Up @@ -402,6 +399,8 @@ namespace llarp::handlers
{
bool ret{true};

log::critical(logcat, "Publishing new EncryptedClientContact: {}", ecc.bt_payload());

{
Lock_t l{paths_mutex};

Expand Down
18 changes: 10 additions & 8 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ namespace llarp
{
if (alpn == alpns::C_ALPNS)
{
log::critical(logcat, "{} node accepting client connection (remote ID:{})!", us, other);
log::critical(logcat, "{} accepting client connection (remote ID:{})!", us, other);
ep->client_conns.emplace(other, nullptr);
return true;
}
Expand Down Expand Up @@ -351,7 +351,7 @@ namespace llarp

log::critical(
logcat,
"{} node received inbound with ongoing outbound to remote "
"{} received inbound with ongoing outbound to remote "
"(RID:{}); {}!",
us,
other,
Expand All @@ -360,13 +360,12 @@ namespace llarp
return defer_to_incoming;
}

log::critical(
logcat, "{} node accepting inbound from registered remote (RID:{})", us, other);
log::critical(logcat, "{} accepting inbound from registered remote (RID:{})", us, other);
}
else
log::critical(
logcat,
"{} node was unable to confirm remote (RID:{}) is registered; "
"{} was unable to confirm remote (RID:{}) is registered; "
"rejecting "
"connection!",
us,
Expand All @@ -375,7 +374,7 @@ namespace llarp
return result;
}

log::critical(logcat, "{} node received unknown ALPN; rejecting connection!", us);
log::critical(logcat, "{} received unknown ALPN; rejecting connection!", us);
return false;
}

Expand Down Expand Up @@ -1127,7 +1126,7 @@ namespace llarp

if (is_relayed)
{
if (is_relayed >= path::DEFAULT_PATHS_HELD)
if (relay_order >= path::DEFAULT_PATHS_HELD)
{
log::error(logcat, "Received PublishClientContact with invalid relay order: {}", relay_order);
return respond(PublishClientContact::INVALID_ORDER);
Expand Down Expand Up @@ -1207,7 +1206,7 @@ namespace llarp

if (is_relayed)
{
if (is_relayed >= path::DEFAULT_PATHS_HELD)
if (relay_order >= path::DEFAULT_PATHS_HELD)
{
log::error(logcat, "Received FindClientContact with invalid relay order: {}", relay_order);
return respond(FindClientContact::INVALID_ORDER);
Expand Down Expand Up @@ -1737,6 +1736,9 @@ namespace llarp
if (not hop)
return;

if (response.timed_out)
log::debug(logcat, "Path control message timed out!");

ustring hop_id, nonce, payload;

try
Expand Down
92 changes: 12 additions & 80 deletions llarp/path/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,37 +144,30 @@ namespace llarp::path
// _role &= ePathRoleExit;
}

std::string Path::make_outer_payload(ustring_view payload, SymmNonce& nonce)
std::string Path::make_outer_payload(char* data, size_t len)
{
auto nonce = SymmNonce::make_random();
// chacha and mutate nonce for each hop
for (const auto& hop : hops)
{
nonce = crypto::onion(
const_cast<unsigned char*>(payload.data()), payload.size(), hop.shared, nonce, hop.nonceXOR);
nonce = crypto::onion(reinterpret_cast<uint8_t*>(data), len, hop.shared, nonce, hop.nonceXOR);
}

return Onion::serialize(nonce, upstream_txid(), payload);
}

std::string Path::make_outer_payload(ustring_view payload)
{
auto nonce = SymmNonce::make_random();

return make_outer_payload(payload, nonce);
return Onion::serialize(nonce, upstream_txid(), {data, len});
}

bool Path::send_path_data_message(std::string data)
{
auto payload = PathData::serialize(std::move(data), _router.local_rid());
auto outer_payload = make_outer_payload(to_usv(payload));
auto outer_payload = make_outer_payload(payload.data(), payload.size());

return _router.send_data_message(upstream_rid(), std::move(outer_payload));
}

bool Path::send_path_control_message(std::string endpoint, std::string body, std::function<void(std::string)> func)
{
auto inner_payload = PathControl::serialize(std::move(endpoint), std::move(body));
auto outer_payload = make_outer_payload(to_usv(inner_payload));
auto outer_payload = make_outer_payload(inner_payload.data(), inner_payload.size());

return _router.send_control_message(
upstream_rid(),
Expand Down Expand Up @@ -332,9 +325,6 @@ namespace llarp::path
{"lastLatencyTest", to_json(last_latency_test)},
{"expired", is_expired(now)},
{"ready", is_ready()},
// {"txRateCurrent", m_LastTXRate},
// {"rxRateCurrent", m_LastRXRate},
// {"hasExit", SupportsAnyRoles(ePathRoleExit)}
};

std::vector<nlohmann::json> hopsObj;
Expand All @@ -343,30 +333,6 @@ namespace llarp::path
});
obj["hops"] = hopsObj;

// switch (_status)
// {
// case PathStatus::BUILDING:
// obj["status"] = "building";
// break;
// case PathStatus::ESTABLISHED:
// obj["status"] = "established";
// break;
// case PathStatus::TIMEOUT:
// obj["status"] = "timeout";
// break;
// case PathStatus::EXPIRED:
// obj["status"] = "expired";
// break;
// case PathStatus::FAILED:
// obj["status"] = "failed";
// break;
// case PathStatus::IGNORE:
// obj["status"] = "ignored";
// break;
// default:
// obj["status"] = "unknown";
// break;
// }
return obj;
}

Expand Down Expand Up @@ -408,6 +374,9 @@ namespace llarp::path

void Path::Tick(std::chrono::milliseconds now)
{
if (not is_ready())
return;

if (is_expired(now))
return;

Expand Down Expand Up @@ -471,58 +440,21 @@ namespace llarp::path

void Path::set_established()
{
log::info(logcat, "Path marked as successfully established!");
_established = true;
intro.expiry = llarp::time_now_ms() + path::DEFAULT_LIFETIME;
}

bool Path::is_expired(std::chrono::milliseconds now) const
{
(void)now;
// if (_status == PathStatus::FAILED)
// return true;
// if (_status == PathStatus::BUILDING)
// return false;
// if (_status == PathStatus::TIMEOUT)
// {
// return now >= last_recv_msg + PathReanimationTimeout;
// }
// if (_status == PathStatus::ESTABLISHED or _status == PathStatus::IGNORE)
// {
// return now >= ExpireTime();
// }
return true;
return intro.is_expired(now);
}

std::string Path::name() const
{
return fmt::format("TX={} RX={}", upstream_txid().to_string(), upstream_rxid().to_string());
}

/* TODO: replace this with sending an onion-ed data message
bool Path::SendRoutingMessage(std::string payload, Router*)
{
std::string buf(MAX_LINK_MSG_SIZE / 2, '\0');
buf.insert(0, payload);
// make nonce
TunnelNonce N;
N.Randomize();
// pad smaller messages
if (payload.size() < PAD_SIZE)
{
// randomize padding
crypto::randbytes(
reinterpret_cast<unsigned char*>(buf.data()) + payload.size(), PAD_SIZE -
payload.size());
}
log::debug(logcat, "Sending {}B routing message to {}", buf.size(), Endpoint());
// TODO: path relaying here
return true;
}
*/

template <typename Samples_t>
static std::chrono::milliseconds computeLatency(const Samples_t& samps)
{
Expand Down
8 changes: 3 additions & 5 deletions llarp/path/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace llarp

std::weak_ptr<PathHandler> handler;

ClientIntro intro;
ClientIntro intro{};

std::chrono::milliseconds buildStarted = 0s;
std::chrono::milliseconds buildStarted{0s};

Path(
Router& rtr,
Expand Down Expand Up @@ -161,9 +161,7 @@ namespace llarp
static constexpr bool to_string_formattable = true;

private:
std::string make_outer_payload(ustring_view payload);

std::string make_outer_payload(ustring_view payload, SymmNonce& nonce);
std::string make_outer_payload(char* data, size_t len);

bool SendLatencyMessage(Router* r);

Expand Down
2 changes: 1 addition & 1 deletion llarp/path/path_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace llarp::path

for (auto& [_, p] : _path_map)
{
if (p->is_ready() and not p->intro.is_expired(now))
if (p->is_ready() and not p->is_expired(now))
intros.emplace(p->intro);
}

Expand Down
14 changes: 11 additions & 3 deletions llarp/path/path_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace llarp::path
size_t PathHandler::paths_at_time(std::chrono::milliseconds futureTime) const
{
size_t num = 0;
Lock_t l(paths_mutex);
Lock_t l{paths_mutex};

for (const auto& item : _paths)
{
Expand All @@ -188,11 +188,14 @@ namespace llarp::path
// called within the scope of locked mutex
void PathHandler::tick_paths()
{
Lock_t l{paths_mutex};

const auto now = llarp::time_now_ms();

for (auto& item : _paths)
for (auto& [_, p] : _paths)
{
item.second->Tick(now);
if (p)
p->Tick(now);
}
}

Expand Down Expand Up @@ -468,6 +471,8 @@ namespace llarp::path

bool PathHandler::build_path_to_random()
{
Lock_t l(paths_mutex);

if (auto maybe_hops = get_hops_to_random())
{
build(*maybe_hops);
Expand All @@ -480,6 +485,8 @@ namespace llarp::path

bool PathHandler::build_path_aligned_to_remote(const NetworkAddress& remote)
{
Lock_t l(paths_mutex);

if (auto maybe_hops = aligned_hops_to_remote(remote.router_id()))
{
build(*maybe_hops);
Expand Down Expand Up @@ -594,6 +601,7 @@ namespace llarp::path
return _router.send_control_message(std::move(upstream), "path_build", std::move(payload), std::move(handler));
}

// called within the scope of a locked mutex
void PathHandler::build(std::vector<RemoteRC> hops)
{
if (pre_build(hops); auto new_path = build1(hops))
Expand Down
2 changes: 1 addition & 1 deletion llarp/path/path_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace llarp
inline constexpr size_t MAX_PATHS{32};

// default number of paths per PathHandler
inline constexpr size_t DEFAULT_PATHS_HELD{1};
inline constexpr size_t DEFAULT_PATHS_HELD{4};

// forward declare
struct Path;
Expand Down

0 comments on commit 0805844

Please sign in to comment.