From: Remi Gacogne Date: Fri, 11 Jul 2025 09:27:38 +0000 (+0200) Subject: dns: Add short descriptions for extended rcodes as well X-Git-Tag: rec-5.4.0-alpha0~36^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a1a3e3ed7ea4b5b9d42657c74b90b821dc95421;p=thirdparty%2Fpdns.git dns: Add short descriptions for extended rcodes as well Signed-off-by: Remi Gacogne --- diff --git a/pdns/dns.cc b/pdns/dns.cc index 768a126fa6..cbab33e45c 100644 --- a/pdns/dns.cc +++ b/pdns/dns.cc @@ -58,7 +58,7 @@ const std::array RCode::rcodes_s = { "Bad/missing Server Cookie" }; -static const std::array rcodes_short_s = { +static const std::array rcodes_short_s = { "noerror", "formerr", "servfail", @@ -70,6 +70,19 @@ static const std::array rcodes_short_s = { "nxrrset", "notauth", "notzone", + "rcode11", + "rcode12", + "rcode13", + "rcode14", + "rcode15", + "badvers", + "badkey", + "badtime", + "badmode", + "badname", + "badalg", + "badtrunc", + "badcookie", }; std::string RCode::to_s(uint8_t rcode) { @@ -80,10 +93,10 @@ 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 RCode::from_short(const std::string_view& rcode_string) @@ -92,16 +105,36 @@ std::optional 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 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 s_opcodes = { "Query", "IQuery", "Status", "3", "Notify", "Update" }; diff --git a/pdns/dns.hh b/pdns/dns.hh index 76fa8e1858..1b55e859d0 100644 --- a/pdns/dns.hh +++ b/pdns/dns.hh @@ -46,6 +46,8 @@ class ERCode 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 from_short(const std::string_view& ercode_string); }; class Opcode diff --git a/pdns/test-dns_cc.cc b/pdns/test-dns_cc.cc index 45eccfa40f..ca27b49d6b 100644 --- a/pdns/test-dns_cc.cc +++ b/pdns/test-dns_cc.cc @@ -33,7 +33,8 @@ BOOST_AUTO_TEST_SUITE(test_dnscc) 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); @@ -45,6 +46,7 @@ BOOST_AUTO_TEST_CASE(test_rcode) } BOOST_CHECK_EQUAL(RCode::to_s(RCode::NotZone + 1), "Err#11"); + BOOST_CHECK(!RCode::from_short("badcookie")); } BOOST_AUTO_TEST_CASE(test_ercode) @@ -52,6 +54,10 @@ 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");