using System;
using System.Collections.Generic;
using System.Net;
using Ubiety.Dns.Core.Records;
using Ubiety.Dns.Core.Records.General;
using Ubiety.Dns.Core.Records.Mail;
namespace Ubiety.Dns.Core
{
///
/// DNS response
///
public class Response
{
///
/// Initializes a new instance of the class
///
public Response()
{
Questions = new List();
Answers = new List();
Authorities = new List();
Additionals = new List();
Server = new IPEndPoint(0, 0);
Error = string.Empty;
MessageSize = 0;
TimeStamp = DateTime.Now;
Header = new Header();
}
///
/// Initializes a new instance of the class
///
/// Address of the response
/// Response data
public Response(IPEndPoint iPEndPoint, byte[] data)
{
Error = string.Empty;
Server = iPEndPoint;
TimeStamp = DateTime.Now;
MessageSize = data.Length;
var rr = new RecordReader(data);
Questions = new List();
Answers = new List();
Authorities = new List();
Additionals = new List();
Header = new Header(rr);
for (var i = 0; i < Header.QuestionCount; i++)
{
Questions.Add(new Question(rr));
}
for (var i = 0; i < Header.AnswerCount; i++)
{
Answers.Add(new AnswerRR(rr));
}
for (var i = 0; i < Header.NameserverCount; i++)
{
Authorities.Add(new AuthorityRR(rr));
}
for (var i = 0; i < Header.AdditionalRecordsCount; i++)
{
Additionals.Add(new AdditionalRR(rr));
}
}
///
/// Gets the list of question records
///
public List Questions { get; }
///
/// Gets the list of answer resource records
///
public List Answers { get; }
///
/// Gets the list of authority resource records
///
public List Authorities { get; }
///
/// Gets the list of additional resource records
///
public List Additionals { get; }
///
/// Gets the response header
///
public Header Header { get; }
///
/// Gets or sets the error message, empty when no error
///
public string Error { get; set; }
///
/// Gets or sets the size of the message
///
public int MessageSize { get; set; }
///
/// Gets the timestamp when cached
///
public DateTime TimeStamp { get; }
///
/// Gets the server which delivered this response
///
public IPEndPoint Server { get; }
///
/// Gets a list of MX records in the answers
///
public List RecordMx
{
get
{
var list = new List();
foreach (var rr in Answers)
{
var record = rr.Record as RecordMx;
if (record != null)
{
list.Add(record);
}
}
list.Sort();
return list;
}
}
///
/// Gets a list of TXT records in the answers
///
public List RecordTxt
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordTxt record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of A records in the answers
///
public List RecordA
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordA record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of PTR records from the answers
///
public List RecordPtr
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordPtr record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of CNAME records from the answers
///
public List RecordCname
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordCname record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of AAAA records in the answers
///
public List RecordAaaa
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordAaaa record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of NS records in the answers
///
public List RecordNs
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordNs record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of SOA records in the answers
///
public List RecordSoa
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordSoa record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of CERT records in the answers
///
public List RecordCert
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordCert record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of SRV records in the answers
///
public List RecordSrv
{
get
{
var list = new List();
foreach (var rr in Answers)
{
if (rr.Record is RecordSrv record)
{
list.Add(record);
}
}
return list;
}
}
///
/// Gets a list of resource records in the answers
///
public IEnumerable ResourceRecords
{
get
{
var list = new List();
foreach (var rr in Answers)
{
list.Add(rr);
}
foreach (var rr in Answers)
{
list.Add(rr);
}
foreach (var rr in Authorities)
{
list.Add(rr);
}
foreach (var rr in Additionals)
{
list.Add(rr);
}
return list;
}
}
}
}