GetARecords() public method

Gets IPv4 host addess records.
public GetARecords ( ) : LumiSoft.Net.Dns.Client.A_Record[]
return LumiSoft.Net.Dns.Client.A_Record[]
コード例 #1
0
        /// <summary>
        /// Gets specified host IP addresses(A and AAAA).
        /// </summary>
        /// <param name="host">Host name.</param>
        /// <returns>Returns specified host IP addresses.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null reference.</exception>
        public IPAddress[] GetHostAddresses(string host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            List <IPAddress> retVal = new List <IPAddress>();

            // This is probably NetBios name
            if (host.IndexOf(".") == -1)
            {
                return(System.Net.Dns.GetHostEntry(host).AddressList);
            }
            else
            {
                DnsServerResponse response = Query(host, QTYPE.A);
                if (response.ResponseCode != RCODE.NO_ERROR)
                {
                    throw new DNS_ClientException(response.ResponseCode);
                }

                foreach (DNS_rr_A record in response.GetARecords())
                {
                    retVal.Add(record.IP);
                }

                response = Query(host, QTYPE.AAAA);
                if (response.ResponseCode != RCODE.NO_ERROR)
                {
                    throw new DNS_ClientException(response.ResponseCode);
                }

                foreach (DNS_rr_A record in response.GetARecords())
                {
                    retVal.Add(record.IP);
                }
            }

            return(retVal.ToArray());
        }
コード例 #2
0
        /// <summary>
        /// Resolves host name to IP addresses.
        /// </summary>
        /// <param name="host">Host name or IP address.</param>
        /// <returns>Return specified host IP addresses.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception>
        public static IPAddress[] Resolve(string host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            // If hostName_IP is IP
            try{
                return(new IPAddress[] { IPAddress.Parse(host) });
            }
            catch {
            }

            // This is probably NetBios name
            if (host.IndexOf(".") == -1)
            {
                return(System.Net.Dns.GetHostEntry(host).AddressList);
            }
            else
            {
                // hostName_IP must be host name, try to resolve it's IP
                Dns_Client        dns  = new Dns_Client();
                DnsServerResponse resp = dns.Query(host, QTYPE.A);
                if (resp.ResponseCode == RCODE.NO_ERROR)
                {
                    DNS_rr_A[]  records = resp.GetARecords();
                    IPAddress[] retVal  = new IPAddress[records.Length];
                    for (int i = 0; i < records.Length; i++)
                    {
                        retVal[i] = records[i].IP;
                    }

                    return(retVal);
                }
                else
                {
                    throw new Exception(resp.ResponseCode.ToString());
                }
            }
        }