public static PackageQuestion FromArray(byte[] clientMessage, int offset) { var question = new PackageQuestion { QNAME = new byte[0] }; var lastIndex = offset; for (var i = offset; i < clientMessage.Length; i++) { var number = BitConverter.ToInt16(new[] { clientMessage[i], new byte() }, 0); if (number >= 192) { question.QNAME = question.QNAME.Concat(new[] { clientMessage[i], clientMessage[i + 1] }).ToArray(); lastIndex += 2; break; } question.QNAME = question.QNAME.Concat(new[] { clientMessage[i] }).ToArray(); if (clientMessage[i].CompareTo(new byte()) != 0) { continue; } lastIndex = i; break; } question.QTYPE = BitConverter.ToInt16(new [] { clientMessage[lastIndex + 2], clientMessage[lastIndex + 1] }, 0); question.QCLASS = BitConverter.ToInt16(new[] { clientMessage[lastIndex + 4], clientMessage[lastIndex + 3] }, 0); return(question); }
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 PackageAnswer FromArray(byte[] clientMessage, int offset) { if (clientMessage.Length <= offset + 1) { return(new PackageAnswer()); } var answer = new PackageAnswer { Question = PackageQuestion.FromArray(clientMessage, offset) }; answer.TTL = BitConverter.ToInt32(clientMessage.Skip(offset + answer.Question.Size).Take(4).Reverse().ToArray(), 0); answer.RDLENGTH = BitConverter.ToInt16(clientMessage.Skip(offset + answer.Question.Size + 4).Take(2).Reverse().ToArray(), 0); answer.RDATA = clientMessage.Skip(offset + answer.Question.Size + 6).Take(answer.RDLENGTH).ToArray(); return(answer); }
protected bool Equals(PackageQuestion other) { return(Equals(Encoding.ASCII.GetString(QNAME), Encoding.ASCII.GetString(other.QNAME)) && TypesEquals(QTYPE, other.QTYPE) && ClassEquals(QCLASS, other.QCLASS)); }
public DnsPackage(PackageHeader header, PackageQuestion question, List <PackageAnswer> answers) { Header = header; Question = question; Answers = answers; }