Exemple #1
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);
     }
 }
Exemple #2
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]);
        }
Exemple #3
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);
        }
Exemple #4
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();
            }
        }