Skip to content

Commit

Permalink
squash
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Dec 9, 2024
1 parent 6d54176 commit e30f40e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 112 deletions.
17 changes: 7 additions & 10 deletions llarp/address/ip_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,12 @@ namespace llarp
return std::nullopt;
}

NetworkPacket IPPacket::make_netpkt()
NetworkPacket IPPacket::make_netpkt() &&
{
return NetworkPacket{oxen::quic::Path{_src_addr, _dst_addr}, bview()};
bstring data{};
data.reserve(_buf.size());
std::memcpy(data.data(), _buf.data(), _buf.size());
return NetworkPacket{oxen::quic::Path{_src_addr, _dst_addr}, std::move(data)};
}

bool IPPacket::load(const uint8_t* buf, size_t len)
Expand Down Expand Up @@ -341,10 +344,7 @@ namespace llarp

std::vector<uint8_t> IPPacket::steal_buffer() &&
{
std::vector<uint8_t> b;
b.resize(size());
_buf.swap(b);
return b;
return std::move(_buf);
}

std::string IPPacket::steal_payload() &&
Expand All @@ -356,10 +356,7 @@ namespace llarp

std::vector<uint8_t> IPPacket::give_buffer()
{
std::vector<uint8_t> b;
b.resize(size());
std::memcpy(b.data(), data(), size());
return b;
return {_buf};
}

std::string IPPacket::to_string()
Expand Down
2 changes: 1 addition & 1 deletion llarp/address/ip_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace llarp
static IPPacket from_netpkt(NetworkPacket pkt);
static std::optional<IPPacket> from_buffer(const uint8_t* buf, size_t len);

NetworkPacket make_netpkt();
NetworkPacket make_netpkt() &&;

bool is_ipv4() const { return _is_v4; }

Expand Down
23 changes: 3 additions & 20 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace llarp::handlers
// _router.loop()->call_later(10s, [this]() {
// try
// {
// RouterID cpk{oxenc::from_base32z("bx13pza3snxgnccpbz1dpry6zsmspn718f9kyo9sipp3bdc848oy")};
// RouterID cpk{oxenc::from_base32z("u3w71idt3i4ysq33bwdc7hdqgitb76bds3d3e8x4qkug56ga3ouo")};
// log::info(logcat, "Beginning session init to client: {}", cpk.to_network_address(false));
// _initiate_session(
// NetworkAddress::from_pubkey(cpk, true), [](ip_v) { log::critical(logcat, "FUCK YEAH");
Expand Down Expand Up @@ -436,7 +436,6 @@ namespace llarp::handlers
bool use_tun)
{
bool ret = true;
// assert(path->is_client_path());

auto inbound = std::make_shared<session::InboundSession>(
initiator, std::move(path), *this, std::move(tag), use_tun, std::move(kx_data));
Expand All @@ -463,7 +462,7 @@ namespace llarp::handlers
else
{
// TODO: if this fails, we should close the session
log::warning(logcat, "TUN devcice failed to route session (remote: {}) to local ip", session->remote());
log::warning(logcat, "TUN device failed to route session (remote: {}) to local ip", session->remote());
ret = false;
}
}
Expand Down Expand Up @@ -491,7 +490,7 @@ namespace llarp::handlers
if (not path or not path->is_ready())
continue;

log::debug(logcat, "Publishing ClientContact to pivot {}", path->pivot_rid());
log::debug(logcat, "Publishing ClientContact on {}", path->hop_string());

ret &= path->publish_client_contact(ecc, [](oxen::quic::message m) {
if (m)
Expand Down Expand Up @@ -562,11 +561,6 @@ namespace llarp::handlers
- 't' : Use Tun interface (bool)
- 'u' : Authentication field
- bt-encoded dict, values TBD
TODO:
- update logic: sessions to relays do not need a shared_kx_data type
- client <-> client rely on symmetric DH across aligned paths
- client <-> relay end at the pivot
*/
void SessionEndpoint::_make_session(
NetworkAddress remote,
Expand Down Expand Up @@ -697,17 +691,6 @@ namespace llarp::handlers

auto& pivot = intro.pivot_rid;

// TOTHINK: why would we ever have a path keyed to remote client intro pivot txid?
// if (auto path = _router.path_context()->get_path(intro.pivot_txid))
// {
// log::info(
// logcat,
// "Found path to pivot (rid: {}, tx_id: {}); initiating session!",
// intro.pivot_rid,
// intro.pivot_txid);
// return _make_session(std::move(remote), std::move(path), std::move(cb), is_exit);
// }

log::info(logcat, "Initiating session path-build to remote:{} via pivot:{}", remote, pivot);

auto maybe_hops = aligned_hops_to_remote(pivot);
Expand Down
8 changes: 7 additions & 1 deletion llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <llarp/nodedb.hpp>
#include <llarp/router/route_poker.hpp>
#include <llarp/router/router.hpp>
#include <llarp/util/logging/buffer.hpp>
#include <llarp/util/str.hpp>

namespace llarp::handlers
Expand Down Expand Up @@ -995,7 +996,12 @@ namespace llarp::handlers

if (auto session = _router.session_endpoint()->get_session(remote))
{
log::debug(logcat, "Dispatching outbound packet for session (remote: {})", remote);
log::debug(
logcat,
"Dispatching outbound {}B packet for session (remote: {}): {}",
pkt.size(),
remote,
buffer_printer{pkt.uview()});
session->send_path_data_message(std::move(pkt).steal_payload());
}
else
Expand Down
99 changes: 53 additions & 46 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ namespace llarp
}

return nullptr;
// return link_manager.router().loop()->call_get([this, rid = remote]() -> std::shared_ptr<link::Connection>
// {
// });
}

bool Endpoint::have_conn(const RouterID& remote) const
Expand Down Expand Up @@ -283,14 +280,7 @@ namespace llarp
{
log::debug(logcat, "Starting gossip ticker...");
_gossip_ticker = _router.loop()->call_every(
_router._gossip_interval,
[this]() {
log::critical(logcat, "Regenerating and gossiping RC...");
_router.relay_contact.resign();
_router.save_rc();
gossip_rc(_router.local_rid(), _router.relay_contact.to_remote());
},
true);
_router._gossip_interval, [this]() { regenerate_and_gossip_rc(); }, true);
}

