コード例 #1
0
        public void TestUnserializeDNSPacketWithUnsupportedQuestion()
        {
            var stream = DNSInput(new byte[]
            {
                // Big-endian 42
                0,
                42,
                // This is the query/resonse bit, the query type, the authority
                // bit, the truncation bit, and the recursion desired bit
                4,
                // This is the recursion available bit, the zero segment,
                // and the return code
                128,
                // 1 question
                0,
                1,
                // 0 answers
                0,
                0,
                // 0 authorities
                0,
                0,
                // 0 additional
                0,
                0,
                // The question - NULL record for example.com
                7,     // Length of example
                101,
                120,
                97,
                109,
                112,
                108,
                101,
                3,                         // Length of com
                99,
                111,
                109,
                0,
                // NULL record has code 10
                0,
                10,
                // CHAOS has class 3
                0,
                3,
            });
            var packet = DNSPacket.Unserialize(stream);

            var question = new DNSQuestion(
                new Domain("example.com"),
                (ResourceRecordType)10,
                (AddressClass)3);

            var expected = new DNSPacket(
                42,
                true, QueryType.STANDARD_QUERY, true, false, false, true, ResponseType.NO_ERROR,
                new DNSQuestion[] { question }, new DNSRecord[0], new DNSRecord[0], new DNSRecord[0]);

            Assert.That(packet, Is.EqualTo(expected));
        }
コード例 #2
0
        public void TestUnserializeDNSUnsupportedQuestion()
        {
            var stream = DNSInput(new byte[]
            {
                7, 101, 120, 97, 109, 112, 108, 101,
                3, 99, 111, 109,
                0,
                0, 10,
                0, 3,
            });

            var question = DNSQuestion.Unserialize(stream);

            var expected = new DNSQuestion(
                new Domain("example.com"),
                (ResourceRecordType)10,
                (AddressClass)3);

            Assert.That(question, Is.EqualTo(expected));
        }
コード例 #3
0
        public void TestUnserializeDNSQuestion()
        {
            var stream = DNSInput(new byte[]
            {
                7, 101, 120, 97, 109, 112, 108, 101,
                3, 99, 111, 109,
                0,
                0, 1,
                0, 1,
            });

            var question = DNSQuestion.Unserialize(stream);

            var expected = new DNSQuestion(
                new Domain("example.com"),
                ResourceRecordType.HOST_ADDRESS,
                AddressClass.INTERNET);

            Assert.That(question, Is.EqualTo(expected));
        }
コード例 #4
0
        public void TestSerializeDNSUnsupportedQuestion()
        {
            Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput();

            var question = new DNSQuestion(
                new Domain("example.com"),
                (ResourceRecordType)10,           // Unused NULL resource record
                (AddressClass)3);                 // Unsupported CHAOSnet class

            question.Serialize(out_info.Item2);
            var question_bytes = out_info.Item1.ToArray();

            var expected = new byte[]
            {
                7, // Length of example
                101,
                120,
                97,
                109,
                112,
                108,
                101,
                3, // Length of com
                99,
                111,
                109,
                0,
                // NULL record has code 1
                0,
                10,
                // CHAOS has class 1
                0,
                3,
            };

            Assert.That(question_bytes, Is.EqualTo(expected));
        }
コード例 #5
0
        public void TestSerializeDNSQuestion()
        {
            Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput();

            var question = new DNSQuestion(
                new Domain("example.com"),
                ResourceRecordType.HOST_ADDRESS,
                AddressClass.INTERNET);

            question.Serialize(out_info.Item2);
            var question_bytes = out_info.Item1.ToArray();

            var expected = new byte[]
            {
                7, // Length of example
                101,
                120,
                97,
                109,
                112,
                108,
                101,
                3, // Length of com
                99,
                111,
                109,
                0,
                // A record has code 1
                0,
                1,
                // INTERNET has class 1
                0,
                1,
            };

            Assert.That(question_bytes, Is.EqualTo(expected));
        }
