/// <summary> /// Parses a resource record from the packet passed. /// </summary> /// <param name="message">The message containing the record being parsed.</param> /// <param name="packet">The raw DNS message packet.</param> /// <param name="offset"> /// The current offset in the packet. Returns as the offset of the first byte /// after the record. /// </param> /// <returns>The resource record or <c>null</c> if there was an error.</returns> public static DnsRR Parse(DnsMessage message, byte[] packet, ref int offset) { string qname; DnsRRType rrtype; DnsRR rr; int savePos; // Peek the qname and record type savePos = offset; if (!message.ReadName(packet, ref offset, out qname)) { return(null); } rrtype = (DnsRRType)Helper.ReadInt16(packet, ref offset); offset = savePos; switch (rrtype) { case DnsRRType.A: rr = new A_RR(); break; case DnsRRType.CNAME: rr = new CNAME_RR(); break; case DnsRRType.NS: rr = new NS_RR(); break; case DnsRRType.MX: rr = new MX_RR(); break; case DnsRRType.SOA: rr = new SOA_RR(); break; default: rr = new DnsRR(); break; } if (!rr.Read(message, packet, ref offset)) { return(null); } return(rr); }
/// <summary> /// This method parses the resource record from the packet beginning /// at the offset passed. /// </summary> /// <param name="message">The message containing the record being parsed.</param> /// <param name="packet">The packet buffer.</param> /// <param name="offset"> /// The offset in the packet where the record is located. /// This parameter will return set to the offset of the first /// byte after the parsed record. /// </param> /// <returns>True on success.</returns> public bool Read(DnsMessage message, byte[] packet, ref int offset) { if (!message.ReadName(packet, ref offset, out rname)) { return(false); } rrtype = (DnsRRType)Helper.ReadInt16(packet, ref offset); qclass = (DnsQClass)Helper.ReadInt16(packet, ref offset); ttl = Helper.ReadInt32(packet, ref offset); return(ReadData(message, packet, ref offset)); }
/// <summary> /// This method parses the resource record's (including its /// length from the packet beginning at the offset passed. /// </summary> /// <param name="message">The message containing the record being parsed.</param> /// <param name="packet">The packet buffer.</param> /// <param name="offset"> /// The offset in the packet where the data will be read. /// This parameter will return set to the offset of the first /// byte after the parsed data. /// </param> /// <returns>True on success.</returns> protected override bool ReadData(DnsMessage message, byte[] packet, ref int offset) { int pos; int cb; cb = Helper.ReadInt16(packet, ref offset); pos = offset; if (!message.ReadName(packet, ref offset, out cname)) { return(false); } return(pos + cb == offset); }