diff --git a/llarp/address/ip_packet.cpp b/llarp/address/ip_packet.cpp index e28fa45937..3ac0b77f94 100644 --- a/llarp/address/ip_packet.cpp +++ b/llarp/address/ip_packet.cpp @@ -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) @@ -341,10 +344,7 @@ namespace llarp std::vector IPPacket::steal_buffer() && { - std::vector b; - b.resize(size()); - _buf.swap(b); - return b; + return std::move(_buf); } std::string IPPacket::steal_payload() && @@ -356,10 +356,7 @@ namespace llarp std::vector IPPacket::give_buffer() { - std::vector b; - b.resize(size()); - std::memcpy(b.data(), data(), size()); - return b; + return {_buf}; } std::string IPPacket::to_string() diff --git a/llarp/address/ip_packet.hpp b/llarp/address/ip_packet.hpp index b2f879bbbd..c3c87ee865 100644 --- a/llarp/address/ip_packet.hpp +++ b/llarp/address/ip_packet.hpp @@ -56,7 +56,7 @@ namespace llarp static IPPacket from_netpkt(NetworkPacket pkt); static std::optional from_buffer(const uint8_t* buf, size_t len); - NetworkPacket make_netpkt(); + NetworkPacket make_netpkt() &&; bool is_ipv4() const { return _is_v4; } diff --git a/llarp/handlers/session.cpp b/llarp/handlers/session.cpp index b3454f81b0..b92f2b6456 100644 --- a/llarp/handlers/session.cpp +++ b/llarp/handlers/session.cpp @@ -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("6e9wdnd4cj3j3rgc9ze8ctxqj4z976tmu8osbzwgabruabb4u1ky")}; // 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"); @@ -436,7 +436,6 @@ namespace llarp::handlers bool use_tun) { bool ret = true; - // assert(path->is_client_path()); auto inbound = std::make_shared( initiator, std::move(path), *this, std::move(tag), use_tun, std::move(kx_data)); @@ -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; } } @@ -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) @@ -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, @@ -601,14 +595,24 @@ namespace llarp::handlers path->send_path_control_message( "path_control", std::move(intermediate_payload), - [this, remote, tag, path, hook = std::move(cb), session_keys = std::move(kx_data)]( - oxen::quic::message m) mutable { + [this, + remote, + tag, + path, + hook = std::move(cb), + session_keys = std::move(kx_data), + remote_intro = std::move(remote_intro)](oxen::quic::message m) mutable { if (m) { log::critical(logcat, "Call to InitiateSession succeeded!"); auto outbound = std::make_shared( - remote, *this, std::move(path), std::move(tag), std::move(session_keys)); + remote, + *this, + std::move(path), + std::move(tag), + std::move(session_keys), + std::move(remote_intro)); auto [session, _] = _sessions.insert_or_assign(std::move(remote), std::move(outbound)); @@ -697,17 +701,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); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index c7b7984b5e..bc8ec16dcc 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace llarp::handlers @@ -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 diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index 2d5ac13848..64cd66d397 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -67,9 +67,6 @@ namespace llarp } return nullptr; - // return link_manager.router().loop()->call_get([this, rid = remote]() -> std::shared_ptr - // { - // }); } bool Endpoint::have_conn(const RouterID& remote) const @@ -282,15 +279,7 @@ namespace llarp void LinkManager::start_tickers() { 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); + _gossip_ticker = _router.loop()->call_every(_router._gossip_interval, [this]() { regenerate_and_gossip_rc(); }); } LinkManager::LinkManager(Router& r) @@ -327,7 +316,7 @@ namespace llarp make_static_secret(_router.identity()), [this](oxen::quic::connection_interface& ci) { return on_conn_open(ci); }, [this](oxen::quic::connection_interface& ci, uint64_t ec) { return on_conn_closed(ci, ec); }, - [this](oxen::quic::dgram_interface&, bstring dgram) { handle_path_data_message(std::move(dgram)); }, + [this](oxen::quic::dgram_interface&, bstring dgram) { return handle_path_data_message(std::move(dgram)); }, is_service_node() ? alpns::SERVICE_INBOUND : alpns::CLIENT_INBOUND, is_service_node() ? alpns::SERVICE_OUTBOUND : alpns::CLIENT_OUTBOUND, oxen::quic::opt::enable_datagrams{}); @@ -747,33 +736,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; @@ -792,13 +786,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 @@ -1342,10 +1338,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()); }); @@ -1393,7 +1390,6 @@ namespace llarp log::info(logcat, "Received path control for local client: {}", buffer_printer{payload}); - // TESTNET: for (auto& hop : path->hops) { nonce = crypto::onion( @@ -1444,15 +1440,18 @@ namespace llarp hop->to_string(), buffer_printer{*inner_body}); - auto hop_is_rx = hop->rxid() == hop_id; + auto next_ids = hop->next_id(hop_id); - const auto& next_id = hop_is_rx ? hop->txid() : hop->rxid(); - const auto& next_router = hop_is_rx ? hop->upstream() : hop->downstream(); + if (not next_ids) + { + log::error(logcat, "Failed to query hop ({}) for next ids (input: {})", hop->to_string(), hop_id); + return m.respond(messages::ERROR_RESPONSE, true); + } - std::string new_payload = ONION::serialize_hop(next_id.to_view(), onion_nonce, std::move(payload)); + std::string new_payload = ONION::serialize_hop(next_ids->second.to_view(), onion_nonce, std::move(payload)); send_control_message( - next_router, + next_ids->first, "path_control", std::move(new_payload), [hop_weak = hop->weak_from_this(), hop_id, prev_message = std::move(m)]( @@ -1501,122 +1500,161 @@ namespace llarp return _handle_path_control(std::move(m)); } - void LinkManager::handle_path_data_message(bstring message) + void LinkManager::handle_path_data_message(bstring data) { - HopID hop_id; - std::string payload; - SymmNonce nonce; - - try - { - std::tie(hop_id, nonce, payload) = ONION::deserialize_hop(oxenc::bt_dict_consumer{message}); - } - catch (const std::exception& e) - { - log::warning(logcat, "Exception: {}", e.what()); - return; - } - - if (!_is_service_node) - { - auto path = _router.path_context()->get_path(hop_id); + _router.loop()->call([this, message = std::move(data)]() { + HopID hop_id; + std::string payload; + SymmNonce nonce; - if (not path) + try + { + std::tie(hop_id, nonce, payload) = ONION::deserialize_hop(oxenc::bt_dict_consumer{message}); + } + catch (const std::exception& e) { - log::warning(logcat, "Client received path data with unknown rxID: {}", hop_id); + log::warning(logcat, "Exception: {}", e.what()); return; } - log::info(logcat, "Received path data for local client: {}", buffer_printer{payload}); - - for (auto& hop : path->hops) + if (!_is_service_node) { - nonce = crypto::onion( - reinterpret_cast(payload.data()), - payload.size(), - hop.kx.shared_secret, - nonce, - hop.kx.xor_nonce); + auto path = _router.path_context()->get_path(hop_id); - log::trace(logcat, "xchacha20 -> {}", buffer_printer{payload}); - } + if (not path) + { + log::warning(logcat, "Client received path data with unknown rxID: {}", hop_id); + return; + } - NetworkAddress sender; - bstring data; + log::info(logcat, "Received path data for local client: {}", buffer_printer{payload}); - try - { - oxenc::bt_dict_consumer btdc{payload}; - std::tie(sender, data) = PATH::DATA::deserialize(btdc); + for (auto& hop : path->hops) + { + nonce = crypto::onion( + reinterpret_cast(payload.data()), + payload.size(), + hop.kx.shared_secret, + nonce, + hop.kx.xor_nonce); + + log::debug(logcat, "xchacha20 -> {}", buffer_printer{payload}); + } + + NetworkAddress sender; + bstring data; - if (auto session = _router.session_endpoint()->get_session(sender)) + try { - session->recv_path_data_message(std::move(data)); + oxenc::bt_dict_consumer btdc{payload}; + std::tie(sender, data) = PATH::DATA::deserialize(btdc); + + if (auto session = _router.session_endpoint()->get_session(sender)) + { + session->recv_path_data_message(std::move(data)); + } + else + { + log::warning(logcat, "Could not find session (remote:{}) to relay path data message!", sender); + } } - else + catch (const std::exception& e) { - log::warning(logcat, "Could not find session (remote:{}) to relay path data message!", sender); + log::warning(logcat, "Exception: {}: {}", e.what(), buffer_printer{data}); } + return; } - catch (const std::exception& e) - { - log::warning(logcat, "Exception: {}", e.what()); - } - return; - } - auto hop = _router.path_context()->get_transit_hop(hop_id); + log::debug(logcat, "Received path data for local relay: {}", buffer_printer{payload}); - if (not hop) - { - log::warning(logcat, "Received path data with unknown next hop (ID: {})", hop_id); - return; - } + auto hop = _router.path_context()->get_transit_hop(hop_id); - nonce = crypto::onion( - reinterpret_cast(payload.data()), - payload.size(), - hop->kx.shared_secret, - nonce, - hop->kx.xor_nonce); - - // if terminal hop, pass to the correct path expecting to receive this message - if (hop->terminal_hop) - { - HopID hop_id; - std::string intermediate; - - try - { - std::tie(hop_id, intermediate) = PATH::DATA::deserialize_intermediate(oxenc::bt_dict_consumer{payload}); - } - catch (const std::exception& e) + if (not hop) { - log::warning(logcat, "Path data intermediate payload exception: {}", e.what()); + log::warning(logcat, "Received path data with unknown next hop (ID: {})", hop_id); return; } - hop = _router.path_context()->get_transit_hop(hop_id); + auto onion_nonce = nonce ^ hop->kx.xor_nonce; - if (not hop) + crypto::onion( + reinterpret_cast(payload.data()), + payload.size(), + hop->kx.shared_secret, + onion_nonce, + hop->kx.xor_nonce); + + std::optional> next_ids = std::nullopt; + std::string next_payload; + + // if terminal hop, pass to the correct path expecting to receive this message + if (hop->terminal_hop) { - log::warning(logcat, "We are bridge node for path data message with unknown rxID: {}", hop_id); - return; - } + log::debug( + logcat, "We are terminal hop for path data: {}: {}", hop->to_string(), buffer_printer{payload}); - payload = std::move(intermediate); - log::debug(logcat, "Bridging path data message on hop: {}", hop->to_string()); - } + HopID ihid; + std::string intermediate; + + try + { + 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(), buffer_printer{payload}); + return; + } + + log::debug(logcat, "Inbound path rxid:{}, outbound path txid:{}", hop_id, ihid); + + auto next_hop = _router.path_context()->get_transit_hop(ihid); - // if not terminal hop, relay datagram onwards - auto hop_is_rx = hop->rxid() == hop_id; + if (not next_hop) + { + log::warning(logcat, "We are bridge node for path data message with unknown txID: {}", ihid); + return; + } - const auto& next_id = hop_is_rx ? hop->txid() : hop->rxid(); - const auto& next_router = hop_is_rx ? hop->upstream() : hop->downstream(); + log::debug(logcat, "Bridging path data message on hop: {}", next_hop->to_string()); - std::string new_payload = ONION::serialize_hop(next_id.to_view(), nonce, std::move(payload)); + next_ids = next_hop->next_id(ihid); - send_data_message(next_router, std::move(new_payload)); + onion_nonce ^= next_hop->kx.xor_nonce; + + crypto::onion( + reinterpret_cast(intermediate.data()), + intermediate.size(), + next_hop->kx.shared_secret, + onion_nonce, + next_hop->kx.xor_nonce); + + if (not next_ids) + { + log::error( + logcat, "Failed to query hop ({}) for next ids (input: {})", next_hop->to_string(), hop_id); + return; + } + + next_payload = ONION::serialize_hop(next_ids->second.to_view(), onion_nonce, std::move(intermediate)); + } + else + { + next_ids = hop->next_id(hop_id); + + if (not next_ids) + { + log::error(logcat, "Failed to query hop ({}) for next ids (input: {})", hop->to_string(), hop_id); + return; + } + + next_payload = ONION::serialize_hop(next_ids->second.to_view(), onion_nonce, std::move(payload)); + } + + send_data_message(next_ids->first, std::move(next_payload)); + }); } void LinkManager::handle_path_request(oxen::quic::message m, std::string payload) diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 2a554d224b..4656a792ab 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -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); @@ -346,7 +348,6 @@ namespace llarp _is_service_node ? RELAY_KEEP_ALIVE : CLIENT_KEEP_ALIVE, std::forward(opts)...); - // auto auto control_stream = conn_interface->template open_stream( [](oxen::quic::Stream&, uint64_t error_code) { log::warning(logcat, "BTRequestStream closed unexpectedly (ec:{})", error_code); @@ -365,7 +366,7 @@ namespace llarp : conn_interface->send_datagram(std::move(body)); itr->second = - std::make_shared(std::move(conn_interface), std::move(control_stream), true); + std::make_shared(std::move(conn_interface), std::move(control_stream)); log::info(logcat, "Outbound connection to RID:{} added to service conns...", rid); return true; @@ -410,7 +411,7 @@ namespace llarp link_manager.register_commands(control_stream, rid, not _is_service_node); itr->second = - std::make_shared(std::move(conn_interface), std::move(control_stream), true); + std::make_shared(std::move(conn_interface), std::move(control_stream)); log::info(logcat, "Outbound connection to RID:{} added to service conns...", rid); return true; diff --git a/llarp/messages/path.hpp b/llarp/messages/path.hpp index 1a62b343e6..69d5bac675 100644 --- a/llarp/messages/path.hpp +++ b/llarp/messages/path.hpp @@ -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(); } @@ -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 diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 4aa4cbb98b..fcadfa5053 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -466,10 +466,6 @@ namespace llarp auto& src = fetch_source; - // TESTNET: - // rid_sources.emplace(oxenc::from_base32z("55fxrrdt9ggkra9yoi58gbespa13is1sqqrykdzjamgkxrq91tto")); - // auto& src = _bootstraps.current().router_id(); - for (const auto& target : rid_sources) { if (target == src) @@ -943,8 +939,18 @@ namespace llarp if (rid == _router.local_rid()) return false; - known_rcs.erase(rc); - rc_lookup.erase(rid); + // Use the rc_lookup RemoteRC to delete from known_rcs, as the differing timestamp between the old and new will + // result in set::insert not matching to the previous value + if (auto it = rc_lookup.find(rid); it != rc_lookup.end()) + { + known_rcs.erase(it->second); + rc_lookup.erase(it); + } + else + { + known_rcs.erase(rc); + rc_lookup.erase(rid); + } auto [itr, b] = known_rcs.insert(std::move(rc)); ret &= b; @@ -980,7 +986,8 @@ namespace llarp return false; } - return put_rc(rc); + put_rc(std::move(rc)); + return true; } void NodeDB::remove_many_from_disk_async(std::unordered_set remove) const diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 7faafe5a11..ec310cdfca 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -76,14 +76,6 @@ namespace llarp::path hops.back().terminal_hop = true; - // _local_hop = std::make_shared(); - // _local_hop->_rid = _router.local_rid(); - // _local_hop->_upstream = hops.front()._rid; - // _local_hop->_downstream = _local_hop->_rid; - // _local_hop->_rxid = HopID::make_random(); - // _local_hop->_txid = hops.front()._rxid; - // _local_hop->terminal_hop = true; - log::trace(logcat, "Path populated with hops: {}", hop_string()); // initialize parts of the clientintro @@ -208,7 +200,6 @@ namespace llarp::path { auto inner_payload = PATH::DATA::serialize(std::move(data), _router.local_rid()); auto outer_payload = make_path_message(std::move(inner_payload)); - return _router.send_data_message(upstream_rid(), std::move(outer_payload)); } @@ -217,7 +208,6 @@ namespace llarp::path { auto inner_payload = PATH::CONTROL::serialize(std::move(endpoint), std::move(body)); auto outer_payload = make_path_message(std::move(inner_payload)); - return _router.send_control_message(upstream_rid(), "path_control", std::move(outer_payload), std::move(func)); } diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 7c8d03a71f..78a87d3c25 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -124,6 +124,8 @@ namespace llarp bool send_path_data_message(std::string body); + std::string make_path_message(std::string payload); + bool is_established() const { return _established; } bool is_ready(std::chrono::milliseconds now = llarp::time_now_ms()) const; @@ -165,9 +167,7 @@ namespace llarp std::string to_string() const; static constexpr bool to_string_formattable = true; - private: - std::string make_path_message(std::string payload); - + protected: void populate_internals(const std::vector& _hops); /// call obtained exit hooks diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index 25145beb20..9f7879e1e3 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -35,7 +35,7 @@ namespace llarp::path // generate hash of hop key for nonce mutation kx.generate_xor(); - log::critical(logcat, "TransitHop data successfully deserialized: {}", to_string()); + log::trace(logcat, "TransitHop data successfully deserialized: {}", to_string()); } void TransitHop::bt_decode(oxenc::bt_dict_consumer&& btdc) @@ -57,6 +57,18 @@ namespace llarp::path return std::move(btdp).str(); } + std::optional> TransitHop::next_id(const HopID& h) const + { + std::optional> ret = std::nullopt; + + if (h == _rxid) + ret = {_upstream, _txid}; + else if (h == _txid) + ret = {_downstream, _rxid}; + + return ret; + } + nlohmann::json TransitHop::ExtractStatus() const { return { diff --git a/llarp/path/transit_hop.hpp b/llarp/path/transit_hop.hpp index 54a13cacfe..07d82b24a5 100644 --- a/llarp/path/transit_hop.hpp +++ b/llarp/path/transit_hop.hpp @@ -51,6 +51,8 @@ namespace llarp HopID txid() { return _txid; } const HopID& txid() const { return _txid; } + std::optional> next_id(const HopID& h) const; + bool operator<(const TransitHop& other) const { return std::tie(_txid, _rxid, _upstream, _downstream) diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 648f44721e..436c3b16d9 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -687,7 +687,7 @@ namespace llarp // All relays have TUN if (_using_tun = conf.network.init_tun; _using_tun) { - log::critical(logcat, "Initializing virtual TUN device..."); + log::debug(logcat, "Initializing virtual TUN device..."); init_tun(); } @@ -757,16 +757,10 @@ namespace llarp void Router::save_rc() { - // _node_db->put_rc(router_contact.view()); log::info(logcat, "Saving RC file to {}", our_rc_file); queue_disk_io([&]() { relay_contact.write(our_rc_file); }); } - // bool Router::is_bootstrap_node(const RouterID r) const - // { - // return _node_db->is_bootstrap_node(r); - // } - bool Router::should_report_stats(std::chrono::milliseconds now) const { return now - _last_stats_report > REPORT_STATS_INTERVAL; diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 24295afba7..02b29d3979 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -52,8 +52,8 @@ namespace llarp // inline constexpr size_t INTROSET_STORAGE_REDUNDANCY{(INTROSET_RELAY_REDUNDANCY * INTROSET_REQS_PER_RELAY)}; // TESTNET: these constants are shortened for testing purposes - inline constexpr std::chrono::milliseconds TESTNET_GOSSIP_INTERVAL{10min}; - inline constexpr std::chrono::milliseconds RC_UPDATE_INTERVAL{10min}; + inline constexpr std::chrono::milliseconds TESTNET_GOSSIP_INTERVAL{5min}; + inline constexpr std::chrono::milliseconds RC_UPDATE_INTERVAL{5min}; inline constexpr std::chrono::milliseconds INITIAL_ATTEMPT_INTERVAL{30s}; // as we advance towards full mesh, we try to connect to this number per tick inline constexpr int FULL_MESH_ITERATION{1}; diff --git a/llarp/session/session.cpp b/llarp/session/session.cpp index 7ce5fb6b28..02d6b1864d 100644 --- a/llarp/session/session.cpp +++ b/llarp/session/session.cpp @@ -22,7 +22,8 @@ namespace llarp::session SessionTag _t, bool use_tun, bool is_outbound, - std::optional kx_data) + std::optional kx_data, + std::optional _remote_intro) : _r{r}, _parent{parent}, _tag{std::move(_t)}, @@ -33,6 +34,8 @@ namespace llarp::session { if (kx_data.has_value()) session_keys = std::move(*kx_data); + if (_remote_intro.has_value()) + remote_intro = std::move(*_remote_intro); set_new_current_path(std::move(_p)); } @@ -45,10 +48,12 @@ namespace llarp::session bool BaseSession::send_path_data_message(std::string data) { + // session_keys.encrypt(to_uspan(data)); auto inner_payload = PATH::DATA::serialize(std::move(data), _r.local_rid()); auto intermediate_payload = - PATH::DATA::serialize_intermediate(std::move(inner_payload), _current_path->intro.pivot_txid); - return _r.send_data_message(_current_path->upstream_rid(), std::move(data)); + PATH::DATA::serialize_intermediate(std::move(inner_payload), remote_intro.pivot_txid); + return _r.send_data_message( + _current_path->upstream_rid(), _current_path->make_path_message(std::move(intermediate_payload))); } void BaseSession::recv_path_data_message(bstring body) @@ -62,8 +67,7 @@ namespace llarp::session _current_path->unlink_session(); _current_path = std::move(_new_path); - - _pivot_txid = _current_path->pivot_rxid(); + _pivot_txid = _current_path->pivot_txid(); if (_use_tun) _current_path->link_session([this](bstring data) { @@ -162,7 +166,8 @@ namespace llarp::session handlers::SessionEndpoint& parent, std::shared_ptr path, SessionTag _t, - std::optional kx_data) + std::optional kx_data, + std::optional remote_intro) : PathHandler{parent._router, path::DEFAULT_PATHS_HELD}, BaseSession{ _router, @@ -172,7 +177,8 @@ namespace llarp::session std::move(_t), _router.using_tun_if(), true, - std::move(kx_data)}, + std::move(kx_data), + std::move(remote_intro)}, _is_snode_session{not _remote.is_client()}, _last_use{_router.now()} { diff --git a/llarp/session/session.hpp b/llarp/session/session.hpp index 24263a63bb..71eac6c511 100644 --- a/llarp/session/session.hpp +++ b/llarp/session/session.hpp @@ -49,6 +49,8 @@ namespace llarp shared_kx_data session_keys{}; + ClientIntro remote_intro; + bool _use_tun; bool _is_outbound; @@ -80,7 +82,8 @@ namespace llarp SessionTag _t, bool use_tun, bool is_outbound, - std::optional kx_data = std::nullopt); + std::optional kx_data = std::nullopt, + std::optional remote_intro = std::nullopt); virtual ~BaseSession() = default; @@ -122,7 +125,8 @@ namespace llarp handlers::SessionEndpoint& parent, std::shared_ptr path, SessionTag _t, - std::optional kx_data = std::nullopt); + std::optional kx_data = std::nullopt, + std::optional remote_intro = std::nullopt); ~OutboundSession() override;