public byte[] ToByteArray() { byte[] header = new byte[12]; int index = 0; DnsUtil.GetBytes(header, ref index, Identification); header[index++] = (byte)((QueryResponse ? 1 << 7 : 0) | (((int)Opcode) << 3) | (AuthoritativeAnswer ? 1 << 2 : 0) | (Truncated ? 1 << 1 : 0) | (RecursionDesired ? 1 : 0)); header[index++] = (byte)((RecursionAvailable ? 1 << 7 : 0) | (AuthenticatedData ? 1 << 5 : 0) | (CheckingDisabled ? 1 << 4 : 0) | ((int)ReturnCode)); DnsUtil.GetBytes(header, ref index, TotalQuestions); DnsUtil.GetBytes(header, ref index, TotalAnswers); DnsUtil.GetBytes(header, ref index, TotalAuthorities); DnsUtil.GetBytes(header, ref index, TotalAdditionalResources); return(header.Concat(Questions.SelectMany(o => o.ToByteArray())) .Concat(Answers.SelectMany(o => o.ToByteArray())) .Concat(Authorities.SelectMany(o => o.ToByteArray())) .Concat(AdditionalResources.SelectMany(o => o.ToByteArray())) .ToArray()); }
public DnsPacket(byte[] buffer) { int index = 0; Identification = DnsUtil.ToUInt16(buffer, ref index); QueryResponse = (buffer[index] & (1 << 7)) != 0; Opcode = (DnsOpcode)((buffer[index] >> 3) & 0b1111); AuthoritativeAnswer = (buffer[index] & (1 << 2)) != 0; Truncated = (buffer[index] & (1 << 1)) != 0; RecursionDesired = (buffer[index++] & 1) != 0; RecursionAvailable = (buffer[index] & (1 << 7)) != 0; AuthenticatedData = (buffer[index] & (1 << 5)) != 0; CheckingDisabled = (buffer[index] & (1 << 4)) != 0; ReturnCode = (DnsReturnCode)(buffer[index++] & 0b1111); TotalQuestions = DnsUtil.ToUInt16(buffer, ref index); TotalAnswers = DnsUtil.ToUInt16(buffer, ref index); TotalAuthorities = DnsUtil.ToUInt16(buffer, ref index); TotalAdditionalResources = DnsUtil.ToUInt16(buffer, ref index); for (int i = 0; i < TotalQuestions; ++i) { Questions[i] = new DnsQuestion(buffer, ref index); } for (int i = 0; i < TotalAnswers; ++i) { Answers[i] = new DnsResourceRecord(buffer, ref index); } for (int i = 0; i < TotalAuthorities; ++i) { Authorities[i] = new DnsResourceRecord(buffer, ref index); } for (int i = 0; i < TotalAdditionalResources; ++i) { AdditionalResources[i] = new DnsResourceRecord(buffer, ref index); } }
public DnsResourceRecord(byte[] buffer, ref int index) { Name = DnsUtil.DecodeQuestionName(buffer, ref index); Type = (DnsRecordType)DnsUtil.ToUInt16(buffer, ref index); Class = (DnsClass)DnsUtil.ToUInt16(buffer, ref index); TimeToLive = DnsUtil.ToUInt32(buffer, ref index); DataLength = DnsUtil.ToUInt16(buffer, ref index); Array.Copy(buffer, index, Data, 0, DataLength); index += DataLength; }
public byte[] ToByteArray() { int index = Name.Length + 2; byte[] data = new byte[index + 10 + DataLength]; DnsUtil.EncodeQuestionName(Name, data, 0); DnsUtil.GetBytes(data, ref index, (ushort)Type); DnsUtil.GetBytes(data, ref index, (ushort)Class); DnsUtil.GetBytes(data, ref index, TimeToLive); DnsUtil.GetBytes(data, ref index, DataLength); Data.CopyTo(data, index); return(data); }
static IEnumerable <string> DecodeQuestionNameParts(byte[] buffer, ref int index) { List <string> parts = new List <string>(); while (buffer[index] != 0) { if ((buffer[index] & 0b11000000) == 0b11000000) { int ptr = DnsUtil.ToUInt16(buffer, ref index); ptr ^= 0b1100000000000000; parts.AddRange(DecodeQuestionNameParts(buffer, ref ptr)); return(parts); }