151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project
362e0b4793b755083e94aac458111ca347314adddYi Kong * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.net;
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamException;
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamField;
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.IOException;
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectInputStream;
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectInputStream.GetField;
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectOutputStream;
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectOutputStream.PutField;
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.net.util.IPAddressUtil;
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.net.spi.nameservice.*;
38f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebskiimport android.system.GaiException;
39f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebskiimport android.system.StructAddrinfo;
40f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebskiimport libcore.io.Libcore;
41f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebskiimport static android.system.OsConstants.*;
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class represents an Internet Protocol (IP) address.
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> An IP address is either a 32-bit or 128-bit unsigned number
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used by IP, a lower-level protocol on which protocols like UDP and
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TCP are built. The IP address architecture is defined by <a
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC&nbsp;790:
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigned Numbers</i></a>, <a
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC&nbsp;1918:
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Address Allocation for Private Internets</i></a>, <a
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Administratively Scoped IP Multicast</i></a>, and <a
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Version 6 Addressing Architecture</i></a>. An instance of an
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * InetAddress consists of an IP address and possibly its
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponding host name (depending on whether it is constructed
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a host name or whether it has already done reverse host name
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resolution).
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
623a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * <h3> Address types </h3>
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table cellspacing=2 summary="Description of unicast and multicast address types">
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><th valign=top><i>unicast</i></th>
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *       <td>An identifier for a single interface. A packet sent to
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         a unicast address is delivered to the interface identified by
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         that address.
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         <p> The Unspecified Address -- Also called anylocal or wildcard
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         address. It must never be assigned to any node. It indicates the
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         absence of an address. One example of its use is as the target of
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         bind, which allows a server to accept a client connection on any
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         interface, in case the server host has multiple interfaces.
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         <p> The <i>unspecified</i> address must not be used as
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         the destination address of an IP packet.
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         <p> The <i>Loopback</i> Addresses -- This is the address
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         assigned to the loopback interface. Anything sent to this
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         IP address loops around and becomes IP input on the local
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         host. This address is often used when testing a
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         client.</td></tr>
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><th valign=top><i>multicast</i></th>
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *       <td>An identifier for a set of interfaces (typically belonging
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         to different nodes). A packet sent to a multicast address is
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         delivered to all interfaces identified by that address.</td></tr>
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote>
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> IP address scope </h4>
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <i>Link-local</i> addresses are designed to be used for addressing
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * on a single link for purposes such as auto-address configuration,
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * neighbor discovery, or when no routers are present.
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <i>Site-local</i> addresses are designed to be used for addressing
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inside of a site without the need for a global prefix.
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <i>Global</i> addresses are unique across the internet.
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> Textual representation of IP addresses </h4>
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The textual representation of an IP address is address family specific.
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For IPv4 address format, please refer to <A
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * HREF="Inet4Address.html#format">Inet4Address#format</A>; For IPv6
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * address format, please refer to <A
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * HREF="Inet6Address.html#format">Inet6Address#format</A>.
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <P>There is a <a href="doc-files/net-properties.html#Ipv4IPv6">couple of
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System Properties</a> affecting how IPv4 and IPv6 addresses are used.</P>
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> Host Name Resolution </h4>
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Host name-to-IP address <i>resolution</i> is accomplished through
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the use of a combination of local machine configuration information
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and network naming services such as the Domain Name System (DNS)
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and Network Information Service(NIS). The particular naming
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * services(s) being used is by default the local machine configured
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one. For any host name, its corresponding IP address is returned.
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <i>Reverse name resolution</i> means that for any IP address,
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the host associated with the IP address is returned.
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The InetAddress class provides methods to resolve host names to
12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * their IP addresses and vice versa.
12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> InetAddress Caching </h4>
13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The InetAddress class has a cache to store successful as well as
13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unsuccessful host name resolutions.
13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> By default, when a security manager is installed, in order to
13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * protect against DNS spoofing attacks,
13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the result of positive host name resolutions are
13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cached forever. When a security manager is not installed, the default
13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * behavior is to cache entries for a finite (implementation dependent)
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * period of time. The result of unsuccessful host
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name resolution is cached for a very short period of time (10
14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * seconds) to improve performance.
14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the default behavior is not desired, then a Java security property
14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be set to a different Time-to-live (TTL) value for positive
14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * caching. Likewise, a system admin can configure a different
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * negative caching TTL value when needed.
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Two Java security properties control the TTL values used for
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *  positive and negative host name resolution caching:
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote>
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl>
15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><b>networkaddress.cache.ttl</b></dt>
15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>Indicates the caching policy for successful name lookups from
15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name service. The value is specified as as integer to indicate
15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the number of seconds to cache the successful lookup. The default
15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting is to cache for an implementation specific period of time.
15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A value of -1 indicates "cache forever".
16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dd>
16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><b>networkaddress.cache.negative.ttl</b> (default: 10)</dt>
16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>Indicates the caching policy for un-successful name lookups
16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the name service. The value is specified as as integer to
16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * indicate the number of seconds to cache the failure for
16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * un-successful lookups.
16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A value of 0 indicates "never cache".
16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A value of -1 indicates "cache forever".
17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dd>
17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl>
17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote>
17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author  Chris Warth
17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.net.InetAddress#getByAddress(byte[])
17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.net.InetAddress#getByAddress(java.lang.String, byte[])
17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.net.InetAddress#getAllByName(java.lang.String)
17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.net.InetAddress#getByName(java.lang.String)
17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.net.InetAddress#getLocalHost()
18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0
18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic
18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiclass InetAddress implements java.io.Serializable {
18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static class InetAddressHolder {
1863dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong        /**
1873dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * Reserve the original application specified hostname.
1883dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         *
1893dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * The original hostname is useful for domain-based endpoint
1903dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * identification (see RFC 2818 and RFC 6125).  If an address
1913dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * was created with a raw IP address, a reverse name lookup
1923dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * may introduce endpoint identification security issue via
1933dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * DNS forging.
1943dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         *
1953dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * Oracle JSSE provider is using this original hostname, via
1963dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * sun.misc.JavaNetAccess, for SSL/TLS endpoint identification.
1973dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         *
1983dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         * Note: May define a new public method in the future if necessary.
1993dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong         */
2003dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong        private String originalHostName;
20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        InetAddressHolder() {}
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        InetAddressHolder(String hostName, int address, int family) {
2053dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong            this.originalHostName = hostName;
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            this.hostName = hostName;
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            this.address = address;
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            this.family = family;
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
21162e0b4793b755083e94aac458111ca347314adddYi Kong        void init(String hostName, int family) {
21262e0b4793b755083e94aac458111ca347314adddYi Kong            this.originalHostName = hostName;
21362e0b4793b755083e94aac458111ca347314adddYi Kong            this.hostName = hostName;
21462e0b4793b755083e94aac458111ca347314adddYi Kong            if (family != -1) {
21562e0b4793b755083e94aac458111ca347314adddYi Kong                this.family = family;
21662e0b4793b755083e94aac458111ca347314adddYi Kong            }
21762e0b4793b755083e94aac458111ca347314adddYi Kong        }
21862e0b4793b755083e94aac458111ca347314adddYi Kong
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String hostName;
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String getHostName() {
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return hostName;
22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2253dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong        String getOriginalHostName() {
2263dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong            return originalHostName;
2273dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong        }
2283dba3551b93036da22e5a80a247d84d09c7d3d53Yi Kong
22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        /**
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski         * Holds a 32-bit IPv4 address.
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski         */
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int address;
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int getAddress() {
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return address;
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        /**
23993c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak         * Specifies the address family type, for instance, AF_INET for IPv4
24093c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak         * addresses, and AF_INET6 for IPv6 addresses.
24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski         */
24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int family;
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int getFamily() {
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return family;
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2495341952e2f75808599a857152f093cbabec0b67bNarayan Kamath    transient InetAddressHolder holder;
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    InetAddressHolder holder() {
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return holder;
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
255c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    /* The implementation is always dual stack IPv6/IPv4 on android */
256c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    static final InetAddressImpl impl = new Inet6AddressImpl();
257c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /* Used to store the name service provider */
259c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    private static final NameService nameService = new NameService() {
260c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        public InetAddress[] lookupAllHostAddr(String host, int netId)
261c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath                throws UnknownHostException {
262c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            return impl.lookupAllHostAddr(host, netId);
263c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        }
264c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        public String getHostByAddr(byte[] addr)
265c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath                throws UnknownHostException {
266c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            return impl.getHostByAddr(addr);
267c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        }
268c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    };
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /* Used to store the best available hostname */
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private transient String canonicalHostName = null;
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** use serialVersionUID from JDK 1.0.2 for interoperability */
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final long serialVersionUID = 3286316764910316507L;
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Constructor for the Socket.accept() method.
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * This creates an empty InetAddress, which is filled in by
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the accept() method.  This InetAddress, however, is not
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * put in the address cache, since it is not created by name.
28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    InetAddress() {
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        holder = new InetAddressHolder();
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Replaces the de-serialized object with an Inet4Address object.
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the alternate object to the de-serialized object.
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ObjectStreamException if a new object replacing this
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object could not be created
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private Object readResolve() throws ObjectStreamException {
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // will replace the deserialized 'this' object
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return new Inet4Address(holder().getHostName(), holder().getAddress());
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is an
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * IP multicast address.
3023a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the InetAddress is
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * an IP multicast address
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since   JDK1.1
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMulticastAddress() {
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress in a wildcard address.
3123a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the Inetaddress is
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         a wildcard address.
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isAnyLocalAddress() {
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is a loopback address.
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3233a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the InetAddress is
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a loopback address; or false otherwise.
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isLoopbackAddress() {
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is an link local address.
33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3343a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the InetAddress is
33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a link local address; or false if address is not a link local unicast address.
33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isLinkLocalAddress() {
33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is a site local address.
34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3453a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the InetAddress is
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a site local address; or false if address is not a site local unicast address.
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isSiteLocalAddress() {
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has global scope.
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3563a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address has
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of global scope, false if it is not
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of global scope or it is not a multicast address
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCGlobal() {
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has node scope.
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3683a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address has
36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of node-local scope, false if it is not
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of node-local scope or it is not a multicast address
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCNodeLocal() {
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has link scope.
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3803a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address has
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of link-local scope, false if it is not
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of link-local scope or it is not a multicast address
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCLinkLocal() {
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has site scope.
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3923a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address has
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of site-local scope, false if it is not
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of site-local scope or it is not a multicast address
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCSiteLocal() {
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has organization scope.
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
4043a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address has
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of organization-local scope,
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         false if it is not of organization-local scope
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or it is not a multicast address
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCOrgLocal() {
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Test whether that address is reachable. Best effort is made by the
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation to try to reach the host, but firewalls and server
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * configuration may block requests resulting in a unreachable status
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * while some specific ports may be accessible.
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * A typical implementation will use ICMP ECHO REQUESTs if the
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * privilege can be obtained, otherwise it will try to establish
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a TCP connection on port 7 (Echo) of the destination host.
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The timeout value, in milliseconds, indicates the maximum amount of time
42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the try should take. If the operation times out before getting an
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * answer, the host is deemed unreachable. A negative value will result
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in an IllegalArgumentException being thrown.
42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   timeout the time, in milliseconds, before the call aborts
4303a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean} indicating if the address is reachable.
43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IOException if a network error occurs
4323a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @throws  IllegalArgumentException if {@code timeout} is negative.
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isReachable(int timeout) throws IOException {
436c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return isReachable(null, 0, timeout);
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Test whether that address is reachable. Best effort is made by the
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation to try to reach the host, but firewalls and server
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * configuration may block requests resulting in a unreachable status
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * while some specific ports may be accessible.
44448e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     * <p>
44548e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     * Android implementation attempts ICMP ECHO REQUESTs first, on failure it
44648e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     * will fall back to TCP ECHO REQUESTs. Success on either protocol will
44748e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     * return true.
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
4493a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * The {@code network interface} and {@code ttl} parameters
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * let the caller specify which network interface the test will go through
45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and the maximum number of hops the packets should go through.
4523a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * A negative value for the {@code ttl} will result in an
45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * IllegalArgumentException being thrown.
45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The timeout value, in milliseconds, indicates the maximum amount of time
45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the try should take. If the operation times out before getting an
45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * answer, the host is deemed unreachable. A negative value will result
45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in an IllegalArgumentException being thrown.
45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   netif   the NetworkInterface through which the
46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *                    test will be done, or null for any interface
46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   ttl     the maximum numbers of hops to try or 0 for the
46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *                  default
46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   timeout the time, in milliseconds, before the call aborts
4653a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @throws  IllegalArgumentException if either {@code timeout}
4663a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     *                          or {@code ttl} are negative.
4673a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return a {@code boolean}indicating if the address is reachable.
46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IOException if a network error occurs
46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isReachable(NetworkInterface netif, int ttl,
47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                               int timeout) throws IOException {
47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (ttl < 0)
47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException("ttl can't be negative");
47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (timeout < 0)
47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException("timeout can't be negative");
47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return impl.isReachable(this, timeout, netif, ttl);
47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
48248e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     * @hide For testing only
48348e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong     */
48448e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong    public boolean isReachableByICMP(int timeout) throws IOException {
48548e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong        return ((Inet6AddressImpl) impl).icmpEcho(this, timeout, null, 0);
48648e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong    }
48748e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong
48848e205e5e0bc55b5c7b11df445da39c5be890623Yi Kong    /**
48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Gets the host name for this IP address.
49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If this InetAddress was created with a host name,
49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this host name will be remembered and returned;
49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * otherwise, a reverse name lookup will be performed
49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and the result will be returned based on the system
49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * configured name lookup service. If a lookup of the name service
49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is required, call
49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #getCanonicalHostName() getCanonicalHostName}.
49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If there is a security manager, its
5003a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * {@code checkConnect} method is first called
5013a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * with the hostname and {@code -1}
50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as its arguments to see if the operation is allowed.
50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the operation is not allowed, it will return
50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the textual representation of the IP address.
50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the host name for this IP address, or if the operation
50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    is not allowed by the security check, the textual
50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    representation of the IP address.
50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see InetAddress#getCanonicalHostName
51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkConnect
51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getHostName() {
51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (holder().getHostName() == null) {
515c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            holder().hostName = InetAddress.getHostFromNameService(this);
51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return holder().getHostName();
51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Gets the fully qualified domain name for this IP address.
52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Best effort method, meaning we may not be able to return
52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the FQDN depending on the underlying system configuration.
52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If there is a security manager, this method first
5263a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * calls its {@code checkConnect} method
5273a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * with the hostname and {@code -1}
52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as its arguments to see if the calling code is allowed to know
52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the hostname for this IP address, i.e., to connect to the host.
53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the operation is not allowed, it will return
53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the textual representation of the IP address.
53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the fully qualified domain name for this IP address,
53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    or if the operation is not allowed by the security check,
53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    the textual representation of the IP address.
53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkConnect
53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getCanonicalHostName() {
54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (canonicalHostName == null) {
543c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            canonicalHostName = InetAddress.getHostFromNameService(this);
54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return canonicalHostName;
54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the hostname for this address.
55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If there is a security manager, this method first
5523a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * calls its {@code checkConnect} method
5533a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * with the hostname and {@code -1}
55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as its arguments to see if the calling code is allowed to know
55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the hostname for this IP address, i.e., to connect to the host.
55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the operation is not allowed, it will return
55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the textual representation of the IP address.
55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the host name for this IP address, or if the operation
56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    is not allowed by the security check, the textual
56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    representation of the IP address.
56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkConnect
56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
565c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    private static String getHostFromNameService(InetAddress addr) {
56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String host = null;
567c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        try {
568c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            // first lookup the hostname
569c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            host = nameService.getHostByAddr(addr.getAddress());
57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                /* now get all the IP addresses for this hostname,
57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                 * and make sure one of them matches the original IP
57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                 * address. We do this to try and prevent spoofing.
57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                 */
575c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            InetAddress[] arr = nameService.lookupAllHostAddr(host, NETID_UNSET);
576c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            boolean ok = false;
57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
578c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            if (arr != null) {
579c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath                for(int i = 0; !ok && i < arr.length; i++) {
580c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath                    ok = addr.equals(arr[i]);
58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
582c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            }
58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
584c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            //XXX: if it looks a spoof just return the address?
585c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            if (!ok) {
58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                host = addr.getHostAddress();
587c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath                return host;
58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
589c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        } catch (UnknownHostException e) {
590c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath            host = addr.getHostAddress();
59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return host;
59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
5973a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * Returns the raw IP address of this {@code InetAddress}
59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object. The result is in network byte order: the highest order
5993a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * byte of the address is in {@code getAddress()[0]}.
60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the raw IP address of this object.
60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public byte[] getAddress() {
60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return null;
60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
607c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    /**
60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the IP address string in textual presentation.
60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the raw IP address in a string format.
61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since   JDK1.0.2
61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getHostAddress() {
61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return null;
61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     }
61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hashcode for this IP address.
61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  a hash code value for this IP address.
62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public int hashCode() {
62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -1;
62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Compares this object against the specified object.
6283a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * The result is {@code true} if and only if the argument is
6293a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * not {@code null} and it represents the same IP address as
63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this object.
63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
6323a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * Two instances of {@code InetAddress} represent the same IP
63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address if the length of the byte arrays returned by
6343a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * {@code getAddress} is the same for both, and each of the
63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array components is the same for the byte arrays.
63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   obj   the object to compare against.
6383a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @return  {@code true} if the objects are the same;
6393a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     *          {@code false} otherwise.
64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see     java.net.InetAddress#getAddress()
64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean equals(Object obj) {
64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6473a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * Converts this IP address to a {@code String}. The
64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * string returned is of the form: hostname / literal IP
64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address.
65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the host name is unresolved, no reverse name service lookup
65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is performed. The hostname part will be represented by an empty string.
65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  a string representation of this IP address.
65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String toString() {
65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String hostName = holder().getHostName();
65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((hostName != null) ? hostName : "")
65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            + "/" + getHostAddress();
66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Creates an InetAddress based on the provided host name and IP address.
66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * No name service is checked for the validity of the address.
66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The host name can either be a machine name, such as
6673a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * "{@code java.sun.com}", or a textual representation of its IP
66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address.
66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> No validity checking is done on the host name either.
67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If addr specifies an IPv4 address an instance of Inet4Address
67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be returned; otherwise, an instance of Inet6Address
67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be returned.
67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * must be 16 bytes long
67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param host the specified host
67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param addr the raw IP address in network byte order
68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  an InetAddress object created from the raw IP address.
68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if IP address is of illegal length
68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
684c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException {
685c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return getByAddress(host, addr, -1 /* scopeId */);
68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6885341952e2f75808599a857152f093cbabec0b67bNarayan Kamath    // Do not delete. Called from native code.
689f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    private static InetAddress getByAddress(String host, byte[] addr, int scopeId)
690f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        throws UnknownHostException {
691f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
692f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            if (host.charAt(host.length()-1) == ']') {
693f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                host = host.substring(1, host.length() -1);
694f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            }
695f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
696f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (addr != null) {
697f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            if (addr.length == Inet4Address.INADDRSZ) {
698f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                return new Inet4Address(host, addr);
699f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            } else if (addr.length == Inet6Address.INADDRSZ) {
700f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                byte[] newAddr
701f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                    = IPAddressUtil.convertFromIPv4MappedAddress(addr);
702f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                if (newAddr != null) {
703f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                    return new Inet4Address(host, newAddr);
704f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                } else {
705f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                    return new Inet6Address(host, addr, scopeId);
706f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski                }
707f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            }
708f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
709f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        throw new UnknownHostException("addr is of illegal length");
710f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
711f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Determines the IP address of a host, given the host's name.
71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The host name can either be a machine name, such as
7173a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * "{@code java.sun.com}", or a textual representation of its
71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * IP address. If a literal IP address is supplied, only the
71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * validity of the address format is checked.
72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7213a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * <p> For {@code host} specified in literal IPv6 address,
72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * either the form defined in RFC 2732 or the literal IPv6 address
72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scoped addresses.
72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7273a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * <p> If the host is {@code null} then an {@code InetAddress}
72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representing an address of the loopback interface is returned.
72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * section&nbsp;2.5.3. </p>
73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7333a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @param      host   the specified host, or {@code null}.
73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return     an IP address for the given host name.
73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if no IP address for the
7363a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     *               {@code host} could be found, or if a scope_id was specified
73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *               for a global IPv6 address.
73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  SecurityException if a security manager exists
73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *             and its checkConnect method doesn't allow the operation
74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static InetAddress getByName(String host)
74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws UnknownHostException {
743d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        return impl.lookupAllHostAddr(host, NETID_UNSET)[0];
74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Given the name of a host, returns an array of its IP addresses,
74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * based on the configured name service on the system.
74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The host name can either be a machine name, such as
7513a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * "{@code java.sun.com}", or a textual representation of its IP
75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address. If a literal IP address is supplied, only the
75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * validity of the address format is checked.
75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7553a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * <p> For {@code host} specified in <i>literal IPv6 address</i>,
75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * either the form defined in RFC 2732 or the literal IPv6 address
75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * format defined in RFC 2373 is accepted. A literal IPv6 address may
75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * also be qualified by appending a scoped zone identifier or scope_id.
75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The syntax and usage of scope_ids is described
76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <a href="Inet6Address.html#scoped">here</a>.
7613a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * <p> If the host is {@code null} then an {@code InetAddress}
76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representing an address of the loopback interface is returned.
76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * section&nbsp;2.5.3. </p>
76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7673a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * <p> If there is a security manager and {@code host} is not
7683a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * null and {@code host.length() } is not equal to zero, the
76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * security manager's
7703a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * {@code checkConnect} method is called
7713a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * with the hostname and {@code -1}
77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as its arguments to see if the operation is allowed.
77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7743a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * @param      host   the name of the host, or {@code null}.
77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return     an array of all the IP addresses for a given host name.
77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if no IP address for the
7783a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     *               {@code host} could be found, or if a scope_id was specified
77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *               for a global IPv6 address.
78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  SecurityException  if a security manager exists and its
7813a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     *               {@code checkConnect} method doesn't allow the operation.
78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkConnect
78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static InetAddress[] getAllByName(String host)
78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws UnknownHostException {
787d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        return impl.lookupAllHostAddr(host, NETID_UNSET).clone();
78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the loopback address.
79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The InetAddress returned will represent the IPv4
79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * loopback address, 127.0.0.1, or the IPv6 loopback
79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address, ::1. The IPv4 loopback address returned
79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is only one of many in the form 127.*.*.*
79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the InetAddress loopback instance.
79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.7
80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static InetAddress getLoopbackAddress() {
802c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return impl.loopbackAddresses()[0];
80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
8063a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * Returns an {@code InetAddress} object given the raw IP address .
80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The argument is in network byte order: the highest order
8083a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * byte of the address is in {@code getAddress()[0]}.
80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> This method doesn't block, i.e. no reverse name service lookup
81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is performed.
81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * must be 16 bytes long
81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param addr the raw IP address in network byte order
81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  an InetAddress object created from the raw IP address.
81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if IP address is of illegal length
81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static InetAddress getByAddress(byte[] addr)
82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws UnknownHostException {
82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return getByAddress(null, addr);
82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the address of the local host. This is achieved by retrieving
82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the name of the host from the system, then resolving that name into
8293a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * an {@code InetAddress}.
83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <P>Note: The resolved address may be cached for a short period of time.
83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </P>
83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If there is a security manager, its
8353a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * {@code checkConnect} method is called
8363a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong     * with the local host name and {@code -1}
83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as its arguments to see if the operation is allowed.
83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the operation is not allowed, an InetAddress representing
83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the loopback address is returned.
84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return     the address of the local host.
84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if the local host name could not
84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *             be resolved into an address.
84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkConnect
84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see java.net.InetAddress#getByName(java.lang.String)
84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static InetAddress getLocalHost() throws UnknownHostException {
850c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        String local = Libcore.os.uname().nodename;
851c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return impl.lookupAllHostAddr(local, NETID_UNSET)[0];
85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the InetAddress representing anyLocalAddress
85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (typically 0.0.0.0 or ::0)
85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static InetAddress anyLocalAddress() {
85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return impl.anyLocalAddress();
86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void readObjectNoData (ObjectInputStream s) throws
86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         IOException, ClassNotFoundException {
8646975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer        // Android-changed: Don't use null to mean the boot classloader.
865f74285406bce55f409becf867e96142abeb9b58fNarayan Kamath        if (getClass().getClassLoader() != BOOT_CLASSLOADER) {
86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new SecurityException ("invalid address type");
86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
8706975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer    // Android-changed: Don't use null to mean the boot classloader.
8715341952e2f75808599a857152f093cbabec0b67bNarayan Kamath    private static final ClassLoader BOOT_CLASSLOADER = Object.class.getClassLoader();
87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void readObject (ObjectInputStream s) throws
87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         IOException, ClassNotFoundException {
8756975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer        // Android-changed: Don't use null to mean the boot classloader.
876f74285406bce55f409becf867e96142abeb9b58fNarayan Kamath        if (getClass().getClassLoader() != BOOT_CLASSLOADER) {
87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new SecurityException ("invalid address type");
87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        GetField gf = s.readFields();
88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String host = (String)gf.get("hostName", null);
88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int address= gf.get("address", 0);
88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int family= gf.get("family", 0);
8835341952e2f75808599a857152f093cbabec0b67bNarayan Kamath        holder = new InetAddressHolder(host, address, family);
88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /* needed because the serializable fields no longer exist */
88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serialField hostName String
89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serialField address int
89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serialField family int
89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final ObjectStreamField[] serialPersistentFields = {
89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        new ObjectStreamField("hostName", String.class),
89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        new ObjectStreamField("address", int.class),
89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        new ObjectStreamField("family", int.class),
89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    };
89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void writeObject (ObjectOutputStream s) throws
90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        IOException {
9016975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer        // Android-changed: Don't use null to mean the boot classloader.
902f74285406bce55f409becf867e96142abeb9b58fNarayan Kamath        if (getClass().getClassLoader() != BOOT_CLASSLOADER) {
90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new SecurityException ("invalid address type");
90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        PutField pf = s.putFields();
90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        pf.put("hostName", holder().hostName);
90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        pf.put("address", holder().address);
90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        pf.put("family", holder().family);
90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        s.writeFields();
91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        s.flush();
91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
912f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
913c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    static final int NETID_UNSET = 0;
914f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
915f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /**
916f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * Returns true if the string is a valid numeric IPv4 or IPv6 address (such as "192.168.0.1").
917f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * This copes with all forms of address that Java supports, detailed in the {@link InetAddress}
918f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * class documentation.
919f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     *
920f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @hide used by frameworks/base to ensure that a getAllByName won't cause a DNS lookup.
921f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     */
922f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static boolean isNumeric(String address) {
923f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        InetAddress inetAddress = parseNumericAddressNoThrow(address);
924f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        return inetAddress != null && disallowDeprecatedFormats(address, inetAddress) != null;
925f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
926f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
927c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    static InetAddress parseNumericAddressNoThrow(String address) {
928f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        // Accept IPv6 addresses (only) in square brackets for compatibility.
929f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
930f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            address = address.substring(1, address.length() - 1);
931f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
932f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        StructAddrinfo hints = new StructAddrinfo();
933f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        hints.ai_flags = AI_NUMERICHOST;
934f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        InetAddress[] addresses = null;
935f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        try {
936f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            addresses = Libcore.os.android_getaddrinfo(address, hints, NETID_UNSET);
937f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        } catch (GaiException ignored) {
938f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
939f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        return (addresses != null) ? addresses[0] : null;
940f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
941f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
942c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    static InetAddress disallowDeprecatedFormats(String address, InetAddress inetAddress) {
943f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        // Only IPv4 addresses are problematic.
944f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (!(inetAddress instanceof Inet4Address) || address.indexOf(':') != -1) {
945f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            return inetAddress;
946f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
947f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        // If inet_pton(3) can't parse it, it must have been a deprecated format.
948f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        // We need to return inet_pton(3)'s result to ensure that numbers assumed to be octal
949f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        // by getaddrinfo(3) are reinterpreted by inet_pton(3) as decimal.
950f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        return Libcore.os.inet_pton(AF_INET, address);
951f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
952f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
953f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /**
954f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * Returns an InetAddress corresponding to the given numeric address (such
955f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}).
956f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * This method will never do a DNS lookup. Non-numeric addresses are errors.
957f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     *
958f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @hide used by frameworks/base's NetworkUtils.numericToInetAddress
959f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @throws IllegalArgumentException if {@code numericAddress} is not a numeric address
960f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     */
961f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static InetAddress parseNumericAddress(String numericAddress) {
962f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (numericAddress == null || numericAddress.isEmpty()) {
963f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            return Inet6Address.LOOPBACK;
964f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
965f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        InetAddress result = parseNumericAddressNoThrow(numericAddress);
966f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        result = disallowDeprecatedFormats(numericAddress, result);
967f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        if (result == null) {
968f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            throw new IllegalArgumentException("Not a numeric address: " + numericAddress);
969f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        }
970f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        return result;
971f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
972f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
973d02671ce79f87bb3bfe8b733b7303633ddedd208Przemyslaw Szczepaniak    /**
974d02671ce79f87bb3bfe8b733b7303633ddedd208Przemyslaw Szczepaniak     * Removes all entries from the VM's DNS cache. This does not affect the C library's DNS
975d02671ce79f87bb3bfe8b733b7303633ddedd208Przemyslaw Szczepaniak     * cache, nor any caching DNS servers between you and the canonical server.
976d02671ce79f87bb3bfe8b733b7303633ddedd208Przemyslaw Szczepaniak     * @hide
977d02671ce79f87bb3bfe8b733b7303633ddedd208Przemyslaw Szczepaniak     */
978f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static void clearDnsCache() {
979c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        impl.clearAddressCache();
980f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
981f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
982f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /**
983f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * Operates identically to {@code getByName} except host resolution is
984f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * performed on the network designated by {@code netId}.
985f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     *
986f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @param host
987f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     *            the hostName to be resolved to an address or {@code null}.
988f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @param netId the network to use for host resolution.
989f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @return the {@code InetAddress} instance representing the host.
990f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @throws UnknownHostException if the address lookup fails.
991f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @hide internal use only
992f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     */
993f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static InetAddress getByNameOnNet(String host, int netId) throws UnknownHostException {
994c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return impl.lookupAllHostAddr(host, netId)[0];
995f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
996f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
997f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /**
998f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * Operates identically to {@code getAllByName} except host resolution is
999f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * performed on the network designated by {@code netId}.
1000f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     *
1001f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @param host the hostname or literal IP string to be resolved.
1002f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @param netId the network to use for host resolution.
1003f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @return the array of addresses associated with the specified host.
1004f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @throws UnknownHostException if the address lookup fails.
1005f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     * @hide internal use only
1006f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski     */
1007f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static InetAddress[] getAllByNameOnNet(String host, int netId) throws UnknownHostException {
1008c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        return impl.lookupAllHostAddr(host, netId).clone();
1009f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
1010f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
1011c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    // Only called by java.net.SocketPermission.
1012c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    static InetAddress[] getAllByName0(String authHost, boolean check) throws UnknownHostException {
1013c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        throw new UnsupportedOperationException();
1014f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
1015f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
1016c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    // Only called by java.net.SocketPermission.
1017c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath    String getHostName(boolean check) {
1018c2614bb1297234d89f1a428a8b589bb1373a68ddNarayan Kamath        throw new UnsupportedOperationException();
101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
102068b18924acfe6325285eaf01f233ce4b27700763Przemyslaw Szczepaniak}
1021