using System;
using System.Collections.Generic;
namespace DnsClient
{
/*
* Reference RFC6895#section-2.3
*/
//
/// RFCs 1035, 2136, 2671, 2845, 2930, 4635.
///
public enum DnsResponseCode : ushort
{
///
/// RFC 1035.
/// No error condition
///
NoError = 0,
///
/// RFC 1035.
/// Format error. The name server was unable to interpret the query.
///
FormatError = 1,
///
/// RFC 1035.
/// Server failure. The name server was unable to process this query due to a problem with the name server.
///
ServerFailure = 2,
///
/// RFC 1035.
/// Name Error. Meaningful only for responses from an authoritative name server,
/// this code signifies that the domain name referenced in the query does not exist.
///
NotExistentDomain = 3,
///
/// RFC 1035.
/// Not Implemented. The name server does not support the requested kind of query.
///
NotImplemented = 4,
///
/// RFC 1035.
/// Refused. The name server refuses to perform the specified operation for policy reasons.
/// For example, a name server may not wish to provide the information to the particular requester,
/// or a name server may not wish to perform a particular operation (e.g., zone transfer) for particular data.
///
Refused = 5,
///
/// RFC 2136.
/// Name Exists when it should not.
///
ExistingDomain = 6,
///
/// RFC 2136.
/// Resource record set exists when it should not.
///
ExistingResourceRecordSet = 7,
///
/// RFC 2136.
/// Resource record set that should exist but does not.
///
MissingResourceRecordSet = 8,
///
/// RFC 2136 / RFC2845
/// Server Not Authoritative for zone / Not Authorized.
///
NotAuthorized = 9,
///
/// RFC 2136.
/// Name not contained in zone.
///
NotZone = 10,
///
/// RFCs 2671 / 2845.
/// Bad OPT Version or TSIG Signature Failure.
///
BadVersionOrBadSignature = 16,
///
/// RFC 2845.
/// Key not recognized.
///
BadKey = 17,
///
/// RFC 2845.
/// Signature out of time window.
///
BadTime = 18,
///
/// RFC 2930.
/// Bad TKEY Mode.
///
BadMode = 19,
///
/// RFC 2930.
/// Duplicate key name.
///
BadName = 20,
///
/// RFC 2930.
/// Algorithm not supported.
///
BadAlgorithm = 21,
///
/// RFC 4635.
/// BADTRUNC - Bad Truncation.
///
BadTruncation = 22,
///
/// RFC 7873
/// Bad/missing Server Cookie
///
BadCookie = 23,
///
/// Unknown error.
///
Unassigned = 666
}
public static class DnsResponseCodeText
{
internal const string BADALG = "Algorithm not supported";
internal const string BADCOOKIE = "Bad/missing Server Cookie";
internal const string BADKEY = "Key not recognized";
internal const string BADMODE = "Bad TKEY Mode";
internal const string BADNAME = "Duplicate key name";
internal const string BADSIG = "TSIG Signature Failure";
internal const string BADTIME = "Signature out of time window";
internal const string BADTRUNC = "Bad Truncation";
internal const string BADVERS = "Bad OPT Version";
internal const string FormErr = "Format Error";
internal const string NoError = "No Error";
internal const string NotAuth = "Server Not Authoritative for zone or Not Authorized";
internal const string NotImp = "Not Implemented";
internal const string NotZone = "Name not contained in zone";
internal const string NXDomain = "Non-Existent Domain";
internal const string NXRRSet = "RR Set that should exist does not";
internal const string Refused = "Query Refused";
internal const string ServFail = "Server Failure";
internal const string Unassigned = "Unknown Error";
internal const string YXDomain = "Name Exists when it should not";
internal const string YXRRSet = "RR Set Exists when it should not";
private static readonly Dictionary errors = new Dictionary()
{
{ DnsResponseCode.NoError, DnsResponseCodeText.NoError },
{ DnsResponseCode.FormatError, DnsResponseCodeText.FormErr },
{ DnsResponseCode.ServerFailure, DnsResponseCodeText.ServFail },
{ DnsResponseCode.NotExistentDomain, DnsResponseCodeText.NXDomain },
{ DnsResponseCode.NotImplemented, DnsResponseCodeText.NotImp },
{ DnsResponseCode.Refused, DnsResponseCodeText.Refused },
{ DnsResponseCode.ExistingDomain, DnsResponseCodeText.YXDomain },
{ DnsResponseCode.ExistingResourceRecordSet, DnsResponseCodeText.YXRRSet },
{ DnsResponseCode.MissingResourceRecordSet, DnsResponseCodeText.NXRRSet },
{ DnsResponseCode.NotAuthorized, DnsResponseCodeText.NotAuth },
{ DnsResponseCode.NotZone, DnsResponseCodeText.NotZone },
{ DnsResponseCode.BadVersionOrBadSignature, DnsResponseCodeText.BADVERS },
{ DnsResponseCode.BadKey, DnsResponseCodeText.BADKEY },
{ DnsResponseCode.BadTime, DnsResponseCodeText.BADTIME },
{ DnsResponseCode.BadMode, DnsResponseCodeText.BADMODE },
{ DnsResponseCode.BadName, DnsResponseCodeText.BADNAME },
{ DnsResponseCode.BadAlgorithm, DnsResponseCodeText.BADALG },
{ DnsResponseCode.BadTruncation, DnsResponseCodeText.BADTRUNC },
{ DnsResponseCode.BadCookie, DnsResponseCodeText.BADCOOKIE },
};
public static string GetErrorText(DnsResponseCode code)
{
if (!errors.ContainsKey(code))
{
return Unassigned;
}
return errors[code];
}
}
public class DnsResponseException : Exception
{
public DnsResponseCode Code { get; }
public string DnsError { get; }
///
/// Creates an instance of with .
///
public DnsResponseException() : base(DnsResponseCodeText.Unassigned)
{
Code = DnsResponseCode.Unassigned;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
///
/// Creates an instance of with
/// and a custom message.
///
public DnsResponseException(string message) : base(message)
{
Code = DnsResponseCode.Unassigned;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
///
/// Creates an instance of with
/// the standard error text for this .
///
public DnsResponseException(DnsResponseCode code) : base(DnsResponseCodeText.GetErrorText(code))
{
Code = code;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
///
/// Creates an instance of with
/// and a custom message.
///
public DnsResponseException(string message, Exception innerException) : base(message, innerException)
{
Code = DnsResponseCode.Unassigned;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
///
/// Creates an instance of with a custom message
/// and the given .
///
public DnsResponseException(DnsResponseCode code, string message) : base(message)
{
Code = code;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
///
/// Creates an instance of with a custom message
/// and the given .
///
public DnsResponseException(DnsResponseCode code, string message, Exception innerException) : base(message, innerException)
{
Code = code;
DnsError = DnsResponseCodeText.GetErrorText(Code);
}
}
}