InetAddress.java revision fc041ff241f9a7556e72236f130de0215ecd17db
1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  the License.  You may obtain a copy of the License at
8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.net;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
207983be972905950b4a4e7d66df908f083c81ee29Brad Fitzpatrickimport dalvik.system.BlockGuard;
21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.FileDescriptor;
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException;
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectInputStream;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectOutputStream;
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectStreamException;
26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectStreamField;
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.Serializable;
280eb70e31581a977afa5df3292d1c96e42e548821Elliott Hughesimport java.nio.ByteOrder;
29ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colittiimport java.util.Arrays;
30d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughesimport java.util.Collections;
31ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colittiimport java.util.Comparator;
32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Enumeration;
33d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughesimport java.util.List;
34ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughesimport libcore.io.ErrnoException;
354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesimport libcore.io.GaiException;
367341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesimport libcore.io.Libcore;
370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport libcore.io.IoBridge;
38f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughesimport libcore.io.Memory;
391c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesimport libcore.io.StructAddrinfo;
404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesimport static libcore.io.OsConstants.*;
41adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
43048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * An Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and
44048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * in practice you'll have an instance of either {@code Inet4Address} or {@code Inet6Address} (this
45048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * class cannot be instantiated directly). Most code does not need to distinguish between the two
46048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * families, and should use {@code InetAddress}.
474adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
484adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>An {@code InetAddress} may have a hostname (accessible via {@code getHostName}), but may not,
49048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * depending on how the {@code InetAddress} was created.
504adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
514adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>IPv4 numeric address formats</h4>
52fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes * <p>The {@code getAllByName} method accepts IPv4 addresses in the "decimal-dotted-quad" form only:
534adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <ul>
544adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li>{@code "1.2.3.4"} - 1.2.3.4
554adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </ul>
564adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
574adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>IPv6 numeric address formats</h4>
584adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>The {@code getAllByName} method accepts IPv6 addresses in the following forms (this text
594adff24306c86433ce4f771da8489a574e63318eElliott Hughes * comes from <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a>, which you should consult
604adff24306c86433ce4f771da8489a574e63318eElliott Hughes * for full details of IPv6 addressing):
614adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <ul>
624adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li><p>The preferred form is {@code x:x:x:x:x:x:x:x}, where the 'x's are the
634adff24306c86433ce4f771da8489a574e63318eElliott Hughes * hexadecimal values of the eight 16-bit pieces of the address.
644adff24306c86433ce4f771da8489a574e63318eElliott Hughes * Note that it is not necessary to write the leading zeros in an
654adff24306c86433ce4f771da8489a574e63318eElliott Hughes * individual field, but there must be at least one numeral in every
664adff24306c86433ce4f771da8489a574e63318eElliott Hughes * field (except for the case described in the next bullet).
674adff24306c86433ce4f771da8489a574e63318eElliott Hughes * Examples:
684adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
694adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
704adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080:0:0:0:8:800:200C:417A</pre>
714adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
724adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li>Due to some methods of allocating certain styles of IPv6
734adff24306c86433ce4f771da8489a574e63318eElliott Hughes * addresses, it will be common for addresses to contain long strings
744adff24306c86433ce4f771da8489a574e63318eElliott Hughes * of zero bits.  In order to make writing addresses containing zero
754adff24306c86433ce4f771da8489a574e63318eElliott Hughes * bits easier a special syntax is available to compress the zeros.
764adff24306c86433ce4f771da8489a574e63318eElliott Hughes * The use of "::" indicates multiple groups of 16-bits of zeros.
774adff24306c86433ce4f771da8489a574e63318eElliott Hughes * The "::" can only appear once in an address.  The "::" can also be
784adff24306c86433ce4f771da8489a574e63318eElliott Hughes * used to compress the leading and/or trailing zeros in an address.
794adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
804adff24306c86433ce4f771da8489a574e63318eElliott Hughes * For example the following addresses:
814adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
824adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080:0:0:0:8:800:200C:417A  a unicast address
834adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FF01:0:0:0:0:0:0:101        a multicast address
844adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:0:1             the loopback address
854adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:0:0             the unspecified addresses</pre>
864adff24306c86433ce4f771da8489a574e63318eElliott Hughes * may be represented as:
874adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
884adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080::8:800:200C:417A       a unicast address
894adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FF01::101                   a multicast address
904adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::1                         the loopback address
914adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::                          the unspecified addresses</pre>
924adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
934adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li><p>An alternative form that is sometimes more convenient when dealing
944adff24306c86433ce4f771da8489a574e63318eElliott Hughes * with a mixed environment of IPv4 and IPv6 nodes is
954adff24306c86433ce4f771da8489a574e63318eElliott Hughes * {@code x:x:x:x:x:x:d.d.d.d}, where the 'x's are the hexadecimal values of
964adff24306c86433ce4f771da8489a574e63318eElliott Hughes * the six high-order 16-bit pieces of the address, and the 'd's are
974adff24306c86433ce4f771da8489a574e63318eElliott Hughes * the decimal values of the four low-order 8-bit pieces of the
984adff24306c86433ce4f771da8489a574e63318eElliott Hughes * address (standard IPv4 representation).  Examples:
994adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
1004adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:13.1.68.3
1014adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:FFFF:129.144.52.38</pre>
1024adff24306c86433ce4f771da8489a574e63318eElliott Hughes * or in compressed form:
1034adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
1044adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::13.1.68.3
1054adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::FFFF:129.144.52.38</pre>
1064adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
1074adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </ul>
1084adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>Scopes are given using a trailing {@code %} followed by the scope id, as in
1094adff24306c86433ce4f771da8489a574e63318eElliott Hughes * {@code 1080::8:800:200C:417A%2} or {@code 1080::8:800:200C:417A%en0}.
1104adff24306c86433ce4f771da8489a574e63318eElliott Hughes * See <a href="https://www.ietf.org/rfc/rfc4007.txt">RFC 4007</a> for more on IPv6's scoped
1114adff24306c86433ce4f771da8489a574e63318eElliott Hughes * address architecture.
1124adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
1130d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes * <p>Additionally, for backwards compatibility, IPv6 addresses may be surrounded by square
1140d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes * brackets.
1150d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes *
1164adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>DNS caching</h4>
1174adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>On Android, addresses are cached for 600 seconds (10 minutes) by default. Failed lookups are
118048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * cached for 10 seconds. The underlying C library or OS may cache for longer, but you can control
119048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * the Java-level caching with the usual {@code "networkaddress.cache.ttl"} and
120048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * {@code "networkaddress.cache.negative.ttl"} system properties. These are parsed as integer
121048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * numbers of seconds, where the special value 0 means "don't cache" and -1 means "cache forever".
1224adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
1234adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>Note also that on Android &ndash; unlike the RI &ndash; the cache is not unbounded. The
1244adff24306c86433ce4f771da8489a574e63318eElliott Hughes * current implementation caches around 512 entries, removed on a least-recently-used basis.
125048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * (Obviously, you should not rely on these details.)
126f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
127048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * @see Inet4Address
128048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * @see Inet6Address
129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
130048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughespublic class InetAddress implements Serializable {
13138607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    /** Our Java-side DNS cache. */
132048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes    private static final AddressCache addressCache = new AddressCache();
133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
134ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    private static final long serialVersionUID = 3286316764910316507L;
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private transient Object waitReachable = new Object();
137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private boolean reached;
139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private int addrCount;
141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private int family;
143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    byte[] ipaddress;
145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    String hostName;
1474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
14932b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * Used by the DatagramSocket.disconnect implementation.
15032b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * @hide internal use only
15132b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     */
15232b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes    public static final InetAddress UNSPECIFIED = new InetAddress(AF_UNSPEC, null, null);
15332b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes
15432b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes    /**
155051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     * Constructs an {@code InetAddress}.
156051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     *
15732b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * Note: this constructor is for subclasses only.
158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1594f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    InetAddress(int family, byte[] ipaddress, String hostName) {
1604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.family = family;
1614f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.ipaddress = ipaddress;
1624f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.hostName = hostName;
1634f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares this {@code InetAddress} instance against the specified address
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * in {@code obj}. Two addresses are equal if their address byte arrays have
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the same length and if the bytes in the arrays are equal.
169f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param obj
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to be tested for equality.
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if both objects are equal, {@code false} otherwise.
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean equals(Object obj) {
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!(obj instanceof InetAddress)) {
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
179051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti        return Arrays.equals(this.ipaddress, ((InetAddress) obj).ipaddress);
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the IP address represented by this {@code InetAddress} instance
184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * as a byte array. The elements are in network order (the highest order
185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address byte is in the zeroth element).
186f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
187adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the address in form of a byte array.
188adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
189adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public byte[] getAddress() {
190adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return ipaddress.clone();
191adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
192adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
193ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
194ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * Converts an array of byte arrays representing raw IP addresses of a host
195ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes     * to an array of InetAddress objects.
196ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     *
197ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param rawAddresses the raw addresses to convert.
198ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param hostName the hostname corresponding to the IP address.
199ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @return the corresponding InetAddresses, appropriately sorted.
200ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
2014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress[] bytesToInetAddresses(byte[][] rawAddresses, String hostName)
202fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throws UnknownHostException {
203ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        // Convert the byte arrays to InetAddresses.
204ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        InetAddress[] returnedAddresses = new InetAddress[rawAddresses.length];
205ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        for (int i = 0; i < rawAddresses.length; i++) {
206fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            returnedAddresses[i] = makeInetAddress(rawAddresses[i], hostName);
207ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        }
208ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        return returnedAddresses;
209ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    }
210ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Gets all IP addresses associated with the given {@code host} identified
213ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * by name or literal IP address. The IP address is resolved by the
214adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * configured name service. If the host name is empty or {@code null} an
215ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * {@code UnknownHostException} is thrown. If the host name is a literal IP
216adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address string an array with the corresponding single {@code InetAddress}
217adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * is returned.
218f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
219ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param host the hostname or literal IP string to be resolved.
220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the array of addresses associated with the specified host.
221ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @throws UnknownHostException if the address lookup fails.
222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
22338607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    public static InetAddress[] getAllByName(String host) throws UnknownHostException {
22438607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return getAllByNameImpl(host).clone();
225ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    }
226ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
227ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
22838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson     * Returns the InetAddresses for {@code host}. The returned array is shared
22938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson     * and must be cloned before it is returned to application code.
230ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
2314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress[] getAllByNameImpl(String host) throws UnknownHostException {
23238607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        if (host == null || host.isEmpty()) {
23346bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes            return loopbackAddresses();
234ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        }
235ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
2360d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes        // Is it a numeric address?
2371c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress result = parseNumericAddressNoThrow(host);
2381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (result != null) {
239fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            result = disallowDeprecatedFormats(host, result);
240fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            if (result == null) {
241fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                throw new UnknownHostException("Deprecated IPv4 address format: " + host);
242fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            }
2431c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            return new InetAddress[] { result };
244adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
245adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
2461c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return lookupHostByName(host).clone();
24738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    }
24838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson
2494f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static InetAddress makeInetAddress(byte[] bytes, String hostName) throws UnknownHostException {
250fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        if (bytes.length == 4) {
2514f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return new Inet4Address(bytes, hostName);
252fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else if (bytes.length == 16) {
2534f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return new Inet6Address(bytes, hostName, 0);
254fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else {
255fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throw badAddressLength(bytes);
256fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        }
257fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    }
258fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes
259fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    private static InetAddress disallowDeprecatedFormats(String address, InetAddress inetAddress) {
260fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Only IPv4 addresses are problematic.
261fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        if (!(inetAddress instanceof Inet4Address) || address.indexOf(':') != -1) {
262fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            return inetAddress;
263fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        }
264fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // If inet_pton(3) can't parse it, it must have been a deprecated format.
265fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // We need to return inet_pton(3)'s result to ensure that numbers assumed to be octal
266fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // by getaddrinfo(3) are reinterpreted by inet_pton(3) as decimal.
267fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return Libcore.os.inet_pton(AF_INET, address);
268fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    }
269fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
2701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    private static InetAddress parseNumericAddressNoThrow(String address) {
2711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Accept IPv6 addresses (only) in square brackets for compatibility.
2721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
2731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            address = address.substring(1, address.length() - 1);
2741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
2751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        StructAddrinfo hints = new StructAddrinfo();
2761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        hints.ai_flags = AI_NUMERICHOST;
2771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress[] addresses = null;
2781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        try {
2791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            addresses = Libcore.os.getaddrinfo(address, hints);
2801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } catch (GaiException ignored) {
2811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
282fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return (addresses != null) ? addresses[0] : null;
2831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
284753dcd862b31e85766225590d90ba0b9f481176fElliott Hughes
285adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
286adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the address of a host according to the given host string name
287adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code host}. The host string may be either a machine name or a dotted
288adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * string IP address. If the latter, the {@code hostName} field is
289adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * determined upon demand. {@code host} can be {@code null} which means that
290adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * an address of the loopback interface is returned.
291f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
292adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param host
293adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the hostName to be resolved to an address or {@code null}.
294adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code InetAddress} instance representing the host.
295adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws UnknownHostException
296adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if the address lookup fails.
297adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
298ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    public static InetAddress getByName(String host) throws UnknownHostException {
29938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return getAllByNameImpl(host)[0];
300adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
301adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
302e8596906d75fd8ccda31600d71ad56391cd7cd3aLorenzo Colitti    /**
3034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the numeric representation of this IP address (such as "127.0.0.1").
304adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
305adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getHostAddress() {
3064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return Libcore.os.getnameinfo(this, NI_NUMERICHOST); // Can't throw.
307adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
308adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
309adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
3104f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the host name corresponding to this IP address. This may or may not be a
3114f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * fully-qualified name. If the IP address could not be resolved, the numeric representation
3124f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * is returned instead (see {@link #getHostAddress}).
313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
314adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getHostName() {
3154f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        if (hostName == null) {
3164f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            try {
3174f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes                hostName = getHostByAddrImpl(this).hostName;
3184f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            } catch (UnknownHostException ex) {
3194f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes                hostName = getHostAddress();
320adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
321adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
322adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return hostName;
323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
325adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
3264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the fully qualified hostname corresponding to this IP address.
327adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
328adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getCanonicalHostName() {
329adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
3304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return getHostByAddrImpl(this).hostName;
3314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        } catch (UnknownHostException ex) {
3324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return getHostAddress();
333adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
334adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
335adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
336adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
337ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * Returns an {@code InetAddress} for the local host if possible, or the
338ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * loopback address otherwise. This method works by getting the hostname,
339ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * performing a DNS lookup, and then taking the first returned address.
340ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * For devices with multiple network interfaces and/or multiple addresses
341ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * per interface, this does not necessarily return the {@code InetAddress}
342ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * you want.
343f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
344ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>Multiple interface/address configurations were relatively rare
345ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * when this API was designed, but multiple interfaces are the default for
346ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * modern mobile devices (with separate wifi and radio interfaces), and
347ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * the need to support both IPv4 and IPv6 has made multiple addresses
348ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * commonplace. New code should thus avoid this method except where it's
349ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * basically being used to get a loopback address or equivalent.
350ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
351ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>There are two main ways to get a more specific answer:
352ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <ul>
353ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <li>If you have a connected socket, you should probably use
354ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@link Socket#getLocalAddress} instead: that will give you the address
355ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * that's actually in use for that connection. (It's not possible to ask
356ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * the question "what local address would a connection to a given remote
357ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * address use?"; you have to actually make the connection and see.)</li>
358ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <li>For other use cases, see {@link NetworkInterface}, which lets you
359ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * enumerate all available network interfaces and their addresses.</li>
360ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * </ul>
361ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
362ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>Note that if the host doesn't have a hostname set&nbsp;&ndash; as
363ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * Android devices typically don't&nbsp;&ndash; this method will
364ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * effectively return the loopback address, albeit by getting the name
365ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@code localhost} and then doing a lookup to translate that to
366ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@code 127.0.0.1}.
367ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
368ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * @return an {@code InetAddress} representing the local host, or the
369ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * loopback address.
370adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws UnknownHostException
371adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if the address lookup fails.
372adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
373adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public static InetAddress getLocalHost() throws UnknownHostException {
3749b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        String host = Libcore.os.uname().nodename;
375ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        return lookupHostByName(host)[0];
376adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
377adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
378adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
379adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Gets the hashcode of the represented IP address.
380f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
381adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the appropriate hashcode value.
382adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
383adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
384adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int hashCode() {
385051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti        return Arrays.hashCode(ipaddress);
386adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
387adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
388051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti    /*
389051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     * Returns whether this address is an IP multicast address or not. This
390051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     * implementation returns always {@code false}.
391f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
392adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this address is in the multicast group, {@code
393adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         false} otherwise.
394adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMulticastAddress() {
396051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti        return false;
397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
398051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti
399ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
400048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes     * Resolves a hostname to its IP addresses using a cache.
401ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     *
402ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param host the hostname to resolve.
403ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @return the IP addresses of the host.
404ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
405048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes    private static InetAddress[] lookupHostByName(String host) throws UnknownHostException {
4067983be972905950b4a4e7d66df908f083c81ee29Brad Fitzpatrick        BlockGuard.getThreadPolicy().onNetwork();
407048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes        // Do we have a result cached?
408fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        Object cachedResult = addressCache.get(host);
409048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes        if (cachedResult != null) {
410fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            if (cachedResult instanceof InetAddress[]) {
411048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes                // A cached positive result.
412fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes                return (InetAddress[]) cachedResult;
413048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            } else {
414048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes                // A cached negative result.
415fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes                throw new UnknownHostException((String) cachedResult);
416adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
417adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
418adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
4191c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            StructAddrinfo hints = new StructAddrinfo();
4201c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_flags = AI_ADDRCONFIG;
4211c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_family = AF_UNSPEC;
4221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // If we don't specify a socket type, every address will appear twice, once
4231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // for SOCK_STREAM and one for SOCK_DGRAM. Since we do not return the family
4241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // anyway, just pick one.
4251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_socktype = SOCK_STREAM;
4261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            InetAddress[] addresses = Libcore.os.getaddrinfo(host, hints);
4271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // TODO: should getaddrinfo set the hostname of the InetAddresses it returns?
4281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            for (InetAddress address : addresses) {
4291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes                address.hostName = host;
4301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            }
431048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            addressCache.put(host, addresses);
432048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            return addresses;
4331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } catch (GaiException gaiException) {
4341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // TODO: bionic currently returns EAI_NODATA, which is indistinguishable from a real
4351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // failure. We need to fix bionic before we can report a more useful error.
4361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // if (gaiException.error == EAI_SYSTEM) {
4371c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            //    throw new SecurityException("Permission denied (missing INTERNET permission?)");
4381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // }
4391c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            String detailMessage = "Unable to resolve host \"" + host + "\": " + Libcore.os.gai_strerror(gaiException.error);
440fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            addressCache.putUnknownHost(host, detailMessage);
4411c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            throw gaiException.rethrowAsUnknownHostException(detailMessage);
442adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
446b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * Removes all entries from the VM's DNS cache. This does not affect the C library's DNS
447b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * cache, nor any caching DNS servers between you and the canonical server.
448b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * @hide
449b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     */
450b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes    public static void clearDnsCache() {
451b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes        addressCache.clear();
452b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes    }
453b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes
4544f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress getHostByAddrImpl(InetAddress address) throws UnknownHostException {
4557983be972905950b4a4e7d66df908f083c81ee29Brad Fitzpatrick        BlockGuard.getThreadPolicy().onNetwork();
4564f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        try {
4574f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            String hostname = Libcore.os.getnameinfo(address, NI_NAMEREQD);
4584f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return makeInetAddress(address.ipaddress.clone(), hostname);
4594f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        } catch (GaiException gaiException) {
4604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            throw gaiException.rethrowAsUnknownHostException();
461adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
462adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
463adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
464adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns a string containing a concise, human-readable description of this
466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * IP address.
467f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
468adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the description, as host/address.
469adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
470adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String toString() {
47238607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return (hostName == null ? "" : hostName) + "/" + getHostAddress();
473adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
474adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
475adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
4760d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * Returns true if the string is a valid numeric IPv4 or IPv6 address (such as "192.168.0.1").
4770d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * This copes with all forms of address that Java supports, detailed in the {@link InetAddress}
4780d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * class documentation.
4790d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     *
480f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @hide used by frameworks/base to ensure that a getAllByName won't cause a DNS lookup.
481adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
4820d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes    public static boolean isNumeric(String address) {
483fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        InetAddress inetAddress = parseNumericAddressNoThrow(address);
484fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return inetAddress != null && disallowDeprecatedFormats(address, inetAddress) != null;
485adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
487adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
488f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * Returns an InetAddress corresponding to the given numeric address (such
489f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}).
490f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * This method will never do a DNS lookup. Non-numeric addresses are errors.
491f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     *
492f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @hide used by frameworks/base's NetworkUtils.numericToInetAddress
493f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @throws IllegalArgumentException if {@code numericAddress} is not a numeric address
494f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     */
495f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    public static InetAddress parseNumericAddress(String numericAddress) {
49646bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        if (numericAddress == null || numericAddress.isEmpty()) {
497ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes            return Inet6Address.LOOPBACK;
49846bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        }
4991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress result = parseNumericAddressNoThrow(numericAddress);
500fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        result = disallowDeprecatedFormats(numericAddress, result);
5011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (result == null) {
502f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes            throw new IllegalArgumentException("Not a numeric address: " + numericAddress);
503f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes        }
5041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return result;
505f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    }
506f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes
50746bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes    private static InetAddress[] loopbackAddresses() {
508ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes        return new InetAddress[] { Inet6Address.LOOPBACK, Inet4Address.LOOPBACK };
50946bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes    }
51046bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes
511f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    /**
5125d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     * Returns the IPv6 loopback address {@code ::1} or the IPv4 loopback address {@code 127.0.0.1}.
5135d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     * @since 1.7
5145d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     * @hide 1.7
5155d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     */
5165d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    public static InetAddress getLoopbackAddress() {
517ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes        return Inet6Address.LOOPBACK;
5185d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    }
5195d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes
5205d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    /**
521adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a loopback address or not. This
522adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * implementation returns always {@code false}. Valid IPv4 loopback
523adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * addresses are 127.d.d.d The only valid IPv6 loopback address is ::1.
524f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
525adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a loopback address,
526adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
527adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
528adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isLoopbackAddress() {
529adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
530adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
531adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
532adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
533adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a link-local address or not. This
534adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * implementation returns always {@code false}.
535adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 link-local addresses are FE80::0 through to
537adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF.
538adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
539adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * There are no valid IPv4 link-local addresses.
540f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
541adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a link-local address,
542adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
543adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
544adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isLinkLocalAddress() {
545adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
546adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
547adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
548adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
549adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a site-local address or not. This
550adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * implementation returns always {@code false}.
551adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
552adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 site-local addresses are FEC0::0 through to
553adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF.
554adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
555adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * There are no valid IPv4 site-local addresses.
556f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
557adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a site-local address,
558adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
559adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
560adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isSiteLocalAddress() {
561adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
562adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
563adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
564adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
565adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a global multicast address or not. This
566adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * implementation returns always {@code false}.
567adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
568adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 link-global multicast addresses are FFxE:/112 where x is a set
569adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of flags, and the additional 112 bits make up the global multicast
570adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
571adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
572adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv4 global multicast addresses are between: 224.0.1.0 to
573adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * 238.255.255.255.
574f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
575adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a global multicast
576adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         address, {@code false} otherwise.
577adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
578adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCGlobal() {
579adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
580adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
581adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
582adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
583adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a node-local multicast address or not.
584adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This implementation returns always {@code false}.
585adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
586adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 node-local multicast addresses are FFx1:/112 where x is a set
587adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of flags, and the additional 112 bits make up the node-local multicast
588adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
589adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
590adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * There are no valid IPv4 node-local multicast addresses.
591f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
592adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a node-local multicast
593adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         address, {@code false} otherwise.
594adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
595adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCNodeLocal() {
596adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
597adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
598adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
599adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
600adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a link-local multicast address or not.
601adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This implementation returns always {@code false}.
602adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
603adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 link-local multicast addresses are FFx2:/112 where x is a set
604adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of flags, and the additional 112 bits make up the link-local multicast
605adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
606adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
607adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv4 link-local addresses are between: 224.0.0.0 to 224.0.0.255
608f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
609adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a link-local multicast
610adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         address, {@code false} otherwise.
611adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
612adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCLinkLocal() {
613adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
614adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
615adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
616adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
617adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a site-local multicast address or not.
618adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This implementation returns always {@code false}.
619adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
620adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 site-local multicast addresses are FFx5:/112 where x is a set
621adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of flags, and the additional 112 bits make up the site-local multicast
622adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
623adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
624adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv4 site-local addresses are between: 239.252.0.0 to
625adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * 239.255.255.255
626f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
627adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a site-local multicast
628adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         address, {@code false} otherwise.
629adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
630adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCSiteLocal() {
631adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
632adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
633adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
634adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
635adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a organization-local multicast address or
636adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * not. This implementation returns always {@code false}.
637adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
638adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv6 organization-local multicast addresses are FFx8:/112 where x
639adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * is a set of flags, and the additional 112 bits make up the
640adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * organization-local multicast address space.
641adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
642adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Valid IPv4 organization-local addresses are between: 239.192.0.0 to
643adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * 239.251.255.255
644f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
645adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a organization-local
646adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         multicast address, {@code false} otherwise.
647adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
648adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCOrgLocal() {
649adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
650adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
651adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
652adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
653adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this is a wildcard address or not. This implementation
654adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * returns always {@code false}.
655f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
656adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this instance represents a wildcard address,
657adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
658adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
659adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isAnyLocalAddress() {
660adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
661adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
662adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
663adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
664adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Tries to reach this {@code InetAddress}. This method first tries to use
665adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * ICMP <i>(ICMP ECHO REQUEST)</i>. When first step fails, a TCP connection
666adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * on port 7 (Echo) of the remote host is established.
667f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
668adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param timeout
669adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            timeout in milliseconds before the test fails if no connection
670adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            could be established.
671adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this address is reachable, {@code false}
672adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         otherwise.
673adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
674adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if an error occurs during an I/O operation.
675adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IllegalArgumentException
676adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if timeout is less than zero.
677adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
678adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isReachable(int timeout) throws IOException {
679adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return isReachable(null, 0, timeout);
680adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
681adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
682adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
683adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Tries to reach this {@code InetAddress}. This method first tries to use
684adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * ICMP <i>(ICMP ECHO REQUEST)</i>. When first step fails, a TCP connection
685adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * on port 7 (Echo) of the remote host is established.
686f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
687d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes     * @param networkInterface
688adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the network interface on which to connection should be
689adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            established.
690adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param ttl
691adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the maximum count of hops (time-to-live).
692adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param timeout
693adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            timeout in milliseconds before the test fails if no connection
694adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            could be established.
695adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this address is reachable, {@code false}
696adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         otherwise.
697adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
698adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if an error occurs during an I/O operation.
699adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IllegalArgumentException
700adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if ttl or timeout is less than zero.
701adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
702d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes    public boolean isReachable(NetworkInterface networkInterface, final int ttl,
703adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            final int timeout) throws IOException {
704d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        if (ttl < 0 || timeout < 0) {
70538607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            throw new IllegalArgumentException("ttl < 0 || timeout < 0");
706adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
707d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        if (networkInterface == null) {
708d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes            return isReachableByTCP(this, null, timeout);
709adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } else {
710d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes            return isReachableByMultiThread(networkInterface, ttl, timeout);
711adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
712adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
713adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
714adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /*
715adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Uses multi-Thread to try if isReachable, returns true if any of threads
716adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * returns in time
717adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
718adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private boolean isReachableByMultiThread(NetworkInterface netif,
719adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            final int ttl, final int timeout)
720adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throws IOException {
721d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        List<InetAddress> addresses = Collections.list(netif.getInetAddresses());
722d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        if (addresses.isEmpty()) {
723adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
724adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
725adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        reached = false;
726d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        addrCount = addresses.size();
727adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        boolean needWait = false;
728d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        for (final InetAddress addr : addresses) {
729adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            // loopback interface can only reach to local addresses
730adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if (addr.isLoopbackAddress()) {
731adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                Enumeration<NetworkInterface> NetworkInterfaces = NetworkInterface
732adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        .getNetworkInterfaces();
733adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                while (NetworkInterfaces.hasMoreElements()) {
734adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    NetworkInterface networkInterface = NetworkInterfaces
735adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            .nextElement();
736adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    Enumeration<InetAddress> localAddresses = networkInterface
737adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            .getInetAddresses();
738adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    while (localAddresses.hasMoreElements()) {
739adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        if (InetAddress.this.equals(localAddresses
740adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                                .nextElement())) {
741adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            return true;
742adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        }
743adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    }
744adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
745adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
746adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                synchronized (waitReachable) {
747adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    addrCount--;
748adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
749adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    if (addrCount == 0) {
750adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        // if count equals zero, all thread
751adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        // expired,notifies main thread
752adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        waitReachable.notifyAll();
753adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    }
754adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
755adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                continue;
756adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
757adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
758adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            needWait = true;
759adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            new Thread() {
76038607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                @Override public void run() {
76138607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                    /*
76238607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                     * Spec violation! This implementation doesn't attempt an
76338607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                     * ICMP; it skips right to TCP echo.
76438607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                     */
765adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    boolean threadReached = false;
76638607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                    try {
76738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                        threadReached = isReachableByTCP(addr, InetAddress.this, timeout);
76838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                    } catch (IOException e) {
76938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                    }
770adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
771adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    synchronized (waitReachable) {
772adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        if (threadReached) {
773adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            // if thread reached this address, sets reached to
774adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            // true and notifies main thread
775adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            reached = true;
776adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            waitReachable.notifyAll();
777adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        } else {
778adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            addrCount--;
779b46dab348e2007bc08abaf7ecae34d89a2474e50Elliott Hughes                            if (addrCount == 0) {
780adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                                // if count equals zero, all thread
781adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                                // expired,notifies main thread
782adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                                waitReachable.notifyAll();
783adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            }
784adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        }
785adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    }
786adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
787adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }.start();
788adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
789adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
790adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (needWait) {
791adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            synchronized (waitReachable) {
792adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                try {
793adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    while (!reached && (addrCount != 0)) {
794adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        // wait for notification
795adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        waitReachable.wait(1000);
796adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    }
797adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                } catch (InterruptedException e) {
798adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    // do nothing
799adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
800adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                return reached;
801adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
802adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
803adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
804adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
805adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
806adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
807454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    private boolean isReachableByTCP(InetAddress destination, InetAddress source, int timeout) throws IOException {
8080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes        FileDescriptor fd = IoBridge.socket(true);
809adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        boolean reached = false;
810adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
811b46dab348e2007bc08abaf7ecae34d89a2474e50Elliott Hughes            if (source != null) {
8120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes                IoBridge.bind(fd, source, 0);
813adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
8140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes            IoBridge.connect(fd, destination, 7, timeout);
815adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            reached = true;
816adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } catch (IOException e) {
817ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes            if (e.getCause() instanceof ErrnoException) {
818ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes                // "Connection refused" means the IP address was reachable.
819ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes                reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
820adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
821adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
822adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
8230b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes        IoBridge.closeSocket(fd);
824adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
825adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return reached;
826adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
827adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
828adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
8294728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes     * Equivalent to {@code getByAddress(null, ipAddress)}. Handy for addresses with
8304f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * no associated hostname.
831adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
8324f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    public static InetAddress getByAddress(byte[] ipAddress) throws UnknownHostException {
8334728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes        return getByAddress(null, ipAddress, 0);
834adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
835adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
836adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
8374f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * Returns an {@code InetAddress} corresponding to the given network-order
8384f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * bytes {@code ipAddress} and {@code scopeId}.
839f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
8404728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes     * <p>For an IPv4 address, the byte array must be of length 4.
8414f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * For IPv6, the byte array must be of length 16. Any other length will cause an {@code
8424f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * UnknownHostException}.
8434f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
8444f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * <p>No reverse lookup is performed. The given {@code hostName} (which may be null) is
8454f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * associated with the new {@code InetAddress} with no validation done.
8464f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
8474f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * <p>(Note that numeric addresses such as {@code "127.0.0.1"} are names for the
8484f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * purposes of this API. Most callers probably want {@link #getAllByName} instead.)
8494f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
8504f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * @throws UnknownHostException if {@code ipAddress} is null or the wrong length.
851adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
8524728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    public static InetAddress getByAddress(String hostName, byte[] ipAddress) throws UnknownHostException {
8534728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes        return getByAddress(hostName, ipAddress, 0);
8544728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    }
8554728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes
8564728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    private static InetAddress getByAddress(String hostName, byte[] ipAddress, int scopeId) throws UnknownHostException {
85755b49708e6a5f5f5047513b67094257b32ff9e93Lorenzo Colitti        if (ipAddress == null) {
85838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            throw new UnknownHostException("ipAddress == null");
859adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
860fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        if (ipAddress.length == 4) {
8611f0c4ff5de23e466032f1810172f16ad0077fa65Jesse Wilson            return new Inet4Address(ipAddress.clone(), hostName);
862fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else if (ipAddress.length == 16) {
863fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // First check to see if the address is an IPv6-mapped
864fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // IPv4 address. If it is, then we can make it a IPv4
865fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // address, otherwise, we'll create an IPv6 address.
866fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            if (isIPv4MappedAddress(ipAddress)) {
8671f0c4ff5de23e466032f1810172f16ad0077fa65Jesse Wilson                return new Inet4Address(ipv4MappedToIPv4(ipAddress), hostName);
868fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            } else {
8694f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes                return new Inet6Address(ipAddress.clone(), hostName, scopeId);
870fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            }
871fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else {
872fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throw badAddressLength(ipAddress);
873adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
874adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
875adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
876fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    private static UnknownHostException badAddressLength(byte[] bytes) throws UnknownHostException {
877fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        throw new UnknownHostException("Address is neither 4 or 16 bytes: " + Arrays.toString(bytes));
878fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    }
879fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes
8804f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static boolean isIPv4MappedAddress(byte[] ipAddress) {
8814f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // Check if the address matches ::FFFF:d.d.d.d
8824f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // The first 10 bytes are 0. The next to are -1 (FF).
8834f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // The last 4 bytes are varied.
8844f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        if (ipAddress == null || ipAddress.length != 16) {
8854f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return false;
8864f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8874f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        for (int i = 0; i < 10; i++) {
8884f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            if (ipAddress[i] != 0) {
8894f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes                return false;
8904f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            }
8914f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8924f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        if (ipAddress[10] != -1 || ipAddress[11] != -1) {
8934f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return false;
8944f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8954f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        return true;
8964f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    }
8974f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes
8984f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static byte[] ipv4MappedToIPv4(byte[] mappedAddress) {
8994f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        byte[] ipv4Address = new byte[4];
9000d4daefcf389b6433a0af481ef44a84a2546541aElliott Hughes        for (int i = 0; i < 4; i++) {
9014f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            ipv4Address[i] = mappedAddress[12 + i];
9024f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
9034f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        return ipv4Address;
9044f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    }
9054f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes
906adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private static final ObjectStreamField[] serialPersistentFields = {
907e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes        new ObjectStreamField("address", int.class),
908e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes        new ObjectStreamField("family", int.class),
90928eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("hostName", String.class),
91028eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes    };
911adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
912adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private void writeObject(ObjectOutputStream stream) throws IOException {
913adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ObjectOutputStream.PutField fields = stream.putFields();
914adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (ipaddress == null) {
91538607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            fields.put("address", 0);
916adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } else {
917f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes            fields.put("address", Memory.peekInt(ipaddress, 0, ByteOrder.BIG_ENDIAN));
918adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
91938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        fields.put("family", family);
92038607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        fields.put("hostName", hostName);
921adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
922adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        stream.writeFields();
923adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
924adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
9250eb70e31581a977afa5df3292d1c96e42e548821Elliott Hughes    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
926adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ObjectInputStream.GetField fields = stream.readFields();
92738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        int addr = fields.get("address", 0);
928adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ipaddress = new byte[4];
929f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        Memory.pokeInt(ipaddress, 0, addr, ByteOrder.BIG_ENDIAN);
93038607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        hostName = (String) fields.get("hostName", null);
93138607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        family = fields.get("family", 2);
932adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
933adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
9345839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson    /*
9355839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson     * The spec requires that if we encounter a generic InetAddress in
9364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * serialized form then we should interpret it as an Inet4Address.
9375839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson     */
938adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private Object readResolve() throws ObjectStreamException {
939adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return new Inet4Address(ipaddress, hostName);
940adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
941adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
942