コード例 #1
0
        private static byte[] GetResponseBytes(DnsQueryResponse message, byte[] answerData)
        {
            using (var writer = new DnsDatagramWriter())
            {
                writer.WriteUInt16NetworkOrder((ushort)message.Header.Id);
                writer.WriteUInt16NetworkOrder((ushort)message.Header.HeaderFlags);
                // lets iterate answers only, makse it easier
                //writer.WriteUInt16Network((ushort)message.Header.QuestionCount);
                writer.WriteUInt16NetworkOrder(0);
                writer.WriteUInt16NetworkOrder(1);
                //writer.WriteUInt16Network((ushort)message.Header.NameServerCount);
                writer.WriteUInt16NetworkOrder(0);
                //writer.WriteUInt16Network((ushort)message.Header.AdditionalCount);
                writer.WriteUInt16NetworkOrder(0);

                var answer = message.Answers.First();
                writer.WriteHostName(answer.DomainName.Value);
                writer.WriteUInt16NetworkOrder((ushort)answer.RecordType);
                writer.WriteUInt16NetworkOrder((ushort)answer.RecordClass);
                writer.WriteUInt32NetworkOrder((uint)answer.TimeToLive);
                writer.WriteUInt16NetworkOrder((ushort)answerData.Length);

                //writer.Extend(answerData.Length);   // the following data->length
                writer.WriteBytes(answerData, answerData.Length);

                return(writer.Data.ToArray());
            }
        }
コード例 #2
0
        public void DnsRecordFactory_NSecRecord()
        {
            var expectedBitMap = new byte[] { 0, 7, 98, 1, 128, 8, 0, 3, 128 };
            var expectedTypes  = new[]
            {
                ResourceRecordType.A,
                ResourceRecordType.NS,
                ResourceRecordType.SOA,
                ResourceRecordType.MX,
                ResourceRecordType.TXT,
                ResourceRecordType.AAAA,
                ResourceRecordType.RRSIG,
                ResourceRecordType.NSEC,
                ResourceRecordType.DNSKEY
            };

            var name   = DnsString.Parse("example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteHostName(name);

            var data = writer.Data.Concat(expectedBitMap).ToArray();

            var factory = GetFactory(data);

            var info = new ResourceRecordInfo(name, ResourceRecordType.NSEC, QueryClass.IN, 0, data.Length);

            var result = factory.GetRecord(info) as NSecRecord;

            Assert.Equal(expectedBitMap, result.TypeBitMapsRaw);
            Assert.Equal(expectedTypes.Length, result.TypeBitMaps.Count);
            Assert.Equal(expectedTypes, result.TypeBitMaps);
            Assert.Equal(name, result.NextDomainName);
        }
コード例 #3
0
        public void DnsRecordFactory_MBRecord()
        {
            var name = DnsString.Parse("Müsli.de");

            using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize))
            {
                var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer));
                writer.WriteHostName(name.Value);
                var factory = GetFactory(
                    writer.Data.ToArray(),
                    out var reader);
                var info = new ResourceRecord(
                    DnsString.Parse("Müsli.de"),
                    MbRecord.ResourceRecordType,
                    QueryClass.In,
                    0,
                    writer.Data.Count);

                var result = factory.GetRecord(
                    info,
                    reader) as MbRecord ?? throw new Exception();

                Assert.Equal(
                    result.MadName,
                    name);
                Assert.Equal(
                    "müsli.de.",
                    result.MadName.Original);
            }
        }
コード例 #4
0
        public void DnsRecordFactory_PTRRecord()
        {
            var name = DnsString.Parse("result.example.com");

            using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize))
            {
                var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer));
                writer.WriteHostName(name.Value);
                var factory = GetFactory(
                    writer.Data.ToArray(),
                    out var reader);
                var info = new ResourceRecord(
                    DnsString.Parse("query.example.com"),
                    PtrRecord.ResourceRecordType,
                    QueryClass.In,
                    0,
                    writer.Data.Count);

                var result = factory.GetRecord(
                    info,
                    reader) as PtrRecord ?? throw new Exception();

                Assert.Equal(
                    result.PtrDomainName,
                    name);
            }
        }
コード例 #5
0
        public void DnsRecordFactory_SRVRecord()
        {
            var name = DnsString.Parse("result.example.com");

            using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize))
            {
                var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer));
                writer.WriteBytes(
                    new byte[] { 0, 1, 1, 0, 2, 3 },
                    6);
                writer.WriteHostName(name.Value);
                var factory = GetFactory(
                    writer.Data.ToArray(),
                    out var reader);

                var info = new ResourceRecord(
                    DnsString.Parse("query.example.com"),
                    SrvRecord.ResourceRecordType,
                    QueryClass.In,
                    0,
                    writer.Data.Count);

                var result = factory.GetRecord(
                    info,
                    reader) as SrvRecord ?? throw new Exception();

                Assert.Equal(
                    result.Target,
                    name);
                Assert.True(result.Priority == 1);
                Assert.True(result.Weight == 256);
                Assert.True(result.Port == 515);
            }
        }
