]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dns: Add short descriptions for extended rcodes as well
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 11 Jul 2025 09:27:38 +0000 (11:27 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 11 Jul 2025 09:39:10 +0000 (11:39 +0200)
Signed-off-by: Remi Gacogne <remi.gacogne@powerdns.com>
pdns/dns.cc
pdns/dns.hh
pdns/test-dns_cc.cc

index 768a126fa6431a1450f808d5ac9ce34cdab9efe4..cbab33e45c508236df0ac773814d3a3927f2a6eb 100644 (file)
@@ -58,7 +58,7 @@ const std::array<std::string, 24> RCode::rcodes_s = {
   "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",
@@ -70,6 +70,19 @@ static const std::array<std::string, 11> 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<uint8_t> RCode::from_short(const std::string_view& rcode_string)
@@ -92,16 +105,36 @@ 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" };
 
index 76fa8e185898bc9a82e4ed1ca82b563c8a104267..1b55e859d01be924fb31ed196e97eb0410d5ad4e 100644 (file)
@@ -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<uint16_t> from_short(const std::string_view& ercode_string);
 };
 
 class Opcode
index 45eccfa40f49c753cacb33623ec8af956f5c2a6a..ca27b49d6b699e38103401f5a45ebbd2bc3e7c0b 100644 (file)
@@ -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");