LinkManager::LinkManager(Router& r)
Expand Down Expand Up @@ -747,33 +737,38 @@ namespace llarp
log::warning(logcat, "NodeDB query for {} random RCs for connection returned none", num_conns);
}

void LinkManager::regenerate_and_gossip_rc()
{
log::info(logcat, "Regenerating and gossiping RC...");
gossip_rc(_router.local_rid(), _router.relay_contact.to_remote());
_router.save_rc();
}

void LinkManager::gossip_rc(const RouterID& last_sender, const RemoteRC& rc)
{
_router.loop()->call([this, last_sender, rc]() {
int count = 0;
const auto& gossip_src = rc.router_id();
int count{};
const auto& gossip_src = rc.router_id();

for (auto& [rid, conn] : ep->service_conns)
{
// don't send back to the gossip source or the last sender
if (rid == gossip_src or rid == last_sender)
continue;

send_control_message(
rid, "gossip_rc"s, GossipRCMessage::serialize(last_sender, rc), [](oxen::quic::message) {
log::trace(logcat, "PLACEHOLDER FOR GOSSIP RC RESPONSE HANDLER");
});
++count;
}
for (auto& [rid, conn] : ep->service_conns)
{
if (not conn or not conn->is_active)
continue;

log::critical(logcat, "Dispatched {} GossipRC requests!", count);
});
// don't send back to the gossip source or the last sender
if (rid == gossip_src or rid == last_sender)
continue;

count += send_control_message(
rid, "gossip_rc"s, GossipRCMessage::serialize(last_sender, rc), [](oxen::quic::message) {
log::trace(logcat, "PLACEHOLDER FOR GOSSIP RC RESPONSE HANDLER");
});
}

log::critical(logcat, "Dispatched {} GossipRC requests!", count);
}

void LinkManager::handle_gossip_rc(oxen::quic::message m)
{
log::debug(logcat, "Handling GossipRC request...");

// RemoteRC constructor wraps deserialization in a try/catch
RemoteRC rc;
RouterID src;
Expand All @@ -792,13 +787,15 @@ namespace llarp
return;
}

log::trace(logcat, "Handling GossipRC request (sender:{}, rc:{})...", src, rc);

if (node_db->verify_store_gossip_rc(rc))
{
log::critical(logcat, "Received updated RC, forwarding to relay peers.");
log::info(logcat, "Received updated RC (rid:{}), forwarding to peers", rc.router_id());
gossip_rc(_router.local_rid(), rc);
}
else
log::debug(logcat, "Received known or old RC, not storing or forwarding.");
log::trace(logcat, "Received known or old RC, not storing or forwarding.");
}

// TODO: can probably use ::send_control_message instead. Need to discuss the potential
Expand Down Expand Up @@ -1342,10 +1339,11 @@ namespace llarp
return prev_message.respond(messages::OK_RESPONSE, false);
}

if (m.timed_out)
log::info(logcat, "Upstream timed out on path build; relaying timeout");
else
log::info(logcat, "Upstream returned path build failure; relaying response");
log::info(
logcat,
"Upstream ({}) returned path build {}; relaying...",
transit_hop->upstream(),
m.timed_out ? "time out" : "failure");

return prev_message.respond(m.body_str(), m.is_error());
});
Expand Down Expand Up @@ -1393,7 +1391,6 @@ namespace llarp

log::info(logcat, "Received path control for local client: {}", buffer_printer{payload});

