Ejemplo n.º 1
0
        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
            });
        }
Ejemplo n.º 2
0
        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);
        }