From d18a480dd203cfecb51c01e093f7adce79cc84da Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 12 Dec 2020 16:40:54 +0530 Subject: [PATCH] DomainTree: throwing InvalidDomainNameException instead of DnsServerException to allow returning format error response. --- DnsServerCore/Dns/Zones/DomainTree.cs | 14 +++--- .../Dns/Zones/InvalidDomainNameException.cs | 46 +++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 DnsServerCore/Dns/Zones/InvalidDomainNameException.cs diff --git a/DnsServerCore/Dns/Zones/DomainTree.cs b/DnsServerCore/Dns/Zones/DomainTree.cs index e909f25e..a1b48a45 100644 --- a/DnsServerCore/Dns/Zones/DomainTree.cs +++ b/DnsServerCore/Dns/Zones/DomainTree.cs @@ -97,7 +97,7 @@ namespace DnsServerCore.Dns.Zones return Array.Empty(); if (domain.Length > 255) - throw new DnsServerException("Invalid domain name [" + domain + "]: length cannot exceed 255 bytes."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: length cannot exceed 255 bytes."); byte[] key = new byte[domain.Length + 1]; int keyOffset = 0; @@ -117,16 +117,16 @@ namespace DnsServerCore.Dns.Zones labelLength = labelEnd - labelStart; if (labelLength == 0) - throw new DnsServerException("Invalid domain name [" + domain + "]: label length cannot be 0 byte."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label length cannot be 0 byte."); if (labelLength > 63) - throw new DnsServerException("Invalid domain name [" + domain + "]: label length cannot exceed 63 bytes."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label length cannot exceed 63 bytes."); if (domain[labelStart + 1] == '-') - throw new DnsServerException("Invalid domain name [" + domain + "]: label cannot start with hyphen."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label cannot start with hyphen."); if (domain[labelEnd] == '-') - throw new DnsServerException("Invalid domain name [" + domain + "]: label cannot end with hyphen."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label cannot end with hyphen."); if ((labelLength == 1) && (domain[labelStart + 1] == '*')) { @@ -138,11 +138,11 @@ namespace DnsServerCore.Dns.Zones { labelChar = domain[i]; if (labelChar >= _keyMap.Length) - throw new DnsServerException("Invalid domain name [" + domain + "]: invalid character [" + labelChar + "] was found."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: invalid character [" + labelChar + "] was found."); labelKeyCode = _keyMap[labelChar]; if (labelKeyCode == 0xff) - throw new DnsServerException("Invalid domain name [" + domain + "]: invalid character [" + labelChar + "] was found."); + throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: invalid character [" + labelChar + "] was found."); key[keyOffset++] = labelKeyCode; } diff --git a/DnsServerCore/Dns/Zones/InvalidDomainNameException.cs b/DnsServerCore/Dns/Zones/InvalidDomainNameException.cs new file mode 100644 index 00000000..64f17c10 --- /dev/null +++ b/DnsServerCore/Dns/Zones/InvalidDomainNameException.cs @@ -0,0 +1,46 @@ +/* +Technitium DNS Server +Copyright (C) 2020 Shreyas Zare (shreyas@technitium.com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +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, see . + +*/ + +using System; + +namespace DnsServerCore.Dns.Zones +{ + public class InvalidDomainNameException : DnsServerException + { + #region constructors + + public InvalidDomainNameException() + : base() + { } + + public InvalidDomainNameException(string message) + : base(message) + { } + + public InvalidDomainNameException(string message, Exception innerException) + : base(message, innerException) + { } + + protected InvalidDomainNameException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) + : base(info, context) + { } + + #endregion + } +}