"Bad/missing Server Cookie"
};
-static const std::array<std::string, 11> rcodes_short_s = {
+static const std::array<std::string, 24> rcodes_short_s = {
"noerror",
"formerr",
"servfail",
"nxrrset",
"notauth",
"notzone",
+ "rcode11",
+ "rcode12",
+ "rcode13",
+ "rcode14",
+ "rcode15",
+ "badvers",
+ "badkey",
+ "badtime",
+ "badmode",
+ "badname",
+ "badalg",
+ "badtrunc",
+ "badcookie",
};
std::string RCode::to_s(uint8_t rcode) {
}
std::string RCode::to_short_s(uint8_t rcode) {
- if (rcode >= rcodes_short_s.size()) {
- return "rcode" + std::to_string(rcode);
+ if (rcode > 0xF) {
+ return std::string("ErrOutOfRange");
}
- return rcodes_short_s.at(rcode);
+ return ERCode::to_short_s(rcode);
}
std::optional<uint8_t> RCode::from_short(const std::string_view& rcode_string)
if (position == rcodes_short_s.end()) {
return std::nullopt;
}
- return std::distance(rcodes_short_s.begin(), position);
+ auto code = std::distance(rcodes_short_s.begin(), position);
+ if (code > 0xF) {
+ return std::nullopt;
+ }
+ return code;
}
std::string ERCode::to_s(uint16_t rcode) {
if (rcode >= RCode::rcodes_s.size()) {
- return std::string("Err#")+std::to_string(rcode);
+ return std::string("Err#") + std::to_string(rcode);
}
return RCode::rcodes_s.at(rcode);
}
+std::string ERCode::to_short_s(uint16_t rcode) {
+ if (rcode >= rcodes_short_s.size()) {
+ return "rcode" + std::to_string(rcode);
+ }
+ return rcodes_short_s.at(rcode);
+}
+
+std::optional<uint16_t> ERCode::from_short(const std::string_view& ercode_string)
+{
+ const auto* position = std::find(rcodes_short_s.begin(), rcodes_short_s.end(), ercode_string);
+ if (position == rcodes_short_s.end()) {
+ return std::nullopt;
+ }
+ return std::distance(rcodes_short_s.begin(), position);
+}
+
std::string Opcode::to_s(uint8_t opcode) {
static const std::array<std::string, 6> s_opcodes = { "Query", "IQuery", "Status", "3", "Notify", "Update" };
public:
enum rcodes_ : uint16_t { BADVERS=16, BADSIG=16, BADKEY=17, BADTIME=18, BADMODE=19, BADNAME=20, BADALG=21, BADTRUNC=22, BADCOOKIE=23 };
static std::string to_s(uint16_t rcode);
+ static std::string to_short_s(uint16_t rcode);
+ static std::optional<uint16_t> from_short(const std::string_view& ercode_string);
};
class Opcode
BOOST_AUTO_TEST_CASE(test_rcode)
{
- BOOST_CHECK_EQUAL(RCode::to_s(255), "ErrOutOfRange");
+ BOOST_CHECK_EQUAL(RCode::to_s(16), "ErrOutOfRange");
+ BOOST_CHECK_EQUAL(RCode::to_short_s(16), "ErrOutOfRange");
for (uint8_t idx = 0; idx <= RCode::NotZone; idx++) {
auto long_s = RCode::to_s(idx);
}
BOOST_CHECK_EQUAL(RCode::to_s(RCode::NotZone + 1), "Err#11");
+ BOOST_CHECK(!RCode::from_short("badcookie"));
}
BOOST_AUTO_TEST_CASE(test_ercode)
for (uint16_t idx = ERCode::BADVERS; idx <= ERCode::BADCOOKIE; idx++) {
auto long_s = ERCode::to_s(idx);
BOOST_CHECK(long_s.size() > 0);
+ auto short_s = ERCode::to_short_s(idx);
+ auto ercode = ERCode::from_short(short_s);
+ BOOST_CHECK(ercode);
+ BOOST_CHECK_EQUAL(*ercode, idx);
}
BOOST_CHECK_EQUAL(ERCode::to_s(ERCode::BADCOOKIE + 1), "Err#24");