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);
        }
        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);
            }
        }
        public void DnsRecordFactory_DsRecord()
        {
            var expectedDigest = "3490A6806D47F17A34C29E2CE80E8A999FFBE4BE";
            var expectedBytes  = Enumerable.Range(0, expectedDigest.Length)
                                 .Where(x => x % 2 == 0)
                                 .Select(x => Convert.ToByte(expectedDigest.Substring(x, 2), 16))
                                 .ToArray();

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

            writer.WriteInt16NetworkOrder(31589);
            writer.WriteByte(8); // algorithm
            writer.WriteByte(1); // type
            writer.WriteBytes(expectedBytes, expectedBytes.Length);

            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo(name, ResourceRecordType.DS, QueryClass.IN, 0, writer.Data.Count);

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

            Assert.Equal(expectedBytes, result.Digest);
            Assert.Equal(expectedDigest, result.DigestAsString);
            Assert.Equal(31589, result.KeyTag);
            Assert.Equal(8, (int)result.Algorithm);
            Assert.Equal(1, result.DigestType);
        }
Exemplo n.º 4
0
        public DnsResponseMessage Resolve(IPEndPoint server, DnsRequestMessage dnsRequest)
        {
            using (communicator)
            {
                IUdpAdapter clientSocket    = communicator.Client;
                int         timeoutInMillis = 5000;
                clientSocket.ReceiveTimeout = timeoutInMillis;
                clientSocket.SendTimeout    = timeoutInMillis;

                //
                // write the dns query request data on in bytes to send to the DNS server.
                //
                using (IDnsDatagramWriter writer = new DnsDatagramWriter())
                {
                    ConstructRequestData(dnsRequest, writer);
                    clientSocket.SendTo(writer.Data.Array, 0, writer.Data.Count, SocketFlags.None, server);
                }

                //
                // Read the dns query response from the DNS server.
                //
                using (PooledBytes memory = new PooledBytes(ReadSize))
                {
                    int received = clientSocket.Receive(memory.Buffer, 0, ReadSize, SocketFlags.None);
                    DnsResponseMessage response = messageProcessor.ProcessResponse(memory.BufferSegment);
                    if (dnsRequest.Header.Identifier != response.Header.Identifier)
                    {
                        throw new DnsResponseException("Header id mismatch.");
                    }

                    return(response);
                }
            }
        }
Exemplo n.º 5
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());
            }
        }
        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);
            }
        }
        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);
            }
        }
        public override DnsResponseMessage Query(
            IPEndPoint server,
            DnsRequestMessage request,
            TimeSpan timeout)
        {
            using (var writer = new DnsDatagramWriter())
            {
                GetRequestData(request, writer);
                if (writer.Data.Count != _questionRaw.Length)
                {
                    throw new Exception();
                }
            }

            // Allocation test will probably include this copy of the array, but the data has to be copied, otherwise the ID change would be shared...
            using (var writer = new DnsDatagramWriter(new ArraySegment <byte>(_answer.ToArray())))
            {
                writer.Index = 0;
                writer.WriteInt16NetworkOrder((short)request.Header.Id);
                writer.Index = _answer.Length;

                var response = GetResponseMessage(writer.Data);

                if (response.Header.Id != request.Header.Id)
                {
                    throw new Exception();
                }

                return(response);
            }
        }
 public ArraySegment <byte> Pooled()
 {
     using (var writer = new DnsDatagramWriter())
     {
         writer.WriteHostName(domainName);
         return(writer.Data);
     }
 }
 public ArraySegment <byte> BlockCopyLarge()
 {
     using (var writer = new DnsDatagramWriter())
     {
         writer.WriteBytes(sourceBig, sourceBig.Length);
         return(writer.Data);
     }
 }
 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);
     }
 }
            public ArraySegment <byte> ForLoopLarge()
            {
                using (var writer = new DnsDatagramWriter())
                {
                    foreach (var b in sourceBig)
                    {
                        writer.WriteByte(b);
                    }

                    return(writer.Data);
                }
            }
            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);
                    }
            }
Exemplo n.º 14
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);
        }
            public ArraySegment <byte> BufferedWriteInt()
            {
                using (var bytes = new PooledBytes(4 * Ops))
                    using (var writer = new DnsDatagramWriter(bytes.BufferSegment))
                    {
                        for (var i = 0; i < 1000; i++)
                        {
                            writer.WriteInt32NetworkOrder(i);
                        }

                        return(writer.Data);
                    }
            }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
            public override DnsResponseMessage Query(
                IPEndPoint server,
                DnsRequestMessage request,
                TimeSpan timeout)
            {
                var writer = new DnsDatagramWriter(new ArraySegment <byte>(ZoneData.ToArray()));

                writer.Index = 0;
                writer.WriteInt16NetworkOrder((short)request.Header.Id);
                writer.Index = ZoneData.Length;

                var response = GetResponseMessage(writer.Data);

                if (response.Header.Id != request.Header.Id)
                {
                    throw new Exception();
                }

                return(response);
            }
        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);
        }
        public override DnsResponseMessage Query(
            IPEndPoint server,
            DnsRequestMessage request,
            TimeSpan timeout)
        {
            if (_request)
            {
                using (var writer = new DnsDatagramWriter())
                {
                    GetRequestData(request, writer);
                }
            }

            if (_response)
            {
                var response = GetResponseMessage(new ArraySegment <byte>(_answer, 0, _answer.Length));

                return(response);
            }

            return(new DnsResponseMessage(new DnsResponseHeader(0, 0, 0, 0, 0, 0), 0));
        }
        public void DnsRecordFactory_DnsKeyRecord()
        {
            var expectedPublicKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var expectedBytes     = Encoding.UTF8.GetBytes(expectedPublicKey);
            var name   = DnsString.Parse("example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteInt16NetworkOrder(256);
            writer.WriteByte(3);
            writer.WriteByte((byte)DnsSecurityAlgorithm.RSASHA256);
            writer.WriteBytes(expectedBytes, expectedBytes.Length);

            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo(name, ResourceRecordType.DNSKEY, QueryClass.IN, 0, writer.Data.Count);

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

            Assert.Equal(expectedBytes, result.PublicKey);
            Assert.Equal(256, result.Flags);
            Assert.Equal(3, result.Protocol);
            Assert.Equal(DnsSecurityAlgorithm.RSASHA256, result.Algorithm);
        }