Exemplo n.º 1
0
        public void BufferOverflow_Bytes()
        {
            var ms     = new MemoryStream(new byte[] { 1, 2 });
            var reader = new WireReader(ms);

            ExceptionAssert.Throws <EndOfStreamException>(() => reader.ReadBytes(3));
        }
Exemplo n.º 2
0
        public void Bitmap()
        {
            // From https://tools.ietf.org/html/rfc3845#section-2.3
            var wire = new byte[]
            {
                0x00, 0x06, 0x40, 0x01, 0x00, 0x00, 0x00, 0x03,
                0x04, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x20
            };
            var ms     = new MemoryStream(wire, false);
            var reader = new WireReader(ms);
            var first  = new ushort[] { 1, 15, 46, 47 };
            var second = new ushort[] { 1234 };

            CollectionAssert.AreEqual(first, reader.ReadBitmap());
            CollectionAssert.AreEqual(second, reader.ReadBitmap());

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

            writer.WriteBitmap(new ushort[] { 1, 15, 46, 47, 1234 });
            CollectionAssert.AreEqual(wire, ms.ToArray());
        }
Exemplo n.º 3
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Priority = reader.ReadUInt16();
     Weight   = reader.ReadUInt16();
     Port     = reader.ReadUInt16();
     Target   = reader.ReadDomainName();
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void BufferOverflow_DomainName()
        {
            var ms     = new MemoryStream(new byte[] { 1, (byte)'a' });
            var reader = new WireReader(ms);

            ExceptionAssert.Throws <EndOfStreamException>(() => reader.ReadDomainName());
        }
Exemplo n.º 6
0
        public void ReadString_NotAscii()
        {
            var ms     = new MemoryStream(new byte[] { 1, 0xFF });
            var reader = new WireReader(ms);

            reader.ReadString();
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public override IWireSerialiser Read(WireReader reader)
        {
            Name  = reader.ReadDomainName();
            Type  = (DnsType)reader.ReadUInt16();
            Class = (DnsClass)reader.ReadUInt16();

            return(this);
        }
Exemplo n.º 8
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     Algorithms.Clear();
     for (; length > 0; --length)
     {
         Algorithms.Add((SecurityAlgorithm)reader.ReadByte());
     }
 }
Exemplo n.º 9
0
        public void ReadDateTime48()
        {
            // From https://tools.ietf.org/html/rfc2845 section 3.3
            var expected = new DateTime(1997, 1, 21, 0, 0, 0, DateTimeKind.Utc);
            var ms       = new MemoryStream(new byte[] { 0x00, 0x00, 0x32, 0xe4, 0x07, 0x00 });
            var reader   = new WireReader(ms);

            Assert.AreEqual(expected, reader.ReadDateTime48());
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     while (length > 0)
     {
         var s = reader.ReadString();
         Strings.Add(s);
         length -= Encoding.UTF8.GetByteCount(s) + 1;
     }
 }
Exemplo n.º 14
0
        /// <inheritdoc />
        public override void ReadData(WireReader reader, int length)
        {
            var end = reader.Position + length;

            NextOwnerName = reader.ReadDomainName();
            while (reader.Position < end)
            {
                Types.AddRange(reader.ReadBitmap().Select(t => (DnsType)t));
            }
        }
Exemplo n.º 15
0
 /// <inheritdoc />
 public override void ReadData(WireReader reader, int length)
 {
     PrimaryName  = reader.ReadDomainName();
     Mailbox      = reader.ReadDomainName();
     SerialNumber = reader.ReadUInt32();
     Refresh      = reader.ReadTimeSpan32();
     Retry        = reader.ReadTimeSpan32();
     Expire       = reader.ReadTimeSpan32();
     Minimum      = reader.ReadTimeSpan32();
 }
Exemplo n.º 16
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();
 }
Exemplo n.º 17
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();
 }
Exemplo n.º 18
0
        public void NullDomainName_Class()
        {
            var ms     = new MemoryStream();
            var writer = new WireWriter(ms);

            writer.WriteDomainName((DomainName)null);
            writer.WriteString("abc");

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

            Assert.AreEqual("", reader.ReadDomainName());
            Assert.AreEqual("abc", reader.ReadString());
        }
Exemplo n.º 19
0
        public void Read_EscapedDotDomainName()
        {
            var domainName = @"a\.b";
            var ms         = new MemoryStream();
            var writer     = new WireWriter(ms);

            writer.WriteDomainName(domainName);

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

            Assert.AreEqual(domainName, name);
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
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));
            }
        }
Exemplo n.º 22
0
        public void Reading()
        {
            var bytes = new byte[]
            {
                0x01, (byte)'a', 0,
                0x01, (byte)'b', 0,
                0XC0, 3
            };
            var ms     = new MemoryStream(bytes);
            var reader = new WireReader(ms);

            Assert.AreEqual("a", reader.ReadDomainName());
            Assert.AreEqual("b", reader.ReadDomainName());
            Assert.AreEqual("b", reader.ReadDomainName());
        }
Exemplo n.º 23
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}'.");
            }
        }
Exemplo n.º 24
0
        public void Writing_Past_MaxPointer()
        {
            var ms     = new MemoryStream();
            var writer = new WireWriter(ms);

            writer.WriteBytes(new byte[0x4000]);
            writer.WriteDomainName("a");
            writer.WriteDomainName("b");
            writer.WriteDomainName("b");

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

            reader.ReadBytes(0x4000);
            Assert.AreEqual("a", reader.ReadDomainName());
            Assert.AreEqual("b", reader.ReadDomainName());
            Assert.AreEqual("b", reader.ReadDomainName());
        }
Exemplo n.º 25
0
        public void CanonicalDomainName()
        {
            var ms     = new MemoryStream();
            var writer = new WireWriter(ms)
            {
                CanonicalForm = true
            };

            writer.WriteDomainName("FOO");
            writer.WriteDomainName("FOO");
            Assert.AreEqual(5 * 2, writer.Position);

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

            Assert.AreEqual("foo", reader.ReadDomainName());
            Assert.AreEqual("foo", reader.ReadDomainName());
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
0
        public void Reading_Labels()
        {
            var bytes = new byte[]
            {
                0x01, (byte)'a', 0x01, (byte)'b', 0x01, (byte)'c', 00,
                0xC0, 0x00,
                0xC0, 0x02,
                0xC0, 0x04,
                0x01, (byte)'x', 0xC0, 0x02,
            };
            var ms     = new MemoryStream(bytes);
            var reader = new WireReader(ms);

            Assert.AreEqual("a.b.c", reader.ReadDomainName());
            Assert.AreEqual("a.b.c", reader.ReadDomainName());
            Assert.AreEqual("b.c", reader.ReadDomainName());
            Assert.AreEqual("c", reader.ReadDomainName());
            Assert.AreEqual("x.b.c", reader.ReadDomainName());
        }
Exemplo n.º 28
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());
        }
Exemplo n.º 29
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());
        }
Exemplo n.º 30
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);
        }