Пример #1
0
        public DnsHeader(Stream s)
        {
            _ID = DnsDatagram.ReadUInt16NetworkOrder(s);

            int lB = s.ReadByte();

            _QR     = Convert.ToByte((lB & 0x80) >> 7);
            _OPCODE = (DnsOpcode)Convert.ToByte((lB & 0x78) >> 3);
            _AA     = Convert.ToByte((lB & 0x4) >> 2);
            _TC     = Convert.ToByte((lB & 0x2) >> 1);
            _RD     = Convert.ToByte(lB & 0x1);

            int rB = s.ReadByte();

            _RA    = Convert.ToByte((rB & 0x80) >> 7);
            _Z     = Convert.ToByte((rB & 0x40) >> 6);
            _AD    = Convert.ToByte((rB & 0x20) >> 5);
            _CD    = Convert.ToByte((rB & 0x10) >> 4);
            _RCODE = (DnsResponseCode)(rB & 0xf);

            _QDCOUNT = DnsDatagram.ReadUInt16NetworkOrder(s);
            _ANCOUNT = DnsDatagram.ReadUInt16NetworkOrder(s);
            _NSCOUNT = DnsDatagram.ReadUInt16NetworkOrder(s);
            _ARCOUNT = DnsDatagram.ReadUInt16NetworkOrder(s);
        }
Пример #2
0
        public DnsPacket(byte[] buffer)
        {
            int index = 0;

            Identification           = DnsUtil.ToUInt16(buffer, ref index);
            QueryResponse            = (buffer[index] & (1 << 7)) != 0;
            Opcode                   = (DnsOpcode)((buffer[index] >> 3) & 0b1111);
            AuthoritativeAnswer      = (buffer[index] & (1 << 2)) != 0;
            Truncated                = (buffer[index] & (1 << 1)) != 0;
            RecursionDesired         = (buffer[index++] & 1) != 0;
            RecursionAvailable       = (buffer[index] & (1 << 7)) != 0;
            AuthenticatedData        = (buffer[index] & (1 << 5)) != 0;
            CheckingDisabled         = (buffer[index] & (1 << 4)) != 0;
            ReturnCode               = (DnsReturnCode)(buffer[index++] & 0b1111);
            TotalQuestions           = DnsUtil.ToUInt16(buffer, ref index);
            TotalAnswers             = DnsUtil.ToUInt16(buffer, ref index);
            TotalAuthorities         = DnsUtil.ToUInt16(buffer, ref index);
            TotalAdditionalResources = DnsUtil.ToUInt16(buffer, ref index);
            for (int i = 0; i < TotalQuestions; ++i)
            {
                Questions[i] = new DnsQuestion(buffer, ref index);
            }
            for (int i = 0; i < TotalAnswers; ++i)
            {
                Answers[i] = new DnsResourceRecord(buffer, ref index);
            }
            for (int i = 0; i < TotalAuthorities; ++i)
            {
                Authorities[i] = new DnsResourceRecord(buffer, ref index);
            }
            for (int i = 0; i < TotalAdditionalResources; ++i)
            {
                AdditionalResources[i] = new DnsResourceRecord(buffer, ref index);
            }
        }
Пример #3
0
        public DnsHeader(dynamic jsonResponse)
        {
            _QR     = 1; //is response
            _OPCODE = DnsOpcode.StandardQuery;

            _TC    = (byte)(jsonResponse.TC.Value ? 1 : 0);
            _RD    = (byte)(jsonResponse.RD.Value ? 1 : 0);
            _RA    = (byte)(jsonResponse.RA.Value ? 1 : 0);
            _AD    = (byte)(jsonResponse.AD.Value ? 1 : 0);
            _CD    = (byte)(jsonResponse.CD.Value ? 1 : 0);
            _RCODE = (DnsResponseCode)jsonResponse.Status;

            _QDCOUNT = Convert.ToUInt16(jsonResponse.Question.Count);

            if (jsonResponse.Answer != null)
            {
                _ANCOUNT = Convert.ToUInt16(jsonResponse.Answer.Count);
            }

            if (jsonResponse.Authority != null)
            {
                _NSCOUNT = Convert.ToUInt16(jsonResponse.Authority.Count);
            }

            if (jsonResponse.Additional != null)
            {
                _ARCOUNT = Convert.ToUInt16(jsonResponse.Additional.Count);
            }
        }
