Example #1
0
        } // GetHostName

        /*****************************************************************************
         * Function :    resolve
         *
         * Abstract:     Converts IP/hostnames to IP numerical address using DNS
         *             Additional methods provided for convenience
         *             (These methods will resolve strings and hostnames. In case of
         *             multiple IP addresses, the address returned is chosen arbitrarily.)
         *
         * Input Parameters: host/IP
         *
         * Returns: IPAddress
         ******************************************************************************/

        /// <include file='doc\DNS.uex' path='docs/doc[@for="Dns.Resolve"]/*' />
        /// <devdoc>
        /// <para>Creates an <see cref='System.Net.IPAddress'/>
        /// instance from a DNS hostname.</para>
        /// </devdoc>
        // UEUE
        public static IPHostEntry Resolve(string hostName)
        {
            //
            // demand Unrestricted DnsPermission for this call
            //
            s_DnsPermission.Demand();
            //This is a perf optimization, this method call others that will demand and we already did that.
            s_DnsPermission.Assert();

            IPHostEntry ipHostEntry = null;

            try {
                if (hostName == null)
                {
                    throw new ArgumentNullException("hostName");
                }

                GlobalLog.Print("Dns.Resolve: " + hostName);

                //
                // as a minor perf improvement, we'll look at the first character
                // in the hostName and if that's a digit, call GetHostByAddress() first.
                // note that GetHostByAddress() will succeed ONLY if the first character is a digit.
                // hence if this is not the case, below, we will only call GetHostByName()
                // specifically, on Win2k, only the following are valid:
                // "11.22.33.44" - success
                // "0x0B16212C" - success
                // while these will fail or return bogus data (note the prepended spaces):
                // " 11.22.33.44" - bogus data
                // " 0x0B16212C" - bogus data
                // " 0B16212C" - bogus data
                // "0B16212C" - failure
                //
                // IPv6 support: added ':' as a legal character in IP addresses so that
                //               we can attempt gethostbyaddress first.
                //
                if (hostName.Length > 0 &&
                    ((hostName[0] >= '0' && hostName[0] <= '9') || // Possible IPv4 or IPv6 numeric
                     (hostName.IndexOf(':') >= 0)))                // Possible IPv6 numeric
                {
                    try {
                        ipHostEntry = GetHostByAddress(hostName);
                    }
                    catch {
                        //
                        // this can fail for weird hostnames like 3com.com
                        //
                    }
                }
                if (ipHostEntry == null)
                {
                    ipHostEntry = GetHostByName(hostName);
                }
            }
            finally {
                DnsPermission.RevertAssert();
            }

            return(ipHostEntry);
        }