using MailSharp.DNS.Records; namespace MailSharp.DNS; public class Response { private readonly byte[] data = []; public List Questions { get; set; } = []; public List Answers { get; set; } = []; public List Authorities { get; set; } = []; public List Additionals { get; set; } = []; /// /// Error message, empty when no error /// public string Error => Header.RCODE == RCode.NoError ? string.Empty : Header.RCODE.ToString(); public string ErrorMessage { get; set; } = string.Empty; /// /// TimeStamp when cached /// public DateTime TimeStamp; public Response() { this.data = new byte[12]; TimeStamp = DateTime.Now; } public int MessageSize => this.data.Length; public DnsHeader Header => new(this.data); public Response(byte[] data) { this.data = data; RecordReader rr = new(this.data, 12); for (int intI = 0; intI < this.Header.QDCount; intI++) { this.Questions.Add(new QuestionRecord(ref rr)); } for (int intI = 0; intI < this.Header.ANCount; intI++) { this.Answers.Add(new RR(rr)); } for (int intI = 0; intI < this.Header.NSCount; intI++) { this.Authorities.Add(new RR(rr)); } for (int intI = 0; intI < this.Header.ARCount; intI++) { this.Additionals.Add(new RR(rr)); } TimeStamp = DateTime.Now; } /// /// List of RecordMX in Response.Answers /// public RecordMX[] RecordsMX => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordTXT in Response.Answers /// public RecordTXT[] RecordsTXT => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordA in Response.Answers /// public RecordA[] RecordsA => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordPTR in Response.Answers /// public RecordPTR[] RecordsPTR => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordCNAME in Response.Answers /// public RecordCNAME[] RecordsCNAME => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordAAAA in Response.Answers /// public RecordAAAA[] RecordsAAAA => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordNS in Response.Answers /// public RecordNS[] RecordsNS => [.. Answers.Select(a => a.Record).OfType()]; /// /// List of RecordSOA in Response.Answers /// public RecordSOA[] RecordsSOA => [.. Answers.Select(a => a.Record).OfType()]; public RR[] RecordsRR { get { List list = [.. this.Answers, .. this.Authorities, .. this.Additionals]; return [.. list]; } } }