/// <summary> /// Reads the values of this <see cref="DnsMessage"/> from the specified /// <see cref="AK.Net.Dns.IDnsReader"/>. /// </summary> /// <param name="reader">The reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the <see cref="DnsMessage"/> could not be read from the /// <paramref name="reader"/>. /// </exception> public virtual void Read(IDnsReader reader) { Guard.NotNull(reader, "reader"); PreRead(); try { this.Header.Read(reader); for (int i = 0, len = this.Header.QuestionCount; i < len; ++i) { this.Questions.Add(new DnsQuestion(reader)); } for (int i = 0, len = this.Header.AnswerCount; i < len; ++i) { this.Answers.Add(reader.ReadRecord()); } for (int i = 0, len = this.Header.AuthorityCount; i < len; ++i) { this.Authorities.Add(reader.ReadRecord()); } for (int i = 0, len = this.Header.AdditionalCount; i < len; ++i) { this.Additionals.Add(reader.ReadRecord()); } } catch (DnsException exc) { throw Guard.InvalidDnsMessageFormat(exc); } }
/// <summary> /// Parses the values of this header from the specified query reader. /// </summary> /// <param name="reader">The quey reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the header could not be read from the /// <paramref name="reader"/>. /// </exception> public virtual void Read(IDnsReader reader) { Guard.NotNull(reader, "reader"); try { this.Id = reader.ReadUInt16(); byte b3 = reader.ReadByte(); this.IsQuery = (b3 & 0x80) == 0x00; this.OpCode = (DnsOpCode)(b3 >> 3 & 0x0F); this.IsAuthorative = (b3 & 0x04) == 0x04; this.IsTruncated = (b3 & 0x02) == 0x02; this.IsRecursionDesired = (b3 & 0x01) == 0x01; byte b4 = reader.ReadByte(); this.IsRecursionAllowed = (b4 & 0x80) == 0x80; this.ResponseCode = (DnsResponseCode)(b4 & 0x0F); this.QuestionCount = reader.ReadUInt16(); this.AnswerCount = reader.ReadUInt16(); this.AuthorityCount = reader.ReadUInt16(); this.AdditionalCount = reader.ReadUInt16(); } catch (DnsException exc) { throw Guard.InvalidDnsHeaderFormat(exc); } }
/// <summary> /// Initialises a new instance of the NullRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public NullRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Null, cls, ttl) { Guard.NotNull(reader, "reader"); _data = reader.ReadBytes(reader.ReadUInt16()); }
/// <summary> /// Initialises a new instance of the SpfRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public SpfRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Spf, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _specification = reader.ReadCharString(); }
/// <summary> /// Initialises a new instance of the MRRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public MRRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.MR, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _newMailbox = reader.ReadName(); }
/// <summary> /// Initialises a new instance of the PtrRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public PtrRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Ptr, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _domain = reader.ReadName(); }
/// <summary> /// Initialises a new instance of the ARecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public ARecord(DnsName owner, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.A, DnsRecordClass.IN, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _address = reader.ReadIPv4Address(); }
/// <summary> /// Initialises a new instance of the TxtRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public TxtRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Txt, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _text = reader.ReadCharString(); }
/// <summary> /// Initialises a new instance of the CNameRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public CNameRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.CName, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _canonical = reader.ReadName(); }
/// <summary> /// Initialises a new instance of the MXRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public MXRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.MX, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _preference = reader.ReadInt16(); _exchange = reader.ReadName(); }
/// <summary> /// Reads the values of this <see cref="DnsReply"/> from the specified /// <see cref="AK.Net.Dns.IDnsReader"/>. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the <see cref="DnsReply"/> could not be read from the /// <paramref name="reader"/>. /// </exception> public override void Read(IDnsReader reader) { Guard.NotNull(reader, "reader"); base.Read(reader); if (this.Header.IsQuery) { throw Guard.DnsReplyExpected(); } }
/// <summary> /// Reads the specified number of questions from the specified reader. /// </summary> /// <param name="reader">The query reader.</param> /// <param name="count">The number of questions to parse.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when <paramref name="count"/> is negative. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when one of the questions could not be read from the /// <paramref name="reader"/>. /// </exception> public void Read(IDnsReader reader, int count) { Guard.NotNull(reader, "reader"); Guard.InRange(count >= 0, "count"); ClearItems(); while (count-- > 0) { Add(new DnsQuestion(reader)); } }
/// <summary> /// Initialises a new instance of the SrvRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public SrvRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Srv, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _priority = reader.ReadUInt16(); _weight = reader.ReadUInt16(); _port = reader.ReadUInt16(); _target = reader.ReadName(); }
/// <summary> /// Initialises a new instance of the DnsQuestion class and the reply /// reader from which the question will be decoded. /// </summary> /// <param name="reader">The reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when question data could not be read from the /// <paramref name="reader"/>. /// </exception> public DnsQuestion(IDnsReader reader) { Guard.NotNull(reader, "reader"); try { _name = reader.ReadName(); _type = reader.ReadQueryType(); _class = reader.ReadQueryClass(); } catch (DnsException exc) { throw Guard.InvalidDnsQuestionFormat(exc); } }
/// <summary> /// Initialises a new instance of the WksRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public WksRecord(DnsName owner, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Wks, DnsRecordClass.IN, ttl) { Guard.NotNull(reader, "reader"); byte[] buf; int rdl = reader.ReadUInt16(); _address = reader.ReadIPv4Address(); _protocol = reader.ReadByte(); buf = new byte[rdl - DnsWireWriter.IPv4AddrLength - 1]; reader.ReadBytes(buf, 0, buf.Length); _bitmap = new BitArray(buf); }
/// <summary> /// Initialises a new instance of the SoaRecord class and specifies the owner name, /// the resource record class, the TTL of the record and the reply reader from /// which the RDLENGTH and RDATA section of the resource record will be read. /// </summary> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public SoaRecord(DnsName owner, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) : base(owner, DnsRecordType.Soa, cls, ttl) { Guard.NotNull(reader, "reader"); // Skip the RDLENGTH. reader.ReadUInt16(); _master = reader.ReadName(); _rMbox = reader.ReadName(); _serial = reader.ReadUInt32(); _refresh = reader.ReadTtl(); _retry = reader.ReadTtl(); _expire = reader.ReadTtl(); _minimum = reader.ReadTtl(); }
/// <summary> /// Builds a record of the specified <paramref name="type"/> using the /// specified owner name, resource record class, TTL and the reply reader /// from which the RDLENGTH and RDATA section of the resource record will /// be read. /// </summary> /// <param name="type">The type of record to build.</param> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <returns>The built <see cref="AK.Net.Dns.DnsRecord"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="System.NotSupportedException"> /// Thrown when the builder is not capable of building records of the /// specified <paramref name="type"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public DnsRecord Build(DnsName owner, DnsRecordType type, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) { switch (type) { case DnsRecordType.A: return(new ARecord(owner, ttl, reader)); case DnsRecordType.Aaaa: return(new AaaaRecord(owner, ttl, reader)); case DnsRecordType.NS: return(new NSRecord(owner, cls, ttl, reader)); case DnsRecordType.CName: return(new CNameRecord(owner, cls, ttl, reader)); case DnsRecordType.Soa: return(new SoaRecord(owner, cls, ttl, reader)); case DnsRecordType.Null: return(new NullRecord(owner, cls, ttl, reader)); case DnsRecordType.Wks: return(new WksRecord(owner, ttl, reader)); case DnsRecordType.Ptr: return(new PtrRecord(owner, cls, ttl, reader)); case DnsRecordType.HInfo: return(new HInfoRecord(owner, cls, ttl, reader)); case DnsRecordType.MB: return(new MBRecord(owner, cls, ttl, reader)); case DnsRecordType.MG: return(new MGRecord(owner, cls, ttl, reader)); case DnsRecordType.MInfo: return(new MInfoRecord(owner, cls, ttl, reader)); case DnsRecordType.MR: return(new MRRecord(owner, cls, ttl, reader)); case DnsRecordType.MX: return(new MXRecord(owner, cls, ttl, reader)); case DnsRecordType.Txt: return(new TxtRecord(owner, cls, ttl, reader)); case DnsRecordType.DN: return(new DNRecord(owner, cls, ttl, reader)); case DnsRecordType.Spf: return(new SpfRecord(owner, cls, ttl, reader)); case DnsRecordType.Srv: return(new SrvRecord(owner, cls, ttl, reader)); default: throw Guard.NotSupported(); } }
/// <summary> /// Builds a record of the specified <paramref name="type"/> using the /// specified owner name, resource record class, TTL and the reply reader /// from which the RDLENGTH and RDATA section of the resource record will /// be read. /// </summary> /// <param name="type">The type of record to build.</param> /// <param name="owner">The owner name.</param> /// <param name="cls">The class of resource record.</param> /// <param name="ttl">The TTL.</param> /// <param name="reader">The reply reader.</param> /// <returns>The built <see cref="AK.Net.Dns.DnsRecord"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="owner"/> or <paramref name="reader"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="System.NotSupportedException"> /// Thrown when the builder is not capable of building records of the /// specified <paramref name="type"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when the RDATA section of the record could not be read from the /// <paramref name="reader"/>. /// </exception> public DnsRecord Build(DnsName owner, DnsRecordType type, DnsRecordClass cls, TimeSpan ttl, IDnsReader reader) { return(new XRecord(owner, type, cls, ttl, reader)); }