Ejemplo n.º 1
0
        public void IncludeZone_NoResources()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(""));

            ExceptionAssert.Throws <InvalidDataException>(() => catalog.IncludeZone(reader));
        }
Ejemplo n.º 2
0
        public void Include()
        {
            var dig     = @"
; <<>> DiG 9.9.5-3ubuntu0.15-Ubuntu <<>> +dnssec +split=8 +all com DNSKEY
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33952
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags: do; udp: 512
;; QUESTION SECTION:
;com.				IN	DNSKEY

;; ANSWER SECTION:
com.			54254	IN	DNSKEY	256 3 8 AQPeabgR 6Fgrk5FS LilDYUed wsHA0HH2 2e8+Zp/u vp4aj1dV DAy5C9bk RA+xot3s G1KaT5hv goE7eNV9 3F7pBW9r vVE3A/BN vJbLXxKh kAJV5KMF C10NRcdb +xF+sM4X TMPESPrY wTLUEpSF ntMIVLAt UzLaBo6Y pTVR20os gGgc3Q==  ; ZSK; alg = RSASHA256; key id = 46475
com.			54254	IN	DNSKEY	257 3 8 AQPDzldN mMvZFX4N cNJ0uEnK Dg7tmv/F 3MyQR0lp BmVcNcsI szxNFxsB fKNW9JYC Yqpik836 6LE7VbIc NRzfp2h9 OO8HRl+H +E08zauK 8k7evWEm u/6od+2b oggPoiEf GNyvNPaS I7FOIroD snw/tagg zHRX1Z7S OiOiPWPN IwSUyWOZ 79VmcQ1G LkC6NlYv G3HwYmyn Qv6oFwGv /KELSw7Z SdrbTQ0H XvZbqMUI 7BaMskmv gm1G7oKZ 1YiF7O9i oVNc0+7A SbqmZN7Z 98EGU/Qh 2K/BgUe8 Hs0XVcdP KrtyYnoQ Hd2ynKPc MMlTEih2 /2HDHjRP J2aywIpK Nnv4oPo/  ; KSK; alg = RSASHA256; key id = 30909
com.			54254	IN	RRSIG	DNSKEY 8 1 86400 20180909182533 20180825182033 30909 com. k2uOB+mv 1Fu2Uy+g Y/vF4xQB oyUo8dgg 4d491Z6C Pi551Lh2 /oTWxVQX fWzE8rUk VBCNPkSH sC0BtInK 8iUOqqoE TDDt8wp3 u6zqzc3t 8nXBPIBD G9RIbciY HwxIQWWJ x55J5fcT Hv51nKqK jHCU0Tfr N95Lqg79 qwesU0E3 HP7Lvq9K UppCgnDx nYDxQqz4 Unq3F4Ts 1T+/lwNF xkUs2jY6 EGwIgNjW 0gpAJJJd bsd0pbsi Sn21ydMz pL+gpPru 0zQKHn67 r5kDUMxQ FnRd691C ZLUAWLBK 0NxSfXYS Xj+VjKAa 7WHgvNoU YPDfK216 4XKibHsb +BOAkj2X Aa04XA==

;; Query time: 1 msec
;; SERVER: 172.17.0.1#53(172.17.0.1)
;; WHEN: Mon Sep 03 01:31:04 UTC 2018
;; MSG SIZE  rcvd: 743
";
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(dig));

            catalog.Include(reader);
            Assert.IsTrue(catalog.ContainsKey("com"));
            var node = catalog["COM"];

            Assert.AreEqual(3, node.Resources.Count);
        }
Ejemplo n.º 3
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Priority = reader.ReadUInt16();
     Weight   = reader.ReadUInt16();
     Port     = reader.ReadUInt16();
     Target   = reader.ReadDomainName();
 }
Ejemplo n.º 4
0
        public void IncludeZone_MissingSOA()
        {
            var text    = "foo.org A 127.0.0.1";
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(text));

            ExceptionAssert.Throws <InvalidDataException>(() => catalog.IncludeZone(reader));
        }
Ejemplo n.º 5
0
        public void IncludeZone_InvalidName()
        {
            // Missing a new line
            var text    = exampleDotOrgZoneText + " not.in.zone. A 127.0.0.1 ; bad";
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(text));

            ExceptionAssert.Throws <InvalidDataException>(() => catalog.IncludeZone(reader));
        }
