Пример #1
0
        /// <summary>
        /// Resolves and returns a <see cref="AK.Net.Dns.DnsName"/> instance for
        /// each of the authoritative name servers for the specified
        /// <paramref name="domain"/>.
        /// </summary>
        /// <param name="domain">The domain.</param>
        /// <returns>
        /// The list of <see cref="AK.Net.Dns.DnsName"/> instances for each of the
        /// authoritative name servers for the specified <paramref name="domain"/>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="domain"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual DnsName[] GetNameServers(DnsName domain)
        {
            DnsName  qname = AppendNameSuffix(domain);
            DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.NS, DnsQueryClass.IN));

            return(reply.Answers.Where(F.IsNS).Select(x => F.ToNS(x).Domain).ToArray());
        }
Пример #2
0
        /// <summary>
        /// Resolves and returns an <see cref="System.Net.IPHostEntry"/> for the
        /// specified <see cref="System.Net.IPAddress"/>.
        /// </summary>
        /// <param name="address">The host's IP address.</param>
        /// <returns>An <see cref="System.Net.IPHostEntry"/> containing information
        /// associated with the host.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="address"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual IPHostEntry GetHostEntry(IPAddress address)
        {
            Guard.NotNull(address, "address");

            DnsName  qname = PtrRecord.MakeName(address);
            DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.Ptr, DnsQueryClass.IN));

            return(new IPHostEntry()
            {
                AddressList = new IPAddress[] { address },
                Aliases = DnsUtility.EMPTY_STRING_ARRAY,
                HostName = (from ptr in reply.Answers
                            where F.IsPtr(ptr) && ptr.Owner.Equals(qname)
                            select F.ToPtr(ptr).Domain).FirstOrDefault() ?? null
            });
        }
Пример #3
0
        /// <summary>
        /// Resolves and returns the <see cref="AK.Net.Dns.MXInfo"/> instance for the
        /// specified <paramref name="domain"/>.
        /// </summary>
        /// <param name="domain">The domain name.</param>
        /// <returns>The mail exchange information associated with the
        /// <paramref name="domain"/>.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="domain"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual MXInfo GetMXInfo(DnsName domain)
        {
            DnsName  qname = AppendNameSuffix(domain);
            DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.MX, DnsQueryClass.IN));

            if (reply.Answers.Count > 0)
            {
                return(new MXInfo(qname,
                                  (from ans in reply.Answers
                                   where F.IsMX(ans)
                                   let mx = F.ToMX(ans)
                                            orderby mx
                                            select mx.Exchange).ToArray()
                                  ));
            }

            return(new MXInfo(domain));
        }
Пример #4
0
        /// <summary>
        /// Resolves and returns an <see cref="System.Net.IPHostEntry"/> for the
        /// specified host name or <see cref="System.Net.IPAddress"/>.
        /// </summary>
        /// <param name="hostOrAddress">A host name or IP address.</param>
        /// <returns>
        /// An <see cref="System.Net.IPHostEntry"/> containing information
        /// associated with the host.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="hostOrAddress"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown when <paramref name="hostOrAddress"/> is not a valid DNS name
        /// or IP address.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual IPHostEntry GetHostEntry(string hostOrAddress)
        {
            Guard.NotEmpty(hostOrAddress, "hostOrAddress");

            DnsName   qname;
            IPAddress address;

            if (IPAddress.TryParse(hostOrAddress, out address))
            {
                return(GetHostEntry(address));
            }
            else if (DnsName.TryParse(hostOrAddress, out qname))
            {
                DnsReply reply;

                qname = AppendNameSuffix(qname);
                reply = Resolve(new DnsQuestion(qname, DnsQueryType.A, DnsQueryClass.IN));
                if (reply.Answers.Count > 0)
                {
                    return(new IPHostEntry()
                    {
                        HostName = (from cn in reply.Answers
                                    where F.IsCName(cn) && cn.Owner.Equals(qname)
                                    select F.ToCName(cn).Canonical).FirstOrDefault() ?? qname,
                        AddressList = (from a in reply.Answers
                                       where F.IsAOrAaaa(a)
                                       select F.ToIP(a)).ToArray(),
                        Aliases = DnsUtility.EMPTY_STRING_ARRAY
                    });
                }
                else
                {
                    return(new IPHostEntry()
                    {
                        HostName = qname,
                        AddressList = DnsUtility.EMPTY_IP_ARRAY,
                        Aliases = DnsUtility.EMPTY_STRING_ARRAY
                    });
                }
            }

            throw Guard.MustBeAnIPAddressOrDnsName("hostOrAddress");
        }