示例#1
0
        /// <inheritdoc />
        public override IWireSerialiser Read(WireReader reader)
        {
            // Read standard properties of a resource record.
            Name  = reader.ReadDomainName();
            Type  = (DnsType)reader.ReadUInt16();
            Class = (DnsClass)reader.ReadUInt16();
            TTL   = reader.ReadTimeSpan32();
            int length = reader.ReadUInt16();

            // Find a specific class for the TYPE or default
            // to UnknownRecord.
            var specific = ResourceRegistry.Create(Type);

            specific.Name  = Name;
            specific.Type  = Type;
            specific.Class = Class;
            specific.TTL   = TTL;

            // Read the specific properties of the resource record.
            var end = reader.Position + length;

            specific.ReadData(reader, length);
            if (reader.Position != end)
            {
                throw new InvalidDataException("Found extra data while decoding RDATA.");
            }

            return(specific);
        }
示例#2
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Priority = reader.ReadUInt16();
     Weight   = reader.ReadUInt16();
     Port     = reader.ReadUInt16();
     Target   = reader.ReadDomainName();
 }
示例#3
0
        /// <inheritdoc />
        public override IWireSerialiser Read(WireReader reader)
        {
            Name  = reader.ReadDomainName();
            Type  = (DnsType)reader.ReadUInt16();
            Class = (DnsClass)reader.ReadUInt16();

            return(this);
        }
示例#4
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Algorithm         = reader.ReadDomainName();
     TimeSigned        = reader.ReadDateTime48();
     Fudge             = reader.ReadTimeSpan16();
     MAC               = reader.ReadUInt16LengthPrefixedBytes();
     OriginalMessageId = reader.ReadUInt16();
     Error             = (MessageStatus)reader.ReadUInt16();
     OtherData         = reader.ReadUInt16LengthPrefixedBytes();
 }
示例#5
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Algorithm  = reader.ReadDomainName();
     Inception  = reader.ReadDateTime32();
     Expiration = reader.ReadDateTime32();
     Mode       = (KeyExchangeMode)reader.ReadUInt16();
     Error      = (MessageStatus)reader.ReadUInt16();
     Key        = reader.ReadUInt16LengthPrefixedBytes();
     OtherData  = reader.ReadUInt16LengthPrefixedBytes();
 }
示例#6
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            TypeCovered         = (DnsType)reader.ReadUInt16();
            Algorithm           = (SecurityAlgorithm)reader.ReadByte();
            Labels              = reader.ReadByte();
            OriginalTTL         = reader.ReadTimeSpan32();
            SignatureExpiration = reader.ReadDateTime32();
            SignatureInception  = reader.ReadDateTime32();
            KeyTag              = reader.ReadUInt16();
            SignerName          = reader.ReadDomainName();
            Signature           = reader.ReadBytes(end - reader.Position);
        }
示例#7
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            HashAlgorithm = (DigestType)reader.ReadByte();
            Flags         = reader.ReadByte();
            Iterations    = reader.ReadUInt16();
            Salt          = reader.ReadByteLengthPrefixedBytes();
        }
示例#8
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            KeyTag        = reader.ReadUInt16();
            Algorithm     = (SecurityAlgorithm)reader.ReadByte();
            HashAlgorithm = (DigestType)reader.ReadByte();
            Digest        = reader.ReadBytes(end - reader.Position);
        }
示例#9
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            Flags     = (DNSKEYFlags)reader.ReadUInt16();
            Protocol  = reader.ReadByte();
            Algorithm = (SecurityAlgorithm)reader.ReadByte();
            PublicKey = reader.ReadBytes(end - reader.Position);
        }
示例#10
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            HashAlgorithm       = (DigestType)reader.ReadByte();
            Flags               = (NSEC3Flags)reader.ReadByte();
            Iterations          = reader.ReadUInt16();
            Salt                = reader.ReadByteLengthPrefixedBytes();
            NextHashedOwnerName = reader.ReadByteLengthPrefixedBytes();

            while (reader.Position < end)
            {
                Types.AddRange(reader.ReadBitmap().Select(t => (DnsType)t));
            }
        }