Ejemplo n.º 6
0
        public void IncludeZone_AlreadyExists()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(exampleDotComZoneText));
            var zone    = catalog.IncludeZone(reader);

            Assert.AreEqual("example.com", zone.Name);

            reader = new PresentationReader(new StringReader(exampleDotComZoneText));
            ExceptionAssert.Throws <InvalidDataException>(() => catalog.IncludeZone(reader));
        }
Ejemplo n.º 7
0
 /// <summary>
 ///   Include the resource records.
 /// </summary>
 /// <param name="reader">
 ///   The source of the resource records.
 /// </param>
 /// <param name="authoritative">
 ///   Indicates if a <see cref="ResourceRecord"/> is authoritative or cached.
 ///   Only used when a <see cref="Node"/> is created.
 /// </param>
 public void Include(PresentationReader reader, bool authoritative = false)
 {
     while (true)
     {
         var r = reader.ReadResourceRecord();
         if (r == null)
         {
             break;
         }
         Add(r, authoritative);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        ///   Include the zone information.
        /// </summary>
        /// <param name="reader">
        ///   The source of the zone information.
        /// </param>
        /// <returns>
        ///   The <see cref="Node"/> that represents the zone.
        /// </returns>
        /// <remarks>
        ///   All included nodes are marked as <see cref="Node.Authoritative"/>.
        /// </remarks>
        public Node IncludeZone(PresentationReader reader)
        {
            // Read the resources.
            var resources = new List <ResourceRecord>();

            while (true)
            {
                var r = reader.ReadResourceRecord();
                if (r == null)
                {
                    break;
                }
                resources.Add(r);
            }

            // Validation
            if (resources.Count == 0)
            {
                throw new InvalidDataException("No resources.");
            }
            if (resources[0].Type != DnsType.SOA)
            {
                throw new InvalidDataException("First resource record must be a SOA.");
            }
            var soa = (SOARecord)resources[0];

            if (resources.Any(r => !r.Name.BelongsTo(soa.Name)))
            {
                throw new InvalidDataException("All resource records must belong to the zone.");
            }

            // Insert the nodes of the zone.
            var nodes = resources.GroupBy(
                r => r.Name,
                (key, results) => new Node
            {
                Name          = key,
                Authoritative = true,
                Resources     = new ConcurrentSet <ResourceRecord>(results)
            }
                );

            foreach (var node in nodes)
            {
                if (!TryAdd(node.Name, node))
                {
                    throw new InvalidDataException($"'{node.Name}' already exists.");
                }
            }

            return(this[soa.Name]);
        }
Ejemplo n.º 9
0
        public void MultipleZones()
        {
            var catalog = new Catalog();

            var reader = new PresentationReader(new StringReader(exampleDotComZoneText));
            var zone   = catalog.IncludeZone(reader);

            Assert.AreEqual("example.com", zone.Name);

            reader = new PresentationReader(new StringReader(exampleDotOrgZoneText));
            zone   = catalog.IncludeZone(reader);
            Assert.AreEqual("example.org", zone.Name);
        }
Ejemplo n.º 10
0
        public void IncludeZone()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(examplZoneText));
            var zone    = catalog.IncludeZone(reader);

            Assert.AreEqual("example", zone.Name);
            Assert.IsTrue(zone.Authoritative);

            Assert.IsTrue(catalog.ContainsKey("example"));
            Assert.IsTrue(catalog.ContainsKey("ns1.example"));
            Assert.IsTrue(catalog.ContainsKey("xx.example"));
        }
Ejemplo n.º 11
0
        public void IncludeReverseLookupRecords()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(exampleDotComZoneText));
            var zone    = catalog.IncludeZone(reader);

            catalog.IncludeReverseLookupRecords();

            Assert.IsTrue(catalog.ContainsKey("1.2.0.192.in-addr.arpa"));
            Assert.IsTrue(catalog.ContainsKey("2.2.0.192.in-addr.arpa"));
            Assert.IsTrue(catalog.ContainsKey("3.2.0.192.in-addr.arpa"));

            Assert.IsTrue(catalog["1.2.0.192.in-addr.arpa"].Authoritative);
        }
