Beispiel #1
0
            /// <summary>
            /// Create an InetAddress with a fully qualified hostname of the given
            /// hostname.
            /// </summary>
            /// <remarks>
            /// Create an InetAddress with a fully qualified hostname of the given
            /// hostname.  InetAddress does not qualify an incomplete hostname that
            /// is resolved via the domain search list.
            /// <see cref="System.Net.IPAddress.ToString()"/>
            /// will fully qualify the
            /// hostname, but it always return the A record whereas the given hostname
            /// may be a CNAME.
            /// </remarks>
            /// <param name="host">a hostname or ip address</param>
            /// <returns>InetAddress with the fully qualified hostname or ip</returns>
            /// <exception cref="UnknownHostException">if host does not exist</exception>
            public virtual IPAddress GetByName(string host)
            {
                IPAddress addr = null;

                if (IPAddressUtil.IsIPv4LiteralAddress(host))
                {
                    // use ipv4 address as-is
                    byte[] ip = IPAddressUtil.TextToNumericFormatV4(host);
                    addr = IPAddress.GetByAddress(host, ip);
                }
                else
                {
                    if (IPAddressUtil.IsIPv6LiteralAddress(host))
                    {
                        // use ipv6 address as-is
                        byte[] ip = IPAddressUtil.TextToNumericFormatV6(host);
                        addr = IPAddress.GetByAddress(host, ip);
                    }
                    else
                    {
                        if (host.EndsWith("."))
                        {
                            // a rooted host ends with a dot, ex. "host."
                            // rooted hosts never use the search path, so only try an exact lookup
                            addr = GetByExactName(host);
                        }
                        else
                        {
                            if (host.Contains("."))
                            {
                                // the host contains a dot (domain), ex. "host.domain"
                                // try an exact host lookup, then fallback to search list
                                addr = GetByExactName(host);
                                if (addr == null)
                                {
                                    addr = GetByNameWithSearch(host);
                                }
                            }
                            else
                            {
                                // it's a simple host with no dots, ex. "host"
                                // try the search list, then fallback to exact host
                                IPAddress loopback = Extensions.GetAddressByName(null);
                                if (Runtime.EqualsIgnoreCase(host, loopback.GetHostName()))
                                {
                                    addr = IPAddress.GetByAddress(host, loopback.GetAddressBytes());
                                }
                                else
                                {
                                    addr = GetByNameWithSearch(host);
                                    if (addr == null)
                                    {
                                        addr = GetByExactName(host);
                                    }
                                }
                            }
                        }
                    }
                }
                // unresolvable!
                if (addr == null)
                {
                    throw new UnknownHostException(host);
                }
                return(addr);
            }