preconstructed dicts for error/timeout/ok

also move messages' statuses into their own namespace
pull/2216/head
Thomas Winget 6 months ago
parent 1ca852d2f5
commit bd4f239aa3

@ -536,7 +536,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
respond(serialize_response({{messages::STATUS_KEY, FindNameMessage::EXCEPTION}}));
respond(messages::status::ERROR_RESPONSE);
}
_router.rpc_client()->lookup_ons_hash(
@ -546,7 +546,8 @@ namespace llarp
if (maybe)
respond(serialize_response({{"NAME", maybe->ciphertext}}));
else
respond(serialize_response({{messages::STATUS_KEY, FindNameMessage::NOT_FOUND}}));
respond(
serialize_response({{messages::status::STATUS_KEY, FindNameMessage::NOT_FOUND}}));
});
}
@ -564,7 +565,7 @@ namespace llarp
try
{
oxenc::bt_dict_consumer btdc{m.body()};
payload = btdc.require<std::string>(m ? "NAME" : messages::STATUS_KEY);
payload = btdc.require<std::string>(m ? "NAME" : messages::status::STATUS_KEY);
}
catch (const std::exception& e)
{
@ -578,7 +579,7 @@ namespace llarp
}
else
{
if (payload == FindNameMessage::EXCEPTION)
if (payload == "ERROR")
{
log::info(link_cat, "FindNameMessage failed with unkown error!");
@ -611,8 +612,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
respond(serialize_response(
{{messages::STATUS_KEY, FindRouterMessage::EXCEPTION}, {"TARGET", ""}}));
respond(messages::status::ERROR_RESPONSE);
return;
}
@ -643,7 +643,7 @@ namespace llarp
}
respond(serialize_response(
{{messages::STATUS_KEY, FindRouterMessage::RETRY_EXP}, {"TARGET", neighbors}}));
{{messages::status::STATUS_KEY, FindRouterMessage::RETRY_EXP}, {"TARGET", neighbors}}));
}
else
{
@ -683,14 +683,14 @@ namespace llarp
else
{
respond(serialize_response(
{{messages::STATUS_KEY, FindRouterMessage::RETRY_ITER},
{{messages::status::STATUS_KEY, FindRouterMessage::RETRY_ITER},
{"TARGET", reinterpret_cast<const char*>(target_addr.data())}}));
}
}
else
{
respond(serialize_response(
{{messages::STATUS_KEY, FindRouterMessage::RETRY_NEW},
{{messages::status::STATUS_KEY, FindRouterMessage::RETRY_NEW},
{"TARGET", reinterpret_cast<const char*>(closest_rid.data())}}));
}
}
@ -731,7 +731,7 @@ namespace llarp
}
else
{
if (status == FindRouterMessage::EXCEPTION)
if (status == "ERROR")
{
log::info(link_cat, "FindRouterMessage failed with remote exception!");
// Do something smart here probably
@ -785,7 +785,7 @@ namespace llarp
return;
}
if (status == FindRouterMessage::EXCEPTION)
if (status == "ERROR")
{
log::info(link_cat, "FindRouterMessage failed with remote exception!");
// Do something smart here probably
@ -840,7 +840,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
respond(serialize_response({{messages::STATUS_KEY, PublishIntroMessage::EXCEPTION}}));
respond(messages::status::ERROR_RESPONSE);
return;
}
@ -851,14 +851,15 @@ namespace llarp
if (not service::EncryptedIntroSet::verify(introset, derived_signing_key, sig))
{
log::error(link_cat, "Received PublishIntroMessage with invalid introset: {}", introset);
respond(serialize_response({{messages::STATUS_KEY, PublishIntroMessage::INVALID_INTROSET}}));
respond(serialize_response(
{{messages::status::STATUS_KEY, PublishIntroMessage::INVALID_INTROSET}}));
return;
}
if (now + service::MAX_INTROSET_TIME_DELTA > signed_at + path::DEFAULT_LIFETIME)
{
log::error(link_cat, "Received PublishIntroMessage with expired introset: {}", introset);
respond(serialize_response({{messages::STATUS_KEY, PublishIntroMessage::EXPIRED}}));
respond(serialize_response({{messages::status::STATUS_KEY, PublishIntroMessage::EXPIRED}}));
return;
}
@ -868,7 +869,8 @@ namespace llarp
{
log::error(
link_cat, "Received PublishIntroMessage but only know {} nodes", closest_rcs.size());
respond(serialize_response({{messages::STATUS_KEY, PublishIntroMessage::INSUFFICIENT}}));
respond(
serialize_response({{messages::status::STATUS_KEY, PublishIntroMessage::INSUFFICIENT}}));
return;
}
@ -880,7 +882,8 @@ namespace llarp
{
log::error(
link_cat, "Received PublishIntroMessage with invalide relay order: {}", relay_order);
respond(serialize_response({{messages::STATUS_KEY, PublishIntroMessage::INVALID_ORDER}}));
respond(serialize_response(
{{messages::status::STATUS_KEY, PublishIntroMessage::INVALID_ORDER}}));
return;
}
@ -897,7 +900,7 @@ namespace llarp
relay_order);
_router.contacts()->services()->PutNode(dht::ISNode{std::move(enc)});
respond(serialize_response({{messages::STATUS_KEY, ""}}));
respond(serialize_response({{messages::status::STATUS_KEY, ""}}));
}
else
{
@ -935,7 +938,7 @@ namespace llarp
log::info(link_cat, "Received PublishIntroMessage for {} (TXID: {}); we are candidate {}");
_router.contacts()->services()->PutNode(dht::ISNode{std::move(enc)});
respond(serialize_response({{messages::STATUS_KEY, ""}}));
respond(serialize_response({{messages::status::STATUS_KEY, ""}}));
}
else
log::warning(
@ -958,7 +961,7 @@ namespace llarp
try
{
oxenc::bt_dict_consumer btdc{m.body()};
payload = btdc.require<std::string>(messages::STATUS_KEY);
payload = btdc.require<std::string>(messages::status::STATUS_KEY);
}
catch (const std::exception& e)
{
@ -972,7 +975,7 @@ namespace llarp
}
else
{
if (payload == PublishIntroMessage::EXCEPTION)
if (payload == "ERROR")
{
log::info(link_cat, "PublishIntroMessage failed with remote exception!");
// Do something smart here probably
@ -1009,7 +1012,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
respond(serialize_response({{messages::STATUS_KEY, FindIntroMessage::EXCEPTION}}));
respond(messages::status::ERROR_RESPONSE);
return;
}
@ -1021,7 +1024,8 @@ namespace llarp
{
log::warning(
link_cat, "Received FindIntroMessage with invalid relay order: {}", relay_order);
respond(serialize_response({{messages::STATUS_KEY, FindIntroMessage::INVALID_ORDER}}));
respond(
serialize_response({{messages::status::STATUS_KEY, FindIntroMessage::INVALID_ORDER}}));
return;
}
@ -1031,7 +1035,8 @@ namespace llarp
{
log::error(
link_cat, "Received FindIntroMessage but only know {} nodes", closest_rcs.size());
respond(serialize_response({{messages::STATUS_KEY, FindIntroMessage::INSUFFICIENT_NODES}}));
respond(serialize_response(
{{messages::status::STATUS_KEY, FindIntroMessage::INSUFFICIENT_NODES}}));
return;
}
@ -1069,7 +1074,7 @@ namespace llarp
log::warning(
link_cat,
"Received FindIntroMessage with relayed == false and no local introset entry");
respond(serialize_response({{messages::STATUS_KEY, FindIntroMessage::NOT_FOUND}}));
respond(serialize_response({{messages::status::STATUS_KEY, FindIntroMessage::NOT_FOUND}}));
}
}
}
@ -1088,7 +1093,7 @@ namespace llarp
try
{
oxenc::bt_dict_consumer btdc{m.body()};
payload = btdc.require<std::string>((m) ? "INTROSET" : messages::STATUS_KEY);
payload = btdc.require<std::string>((m) ? "INTROSET" : messages::status::STATUS_KEY);
}
catch (const std::exception& e)
{
@ -1115,7 +1120,8 @@ namespace llarp
if (!_router.path_context().AllowingTransit())
{
log::warning(link_cat, "got path build request when not permitting transit");
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::NO_TRANSIT}}), true);
m.respond(
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::NO_TRANSIT}}), true);
return;
}
try
@ -1146,7 +1152,8 @@ namespace llarp
{
log::info(link_cat, "DH server initialization failed during path build");
m.respond(
serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}), true);
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}),
true);
return;
}
@ -1160,7 +1167,8 @@ namespace llarp
{
log::error(link_cat, "HMAC failed on path build request");
m.respond(
serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}), true);
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}),
true);
return;
}
if (!std::equal(
@ -1168,7 +1176,8 @@ namespace llarp
{
log::info(link_cat, "HMAC mismatch on path build request");
m.respond(
serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}), true);
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}),
true);
return;
}
@ -1181,7 +1190,8 @@ namespace llarp
{
log::info(link_cat, "Decrypt failed on path build request");
m.respond(
serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}), true);
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}),
true);
return;
}
@ -1202,7 +1212,9 @@ namespace llarp
if (frame.empty())
{
log::info(link_cat, "Path build request received invalid frame");
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_FRAMES}}), true);
m.respond(
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_FRAMES}}),
true);
return;
}
@ -1219,7 +1231,9 @@ namespace llarp
if (hop_info.txID.IsZero() || hop_info.rxID.IsZero())
{
log::warning(link_cat, "Invalid PathID; PathIDs must be non-zero");
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_PATHID}}), true);
m.respond(
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_PATHID}}),
true);
return;
}
@ -1228,7 +1242,9 @@ namespace llarp
if (_router.path_context().HasTransitHop(hop_info))
{
log::warning(link_cat, "Invalid PathID; PathIDs must be unique");
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_PATHID}}), true);
m.respond(
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_PATHID}}),
true);
return;
}
@ -1236,7 +1252,9 @@ namespace llarp
hop->pathKey.data(), other_pubkey.data(), _router.pubkey(), inner_nonce.data()))
{
log::warning(link_cat, "DH failed during path build.");
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}), true);
m.respond(
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_CRYPTO}}),
true);
return;
}
// generate hash of hop key for nonce mutation
@ -1251,7 +1269,8 @@ namespace llarp
{
log::warning(link_cat, "Path build attempt with too long of a lifetime.");
m.respond(
serialize_response({{messages::STATUS_KEY, PathBuildMessage::BAD_LIFETIME}}), true);
serialize_response({{messages::status::STATUS_KEY, PathBuildMessage::BAD_LIFETIME}}),
true);
return;
}
@ -1263,7 +1282,7 @@ namespace llarp
hop->terminal_hop = true;
// we are terminal hop and everything is okay
_router.path_context().PutTransitHop(hop);
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::OK}}), false);
m.respond(messages::status::OK_RESPONSE, false);
return;
}
@ -1295,7 +1314,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, PathBuildMessage::EXCEPTION}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1310,7 +1329,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
// m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
// m.respond(serialize_response({{messages::status::STATUS_KEY, "EXCEPTION"}}), true);
return;
}
}
@ -1325,7 +1344,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1340,7 +1359,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
// m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
// m.respond(serialize_response({{messages::status::STATUS_KEY, "EXCEPTION"}}), true);
return;
}
}
@ -1355,7 +1374,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1370,7 +1389,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
// m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
// m.respond(serialize_response({{messages::status::STATUS_KEY, "EXCEPTION"}}), true);
return;
}
}
@ -1385,7 +1404,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1400,7 +1419,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, "EXCEPTION"}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1439,7 +1458,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, ObtainExitMessage::EXCEPTION}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
throw;
}
}
@ -1508,7 +1527,8 @@ namespace llarp
(exit_ep->UpdateLocalPath(transit_hop->info.rxID))
? m.respond(UpdateExitMessage::sign_and_serialize_response(_router.identity(), tx_id))
: m.respond(
serialize_response({{messages::STATUS_KEY, UpdateExitMessage::UPDATE_FAILED}}),
serialize_response(
{{messages::status::STATUS_KEY, UpdateExitMessage::UPDATE_FAILED}}),
true);
}
// If we fail to verify the message, no-op
@ -1517,7 +1537,7 @@ namespace llarp
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, UpdateExitMessage::EXCEPTION}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}
@ -1597,12 +1617,13 @@ namespace llarp
}
m.respond(
serialize_response({{messages::STATUS_KEY, CloseExitMessage::UPDATE_FAILED}}), true);
serialize_response({{messages::status::STATUS_KEY, CloseExitMessage::UPDATE_FAILED}}),
true);
}
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{messages::STATUS_KEY, CloseExitMessage::EXCEPTION}}), true);
m.respond(messages::status::ERROR_RESPONSE, true);
return;
}
}

