Skip to content

Commit

Permalink
path control handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Oct 28, 2024
1 parent f8d625b commit bf02987
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 139 deletions.
2 changes: 1 addition & 1 deletion llarp/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace llarp

if (not fs::exists(fpath))
{
log::critical(logcat, "Bootstrap RC file non-existant at path:{}", fpath);
log::critical(logcat, "Bootstrap RC file non-existent at path:{}", fpath);
return result;
}

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{1min};
inline static constexpr auto CC_PUBLISH_INTERVAL{30s};

/** ClientContact
On the wire we encode the data as a dict containing:
Expand Down
24 changes: 21 additions & 3 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ namespace llarp::handlers
if (auto s = btdc.maybe<std::string>(messages::STATUS_KEY))
status = s;
}
catch (...)
catch (const std::exception& e)
{
log::warning(logcat, "Exception caught parsing 'find_name' response!");
log::warning(logcat, "Exception caught parsing 'find_name' response: {}", e.what());
}

log::warning(logcat, "Call to endpoint 'lookup_name' failed -- status:{}", status.value_or("<none given>"));
Expand Down Expand Up @@ -414,7 +414,25 @@ namespace llarp::handlers
{
log::debug(logcat, "Publishing ClientContact to pivot {}", path->pivot_rid());

ret &= path->publish_client_contact(ecc, true);
ret &= path->publish_client_contact(ecc, true, 0, [](std::string response) {
log::info(logcat, "Received response to PublishClientContact...");

std::optional<std::string> status = std::nullopt;
try
{
oxenc::bt_dict_consumer btdc{response};

if (auto s = btdc.maybe<std::string>(messages::STATUS_KEY))
status = s;
}
catch (const std::exception& e)
{
log::warning(logcat, "Exception: {}", e.what());
}

log::warning(
logcat, "Call to PublishClientContact failed -- status:{}", status.value_or("<none given>"));
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ namespace llarp::handlers
auto pkt_hook = [this]() {
for (auto pkt = _net_if->read_next_packet(); not pkt.empty(); pkt = _net_if->read_next_packet())
{
log::debug(logcat, "packet router receiving {}", pkt.info_line());
log::trace(logcat, "packet router receiving {}", pkt.info_line());
_packet_router->handle_ip_packet(std::move(pkt));
}
};
Expand Down
72 changes: 48 additions & 24 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,8 @@ namespace llarp

void LinkManager::handle_path_control(oxen::quic::message m, const RouterID& /* from */)
{
log::debug(logcat, "{} called", __PRETTY_FUNCTION__);

HopID hop_id;
std::string payload;
SymmNonce nonce;
Expand All @@ -1313,12 +1315,20 @@ namespace llarp
// bidirectional, will need to check if we have a Path with path_id.
if (not hop)
{
// if (auto path = _router.path_context()->get_path(hop_id))
// {
// log::info(logcat, "Received path control corresponding to known path!");
// //
// }

log::warning(logcat, "Received path control with unknown next hop (ID: {})", hop_id);
return m.respond(messages::ERROR_RESPONSE, true);
}

nonce = crypto::onion(
reinterpret_cast<unsigned char*>(payload.data()), payload.size(), hop->shared, nonce, hop->nonceXOR);
auto onion_nonce = nonce ^ hop->nonceXOR;

crypto::onion(
reinterpret_cast<unsigned char*>(payload.data()), payload.size(), hop->shared, onion_nonce, hop->nonceXOR);

// if terminal hop, payload should contain a request (e.g. "ons_resolve"); handle and respond.
if (hop->terminal_hop)
Expand All @@ -1332,7 +1342,7 @@ namespace llarp
const auto& next_id = hop_is_rx ? hop->txid() : hop->rxid();
const auto& next_router = hop_is_rx ? hop->upstream() : hop->downstream();

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

send_control_message(
next_router,
Expand All @@ -1344,33 +1354,44 @@ namespace llarp

if (not hop)
{
log::warning(logcat, "Received response to path control message with non-existant TransitHop!");
log::warning(logcat, "Received response to path control message with non-existent TransitHop!");
return prev_message.respond(messages::ERROR_RESPONSE, true);
}

if (response.timed_out)
if (response)
{
log::info(logcat, "Path control message returned as time out!");
return prev_message.respond(messages::TIMEOUT_RESPONSE, true);
log::info(logcat, "Path control message returned successfully!");
prev_message.respond(messages::OK_RESPONSE, false);
}

HopID hop_id;
SymmNonce nonce;
std::string payload;

try
else if (response.timed_out)
{
std::tie(hop_id, nonce, payload) = ONION::deserialize_hop(oxenc::bt_dict_consumer{response.body()});
log::warning(logcat, "Path control message returned as time out!");
prev_message.respond(messages::TIMEOUT_RESPONSE, true);
}
catch (const std::exception& e)
else
{
log::warning(logcat, "Exception: {}", e.what());
return prev_message.respond(messages::ERROR_RESPONSE, true);
;
log::warning(logcat, "Path control message returned as error!");
prev_message.respond(messages::ERROR_RESPONSE, true);
}

auto resp_payload = ONION::serialize_hop(hop_id.to_view(), nonce, std::move(payload));
prev_message.respond(std::move(resp_payload), false);
// TODO: onion encrypt path message responses
// HopID hop_id;
// SymmNonce nonce;
// std::string payload;

// try
// {
// std::tie(hop_id, nonce, payload) =
// ONION::deserialize_hop(oxenc::bt_dict_consumer{response.body()});
// }
// catch (const std::exception& e)
// {
// log::warning(logcat, "Exception: {}; payload: {}", e.what(), buffer_printer{response.body()});
// return prev_message.respond(messages::ERROR_RESPONSE, true);
// }

// auto resp_payload = ONION::serialize_hop(hop_id.to_view(), nonce, std::move(payload));
// prev_message.respond(std::move(resp_payload), false);
});
}