// TESTNET:
for (auto& hop : path->hops)
{
nonce = crypto::onion(
Expand Down Expand Up @@ -1565,6 +1562,8 @@ namespace llarp
return;
}

log::debug(logcat, "Received path data for local relay: {}", buffer_printer{payload});

auto hop = _router.path_context()->get_transit_hop(hop_id);

if (not hop)
Expand All @@ -1573,34 +1572,42 @@ namespace llarp
return;
}

nonce = crypto::onion(
auto onion_nonce = nonce ^ hop->kx.xor_nonce;

crypto::onion(
reinterpret_cast<unsigned char*>(payload.data()),
payload.size(),
hop->kx.shared_secret,
nonce,
onion_nonce,
hop->kx.xor_nonce);

RouterID next_router;
std::string new_payload;

// if terminal hop, pass to the correct path expecting to receive this message
if (hop->terminal_hop)
{
HopID hop_id;
log::debug(logcat, "We are terminal hop for path data: {}: {}", hop->to_string(), buffer_printer{payload});

HopID ihid;
std::string intermediate;

try
{
std::tie(hop_id, intermediate) = PATH::DATA::deserialize_intermediate(oxenc::bt_dict_consumer{payload});
std::tie(ihid, intermediate) = PATH::DATA::deserialize_intermediate(oxenc::bt_dict_consumer{payload});
}
catch (const std::exception& e)
{
log::warning(logcat, "Path data intermediate payload exception: {}", e.what());
log::warning(
logcat, "Path data intermediate payload exception: {}: {}", e.what(), buffer_printer{payload});
return;
}

hop = _router.path_context()->get_transit_hop(hop_id);
hop = _router.path_context()->get_transit_hop(ihid);

if (not hop)
{
log::warning(logcat, "We are bridge node for path data message with unknown rxID: {}", hop_id);
log::warning(logcat, "We are bridge node for path data message with unknown rxID: {}", ihid);
return;
}

Expand All @@ -1612,9 +1619,9 @@ namespace llarp
auto hop_is_rx = hop->rxid() == hop_id;

const auto& next_id = hop_is_rx ? hop->txid() : hop->rxid();
const auto& next_router = hop_is_rx ? hop->upstream() : hop->downstream();
next_router = hop_is_rx ? hop->upstream() : hop->downstream();

std::string new_payload = ONION::serialize_hop(next_id.to_view(), nonce, std::move(payload));
new_payload = ONION::serialize_hop(next_id.to_view(), onion_nonce, std::move(payload));

send_data_message(next_router, std::move(new_payload));
}
Expand Down
7 changes: 4 additions & 3 deletions llarp/link/link_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ namespace llarp

const oxen::quic::Address& local() { return addr; }

void regenerate_and_gossip_rc();

void gossip_rc(const RouterID& last_sender, const RemoteRC& rc);

void handle_gossip_rc(oxen::quic::message m);
Expand Down Expand Up @@ -346,7 +348,6 @@ namespace llarp
_is_service_node ? RELAY_KEEP_ALIVE : CLIENT_KEEP_ALIVE,
std::forward<Opt>(opts)...);

// auto
auto control_stream = conn_interface->template open_stream<oxen::quic::BTRequestStream>(
[](oxen::quic::Stream&, uint64_t error_code) {
log::warning(logcat, "BTRequestStream closed unexpectedly (ec:{})", error_code);
Expand All @@ -365,7 +366,7 @@ namespace llarp
: conn_interface->send_datagram(std::move(body));

itr->second =
std::make_shared<link::Connection>(std::move(conn_interface), std::move(control_stream), true);
std::make_shared<link::Connection>(std::move(conn_interface), std::move(control_stream));

log::info(logcat, "Outbound connection to RID:{} added to service conns...", rid);
return true;
Expand Down Expand Up @@ -410,7 +411,7 @@ namespace llarp
link_manager.register_commands(control_stream, rid, not _is_service_node);

itr->second =
std::make_shared<link::Connection>(std::move(conn_interface), std::move(control_stream), true);
std::make_shared<link::Connection>(std::move(conn_interface), std::move(control_stream));

log::info(logcat, "Outbound connection to RID:{} added to service conns...", rid);
return true;
Expand Down
6 changes: 3 additions & 3 deletions llarp/messages/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ namespace llarp
throw std::runtime_error{messages::ERROR_RESPONSE};
}

log::info(logcat, "payload: {}", buffer_printer{payload});
log::trace(logcat, "payload: {}", buffer_printer{payload});

try
{
kx_data.server_dh(local_sk);
kx_data.decrypt(to_uspan(payload));

log::info(logcat, "xchacha -> payload: {}", buffer_printer{payload});
log::trace(logcat, "xchacha -> payload: {}", buffer_printer{payload});

kx_data.generate_xor();
}
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace llarp
throw std::runtime_error{BAD_CRYPTO};
}

log::critical(logcat, "TransitHop data successfully deserialized: {}", hop->to_string());
log::trace(logcat, "TransitHop data successfully deserialized: {}", hop->to_string());
return hop;
}
} // namespace BUILD
Expand Down
Loading

0 comments on commit e30f40e

Please sign in to comment.