コード例 #6
0
 public ArraySegment <byte> Pooled()
 {
     using (var writer = new DnsDatagramWriter())
     {
         writer.WriteHostName(domainName);
         return(writer.Data);
     }
 }
コード例 #7
0
 public ArraySegment <byte> Allocate()
 {
     // allocate array manually (not pooled)
     using (var writer = new DnsDatagramWriter(new ArraySegment <byte>(new byte[DnsDatagramWriter.BufferSize])))
     {
         writer.WriteHostName(domainName);
         return(writer.Data);
     }
 }
コード例 #8
0
        public void DnsRecordFactory_PTRRecord()
        {
            var name   = DnsString.Parse("result.example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteHostName(name.Value);
            var factory = GetFactory(writer.Data.ToArray());
            var info    = new ResourceRecordInfo("query.example.com", ResourceRecordType.PTR, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as PtrRecord;

            Assert.Equal(result.PtrDomainName, name);
        }
コード例 #9
0
            public ArraySegment <byte> BufferedWrite()
            {
                using (var bytes = new PooledBytes(ShortTestValueByteLength * Ops + Ops))
                    using (var writer = new DnsDatagramWriter(bytes.BufferSegment))
                    {
                        for (var i = 0; i < Ops; i++)
                        {
                            writer.WriteHostName(ShortTestValue);
                        }

                        return(writer.Data);
                    }
            }
コード例 #10
0
        public void DnsRecordFactory_MBRecord()
        {
            var name   = DnsString.Parse("Müsli.de");
            var writer = new DnsDatagramWriter();

            writer.WriteHostName(name.Value);
            var factory = GetFactory(writer.Data.ToArray());
            var info    = new ResourceRecordInfo("Müsli.de", ResourceRecordType.MB, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as MbRecord;

            Assert.Equal(result.MadName, name);
            Assert.Equal("müsli.de.", result.MadName.Original);
        }
コード例 #11
0
        public void DnsRecordFactory_MXRecord()
        {
            var name   = DnsString.Parse("result.example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteByte(0);
            writer.WriteByte(1);
            writer.WriteHostName(name.Value);

            var factory = GetFactory(writer.Data.ToArray());
            var info    = new ResourceRecordInfo("query.example.com", ResourceRecordType.MX, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as MxRecord;

            Assert.Equal(1, result.Preference);
            Assert.Equal(result.Exchange, name);
        }
コード例 #12
0
        public void DnsRecordFactory_SRVRecord()
        {
            var name   = DnsString.Parse("result.example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteBytes(new byte[] { 0, 1, 1, 0, 2, 3 }, 6);
            writer.WriteHostName(name.Value);
            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo("query.example.com", ResourceRecordType.SRV, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as SrvRecord;

            Assert.Equal(result.Target, name);
            Assert.True(result.Priority == 1);
            Assert.True(result.Weight == 256);
            Assert.True(result.Port == 515);
        }
コード例 #13
0
        public void DnsRecordFactory_RRSIGRecord()
        {
            var   type                = ResourceRecordType.NSEC;
            var   algorithmNumber     = DnsSecurityAlgorithm.ECDSAP256SHA256;
            var   labels              = 5;
            var   originalTtl         = 300;
            var   signatureExpiration = 1589414400;
            var   signatureInception  = 1587600000;
            short keytag              = 3942;
            var   signersName         = DnsString.Parse("result.example.com");
            var   signatureString     = "kfyyKQoPZJFyOFSDqav7wj5XNRPqZssV2K2k8MJun28QSsCMHyWOjw9Hk4KofnEIUWNui3mMgAEFYbwoeRKkMf5uDAh6ryJ4veQNj86mgYJrpJppUplqlqJE8o1bx0I1VfwheL+M23bL5MnqSGiI5igmMDyeVUraVOO4RQyfGN0=";
            var   signature           = Convert.FromBase64String(signatureString);

            var writer = new DnsDatagramWriter();

            writer.WriteInt16NetworkOrder((short)type);
            writer.WriteByte((byte)algorithmNumber);
            writer.WriteByte((byte)labels);
            writer.WriteInt32NetworkOrder(originalTtl);
            writer.WriteInt32NetworkOrder(signatureExpiration);
            writer.WriteInt32NetworkOrder(signatureInception);
            writer.WriteInt16NetworkOrder(keytag);
            writer.WriteHostName(signersName.Value);
            writer.WriteBytes(signature, signature.Length);

            var factory = GetFactory(writer.Data.ToArray());
            var info    = new ResourceRecordInfo("query.example.com", ResourceRecordType.RRSIG, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as RRSigRecord;

            Assert.Equal(type, result.CoveredType);
            Assert.Equal(algorithmNumber, result.Algorithm);
            Assert.Equal(labels, result.Labels);
            Assert.Equal(originalTtl, result.OriginalTtl);
            Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(signatureExpiration), result.SignatureExpiration);
            Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(signatureInception), result.SignatureInception);
            Assert.Equal(signersName.Value, result.SignersName);

            Assert.Equal(signature, result.Signature);
            Assert.Equal(signatureString, result.SignatureAsString);
        }