From 674edab31f19fe91274ac6f0e1f3cfd83bd6f934 Mon Sep 17 00:00:00 2001 From: dr7ana Date: Thu, 21 Dec 2023 11:28:24 -0800 Subject: [PATCH] No fetch only throw --- llarp/link/link_manager.cpp | 7 +++---- llarp/messages/fetch.hpp | 15 ++++++++++++--- llarp/nodedb.cpp | 37 ++++++++++++++++++++++--------------- llarp/router_id.hpp | 3 +++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index ff14edab3..b850ec462 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -960,7 +960,7 @@ namespace llarp LinkManager::fetch_router_ids( const RouterID& via, std::string payload, std::function func) { - send_control_message(via, "fetch_router_ids"s, std::move(payload), std::move(func)); + send_control_message(via, "fetch_rids"s, std::move(payload), std::move(func)); } void @@ -974,8 +974,7 @@ namespace llarp try { oxenc::bt_dict_consumer btdc{m.body()}; - - source.from_string(btdc.require("source")); + source = RouterID{btdc.require("source")}; } catch (const std::exception& e) { @@ -993,7 +992,7 @@ namespace llarp log::critical(logcat, "Relaying FetchRID request to intended target RID:{}", source); send_control_message( source, - "fetch_router_ids"s, + "fetch_rids"s, m.body_str(), [source_rid = std::move(source), original = std::move(m)](oxen::quic::message m) mutable { original.respond(m.body_str(), m.is_error()); diff --git a/llarp/messages/fetch.hpp b/llarp/messages/fetch.hpp index af6ffe683..f8c194ff7 100644 --- a/llarp/messages/fetch.hpp +++ b/llarp/messages/fetch.hpp @@ -105,9 +105,18 @@ namespace llarp inline static std::string serialize(const RouterID& source) { - // serialize_response is a bit weird here, and perhaps could have a sister function - // with the same purpose but as a request, but...it works. - return messages::serialize_response({{"source", source.ToView()}}); + oxenc::bt_dict_producer btdp; + + try + { + btdp.append("source", source.ToView()); + } + catch (...) + { + log::error(link_cat, "Error: FetchRIDMessage failed to bt encode contents!"); + } + + return std::move(btdp).str(); } } // namespace FetchRIDMessage diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index be8140304..5b9eb6bce 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -233,8 +233,13 @@ namespace llarp // if we are not bootstrapping, we should check the rc's against the ones we currently hold if (not _using_bootstrap_fallback) { - if (not process_fetched_rcs(rcs)) - return false; + log::critical(logcat, "Checking returned RCs against locally held..."); + + auto success = process_fetched_rcs(rcs); + + log::critical( + logcat, "RCs returned by FetchRC {} by trust model", success ? "approved" : "rejected"); + return success; } while (!rcs.empty()) @@ -383,10 +388,10 @@ namespace llarp _router.link_manager().fetch_rcs( src, FetchRCMessage::serialize(_router.last_rc_fetch, needed), - [this, src, initial](oxen::quic::message m) mutable { + [this, source = src, initial](oxen::quic::message m) mutable { if (m.timed_out) { - log::critical(logcat, "RC fetch to {} timed out!", src); + log::critical(logcat, "RC fetch to {} timed out!", source); fetch_rcs_result(initial, m.timed_out); return; } @@ -397,7 +402,7 @@ namespace llarp if (m.is_error()) { auto reason = btdc.require(messages::STATUS_KEY); - log::critical(logcat, "RC fetch to {} returned error: {}", src, reason); + log::critical(logcat, "RC fetch to {} returned error: {}", source, reason); fetch_rcs_result(initial, m.is_error()); return; } @@ -414,7 +419,8 @@ namespace llarp } catch (const std::exception& e) { - log::critical(logcat, "Failed to parse RC fetch response from {}: {}", src, e.what()); + log::critical( + logcat, "Failed to parse RC fetch response from {}: {}", source, e.what()); fetch_rcs_result(initial, true); return; } @@ -454,11 +460,11 @@ namespace llarp _router.link_manager().fetch_router_ids( src, FetchRIDMessage::serialize(target), - [this, src, target, initial](oxen::quic::message m) mutable { + [this, source = src, target, initial](oxen::quic::message m) mutable { if (m.is_error()) { auto err = "RID fetch from {} via {} {}"_format( - src, target, m.timed_out ? "timed out" : "failed"); + target, source, m.timed_out ? "timed out" : "failed"); log::critical(link_cat, err); ingest_rid_fetch_responses(target); fetch_rids_result(initial); @@ -472,10 +478,10 @@ namespace llarp btdc.required("routers"); auto router_id_strings = btdc.consume_list>(); - btdc.require_signature("signature", [&src](ustring_view msg, ustring_view sig) { + btdc.require_signature("signature", [&source](ustring_view msg, ustring_view sig) { if (sig.size() != 64) throw std::runtime_error{"Invalid signature: not 64 bytes"}; - if (not crypto::verify(src, msg, sig)) + if (not crypto::verify(source, msg, sig)) throw std::runtime_error{ "Failed to verify signature for fetch RouterIDs response."}; }); @@ -487,7 +493,7 @@ namespace llarp if (s.size() != RouterID::SIZE) { log::critical( - link_cat, "RID fetch from {} via {} returned bad RouterID", target, src); + link_cat, "RID fetch from {} via {} returned bad RouterID", target, source); ingest_rid_fetch_responses(target); fetch_rids_result(initial); return; @@ -677,7 +683,8 @@ namespace llarp rc, BootstrapFetchMessage::serialize( is_snode ? std::make_optional(_router.router_contact) : std::nullopt, num_needed), - [this, is_snode = _router.is_service_node()](oxen::quic::message m) mutable { + [this, is_snode = _router.is_service_node(), src = rc.router_id()]( + oxen::quic::message m) mutable { log::critical(logcat, "Received response to BootstrapRC fetch request..."); if (not m) @@ -685,7 +692,7 @@ namespace llarp log::warning( logcat, "BootstrapRC fetch request to {} failed (error {}/{})", - fetch_source, + src, bootstrap_attempts, MAX_BOOTSTRAP_FETCH_ATTEMPTS); fallback_to_bootstrap(); @@ -714,7 +721,7 @@ namespace llarp log::warning( logcat, "Failed to parse BootstrapRC fetch response from {} (error {}/{}): {}", - fetch_source, + src, bootstrap_attempts, MAX_BOOTSTRAP_FETCH_ATTEMPTS, e.what()); @@ -725,7 +732,7 @@ namespace llarp log::critical( logcat, "BootstrapRC fetch response from {} returned {}/{} needed RCs", - fetch_source, + src, num, MIN_ACTIVE_RCS); diff --git a/llarp/router_id.hpp b/llarp/router_id.hpp index d587f8365..f82f78402 100644 --- a/llarp/router_id.hpp +++ b/llarp/router_id.hpp @@ -24,6 +24,9 @@ namespace llarp RouterID(ustring_view data) : PubKey(data.data()) {} + RouterID(std::string_view data) : RouterID(to_usv(data)) + {} + util::StatusObject ExtractStatus() const;