/// <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 using (Dns_Client dns = new Dns_Client()) { DnsServerResponse resp = dns.Query(host, DNS_QType.A); if (resp.ResponseCode == DNS_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()); } } } }
/// <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, DNS_QType.A); if (response.ResponseCode != DNS_RCode.NO_ERROR) { throw new DNS_ClientException(response.ResponseCode); } foreach (DNS_rr_A record in response.GetARecords()) { retVal.Add(record.IP); } response = Query(host, DNS_QType.AAAA); if (response.ResponseCode != DNS_RCode.NO_ERROR) { throw new DNS_ClientException(response.ResponseCode); } foreach (DNS_rr_AAAA record in response.GetAAAARecords()) { retVal.Add(record.IP); } } return(retVal.ToArray()); }