From: Remi Gacogne Date: Tue, 24 Dec 2024 10:38:59 +0000 (+0100) Subject: dnsdist: Move functions from dnsdist-actions.hh to dnsdist-actions.cc X-Git-Tag: dnsdist-2.0.0-alpha1~160^2~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ccbe637936d5ba0a330a643ae215c15b2c0c794;p=thirdparty%2Fpdns.git dnsdist: Move functions from dnsdist-actions.hh to dnsdist-actions.cc --- diff --git a/pdns/dnsdistdist/Makefile.am b/pdns/dnsdistdist/Makefile.am index 91a4e3d35a..3ab92cb6d3 100644 --- a/pdns/dnsdistdist/Makefile.am +++ b/pdns/dnsdistdist/Makefile.am @@ -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 index 0000000000..ad19901f2a --- /dev/null +++ b/pdns/dnsdistdist/dnsdist-actions.cc @@ -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 + +DNSAction::Action DNSAction::typeFromString(const std::string& str) +{ + static const std::unordered_map 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"; +} diff --git a/pdns/dnsdistdist/dnsdist-actions.hh b/pdns/dnsdistdist/dnsdist-actions.hh index 5bb08a0c6e..30f7ec187a 100644 --- a/pdns/dnsdistdist/dnsdist-actions.hh +++ b/pdns/dnsdistdist/dnsdist-actions.hh @@ -21,6 +21,10 @@ */ #pragma once +#include +#include +#include + /* 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;