@ -27,11 +27,15 @@ namespace llarp
return oxenc::bt_serialize(supplement);
}
// ideally STATUS is the first key in a bt-dict, so use a single, early ascii char
inline const auto STATUS_KEY = "!"s;
inline const auto TIMEOUT_BT_DICT = serialize_response({{STATUS_KEY, "TIMEOUT"}});
inline const auto ERROR_BT_DICT = serialize_response({{STATUS_KEY, "ERROR"}});
} // namespace messages
namespace status
{
// ideally STATUS is the first key in a bt-dict, so use a single, early ascii char
inline const auto STATUS_KEY = "!"s;
inline const auto TIMEOUT_RESPONSE = serialize_response({{STATUS_KEY, "TIMEOUT"}});
inline const auto ERROR_RESPONSE = serialize_response({{STATUS_KEY, "ERROR"}});
inline const auto OK_RESPONSE = serialize_response({{STATUS_KEY, "OK"}});
} // namespace status
} // namespace messages
/// abstract base class for serialized messages
struct AbstractSerializable

@ -6,7 +6,6 @@ namespace llarp
{
namespace FindRouterMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto RETRY_EXP = "RETRY AS EXPLORATORY"sv;
inline auto RETRY_ITER = "RETRY AS ITERATIVE"sv;
inline auto RETRY_NEW = "RETRY WITH NEW RECIPIENT"sv;
@ -52,9 +51,7 @@ namespace llarp
namespace FindIntroMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto NOT_FOUND = "NOT FOUND"sv;
inline auto TIMED_OUT = "TIMED OUT"sv;
inline auto INVALID_ORDER = "INVALID ORDER"sv;
inline auto INSUFFICIENT_NODES = "INSUFFICIENT NODES"sv;
@ -80,7 +77,6 @@ namespace llarp
namespace FindNameMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto NOT_FOUND = "NOT FOUND"sv;
inline static std::string
@ -120,7 +116,6 @@ namespace llarp
namespace PublishIntroMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto INVALID_INTROSET = "INVALID INTROSET"sv;
inline auto EXPIRED = "EXPIRED INTROSET"sv;
inline auto INSUFFICIENT = "INSUFFICIENT NODES"sv;

@ -12,7 +12,6 @@ namespace llarp
namespace ObtainExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
// flag: 0 = Exit, 1 = Snode
inline std::string
@ -63,7 +62,6 @@ namespace llarp
namespace UpdateExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto UPDATE_FAILED = "EXIT UPDATE FAILED"sv;
inline std::string
@ -113,7 +111,6 @@ namespace llarp
namespace CloseExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto UPDATE_FAILED = "CLOSE EXIT FAILED"sv;
inline std::string

@ -6,8 +6,6 @@ namespace llarp
{
namespace PathBuildMessage
{
inline auto OK = "OK"sv;
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto BAD_FRAMES = "BAD_FRAMES"sv;
inline auto BAD_CRYPTO = "BAD_CRYPTO"sv;
inline auto NO_TRANSIT = "NOT ALLOWING TRANSIT"sv;

@ -127,7 +127,7 @@ namespace llarp::path
if (m.timed_out)
{
response_cb(messages::TIMEOUT_BT_DICT);
response_cb(messages::status::TIMEOUT_RESPONSE);
return;
}
@ -143,7 +143,7 @@ namespace llarp::path
catch (const std::exception& e)
{
log::warning(path_cat, "Error parsing path control message response: {}", e.what());
response_cb(messages::ERROR_BT_DICT);
response_cb(messages::status::ERROR_RESPONSE);
return;
}

@ -489,7 +489,7 @@ namespace llarp
else
{
oxenc::bt_dict_consumer d{m.body()};
auto status = d.require<std::string_view>(messages::STATUS_KEY);
auto status = d.require<std::string_view>(messages::status::STATUS_KEY);
log::warning(path_cat, "Path build returned failure status: {}", status);
}
}

@ -822,7 +822,7 @@ namespace llarp::service
try
{
oxenc::bt_dict_consumer btdc{resp};
auto status = btdc.require<std::string_view>(messages::STATUS_KEY);
auto status = btdc.require<std::string_view>(messages::status::STATUS_KEY);
if (status != "OK"sv)
{
log::info(link_cat, "Error on ONS lookup: {}", status);
@ -1367,7 +1367,7 @@ namespace llarp::service
try
{
oxenc::bt_dict_consumer btdc{resp};
auto status = btdc.require<std::string_view>(messages::STATUS_KEY);
auto status = btdc.require<std::string_view>(messages::status::STATUS_KEY);
if (status != "OK"sv)
{
log::info(link_cat, "Error in find intro set response: {}", status);

@ -188,7 +188,7 @@ namespace llarp::service
try
{
oxenc::bt_dict_consumer btdc{resp};
auto status = btdc.require<std::string_view>(messages::STATUS_KEY);
auto status = btdc.require<std::string_view>(messages::status::STATUS_KEY);
if (status != "OK"sv)
{
log::info(link_cat, "Error in find intro set response: {}", status);

Loading…
Cancel
Save