/// <summary> /// Create from an array /// </summary> /// <param name="data">The data</param> /// <returns>The parsed DNS packet</returns> public static DNSPacket FromArray(byte[] data) { DataReader reader = new DataReader(new MemoryStream(data)); DNSPacket ret = new DNSPacket(); ret.Id = reader.ReadUInt16(false); ushort flags = reader.ReadUInt16(false); ret.Query = GetBooleanFlag(flags, 0); ret.Opcode = (DNSOpcode)GetFlagValue(flags, 1); ret.AuthoritiveAnswer = GetBooleanFlag(flags, 5); ret.Truncation = GetBooleanFlag(flags, 6); ret.RecursionDesired = GetBooleanFlag(flags, 7); ret.RecursionAvailable = GetBooleanFlag(flags, 8); ret.ResponseCode = (DNSRCode)GetFlagValue(flags, 12); ushort qdcount = reader.ReadUInt16(false); ushort ancount = reader.ReadUInt16(false); ushort nscount = reader.ReadUInt16(false); ushort arcount = reader.ReadUInt16(false); if (qdcount > 0) { DNSQuestion[] questions = new DNSQuestion[qdcount]; for (int i = 0; i < qdcount; i++) { questions[i] = ReadQuestion(data, reader); } ret.Questions = questions; } if (ancount > 0) { ret.Answers = ReadResourceRecords(data, reader, ancount); } if (nscount > 0) { ret.NameServers = ReadResourceRecords(data, reader, nscount); } if (arcount > 0) { ret.Additional = ReadResourceRecords(data, reader, arcount); } return(ret); }
/// <summary> /// Write record to write /// </summary> /// <param name="writer">The writer</param> /// <param name="stringCache">String cache</param> public void ToWriter(DataWriter writer, Dictionary <string, int> stringCache) { DNSPacket.WriteString(Name, writer, stringCache); writer.WriteUInt16((ushort)Type, false); writer.WriteUInt16((ushort)Class, false); writer.WriteUInt32(TimeToLive, false); long currPos = writer.GetStream().Position; writer.WriteUInt16(0, false); WriteData(writer, stringCache); long endPos = writer.GetStream().Position; writer.GetStream().Position = currPos; if ((endPos - currPos - 2) > (long)ushort.MaxValue) { throw new ArgumentException(String.Format("RR data cannot be longer than {0}", ushort.MaxValue)); } writer.WriteUInt16((ushort)(endPos - currPos - 2), false); writer.GetStream().Position = endPos; }
/// <summary> /// Create from an array /// </summary> /// <param name="data">The data</param> /// <returns>The parsed DNS packet</returns> public static DNSPacket FromArray(byte[] data) { DataReader reader = new DataReader(new MemoryStream(data)); DNSPacket ret = new DNSPacket(); ret.Id = reader.ReadUInt16(false); ushort flags = reader.ReadUInt16(false); ret.Query = GetBooleanFlag(flags, 0); ret.Opcode = (DNSOpcode)GetFlagValue(flags, 1); ret.AuthoritiveAnswer = GetBooleanFlag(flags, 5); ret.Truncation = GetBooleanFlag(flags, 6); ret.RecursionDesired = GetBooleanFlag(flags, 7); ret.RecursionAvailable = GetBooleanFlag(flags, 8); ret.ResponseCode = (DNSRCode)GetFlagValue(flags, 12); ushort qdcount = reader.ReadUInt16(false); ushort ancount = reader.ReadUInt16(false); ushort nscount = reader.ReadUInt16(false); ushort arcount = reader.ReadUInt16(false); if (qdcount > 0) { DNSQuestion[] questions = new DNSQuestion[qdcount]; for (int i = 0; i < qdcount; i++) { questions[i] = ReadQuestion(data, reader); } ret.Questions = questions; } if (ancount > 0) { ret.Answers = ReadResourceRecords(data, reader, ancount); } if (nscount > 0) { ret.NameServers = ReadResourceRecords(data, reader, nscount); } if (arcount > 0) { ret.Additional = ReadResourceRecords(data, reader, arcount); } return ret; }