示例#11
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            while (reader.Position < end)
            {
                var type    = (EdnsOptionType)reader.ReadUInt16();
                int olength = reader.ReadUInt16();

                EdnsOption option;
                if (EdnsOptionRegistry.Options.TryGetValue(type, out Func <EdnsOption> maker))
                {
                    option = maker();
                }
                else
                {
                    option = new UnknownEdnsOption {
                        Type = type
                    };
                }
                Options.Add(option);
                option.ReadData(reader, olength);
            }
        }
示例#12
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            switch (length)
            {
            case 0:
                Timeout = null;
                break;

            case 2:
                Timeout = TimeSpan.FromMilliseconds((int)reader.ReadUInt16() * 100);
                break;

            default:
                throw new InvalidDataException($"Invalid EdnsKeepAlive length of '{length}'.");
            }
        }
示例#13
0
        public void LengthPrefixedScope()
        {
            var ms     = new MemoryStream();
            var writer = new WireWriter(ms);

            writer.WriteString("abc");
            writer.PushLengthPrefixedScope();
            writer.WriteDomainName("a");
            writer.WriteDomainName("a");
            writer.PopLengthPrefixedScope();

            ms.Position = 0;
            var reader = new WireReader(ms);

            Assert.AreEqual("abc", reader.ReadString());
            Assert.AreEqual(5, reader.ReadUInt16());
            Assert.AreEqual("a", reader.ReadDomainName());
            Assert.AreEqual("a", reader.ReadDomainName());
        }
示例#14
0
        public void Roundtrip()
        {
            var someBytes = new byte[] { 1, 2, 3 };
            var someDate  = new DateTime(1997, 1, 21, 3, 4, 5, DateTimeKind.Utc);

            var ms     = new MemoryStream();
            var writer = new WireWriter(ms);

            writer.WriteDomainName("emanon.org");
            writer.WriteString("alpha");
            writer.WriteTimeSpan32(TimeSpan.FromHours(3));
            writer.WriteUInt16(ushort.MaxValue);
            writer.WriteUInt32(uint.MaxValue);
            writer.WriteUInt48(0XFFFFFFFFFFFFul);
            writer.WriteBytes(someBytes);
            writer.WriteByteLengthPrefixedBytes(someBytes);
            writer.WriteByteLengthPrefixedBytes(null);
            writer.WriteIPAddress(IPAddress.Parse("127.0.0.1"));
            writer.WriteIPAddress(IPAddress.Parse("2406:e001:13c7:1:7173:ef8:852f:25cb"));
            writer.WriteDateTime32(someDate);
            writer.WriteDateTime48(someDate);

            ms.Position = 0;
            var reader = new WireReader(ms);

            Assert.AreEqual("emanon.org", reader.ReadDomainName());
            Assert.AreEqual("alpha", reader.ReadString());
            Assert.AreEqual(TimeSpan.FromHours(3), reader.ReadTimeSpan32());
            Assert.AreEqual(ushort.MaxValue, reader.ReadUInt16());
            Assert.AreEqual(uint.MaxValue, reader.ReadUInt32());
            Assert.AreEqual(0XFFFFFFFFFFFFul, reader.ReadUInt48());
            CollectionAssert.AreEqual(someBytes, reader.ReadBytes(3));
            CollectionAssert.AreEqual(someBytes, reader.ReadByteLengthPrefixedBytes());
            CollectionAssert.AreEqual(new byte[0], reader.ReadByteLengthPrefixedBytes());
            Assert.AreEqual(IPAddress.Parse("127.0.0.1"), reader.ReadIPAddress());
            Assert.AreEqual(IPAddress.Parse("2406:e001:13c7:1:7173:ef8:852f:25cb"), reader.ReadIPAddress(16));
            Assert.AreEqual(someDate, reader.ReadDateTime32());
            Assert.AreEqual(someDate, reader.ReadDateTime48());
        }
