From 7bcdeaa28c9453eb84a580ec2a00527aa1feaaa2 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 17 Feb 2020 15:08:21 +0100 Subject: [PATCH] Move the logic of validDNSName to DNSName::has8bitBytes() (cherry picked from commit bf7ef5b4ee0ce310db0a3761a8250f86a5fea20d) --- pdns/dnsname.cc | 22 ++++++++++++++++++++++ pdns/dnsname.hh | 3 +++ pdns/packethandler.cc | 19 ++----------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index c262d28202..7ebc2c4f5b 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -456,3 +456,25 @@ void DNSName::appendEscapedLabel(std::string& appendTo, const char* orig, size_t ++pos; } } + +bool DNSName::has8bitBytes() const +{ + const auto& s = d_storage; + string::size_type pos = 0; + uint8_t length = s.at(pos); + while (length > 0) { + for (size_t idx = 0; idx < length; idx++) { + ++pos; + char c = s.at(pos); + if(!((c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c =='-' || c == '_' || c=='*' || c=='.' || c=='/' || c=='@' || c==' ' || c=='\\' || c==':')) + return true; + } + ++pos; + length = s.at(pos); + } + + return false; +} diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index 1f8ddf49a1..f933a62252 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -148,6 +148,9 @@ public: const string_t& getStorage() const { return d_storage; } + + bool has8bitBytes() const; /* returns true if at least one byte of the labels forming the name is not included in [A-Za-z0-9_*./@ \\:-] */ + private: string_t d_storage; diff --git a/pdns/packethandler.cc b/pdns/packethandler.cc index 2a955664c1..197bab40a5 100644 --- a/pdns/packethandler.cc +++ b/pdns/packethandler.cc @@ -942,25 +942,10 @@ int PacketHandler::processNotify(const DNSPacket& p) return 0; } -static bool validDNSName(const DNSName &name) +static bool validDNSName(const DNSName& name) { if (!g_8bitDNS) { - const auto& s = name.getStorage(); - string::size_type pos = 0; - uint8_t length = s.at(pos); - while (length > 0) { - for (size_t idx = 0; idx < length; idx++) { - ++pos; - char c = s.at(pos); - if(!((c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9') || - c =='-' || c == '_' || c=='*' || c=='.' || c=='/' || c=='@' || c==' ' || c=='\\' || c==':')) - return false; - } - ++pos; - length = s.at(pos); - } + return name.has8bitBytes() == false; } return true; } -- 2.47.2