diff --git a/daemon/lokinet.cpp b/daemon/lokinet.cpp index 1e4b406f4..3f930dc4d 100644 --- a/daemon/lokinet.cpp +++ b/daemon/lokinet.cpp @@ -444,6 +444,7 @@ namespace // TESTNET: oxen::log::set_level("quic", oxen::log::Level::critical); + oxen::log::set_level("quicverbose", oxen::log::Level::debug); if (configFile.has_value()) { diff --git a/external/oxen-libquic b/external/oxen-libquic index ce1b3c937..84721f477 160000 --- a/external/oxen-libquic +++ b/external/oxen-libquic @@ -1 +1 @@ -Subproject commit ce1b3c9372c317ab2914870cbd44c8630eaa96a5 +Subproject commit 84721f477f5dfa52e04a43072cc176a117dfdc58 diff --git a/llarp/context.cpp b/llarp/context.cpp index fb2418fe6..18cb8e5e1 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/llarp/endpoint_base.hpp b/llarp/endpoint_base.hpp index abf9e8ba8..1abdb3c8a 100644 --- a/llarp/endpoint_base.hpp +++ b/llarp/endpoint_base.hpp @@ -9,8 +9,8 @@ #include #include -#include #include +#include #include #include diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index b5b5c2050..a170bbf77 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -100,9 +100,9 @@ namespace llarp::exit // TODO: add callback here if (p->obtain_exit( exit_key, std::is_same_v ? 1 : 0, p->TXID().bt_encode())) - log::info(link_cat, "Asking {} for exit", exit_router); + log::info(logcat, "Asking {} for exit", exit_router); else - log::warning(link_cat, "Failed to send exit request"); + log::warning(logcat, "Failed to send exit request"); } void @@ -150,7 +150,7 @@ namespace llarp::exit const static auto roles = llarp::path::ePathRoleExit | llarp::path::ePathRoleSVC; if (p->SupportsAnyRoles(roles)) { - log::info(link_cat, "{} closing exit path", p->name()); + log::info(logcat, "{} closing exit path", p->name()); if (p->close_exit(exit_key, p->TXID().bt_encode())) p->ClearRoles(roles); else @@ -173,9 +173,9 @@ namespace llarp::exit // TODO: add callback here if (p->close_exit(exit_key, p->TXID().bt_encode())) - log::info(link_cat, ""); + log::info(logcat, ""); else - log::warning(link_cat, "{} failed to send exit close message", p->name()); + log::warning(logcat, "{} failed to send exit close message", p->name()); } }; ForEachPath(sendExitClose); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 8b680ab70..a67b14167 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -269,12 +269,12 @@ namespace llarp::handlers if (conf.is_reachable) { _publish_introset = true; - log::info(link_cat, "TunEndpoint setting to be reachable by default"); + log::info(logcat, "TunEndpoint setting to be reachable by default"); } else { _publish_introset = false; - log::info(link_cat, "TunEndpoint setting to be not reachable by default"); + log::info(logcat, "TunEndpoint setting to be not reachable by default"); } if (conf.auth_type == service::AuthType::FILE) diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index a1af33d64..8fd2eb8d3 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -60,13 +60,15 @@ namespace llarp bool Endpoint::have_client_conn(const RouterID& remote) const { - return client_conns.count(remote); + return link_manager.router().loop()->call_get( + [this, remote]() { return client_conns.count(remote); }); } bool Endpoint::have_service_conn(const RouterID& remote) const { - return service_conns.count(remote); + return link_manager.router().loop()->call_get( + [this, remote]() { return service_conns.count(remote); }); } std::pair @@ -156,7 +158,7 @@ namespace llarp void LinkManager::register_commands( - std::shared_ptr& s, const RouterID& router_id, bool) + const std::shared_ptr& s, const RouterID& router_id, bool) { log::debug(logcat, "{} called", __PRETTY_FUNCTION__); @@ -251,44 +253,54 @@ namespace llarp // callback set. It will reject any attempted inbound connection to a lokinet client prior to // handshake completion tls_creds->set_key_verify_callback([this](const ustring_view& key, const ustring_view& alpn) { - RouterID other{key.data()}; - auto us = router().is_bootstrap_seed() ? "Bootstrap seed node"s : "Service node"s; - auto is_snode = is_service_node(); + return _router.loop()->call_get([&]() { + RouterID other{key.data()}; + auto us = router().is_bootstrap_seed() ? "Bootstrap seed node"s : "Service node"s; + auto is_snode = is_service_node(); - if (is_snode) - { - if (alpn == alpns::C_ALPNS) + if (is_snode) { - log::critical(logcat, "{} node accepting client connection (remote ID:{})!", us, other); - ep.client_conns.emplace(other, nullptr); - return true; - } - if (alpn == alpns::SN_ALPNS) - { - // verify as service node! - bool result = node_db->registered_routers().count(other); + if (alpn == alpns::C_ALPNS) + { + log::critical(logcat, "{} node accepting client connection (remote ID:{})!", us, other); + ep.client_conns.emplace(other, nullptr); + return true; + } + if (alpn == alpns::SN_ALPNS) + { + // verify as service node! + bool result = node_db->registered_routers().count(other); + + log::critical( + logcat, + "{} node was {} to confirm remote (RID:{}) is registered; {} connection!", + us, + result ? "able" : "unable", + other, + result ? "allowing" : "rejecting"); + + if (result) + { + auto [_, b] = ep.service_conns.try_emplace(other, nullptr); - log::critical( - logcat, - "{} node was {} to confirm remote (RID:{}) is registered; {} connection!", - us, - result ? "able" : "unable", - other, - result ? "allowing" : "rejecting"); + if (not b) + log::critical( + logcat, "{} node rejecting inbound -- already have connection to remote!", us); - if (result) - ep.service_conns.emplace(other, nullptr); + return result and b; + } - return result; - } + return result; + } - log::critical(logcat, "{} node received unknown ALPN; rejecting connection!", us); - return false; - } + log::critical(logcat, "{} node received unknown ALPN; rejecting connection!", us); + return false; + } - // TESTNET: change this to an error message later; just because someone tries to erroneously - // connect to a local lokinet client doesn't mean we should kill the program? - throw std::runtime_error{"Clients should not be validating inbound connections!"}; + // TESTNET: change this to an error message later; just because someone tries to erroneously + // connect to a local lokinet client doesn't mean we should kill the program? + throw std::runtime_error{"Clients should not be validating inbound connections!"}; + }); }); if (_router.is_service_node()) { @@ -301,12 +313,12 @@ namespace llarp LinkManager::make_control(oxen::quic::connection_interface& ci, const RouterID& rid) { auto control_stream = ci.queue_incoming_stream( - [this, rid = rid](oxen::quic::Stream&, uint64_t error_code) { + [/* this, */ rid = rid](oxen::quic::Stream&, uint64_t error_code) { log::warning( logcat, "BTRequestStream closed unexpectedly (ec:{}); closing inbound connection...", error_code); - ep.close_connection(rid); + // ep.close_connection(rid); }); log::critical(logcat, "Queued BTStream to be opened (ID:{})", control_stream->stream_id()); @@ -325,7 +337,10 @@ namespace llarp if (auto it = ep.service_conns.find(rid); it != ep.service_conns.end()) { log::critical(logcat, "Configuring inbound connection from relay RID:{}", rid); - it->second = std::make_shared(ci.shared_from_this(), make_control(ci, rid)); + + if (!it->second) + it->second = + std::make_shared(ci.shared_from_this(), make_control(ci, rid)); } else if (auto it = ep.client_conns.find(rid); it != ep.client_conns.end()) { @@ -357,7 +372,8 @@ namespace llarp { log::critical( logcat, - "ERROR: connection established to RID:{} that was not logged in key verifrication!", + "ERROR: connection established to RID:{} that was not logged in connection " + "establishment!", rid); } } @@ -384,7 +400,7 @@ namespace llarp } else { - log::critical(logcat, "Outbound connection from {} (remote:{})", rid, remote); + log::critical(logcat, "Outbound connection to {} (remote:{})", rid, remote); on_outbound_conn(conn_interface); } }); diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 8c812cffa..2be9c4ef1 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -245,7 +246,7 @@ namespace llarp void register_commands( - std::shared_ptr& s, + const std::shared_ptr& s, const RouterID& rid, bool client_only = false); @@ -437,59 +438,71 @@ namespace llarp std::function func, Opt&&... opts) { - try - { - const auto& rid = rc.router_id(); - const auto& is_snode = _is_service_node; - const auto& is_control = ep.has_value(); - const auto us = is_snode ? "Relay"s : "Client"s; - - log::critical(logcat, "Establishing connection to RID:{}", rid); - // add to service conns - auto [itr, b] = service_conns.emplace(rid, nullptr); - - auto conn_interface = endpoint->connect( - remote, - link_manager.tls_creds, - is_snode ? ROUTER_KEEP_ALIVE : CLIENT_KEEP_ALIVE, - std::forward(opts)...); - - // auto - std::shared_ptr control_stream = - conn_interface->template open_stream( - [this, rid = rid](oxen::quic::Stream&, uint64_t error_code) { - log::warning( - logcat, - "BTRequestStream closed unexpectedly (ec:{}); closing outbound connection...", - error_code); - close_connection(rid); - }); - - if (is_snode) - link_manager.register_commands(control_stream, rid); - else - log::critical(logcat, "Client NOT registering BTStream commands!"); - - log::critical( - logcat, - "{} dispatching {} on outbound connection to remote (rid:{})", - us, - is_control ? "control message (ep:{})"_format(ep) : "data message", - rid); - - (is_control) ? control_stream->command(std::move(*ep), std::move(body), std::move(func)) - : conn_interface->send_datagram(std::move(body)); - - itr->second = std::make_shared(conn_interface, control_stream, true); - - log::critical(logcat, "Outbound connection to RID:{} added to service conns...", rid); - return true; - } - catch (...) - { - log::error(quic_cat, "Error: failed to establish connection to {}", remote); - return false; - } + return link_manager.router().loop()->call_get([&]() { + try + { + const auto& rid = rc.router_id(); + const auto& is_snode = _is_service_node; + const auto& is_control = ep.has_value(); + const auto us = is_snode ? "Relay"s : "Client"s; + + log::critical(logcat, "Establishing connection to RID:{}", rid); + // add to service conns + auto [itr, b] = service_conns.try_emplace(rid, nullptr); + + if (not b) + { + log::critical(logcat, "ERROR: attempting to establish an already-existing connection"); + (is_control) ? itr->second->control_stream->command( + std::move(*ep), std::move(body), std::move(func)) + : itr->second->conn->send_datagram(std::move(body)); + return true; + } + + auto conn_interface = endpoint->connect( + remote, + link_manager.tls_creds, + is_snode ? ROUTER_KEEP_ALIVE : CLIENT_KEEP_ALIVE, + std::forward(opts)...); + + // auto + std::shared_ptr control_stream = + conn_interface->template open_stream( + [rid = rid](oxen::quic::Stream&, uint64_t error_code) { + log::warning( + logcat, + "BTRequestStream closed unexpectedly (ec:{}); closing outbound " + "connection...", + error_code); + // close_connection(rid); + }); + + if (is_snode) + link_manager.register_commands(control_stream, rid); + else + log::critical(logcat, "Client NOT registering BTStream commands!"); + + log::critical( + logcat, + "{} dispatching {} on outbound connection to remote (rid:{})", + us, + is_control ? "control message (ep:{})"_format(ep) : "data message", + rid); + + (is_control) ? control_stream->command(std::move(*ep), std::move(body), std::move(func)) + : conn_interface->send_datagram(std::move(body)); + + itr->second = std::make_shared(conn_interface, control_stream, true); + + log::critical(logcat, "Outbound connection to RID:{} added to service conns...", rid); + return true; + } + catch (...) + { + log::error(quic_cat, "Error: failed to establish connection to {}", remote); + return false; + } + }); } template @@ -497,44 +510,52 @@ namespace llarp Endpoint::establish_connection( const oxen::quic::RemoteAddress& remote, const RemoteRC& rc, Opt&&... opts) { - try - { - const auto& rid = rc.router_id(); - const auto& is_snode = _is_service_node; - - log::critical(logcat, "Establishing connection to RID:{}", rid); - // add to service conns - auto [itr, b] = service_conns.emplace(rid, nullptr); - - auto conn_interface = endpoint->connect( - remote, - link_manager.tls_creds, - is_snode ? ROUTER_KEEP_ALIVE : CLIENT_KEEP_ALIVE, - std::forward(opts)...); - - auto control_stream = conn_interface->template open_stream( - [this, rid = rid](oxen::quic::Stream&, uint64_t error_code) { - log::warning( - logcat, - "BTRequestStream closed unexpectedly (ec:{}); closing outbound connection...", - error_code); - close_connection(rid); - }); - - if (is_snode) - link_manager.register_commands(control_stream, rid); - else - log::critical(logcat, "Client NOT registering BTStream commands!"); - itr->second = std::make_shared(conn_interface, control_stream, true); - - log::critical(logcat, "Outbound connection to RID:{} added to service conns...", rid); - return true; - } - catch (...) - { - log::error(quic_cat, "Error: failed to establish connection to {}", remote); - return false; - } + return link_manager.router().loop()->call_get([&]() { + try + { + const auto& rid = rc.router_id(); + const auto& is_snode = _is_service_node; + + log::critical(logcat, "Establishing connection to RID:{}", rid); + // add to service conns + auto [itr, b] = service_conns.try_emplace(rid, nullptr); + + if (not b) + { + log::critical(logcat, "ERROR: attempting to establish an already-existing connection"); + return b; + } + + auto conn_interface = endpoint->connect( + remote, + link_manager.tls_creds, + is_snode ? ROUTER_KEEP_ALIVE : CLIENT_KEEP_ALIVE, + std::forward(opts)...); + + auto control_stream = conn_interface->template open_stream( + [rid = rid](oxen::quic::Stream&, uint64_t error_code) { + log::warning( + logcat, + "BTRequestStream closed unexpectedly (ec:{}); closing outbound connection...", + error_code); + // close_connection(rid); + }); + + if (is_snode) + link_manager.register_commands(control_stream, rid); + else + log::critical(logcat, "Client NOT registering BTStream commands!"); + itr->second = std::make_shared(conn_interface, control_stream, true); + + log::critical(logcat, "Outbound connection to RID:{} added to service conns...", rid); + return true; + } + catch (...) + { + log::error(quic_cat, "Error: failed to establish connection to {}", remote); + return false; + } + }); } } // namespace link } // namespace llarp diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 5b9eb6bce..c7f64c3be 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -2,6 +2,7 @@ #include "crypto/types.hpp" #include "dht/kademlia.hpp" +#include "link/link_manager.hpp" #include "messages/fetch.hpp" #include "router_contact.hpp" #include "util/time.hpp" diff --git a/llarp/router/route_poker.cpp b/llarp/router/route_poker.cpp index d578ff7d4..8ad000193 100644 --- a/llarp/router/route_poker.cpp +++ b/llarp/router/route_poker.cpp @@ -2,6 +2,7 @@ #include "router.hpp" +#include #include namespace llarp diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 85e3b7ba3..329674ed1 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -381,6 +382,7 @@ namespace llarp // TESTNET: oxen::log::set_level("quic", oxen::log::Level::critical); + oxen::log::set_level("quicverbose", oxen::log::Level::debug); log::debug(logcat, "Configuring router"); @@ -1076,7 +1078,7 @@ namespace llarp continue; } - log::debug(logcat, "Establishing session to {} for service node testing", router); + log::critical(logcat, "Establishing session to {} for service node testing", router); // try to make a session to this random router // this will do a dht lookup if needed diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index ea2774e15..bc1f67ccf 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +// #include #include #include #include @@ -37,8 +37,15 @@ #include #include +namespace llarp::link +{ + struct Connection; +} + namespace llarp { + struct LinkManager; + /// number of routers to publish to inline constexpr size_t INTROSET_RELAY_REDUNDANCY{2}; diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 3068dd54c..79dfd9795 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -12,8 +12,8 @@ #include #include -#include #include +#include #include #include diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 70ab4c28d..6c06af647 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -214,7 +215,7 @@ namespace llarp::service } catch (...) { - log::warning(link_cat, "Failed to parse find name response!"); + log::warning(logcat, "Failed to parse find name response!"); return resultHandler({}); } @@ -797,7 +798,7 @@ namespace llarp::service // return; // } - log::info(link_cat, "{} looking up ONS name {}", Name(), name); + log::info(logcat, "{} looking up ONS name {}", Name(), name); auto paths = GetUniqueEndpointsForLookup(); // // not enough paths @@ -828,14 +829,14 @@ namespace llarp::service auto status = btdc.require(messages::STATUS_KEY); if (status != "OK"sv) { - log::info(link_cat, "Error on ONS lookup: {}", status); + log::info(logcat, "Error on ONS lookup: {}", status); func(std::string{status}, false); } name = btdc.require("NAME"); } catch (...) { - log::warning(link_cat, "Failed to parse find name response!"); + log::warning(logcat, "Failed to parse find name response!"); func("ERROR"s, false); } @@ -844,7 +845,7 @@ namespace llarp::service for (const auto& path : chosenpaths) { - log::info(link_cat, "{} lookup {} from {}", Name(), name, path->Endpoint()); + log::info(logcat, "{} lookup {} from {}", Name(), name, path->Endpoint()); path->find_name(name, response_cb); } } @@ -1329,14 +1330,14 @@ namespace llarp::service auto status = btdc.require(messages::STATUS_KEY); if (status != "OK"sv) { - log::info(link_cat, "Error in find intro set response: {}", status); + log::info(logcat, "Error in find intro set response: {}", status); return; } introset = btdc.require("INTROSET"); } catch (...) { - log::warning(link_cat, "Failed to parse find name response!"); + log::warning(logcat, "Failed to parse find name response!"); throw; } @@ -1369,11 +1370,11 @@ namespace llarp::service { if (tag.IsZero()) { - log::warning(link_cat, "SendToOrQueue failed: convo tag is zero"); + log::warning(logcat, "SendToOrQueue failed: convo tag is zero"); return false; } - log::debug(link_cat, "{} sending {} bytes (Tag: {})", Name(), payload.size(), tag); + log::debug(logcat, "{} sending {} bytes (Tag: {})", Name(), payload.size(), tag); if (auto maybe = GetEndpointWithConvoTag(tag)) { @@ -1387,7 +1388,7 @@ namespace llarp::service } } - log::debug(link_cat, "SendToOrQueue failed: no endpoint for convo tag {}", tag); + log::debug(logcat, "SendToOrQueue failed: no endpoint for convo tag {}", tag); return false; } diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index e3e2b6e16..a8099aa4c 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -5,6 +5,7 @@ #include "endpoint_util.hpp" #include "protocol_type.hpp" +#include #include #include @@ -167,7 +168,7 @@ namespace llarp::service if (updatingIntroSet or marked_bad or now < last_introset_update + IntrosetUpdateInterval) return; - log::info(link_cat, "{} updating introset", Name()); + log::info(logcat, "{} updating introset", Name()); last_introset_update = now; const auto paths = GetManyPathsWithUniqueEndpoints(&ep, 2, location); @@ -178,7 +179,7 @@ namespace llarp::service path->find_intro(location, false, relayOrder, [this](std::string resp) mutable { if (marked_bad) { - log::info(link_cat, "Outbound context has been marked bad (whatever that means)"); + log::info(logcat, "Outbound context has been marked bad (whatever that means)"); return; } @@ -192,14 +193,14 @@ namespace llarp::service auto status = btdc.require(messages::STATUS_KEY); if (status != "OK"sv) { - log::info(link_cat, "Error in find intro set response: {}", status); + log::info(logcat, "Error in find intro set response: {}", status); return; } introset = btdc.require("INTROSET"); } catch (...) { - log::warning(link_cat, "Failed to parse find name response!"); + log::warning(logcat, "Failed to parse find name response!"); throw; } @@ -208,12 +209,12 @@ namespace llarp::service if (intro.time_signed == 0s) { - log::warning(link_cat, "{} recieved introset with zero timestamp"); + log::warning(logcat, "{} recieved introset with zero timestamp"); return; } if (current_intro.time_signed > intro.time_signed) { - log::info(link_cat, "{} received outdated introset; dropping", Name()); + log::info(logcat, "{} received outdated introset; dropping", Name()); return; } @@ -223,7 +224,7 @@ namespace llarp::service if (intro.IsExpired(llarp::time_now_ms())) { - log::warning(link_cat, "{} received expired introset", Name()); + log::warning(logcat, "{} received expired introset", Name()); return; } @@ -605,7 +606,7 @@ namespace llarp::service if (generated_convo_intro) { - log::warning(link_cat, "{} has generated an unsent initial handshake; dropping packet"); + log::warning(logcat, "{} has generated an unsent initial handshake; dropping packet"); return; } diff --git a/llarp/vpn/platform.hpp b/llarp/vpn/platform.hpp index 367664a38..f026d5266 100644 --- a/llarp/vpn/platform.hpp +++ b/llarp/vpn/platform.hpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include #include