using DnsClient.Protocol; using System; using System.Collections.Generic; using System.Linq; namespace DnsClient { /// /// The response returned by any query performed by with all answer sections, header and message information. /// /// /// public class DnsQueryResponse : IDnsQueryResponse { private int? _hashCode; /// /// Gets the name server which responded with this result. /// /// /// The name server. /// public NameServer NameServer { get; } /// /// Gets a list of additional records. /// public IReadOnlyList Additionals { get; } /// /// Gets a list of all answers, addtional and authority records. /// public IEnumerable AllRecords { get { return Answers.Concat(Additionals).Concat(Authorities); } } /// /// Gets the audit trail if . as set to true, null otherwise. /// /// /// The audit trail. /// public string AuditTrail => Audit?.Build(this); /// /// Gets a list of answer records. /// public IReadOnlyList Answers { get; } /// /// Gets a list of authority records. /// public IReadOnlyList Authorities { get; } /// /// Returns a string value representing the error response code in case an error occured, /// otherwise ''. /// public string ErrorMessage => DnsResponseCodeText.GetErrorText(Header.ResponseCode); /// /// A flag indicating if the header contains a response codde other than . /// public bool HasError => Header?.ResponseCode != DnsResponseCode.NoError; /// /// Gets the header of the response. /// public DnsResponseHeader Header { get; } /// /// Gets the list of questions. /// public IReadOnlyList Questions { get; } /// /// Gets the size of the message. /// /// /// The size of the message. /// public int MessageSize { get; } /// /// Gets the settings used to produce this response. /// public DnsQuerySettings Settings { get; } internal LookupClientAudit Audit { get; } internal DnsQueryResponse(DnsResponseMessage dnsResponseMessage, NameServer nameServer, LookupClientAudit audit, DnsQuerySettings settings) { if (dnsResponseMessage == null) throw new ArgumentNullException(nameof(dnsResponseMessage)); Header = dnsResponseMessage.Header; MessageSize = dnsResponseMessage.MessageSize; Questions = dnsResponseMessage.Questions.ToArray(); Answers = dnsResponseMessage.Answers.ToArray(); Additionals = dnsResponseMessage.Additionals.ToArray(); Authorities = dnsResponseMessage.Authorities.ToArray(); NameServer = nameServer ?? throw new ArgumentNullException(nameof(nameServer)); Audit = audit; Settings = settings; } /// public override bool Equals(object obj) { if (obj == null) { return false; } if (!(obj is DnsQueryResponse response)) { return false; } return Header.ToString().Equals(response.Header.ToString()) && string.Join("", Questions).Equals(string.Join("", response.Questions)) && string.Join("", AllRecords).Equals(string.Join("", response.AllRecords)); } /// public override int GetHashCode() { if (!_hashCode.HasValue) { _hashCode = (Header.ToString() + string.Join("", Questions) + string.Join("", AllRecords)).GetHashCode(); } return _hashCode.Value; } } }