Ejemplo n.º 12
0
        public void NamesAreCaseInsenstive()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(exampleDotComZoneText));

            catalog.IncludeZone(reader);

            Assert.IsTrue(catalog.ContainsKey("EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("NS.EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("WWW.EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("WWWTEST.EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("MAIL.EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("MAIL2.EXAMPLE.COM"));
            Assert.IsTrue(catalog.ContainsKey("MAIL3.EXAMPLE.COM"));
        }
Ejemplo n.º 13
0
        /// <summary>
        ///   Include the root name servers.
        /// </summary>
        /// <returns>
        ///   The <see cref="Node"/> that represents the "root".
        /// </returns>
        /// <remarks>
        ///   A DNS recursive resolver typically needs a "root hints file". This file
        ///   contains the names and IP addresses of the authoritative name servers for the root zone,
        ///   so the software can bootstrap the DNS resolution process.
        /// </remarks>
        public Node IncludeRootHints()
        {
            var assembly = typeof(Catalog).GetTypeInfo().Assembly;

            using (var hints = assembly.GetManifestResourceStream("Makaretu.Dns.Resolving.RootHints"))
            {
                var            reader = new PresentationReader(new StreamReader(hints));
                ResourceRecord r;
                while (null != (r = reader.ReadResourceRecord()))
                {
                    Add(r);
                }
            }

            var root = this[new DomainName("")];

            root.Authoritative = true;
            return(root);
        }
Ejemplo n.º 14
0
        public void IncludeZone()
        {
            var catalog = new Catalog();
            var reader  = new PresentationReader(new StringReader(exampleDotComZoneText));
            var zone    = catalog.IncludeZone(reader);

            Assert.AreEqual("example.com", zone.Name);
            Assert.IsTrue(zone.Authoritative);

            Assert.IsTrue(catalog.ContainsKey("example.com"));
            Assert.IsTrue(catalog.ContainsKey("ns.example.com"));
            Assert.IsTrue(catalog.ContainsKey("www.example.com"));
            Assert.IsTrue(catalog.ContainsKey("wwwtest.example.com"));
            Assert.IsTrue(catalog.ContainsKey("mail.example.com"));
            Assert.IsTrue(catalog.ContainsKey("mail2.example.com"));
            Assert.IsTrue(catalog.ContainsKey("mail3.example.com"));

            Assert.IsTrue(catalog["mail.example.com"].Authoritative);
        }
Ejemplo n.º 15
0
        public void Deserialize()
        {
            if (File.Exists("settings.txt"))
            {
                var stream = new FileStream("settings.txt", FileMode.Open);
                var sr     = new StreamReader(stream);
                var reader = new PresentationReader(sr);
                var res    = new ResourceRecord();
                while (res != null)
                {
                    res = reader.ReadResourceRecord();
                    if (res != null)
                    {
                        AddRecord(res);
                    }
                }

                sr.Close();
            }
        }
Ejemplo n.º 16
0
        public void RemoveZone()
        {
            var catalog = new Catalog();

            var reader = new PresentationReader(new StringReader(exampleDotComZoneText));
            var zone   = catalog.IncludeZone(reader);

            Assert.AreEqual("example.com", zone.Name);
            Assert.AreEqual(7, catalog.Count);

            reader = new PresentationReader(new StringReader(exampleDotOrgZoneText));
            zone   = catalog.IncludeZone(reader);
            Assert.AreEqual("example.org", zone.Name);
            Assert.AreEqual(7 + 7, catalog.Count);

            catalog.RemoveZone("example.org");
            Assert.AreEqual(7, catalog.Count);

            catalog.RemoveZone("example.com");
            Assert.AreEqual(0, catalog.Count);
        }
Ejemplo n.º 17
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Subtype = reader.ReadUInt16();
     Target  = reader.ReadDomainName();
 }
Ejemplo n.º 18
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Data = reader.ReadResourceData();
 }
Ejemplo n.º 19
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Mailbox  = reader.ReadDomainName();
     TextName = reader.ReadDomainName();
 }
Ejemplo n.º 20
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Cpu = reader.ReadString();
     OS  = reader.ReadString();
 }
Ejemplo n.º 21
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Authority = reader.ReadDomainName();
 }
Ejemplo n.º 22
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Target = reader.ReadDomainName();
 }
Ejemplo n.º 23
0
 /// <inheritdoc />
 public override void ReadData(PresentationReader reader)
 {
     Preference = reader.ReadUInt16();
     Exchange   = reader.ReadDomainName();
 }