preconstructed dicts for error/timeout/ok

also move messages' statuses into their own namespace
This commit is contained in:
Thomas Winget 2023-11-16 15:35:58 -05:00 committed by dr7ana
parent 957280e628
commit f995b30348
9 changed files with 85 additions and 70 deletions

View File

@ -538,7 +538,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(
@ -548,7 +548,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}}));
});
}
@ -566,7 +567,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)
{
@ -580,7 +581,7 @@ namespace llarp
}
else
{
if (payload == FindNameMessage::EXCEPTION)
if (payload == "ERROR")
{
log::info(link_cat, "FindNameMessage failed with unkown error!");
@ -613,8 +614,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;
}
@ -645,7 +645,7 @@ namespace llarp
}
respond(serialize_response(
{{messages::STATUS_KEY, FindRouterMessage::RETRY_EXP}, {"TARGET", neighbors}}));
{{messages::status::STATUS_KEY, FindRouterMessage::RETRY_EXP}, {"TARGET", neighbors}}));
}
else
{
@ -685,14 +685,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())}}));
}
}
@ -733,7 +733,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
@ -787,7 +787,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
@ -842,7 +842,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;
}
@ -853,14 +853,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;
}
@ -870,7 +871,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;
}
@ -882,7 +884,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;
}
@ -899,7 +902,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
{
@ -937,7 +940,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(
@ -960,7 +963,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)
{
@ -974,7 +977,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
@ -1011,7 +1014,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;
}
@ -1023,7 +1026,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;
}
@ -1033,7 +1037,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;
}
@ -1071,7 +1076,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}}));
}
}
}
@ -1090,7 +1095,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)
{
@ -1117,7 +1122,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
@ -1148,7 +1154,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;
}
@ -1162,7 +1169,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(
@ -1170,7 +1178,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;
}
@ -1183,7 +1192,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;
}
@ -1204,7 +1214,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;
}
@ -1221,7 +1233,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;
}
@ -1230,7 +1244,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;
}
@ -1238,7 +1254,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
@ -1253,7 +1271,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;
}
@ -1265,7 +1284,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;
}
@ -1297,7 +1316,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;
}
}
@ -1312,7 +1331,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;
}
}
@ -1327,7 +1346,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;
}
}
@ -1342,7 +1361,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;
}
}
@ -1357,7 +1376,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;
}
}
@ -1372,7 +1391,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;
}
}
@ -1387,7 +1406,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;
}
}
@ -1402,7 +1421,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;
}
}
@ -1441,7 +1460,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;
}
}
@ -1510,7 +1529,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
@ -1519,7 +1539,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;
}
}
@ -1599,12 +1619,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;
}
}

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);