Exemple #1
0
        internal static DomainName Get(BackReferenceBinaryReader reader)
        {
            byte stringLength = reader.ReadByte();

            if (stringLength >> 6 == 3)
            {
                //In case of pointer
                ushort ptr;
                BinaryHelper.FromBytes(new byte[] { (byte)(stringLength - (3 << 6)), reader.ReadByte() }, out ptr);

                return(reader.Get <DomainName>(ptr));
            }
            else
            {
                DomainName dn = new DomainName();

                reader.Register((int)reader.BaseStream.Position - 1, dn);

                //stringLength = reader.ReadByte();
                if (stringLength != 0)
                {
                    dn.Add(Encoding.UTF8.GetString(reader.ReadBytes(stringLength), 0, stringLength));
                    //dn.Add(Encoding.UTF8.GetString(bytes, index + 1, bytes[index]));

                    dn.AddRange(DomainName.Get(reader));
                }
                //else
                //    index++;
                return(dn);
            }
        }
Exemple #2
0
        private static Message GetMessage(BackReferenceBinaryReader reader)
        {
            ushort  id = BinaryHelper.ReadUInt16(reader);
            Message m  = new Message(id);

            byte b = reader.ReadByte();

            //Qr, Opcode, Aa, Tc, Rd
            m.RecursionDesired = (b % 2) == 1;
            b                     = (byte)(b >> 1);
            m.Truncated           = (b % 2) == 1;
            b                     = (byte)(b >> 1);
            m.AuthoritativeAnswer = (b % 2) == 1;
            b                     = (byte)(b >> 1);
            int opCodeNumber = b % 16;

            m.OpCode        = (OpCode)opCodeNumber;
            b               = (byte)(b >> 4);
            m.QueryResponse = (Qr)b;

            //Ra, Z, Rcode
            b = reader.ReadByte();
            m.RecursionAvailable = b > 127;
            b = (byte)((b << 1) >> 1);
            m.ResponseCode = (ResponseCode)b;

            ushort questionEntryCount   = BinaryHelper.ReadUInt16(reader);
            ushort answerEntryCount     = BinaryHelper.ReadUInt16(reader);
            ushort authorityEntryCount  = BinaryHelper.ReadUInt16(reader);
            ushort additionalEntryCount = BinaryHelper.ReadUInt16(reader);

            for (int i = 0; i < questionEntryCount; i++)
            {
                m.Questions.Add(Question.Get(reader));
            }

            for (int i = 0; i < answerEntryCount; i++)
            {
                m.Answers.Add(Answer.Get(reader));
            }

            for (int i = 0; i < authorityEntryCount; i++)
            {
                m.Authorities.Add(Answer.Get(reader));
            }

            for (int i = 0; i < additionalEntryCount; i++)
            {
                m.Additionals.Add(Answer.Get(reader));
            }

            return(m);
        }