Expand Down Expand Up @@ -1451,8 +1472,8 @@ namespace llarp
}
catch (const std::exception& e)
{
log::warning(logcat, "Exception: {}", e.what());
return;
log::warning(logcat, "Exception: {}; Payload: {}", e.what(), buffer_printer{payload});
return m.respond(messages::serialize_response({{messages::STATUS_KEY, e.what()}}), true);
}

// If a handler exists for "method", call it; else drop request on the floor.
Expand All @@ -1471,8 +1492,11 @@ namespace llarp
if (not hop)
return; // transit hop gone, drop response

auto n = SymmNonce::make_random();
m.respond(ONION::serialize_hop(hop->rxid().to_view(), n, response), false);
m.respond(std::move(response));

// TODO: onion encrypt path message responses
// auto n = SymmNonce::make_random();
// m.respond(ONION::serialize_hop(hop->rxid().to_view(), n, response), false);
};

std::invoke(itr->second, this, std::move(body), std::move(respond));
Expand Down
23 changes: 3 additions & 20 deletions llarp/messages/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,12 @@ namespace llarp
}
catch (const std::exception& e)
{
throw std::runtime_error{"Exception caught deserializing onion data:{}"_format(e.what())};
throw std::runtime_error{"Exception caught deserializing onion data: {}"_format(e.what())};
}

return {std::move(hop_id), std::move(nonce), std::move(payload)};
}

inline static std::tuple<ustring, ustring, ustring> deserialize_hop(oxenc::bt_dict_consumer& btdc)
{
ustring hopid, nonce, payload;

try
{
hopid = btdc.require<ustring>("k");
nonce = btdc.require<ustring>("n");
payload = btdc.require<ustring>("x");
}
catch (const std::exception& e)
{
throw std::runtime_error{"Exception caught deserializing onion data:{}"_format(e.what())};
}

return {std::move(hopid), std::move(nonce), std::move(payload)};
}
} // namespace ONION

namespace PATH
Expand Down Expand Up @@ -170,7 +153,7 @@ namespace llarp
}
catch (const std::exception& e)
{
log::warning(logcat, "Exception caught deserializing hop dict:{}", e.what());
log::warning(logcat, "Exception caught deserializing hop dict: {}", e.what());
throw;
}

Expand Down Expand Up @@ -227,7 +210,7 @@ namespace llarp
}
catch (const std::exception& e)
{
throw std::runtime_error{"Exception caught deserializing path control:{}"_format(e.what())};
throw std::runtime_error{"Exception caught deserializing path control: {}"_format(e.what())};
}

return {std::move(endpoint), std::move(payload)};
Expand Down
Loading

0 comments on commit bf02987

Please sign in to comment.