public static DnsPackage FromArray(byte[] clientMessage) { Console.WriteLine(clientMessage.Length); var header = PackageHeader.FromArray(clientMessage); Console.WriteLine("HEADER DONE"); var question = PackageQuestion.FromArray(clientMessage, 12); Console.WriteLine("QUESTION DONE"); var answers = new List <PackageAnswer>(); var lastSize = 12 + question.Size; for (int i = 0; i < header.ANCOUNT; i++) { var answer = PackageAnswer.FromArray(clientMessage, lastSize); if (answer.Size == 6) { break; } answers.Add(answer); lastSize += answer.Size; } var auths = new List <PackageAnswer>(); for (int i = 0; i < header.NSCOUNT; i++) { var authorityAnswer = PackageAnswer.FromArray(clientMessage, lastSize); lastSize += authorityAnswer.Size; authorityAnswer.SetNormalName(clientMessage); auths.Add(authorityAnswer); } var additionals = new List <PackageAnswer>(); for (int i = 0; i < header.ARCOUNT; i++) { var additionalAnswer = PackageAnswer.FromArray(clientMessage, lastSize); lastSize += additionalAnswer.Size; additionalAnswer.Question.QTYPE = (int)Type.NS; additionalAnswer.SetNormalName(clientMessage); additionals.Add(additionalAnswer); } foreach (var add in additionals) { var q = auths.FirstOrDefault(x => ArraysEquals(x.RDATA, add.Question.QNAME)); if (q != null) { add.Question.QNAME = q.Question.QNAME; } } Console.WriteLine("ANSWER DONE"); return(new DnsPackage(header, question, answers) { Additionals = additionals, Auth = auths }); }
public static PackageHeader FromArray(byte[] clientMessage) { var header = new PackageHeader { ID = BitConverter.ToInt16(clientMessage, 0), QR = BitConverter.ToUInt16(clientMessage, 2) >> 15, AA = (BitConverter.ToUInt16(clientMessage, 2) << 5) >> 15, TC = (BitConverter.ToUInt16(clientMessage, 2) << 6) >> 15, RA = (BitConverter.ToUInt16(clientMessage, 2) << 8) >> 15, RCODE = (BitConverter.ToUInt16(clientMessage, 2) << 12) >> 15, QDCOUNT = BitConverter.ToInt16(clientMessage.Skip(4).Take(2).Reverse().ToArray(), 0), ANCOUNT = BitConverter.ToInt16(clientMessage.Skip(6).Take(2).Reverse().ToArray(), 0), NSCOUNT = BitConverter.ToInt16(clientMessage.Skip(8).Take(2).Reverse().ToArray(), 0), ARCOUNT = BitConverter.ToInt16(clientMessage.Skip(10).Take(2).Reverse().ToArray(), 0) }; return(header); }
public DnsPackage(PackageHeader header, PackageQuestion question, List <PackageAnswer> answers) { Header = header; Question = question; Answers = answers; }