Beispiel #1
0
        public static Message FromBytes(byte[] bytes, int index, int count)
        {
            Message message = new Message();

            using (MemoryStream stream = new MemoryStream(bytes, index, count))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    // ID
                    message.TransactionID = reader.ReadNetworkOrderUInt16();

                    // Flags
                    // QR, OPCODE, AA, TC, RD
                    byte flags = reader.ReadByte();
                    // QR
                    message.QueryResponse = flags.GetBit(7);
                    // OPCODE
                    message.Opcode = (byte)((flags >> 3) & 0x0f);
                    // AA
                    message.AuthoritativeAnswer = flags.GetBit(2);
                    // TC
                    message.Truncated = flags.GetBit(1);
                    // RD
                    message.RecursionDesired = flags.GetBit(0);

                    // RA, Z, AD, CD, RCODE
                    flags = reader.ReadByte();
                    // RA
                    message.RecursionAvailable = flags.GetBit(7);
                    // Z
                    // (zero bit)
                    // AD
                    message.AuthenticData = flags.GetBit(5);
                    // CD
                    message.CheckingDisabled = flags.GetBit(4);
                    // RCODE
                    message.ResponseCode = (byte)(flags & 0x0f);

                    // Number of questions and answer records
                    ushort questions, answers, authorityRecords, additionalRecords;

                    // Number of Questions
                    questions = reader.ReadNetworkOrderUInt16();
                    // Number of Answers
                    answers = reader.ReadNetworkOrderUInt16();
                    // Number of Authority Records
                    authorityRecords = reader.ReadNetworkOrderUInt16();
                    // Number of Additional Records
                    additionalRecords = reader.ReadNetworkOrderUInt16();

                    // Read each Question and Resource Record
                    // Questions
                    while (questions-- > 0)
                    {
                        message.Questions.Add(Question.FromBinaryReader(reader));
                    }
                    // Answers
                    while (answers-- > 0)
                    {
                        message.AnswerRecords.Add(ResourceRecord.FromBinaryReader(reader));
                    }
                    // Authority Records
                    while (authorityRecords-- > 0)
                    {
                        message.AnswerRecords.Add(ResourceRecord.FromBinaryReader(reader));
                    }
                    // Additional Records
                    while (additionalRecords-- > 0)
                    {
                        message.AdditionalRecords.Add(ResourceRecord.FromBinaryReader(reader));
                    }
                }

            return(message);
        }