using System; using System.Collections.Generic; using System.Linq; using DnsClient.Protocol; using DnsClient.Protocol.Record; namespace DnsClient { /// /// Immutable version of the . /// public class DnsQueryResponse { private int? _hashCode; /// /// Gets a list of additional records. /// public IReadOnlyCollection Additionals { get; } /// /// Gets a list of all answers, addtional and authority records. /// public IReadOnlyCollection AllRecords { get { return Answers.Concat(Additionals).Concat(Authorities).ToArray(); } } /// /// Gets a list of answer records. /// public IReadOnlyCollection Answers { get; } /// /// Gets a list of authority records. /// public IReadOnlyCollection Authorities { get; } /// /// Returns a string value representing the error response code in case an error occured, otherwise empty. /// public string ErrorMessage => HasError ? DnsResponseCodeText.GetErrorText(Header.ResponseCode) : string.Empty; /// /// 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 IReadOnlyCollection Questions { get; } /// /// Creates a new instace of . /// /// public DnsQueryResponse( DnsResponseHeader header, IReadOnlyCollection questions, IReadOnlyCollection answers, IReadOnlyCollection additionals, IReadOnlyCollection authorities) { if (header == null) throw new ArgumentNullException(nameof(header)); if (questions == null) throw new ArgumentNullException(nameof(questions)); if (answers == null) throw new ArgumentNullException(nameof(answers)); if (additionals == null) throw new ArgumentNullException(nameof(additionals)); if (authorities == null) throw new ArgumentNullException(nameof(authorities)); Header = header; Questions = questions; Answers = answers; Additionals = additionals; Authorities = authorities; } /// public override bool Equals(object obj) { if (obj == null) { return false; } var response = obj as DnsQueryResponse; if (response == null) { 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; } } }