]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Move functions from dnsdist-actions.hh to dnsdist-actions.cc
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 24 Dec 2024 10:38:59 +0000 (11:38 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 16 Jan 2025 08:50:20 +0000 (09:50 +0100)
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/dnsdist-actions.cc [new file with mode: 0644]
pdns/dnsdistdist/dnsdist-actions.hh

index 91a4e3d35a7297116d3c54877806e0988c87d2d8..3ab92cb6d388eadee43b46e3e2a01dc5b3aefe33 100644 (file)
@@ -144,7 +144,7 @@ dnsdist_SOURCES = \
        dns.cc dns.hh \
        dns_random.hh \
        dnscrypt.cc dnscrypt.hh \
-       dnsdist-actions.hh \
+       dnsdist-actions.cc dnsdist-actions.hh \
        dnsdist-async.cc dnsdist-async.hh \
        dnsdist-backend.cc dnsdist-backend.hh \
        dnsdist-backoff.hh \
@@ -274,6 +274,7 @@ testrunner_SOURCES = \
        credentials.cc credentials.hh \
        dns.cc dns.hh \
        dnscrypt.cc dnscrypt.hh \
+       dnsdist-actions.cc dnsdist-actions.hh \
        dnsdist-async.cc dnsdist-async.hh \
        dnsdist-backend.cc dnsdist-backend.hh \
        dnsdist-backoff.hh \
diff --git a/pdns/dnsdistdist/dnsdist-actions.cc b/pdns/dnsdistdist/dnsdist-actions.cc
new file mode 100644 (file)
index 0000000..ad19901
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include "dnsdist-actions.hh"
+
+#include <boost/algorithm/string.hpp>
+
+DNSAction::Action DNSAction::typeFromString(const std::string& str)
+{
+  static const std::unordered_map<std::string, Action> s_mappings{
+    {"allow", Action::Allow},
+    {"delay", Action::Delay},
+    {"drop", Action::Drop},
+    {"headermodify", Action::HeaderModify},
+    {"none", Action::None},
+    {"noop", Action::NoOp},
+    {"norecurse", Action::NoRecurse},
+    {"nxdomain", Action::Nxdomain},
+    {"pool", Action::Pool},
+    {"refused", Action::Refused},
+    {"servfail", Action::ServFail},
+    {"settag", Action::SetTag},
+    {"spoof", Action::Spoof},
+    {"spoofpacket", Action::SpoofPacket},
+    {"spoofraw", Action::SpoofRaw},
+    {"truncate", Action::Truncate},
+  };
+
+  auto lower = boost::to_lower_copy(str);
+  boost::replace_all(lower, "-", "");
+  auto mappingIt = s_mappings.find(lower);
+  if (mappingIt != s_mappings.end()) {
+    return mappingIt->second;
+  }
+  throw std::runtime_error("Unable to convert '" + str + "' into a DNS Action");
+}
+
+std::string DNSAction::typeToString(DNSAction::Action action)
+{
+  switch (action) {
+  case Action::Drop:
+    return "Drop";
+  case Action::Nxdomain:
+    return "Send NXDomain";
+  case Action::Refused:
+    return "Send Refused";
+  case Action::Spoof:
+    return "Spoof an answer";
+  case Action::SpoofPacket:
+    return "Spoof a raw answer from bytes";
+  case Action::SpoofRaw:
+    return "Spoof an answer from raw bytes";
+  case Action::Allow:
+    return "Allow";
+  case Action::HeaderModify:
+    return "Modify the header";
+  case Action::Pool:
+    return "Route to a pool";
+  case Action::Delay:
+    return "Delay";
+  case Action::Truncate:
+    return "Truncate over UDP";
+  case Action::ServFail:
+    return "Send ServFail";
+  case Action::SetTag:
+    return "Set Tag";
+  case Action::None:
+  case Action::NoOp:
+    return "Do nothing";
+  case Action::NoRecurse:
+    return "Set rd=0";
+  }
+
+  return "Unknown";
+}
index 5bb08a0c6eac63514a6966c9cc616b5140121eae..30f7ec187a5a416966cf839d623b07f29dc4c95f 100644 (file)
  */
 #pragma once
 
+#include <cstdint>
+#include <map>
+#include <string>
+
 /* so what could you do:
    drop,
    fake up nxdomain,
@@ -55,44 +59,8 @@ public:
     SpoofPacket,
     SetTag,
   };
-  static std::string typeToString(const Action& action)
-  {
-    switch (action) {
-    case Action::Drop:
-      return "Drop";
-    case Action::Nxdomain:
-      return "Send NXDomain";
-    case Action::Refused:
-      return "Send Refused";
-    case Action::Spoof:
-      return "Spoof an answer";
-    case Action::SpoofPacket:
-      return "Spoof a raw answer from bytes";
-    case Action::SpoofRaw:
-      return "Spoof an answer from raw bytes";
-    case Action::Allow:
-      return "Allow";
-    case Action::HeaderModify:
-      return "Modify the header";
-    case Action::Pool:
-      return "Route to a pool";
-    case Action::Delay:
-      return "Delay";
-    case Action::Truncate:
-      return "Truncate over UDP";
-    case Action::ServFail:
-      return "Send ServFail";
-    case Action::SetTag:
-      return "Set Tag";
-    case Action::None:
-    case Action::NoOp:
-      return "Do nothing";
-    case Action::NoRecurse:
-      return "Set rd=0";
-    }
-
-    return "Unknown";
-  }
+  static Action typeFromString(const std::string& str);
+  static std::string typeToString(Action action);
 
   virtual Action operator()(DNSQuestion*, std::string* ruleresult) const = 0;
   virtual ~DNSAction() = default;