示例#15
0
        /// <inheritdoc />
        public override IWireSerialiser Read(WireReader reader)
        {
            Id = reader.ReadUInt16();
            var flags = reader.ReadUInt16();

            QR      = (flags & 0x8000) == 0x8000;
            AA      = (flags & 0x0400) == 0x0400;
            TC      = (flags & 0x0200) == 0x0200;
            RD      = (flags & 0x0100) == 0x0100;
            RA      = (flags & 0x0080) == 0x0080;
            opcode4 = (byte)((flags & 0x7800) >> 11);
            Z       = (flags & 0x0040) >> 6;
            AD      = (flags & 0x0020) == 0x0020;
            CD      = (flags & 0x0010) == 0x0010;
            Status  = (MessageStatus)(flags & 0x000F);
            var qdcount = reader.ReadUInt16();
            var ancount = reader.ReadUInt16();
            var nscount = reader.ReadUInt16();
            var arcount = reader.ReadUInt16();

            for (var i = 0; i < qdcount; ++i)
            {
                var question = (Question) new Question().Read(reader);
                Questions.Add(question);
            }
            for (var i = 0; i < ancount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                Answers.Add(rr);
            }
            for (var i = 0; i < nscount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                AuthorityRecords.Add(rr);
            }
            for (var i = 0; i < arcount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                AdditionalRecords.Add(rr);
            }

            return(this);
        }
示例#16
0
        /// <inheritdoc />
        public override IWireSerialiser Read(WireReader reader)
        {
            Id = reader.ReadUInt16();
            var flags = reader.ReadUInt16();

            QR     = (flags & 0x8000) == 0x8000;
            Opcode = (MessageOperation)((flags & 0x7800) >> 11);
            Z      = (flags & 0x07F0) >> 4;
            Status = (MessageStatus)(flags & 0x000F);
            var zocount = reader.ReadUInt16();
            var prcount = reader.ReadUInt16();
            var upcount = reader.ReadUInt16();
            var arcount = reader.ReadUInt16();

            for (var i = 0; i < zocount; ++i)
            {
                Zone = (Question) new Question().Read(reader);
            }
            for (var i = 0; i < prcount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                Prerequisites.Add(rr);
            }
            for (var i = 0; i < upcount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                Updates.Add(rr);
            }
            for (var i = 0; i < arcount; ++i)
            {
                var rr = (ResourceRecord) new ResourceRecord().Read(reader);
                AdditionalResources.Add(rr);
            }

            return(this);
        }
示例#17
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Subtype = reader.ReadUInt16();
     Target  = reader.ReadDomainName();
 }
示例#18
0
        void ReadResponses(Stream stream)
        {
            var reader = new WireReader(stream);

            while (stream.CanRead)
            {
                try
                {
                    var length = reader.ReadUInt16();
                    if (length < Message.MinLength)
                    {
                        throw new InvalidDataException("DNS response is too small.");
                    }
                    if (length > Message.MaxLength)
                    {
                        throw new InvalidDataException("DNS response exceeded max length.");
                    }
                    Message response;
                    var     packet = reader.ReadBytes(length);
                    try
                    {
                        // TODO: Should work, but doesn't
                        //var response = (Message)new Message().Read(reader);
                        response = (Message) new Message().Read(packet);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Failed to read response {Convert.ToBase64String(packet)}", e);
                        continue;
                    }

                    // Find matching request.
                    if (log.IsDebugEnabled)
                    {
                        log.Debug($"Got response #{response.Id} {response.Status}");
                    }
                    if (log.IsTraceEnabled)
                    {
                        log.Trace(response);
                    }
                    if (!OutstandingRequests.TryGetValue(response.Id, out var task))
                    {
                        log.Warn("DNS response is missing a matching request ID.");
                        continue;
                    }

                    // Continue the request.
                    task.SetResult(response);
                }
                catch (EndOfStreamException)
                {
                    stream.Dispose();
                }
                catch (Exception e)
                {
#if NETSTANDARD14
                    if (stream.CanRead)
#else
                    if (stream.CanRead && !(e.InnerException is ThreadAbortException))
#endif
                    {
                        log.Error(e);
                    }
                    stream.Dispose();
                }
            }

            // Cancel any outstanding queries.
            foreach (var task in OutstandingRequests.Values)
            {
                task.SetCanceled();
            }
        }
示例#19
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Preference = reader.ReadUInt16();
     Exchange   = reader.ReadDomainName();
 }