コード例 #6
0
ファイル: DNSPacket.cs プロジェクト: adamnew123456/DNS
        public static DNSPacket Unserialize(DNSInputStream stream)
        {
            var id = stream.ReadUInt16();

            var is_query_bit          = new Field(1);
            var query_type_flag       = new Field(4);
            var is_authority_bit      = new Field(1);
            var is_truncated_bit      = new Field(1);
            var recursive_request_bit = new Field(1);

            new FieldGroup()
            .Add(is_query_bit)
            .Add(query_type_flag)
            .Add(is_authority_bit)
            .Add(is_truncated_bit)
            .Add(recursive_request_bit)
            .Unpack(stream.ReadByte());

            var is_query          = is_query_bit.Value == 0;
            var query_type        = (QueryType)query_type_flag.Value;
            var is_authority      = is_authority_bit.Value == 1;
            var is_truncated      = is_truncated_bit.Value == 1;
            var recursive_request = recursive_request_bit.Value == 1;

            var recursion_availble_bit = new Field(1);
            var zeroes             = new Field(3);
            var response_type_flag = new Field(4);

            new FieldGroup()
            .Add(recursion_availble_bit)
            .Add(zeroes)
            .Add(response_type_flag)
            .Unpack(stream.ReadByte());

            var recursive_response = recursion_availble_bit.Value == 1;
            var response_type      = (ResponseType)response_type_flag.Value;

            var question_count   = stream.ReadUInt16();
            var answer_count     = stream.ReadUInt16();
            var authority_count  = stream.ReadUInt16();
            var additional_count = stream.ReadUInt16();

            var questions = new DNSQuestion[question_count];

            for (int i = 0; i < question_count; i++)
            {
                questions[i] = DNSQuestion.Unserialize(stream);
                if (questions[i] == null)
                {
                    throw new InvalidDataException("null Question " + i);
                }
            }

            var answers = new DNSRecord[answer_count];

            for (int i = 0; i < answer_count; i++)
            {
                answers[i] = DNSRecord.Unserialize(stream);
                if (answers[i] == null)
                {
                    throw new InvalidDataException("null Answer " + i);
                }
            }

            var authority = new DNSRecord[authority_count];

            for (int i = 0; i < authority_count; i++)
            {
                authority[i] = DNSRecord.Unserialize(stream);
                if (authority[i] == null)
                {
                    throw new InvalidDataException("null Authority " + i);
                }
            }

            var additional = new DNSRecord[additional_count];

            for (int i = 0; i < additional_count; i++)
            {
                additional[i] = DNSRecord.Unserialize(stream);
                if (additional[i] == null)
                {
                    throw new InvalidDataException("null Additional " + i);
                }
            }

            return(new DNSPacket(id, is_query, query_type.Normalize(), is_authority, is_truncated,
                                 recursive_request, recursive_response, response_type.Normalize(),
                                 questions, answers, authority, additional));
        }
コード例 #7
0
        public void TestUnserializeDNSPacket()
        {
            var stream = DNSInput(new byte[]
            {
                // Big-endian 42
                0,
                42,
                // This is the query/resonse bit, the query type, the authority
                // bit, the truncation bit, and the recursion desired bit
                4,
                // This is the recursion available bit, the zero segment,
                // and the return code
                128,
                // 1 question
                0,
                1,
                // 1 answer
                0,
                1,
                // 0 authorities
                0,
                0,
                // 0 additional
                0,
                0,
                // The question - A record for example.com
                7,     // Length of example
                101,
                120,
                97,
                109,
                112,
                108,
                101,
                3,                         // Length of com
                99,
                111,
                109,
                0,
                // A record has code 1
                0,
                1,
                // INTERNET has class 1
                0,
                1,
                // The answer - the A record for example.com
                // Pointer to byte 96 - "example.com"
                192,
                12,
                // A record has code 1
                0,
                1,
                // INTERNET has class 1
                0,
                1,
                // Big-endian representation of 42
                0,
                0,
                0,
                42,
                // Record is 4 bytes long
                0,
                4,
                // The record itself
                192,
                168,
                0,
                1
            });
            var packet = DNSPacket.Unserialize(stream);

            var question = new DNSQuestion(
                new Domain("example.com"),
                ResourceRecordType.HOST_ADDRESS,
                AddressClass.INTERNET);

            var answer = new DNSRecord(
                new Domain("example.com"),
                AddressClass.INTERNET,
                42,
                new AResource(IPv4Address.Parse("192.168.0.1")));

            var expected = new DNSPacket(
                42,
                true, QueryType.STANDARD_QUERY, true, false, false, true, ResponseType.NO_ERROR,
                new DNSQuestion[] { question }, new DNSRecord[] { answer }, new DNSRecord[0], new DNSRecord[0]);

            Assert.That(packet, Is.EqualTo(expected));
        }
コード例 #8
0
        public void TestSerializeDNSPacketWithUnsupportedQuestion()
        {
            var question = new DNSQuestion(
                new Domain("example.com"),
                (ResourceRecordType)10,           // The NULL RR, supposedly not used
                (AddressClass)3);                 // CHAOSNet

            var packet = new DNSPacket(
                42,
                true, QueryType.STANDARD_QUERY, true, false, false, true, ResponseType.NO_ERROR,
                new DNSQuestion[] { question }, new DNSRecord[0], new DNSRecord[0], new DNSRecord[0]);

            Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput();

            packet.Serialize(out_info.Item2);
            var packet_bytes = out_info.Item1.ToArray();

            var expected = new byte[]
            {
                // Big-endian 42
                0,
                42,
                // This is the query/resonse bit, the query type, the authority
                // bit, the truncation bit, and the recursion desired bit
                4,
                // This is the recursion available bit, the zero segment,
                // and the return code
                128,
                // 1 question
                0,
                1,
                // 0 answers
                0,
                0,
                // 0 authorities
                0,
                0,
                // 0 additional
                0,
                0,
                // The question - NULL record for example.com
                7, // Length of example
                101,
                120,
                97,
                109,
                112,
                108,
                101,
                3, // Length of com
                99,
                111,
                109,
                0,
                // NULL record has class 10
                0,
                10,
                // CHAOS has class 3
                0,
                3
            };

            Assert.That(packet_bytes, Is.EqualTo(expected));
        }