예제 #1
0
        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());
                    }
                }
            }
        }
예제 #2
0
        public static string[] GetDomainHosts(string domain)
        {
            if(domain == null){
                throw new ArgumentNullException("domain");
            }
            if(string.IsNullOrEmpty(domain)){
                throw new ArgumentException("Invalid argument 'domain' value, you need to specify domain value.");
            }

            // We have email address, parse domain.
            if(domain.IndexOf("@") > -1){
                domain = domain.Substring(domain.IndexOf('@') + 1);
            }

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

            // Get MX records.
            using(Dns_Client dns = new Dns_Client()){
                DnsServerResponse response = dns.Query(domain,DNS_QType.MX);
                if(response.ResponseCode == DNS_RCode.NO_ERROR){
                    foreach(DNS_rr_MX mx in response.GetMXRecords()){
                        // Block invalid MX records.
                        if(!string.IsNullOrEmpty(mx.Host)){
                            retVal.Add(mx.Host);
                        }
                    }
                }
                else{
                    throw new DNS_ClientException(response.ResponseCode);
                }
            }

            /* RFC 2821 5.
                If no MX records are found, but an A RR is found, the A RR is treated as if it
                was associated with an implicit MX RR, with a preference of 0, pointing to that host.
            */
            if(retVal.Count == 0){
                retVal.Add(domain);
            }

            return retVal.ToArray();
        }
예제 #3
0
파일: Net_Core.cs 프로젝트: nbhopson/QMail
        /// <summary>
        /// Gets host name. If fails returns ip address.
        /// </summary>
        /// <param name="ip">IP address which to reverse lookup.</param>
        /// <returns>Returns host name of specified IP address.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        public static string GetHostName(IPAddress ip)
        {
            if(ip == null){
                throw new ArgumentNullException("ip");
            }

            string retVal = ip.ToString();
            try{
                Dns_Client dns = new Dns_Client();
                DnsServerResponse response = dns.Query(ip.ToString(),DNS_QType.PTR);
                if(response.ResponseCode == DNS_RCode.NO_ERROR){
                    DNS_rr_PTR[] ptrs = response.GetPTRRecords();
                    if(ptrs.Length > 0){
                        retVal = ptrs[0].DomainName;
                    }
                }
            }
            catch{
            }

            return retVal;
        }