Пример #4
0
        public DnsHeader(ushort ID, bool isResponse, DnsOpcode OPCODE, bool authoritativeAnswer, bool truncation, bool recursionDesired, bool recursionAvailable, bool authenticData, bool checkingDisabled, DnsResponseCode RCODE, ushort QDCOUNT, ushort ANCOUNT, ushort NSCOUNT, ushort ARCOUNT)
        {
            _ID = ID;

            if (_ID == 0)
            {
                byte[] buffer = new byte[2];
                DnsClient._rnd.GetBytes(buffer);

                _ID = BitConverter.ToUInt16(buffer, 0);
            }

            if (isResponse)
            {
                _QR = 1;
            }

            _OPCODE = OPCODE;

            if (authoritativeAnswer)
            {
                _AA = 1;
            }

            if (truncation)
            {
                _TC = 1;
            }

            if (recursionDesired)
            {
                _RD = 1;
            }

            if (recursionAvailable)
            {
                _RA = 1;
            }

            if (authenticData)
            {
                _AD = 1;
            }

            if (checkingDisabled)
            {
                _CD = 1;
            }

            _RCODE = RCODE;

            _QDCOUNT = QDCOUNT;
            _ANCOUNT = ANCOUNT;
            _NSCOUNT = NSCOUNT;
            _ARCOUNT = ARCOUNT;
        }
Пример #5
0
        public DnsHeader(ushort ID, bool isResponse, DnsOpcode OPCODE, bool authoritativeAnswer, bool truncation, bool recursionDesired, bool recursionAvailable, bool authenticData, bool checkingDisabled, DnsResponseCode RCODE, ushort QDCOUNT, ushort ANCOUNT, ushort NSCOUNT, ushort ARCOUNT)
        {
            _ID = ID;

            if (isResponse)
            {
                _QR = 1;
            }

            _OPCODE = OPCODE;

            if (authoritativeAnswer)
            {
                _AA = 1;
            }

            if (truncation)
            {
                _TC = 1;
            }

            if (recursionDesired)
            {
                _RD = 1;
            }

            if (recursionAvailable)
            {
                _RA = 1;
            }

            if (authenticData)
            {
                _AD = 1;
            }

            if (checkingDisabled)
            {
                _CD = 1;
            }

            _RCODE = RCODE;

            _QDCOUNT = QDCOUNT;
            _ANCOUNT = ANCOUNT;
            _NSCOUNT = NSCOUNT;
            _ARCOUNT = ARCOUNT;
        }
Пример #6
0
        public DnsDatagram(ushort ID, bool isResponse, DnsOpcode OPCODE, bool authoritativeAnswer, bool truncation, bool recursionDesired, bool recursionAvailable, bool authenticData, bool checkingDisabled, DnsResponseCode RCODE, IReadOnlyList <DnsQuestionRecord> question, IReadOnlyList <DnsResourceRecord> answer = null, IReadOnlyList <DnsResourceRecord> authority = null, IReadOnlyList <DnsResourceRecord> additional = null)
        {
            _ID = ID;

            if (isResponse)
            {
                _QR = 1;
            }

            _OPCODE = OPCODE;

            if (authoritativeAnswer)
            {
                _AA = 1;
            }

            if (truncation)
            {
                _TC = 1;
            }

            if (recursionDesired)
            {
                _RD = 1;
            }

            if (recursionAvailable)
            {
                _RA = 1;
            }

            if (authenticData)
            {
                _AD = 1;
            }

            if (checkingDisabled)
            {
                _CD = 1;
            }

            _RCODE = RCODE;

            _question   = question;
            _answer     = answer;
            _authority  = authority;
            _additional = additional;

            if (_answer == null)
            {
                _answer = Array.Empty <DnsResourceRecord>();
            }

            if (_authority == null)
            {
                _authority = Array.Empty <DnsResourceRecord>();
            }

            if (_additional == null)
            {
                _additional = Array.Empty <DnsResourceRecord>();
            }
        }