/// <summary>
        /// Gets an IP address by name, via DNS.
        /// </summary>
        /// <param name="hostname">The hostname.</param>
        /// <returns>The corresponding IP address.</returns>
        /// <remarks>
        /// <para>Returns the first IP v4 address available, if any,
        /// else the first IP v6 address available. Throws if it cannot
        /// get an IP for the hostname via DNS.</para>
        /// </remarks>
        // ReSharper disable once InconsistentNaming
        public static IPAddress GetIPAddressByName(string hostname)
        {
            if (hostname == "0.0.0.0")
            {
                return(IPAddress.Any);
            }

            var addresses = HDns.GetHostAddresses(hostname);

            // prefer an IP v4, if possible
            return(addresses.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) ??
                   addresses.FirstOrDefault());
        }
示例#2
0
        /// <summary>
        /// (internal for tests only)
        /// Gets all scoped IP addresses corresponding to a non-scoped IP v6 local address.
        /// </summary>
        /// <returns>All scoped IP addresses corresponding to the specified address.</returns>
        internal static IEnumerable <IPAddress> GetV6LocalAddresses()
        {
            // if the address is IP v6 local without a scope,
            // resolve -> the local address, with all avail scopes?

            var hostname = HDns.GetHostName();
            var entry    = HDns.GetHostEntry(hostname);

            foreach (var address in entry.AddressList)
            {
                if (address.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    yield return(address);
                }
            }
        }