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;
349b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.util.concurrent.CountDownLatch;
359b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.util.concurrent.atomic.AtomicBoolean;
36ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughesimport libcore.io.ErrnoException;
374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesimport libcore.io.GaiException;
380b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport libcore.io.IoBridge;
399b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport libcore.io.Libcore;
40f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughesimport libcore.io.Memory;
411c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesimport libcore.io.StructAddrinfo;
424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesimport static libcore.io.OsConstants.*;
43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
44adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
45048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * An Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and
46048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * in practice you'll have an instance of either {@code Inet4Address} or {@code Inet6Address} (this
47048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * class cannot be instantiated directly). Most code does not need to distinguish between the two
48048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * families, and should use {@code InetAddress}.
494adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
504adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>An {@code InetAddress} may have a hostname (accessible via {@code getHostName}), but may not,
51048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * depending on how the {@code InetAddress} was created.
524adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
534adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>IPv4 numeric address formats</h4>
54fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes * <p>The {@code getAllByName} method accepts IPv4 addresses in the "decimal-dotted-quad" form only:
554adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <ul>
564adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li>{@code "1.2.3.4"} - 1.2.3.4
574adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </ul>
584adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
594adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>IPv6 numeric address formats</h4>
604adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>The {@code getAllByName} method accepts IPv6 addresses in the following forms (this text
614adff24306c86433ce4f771da8489a574e63318eElliott Hughes * comes from <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a>, which you should consult
624adff24306c86433ce4f771da8489a574e63318eElliott Hughes * for full details of IPv6 addressing):
634adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <ul>
644adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li><p>The preferred form is {@code x:x:x:x:x:x:x:x}, where the 'x's are the
654adff24306c86433ce4f771da8489a574e63318eElliott Hughes * hexadecimal values of the eight 16-bit pieces of the address.
664adff24306c86433ce4f771da8489a574e63318eElliott Hughes * Note that it is not necessary to write the leading zeros in an
674adff24306c86433ce4f771da8489a574e63318eElliott Hughes * individual field, but there must be at least one numeral in every
684adff24306c86433ce4f771da8489a574e63318eElliott Hughes * field (except for the case described in the next bullet).
694adff24306c86433ce4f771da8489a574e63318eElliott Hughes * Examples:
704adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
714adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
724adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080:0:0:0:8:800:200C:417A</pre>
734adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
744adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li>Due to some methods of allocating certain styles of IPv6
754adff24306c86433ce4f771da8489a574e63318eElliott Hughes * addresses, it will be common for addresses to contain long strings
764adff24306c86433ce4f771da8489a574e63318eElliott Hughes * of zero bits.  In order to make writing addresses containing zero
774adff24306c86433ce4f771da8489a574e63318eElliott Hughes * bits easier a special syntax is available to compress the zeros.
784adff24306c86433ce4f771da8489a574e63318eElliott Hughes * The use of "::" indicates multiple groups of 16-bits of zeros.
794adff24306c86433ce4f771da8489a574e63318eElliott Hughes * The "::" can only appear once in an address.  The "::" can also be
804adff24306c86433ce4f771da8489a574e63318eElliott Hughes * used to compress the leading and/or trailing zeros in an address.
814adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
824adff24306c86433ce4f771da8489a574e63318eElliott Hughes * For example the following addresses:
834adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
844adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080:0:0:0:8:800:200C:417A  a unicast address
854adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FF01:0:0:0:0:0:0:101        a multicast address
864adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:0:1             the loopback address
874adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:0:0             the unspecified addresses</pre>
884adff24306c86433ce4f771da8489a574e63318eElliott Hughes * may be represented as:
894adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
904adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     1080::8:800:200C:417A       a unicast address
914adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     FF01::101                   a multicast address
924adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::1                         the loopback address
934adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::                          the unspecified addresses</pre>
944adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
954adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <li><p>An alternative form that is sometimes more convenient when dealing
964adff24306c86433ce4f771da8489a574e63318eElliott Hughes * with a mixed environment of IPv4 and IPv6 nodes is
974adff24306c86433ce4f771da8489a574e63318eElliott Hughes * {@code x:x:x:x:x:x:d.d.d.d}, where the 'x's are the hexadecimal values of
984adff24306c86433ce4f771da8489a574e63318eElliott Hughes * the six high-order 16-bit pieces of the address, and the 'd's are
994adff24306c86433ce4f771da8489a574e63318eElliott Hughes * the decimal values of the four low-order 8-bit pieces of the
1004adff24306c86433ce4f771da8489a574e63318eElliott Hughes * address (standard IPv4 representation).  Examples:
1014adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
1024adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:0:13.1.68.3
1034adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     0:0:0:0:0:FFFF:129.144.52.38</pre>
1044adff24306c86433ce4f771da8489a574e63318eElliott Hughes * or in compressed form:
1054adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <pre>
1064adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::13.1.68.3
1074adff24306c86433ce4f771da8489a574e63318eElliott Hughes *     ::FFFF:129.144.52.38</pre>
1084adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </li>
1094adff24306c86433ce4f771da8489a574e63318eElliott Hughes * </ul>
1104adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <p>Scopes are given using a trailing {@code %} followed by the scope id, as in
1114adff24306c86433ce4f771da8489a574e63318eElliott Hughes * {@code 1080::8:800:200C:417A%2} or {@code 1080::8:800:200C:417A%en0}.
1124adff24306c86433ce4f771da8489a574e63318eElliott Hughes * See <a href="https://www.ietf.org/rfc/rfc4007.txt">RFC 4007</a> for more on IPv6's scoped
1134adff24306c86433ce4f771da8489a574e63318eElliott Hughes * address architecture.
1144adff24306c86433ce4f771da8489a574e63318eElliott Hughes *
1150d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes * <p>Additionally, for backwards compatibility, IPv6 addresses may be surrounded by square
1160d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes * brackets.
1170d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes *
1184adff24306c86433ce4f771da8489a574e63318eElliott Hughes * <h4>DNS caching</h4>
119afd70b773bd938c845a3bb0d9a3e21ec64d4db1aElliott Hughes * <p>In Android 4.0 (Ice Cream Sandwich) and earlier, DNS caching was performed both by
120afd70b773bd938c845a3bb0d9a3e21ec64d4db1aElliott Hughes * InetAddress and by the C library, which meant that DNS TTLs could not be honored correctly.
121afd70b773bd938c845a3bb0d9a3e21ec64d4db1aElliott Hughes * In later releases, caching is done solely by the C library and DNS TTLs are honored.
122f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
123048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * @see Inet4Address
124048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes * @see Inet6Address
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
126048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughespublic class InetAddress implements Serializable {
12738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    /** Our Java-side DNS cache. */
128048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes    private static final AddressCache addressCache = new AddressCache();
129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
130ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    private static final long serialVersionUID = 3286316764910316507L;
131adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private int family;
133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    byte[] ipaddress;
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    String hostName;
1374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
13932b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * Used by the DatagramSocket.disconnect implementation.
14032b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * @hide internal use only
14132b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     */
14232b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes    public static final InetAddress UNSPECIFIED = new InetAddress(AF_UNSPEC, null, null);
14332b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes
14432b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes    /**
145051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     * Constructs an {@code InetAddress}.
146051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti     *
14732b0fa4d1a31ef07bc4297c615f0fe50e9aa7c21Elliott Hughes     * Note: this constructor is for subclasses only.
148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    InetAddress(int family, byte[] ipaddress, String hostName) {
1504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.family = family;
1514f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.ipaddress = ipaddress;
1524f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        this.hostName = hostName;
1534f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
156adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares this {@code InetAddress} instance against the specified address
157adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * in {@code obj}. Two addresses are equal if their address byte arrays have
158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the same length and if the bytes in the arrays are equal.
159f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param obj
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to be tested for equality.
162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if both objects are equal, {@code false} otherwise.
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean equals(Object obj) {
166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!(obj instanceof InetAddress)) {
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
169051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti        return Arrays.equals(this.ipaddress, ((InetAddress) obj).ipaddress);
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the IP address represented by this {@code InetAddress} instance
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * as a byte array. The elements are in network order (the highest order
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address byte is in the zeroth element).
176f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the address in form of a byte array.
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public byte[] getAddress() {
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return ipaddress.clone();
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
183ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
184ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * Converts an array of byte arrays representing raw IP addresses of a host
185ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes     * to an array of InetAddress objects.
186ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     *
187ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param rawAddresses the raw addresses to convert.
188ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param hostName the hostname corresponding to the IP address.
189ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @return the corresponding InetAddresses, appropriately sorted.
190ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
1914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress[] bytesToInetAddresses(byte[][] rawAddresses, String hostName)
192fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throws UnknownHostException {
193ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        // Convert the byte arrays to InetAddresses.
194ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        InetAddress[] returnedAddresses = new InetAddress[rawAddresses.length];
195ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        for (int i = 0; i < rawAddresses.length; i++) {
196fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            returnedAddresses[i] = makeInetAddress(rawAddresses[i], hostName);
197ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        }
198ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        return returnedAddresses;
199ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    }
200ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
201adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
202adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Gets all IP addresses associated with the given {@code host} identified
203ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * by name or literal IP address. The IP address is resolved by the
204adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * configured name service. If the host name is empty or {@code null} an
205ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * {@code UnknownHostException} is thrown. If the host name is a literal IP
206adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address string an array with the corresponding single {@code InetAddress}
207adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * is returned.
208f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
209ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param host the hostname or literal IP string to be resolved.
210adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the array of addresses associated with the specified host.
211ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @throws UnknownHostException if the address lookup fails.
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
21338607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    public static InetAddress[] getAllByName(String host) throws UnknownHostException {
21438607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return getAllByNameImpl(host).clone();
215ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    }
216ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
217ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
21838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson     * Returns the InetAddresses for {@code host}. The returned array is shared
21938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson     * and must be cloned before it is returned to application code.
220ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
2214f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress[] getAllByNameImpl(String host) throws UnknownHostException {
22238607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        if (host == null || host.isEmpty()) {
22346bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes            return loopbackAddresses();
224ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        }
225ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti
2260d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes        // Is it a numeric address?
2271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress result = parseNumericAddressNoThrow(host);
2281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (result != null) {
229fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            result = disallowDeprecatedFormats(host, result);
230fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            if (result == null) {
231fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                throw new UnknownHostException("Deprecated IPv4 address format: " + host);
232fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            }
2331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            return new InetAddress[] { result };
234adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
235adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
2361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return lookupHostByName(host).clone();
23738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson    }
23838607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson
2394f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static InetAddress makeInetAddress(byte[] bytes, String hostName) throws UnknownHostException {
240fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        if (bytes.length == 4) {
2414f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return new Inet4Address(bytes, hostName);
242fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else if (bytes.length == 16) {
2434f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return new Inet6Address(bytes, hostName, 0);
244fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else {
245fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throw badAddressLength(bytes);
246fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        }
247fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    }
248fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes
249fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    private static InetAddress disallowDeprecatedFormats(String address, InetAddress inetAddress) {
250fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Only IPv4 addresses are problematic.
251fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        if (!(inetAddress instanceof Inet4Address) || address.indexOf(':') != -1) {
252fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            return inetAddress;
253fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        }
254fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // If inet_pton(3) can't parse it, it must have been a deprecated format.
255fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // We need to return inet_pton(3)'s result to ensure that numbers assumed to be octal
256fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // by getaddrinfo(3) are reinterpreted by inet_pton(3) as decimal.
257fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return Libcore.os.inet_pton(AF_INET, address);
258fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    }
259fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
2601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    private static InetAddress parseNumericAddressNoThrow(String address) {
2611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Accept IPv6 addresses (only) in square brackets for compatibility.
2621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
2631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            address = address.substring(1, address.length() - 1);
2641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
2651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        StructAddrinfo hints = new StructAddrinfo();
2661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        hints.ai_flags = AI_NUMERICHOST;
2671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress[] addresses = null;
2681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        try {
2691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            addresses = Libcore.os.getaddrinfo(address, hints);
2701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } catch (GaiException ignored) {
2711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
272fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return (addresses != null) ? addresses[0] : null;
2731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
274753dcd862b31e85766225590d90ba0b9f481176fElliott Hughes
275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the address of a host according to the given host string name
277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code host}. The host string may be either a machine name or a dotted
278adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * string IP address. If the latter, the {@code hostName} field is
279adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * determined upon demand. {@code host} can be {@code null} which means that
280adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * an address of the loopback interface is returned.
281f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param host
283adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the hostName to be resolved to an address or {@code null}.
284adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code InetAddress} instance representing the host.
285adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws UnknownHostException
286adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if the address lookup fails.
287adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
288ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    public static InetAddress getByName(String host) throws UnknownHostException {
28938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return getAllByNameImpl(host)[0];
290adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
291adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
292e8596906d75fd8ccda31600d71ad56391cd7cd3aLorenzo Colitti    /**
2934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the numeric representation of this IP address (such as "127.0.0.1").
294adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
295adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getHostAddress() {
2964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return Libcore.os.getnameinfo(this, NI_NUMERICHOST); // Can't throw.
297adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
298adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
299adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
3004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the host name corresponding to this IP address. This may or may not be a
3014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * fully-qualified name. If the IP address could not be resolved, the numeric representation
3024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * is returned instead (see {@link #getHostAddress}).
303adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
304adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getHostName() {
3054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        if (hostName == null) {
3064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            try {
3074f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes                hostName = getHostByAddrImpl(this).hostName;
3084f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            } catch (UnknownHostException ex) {
3094f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes                hostName = getHostAddress();
310adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
311adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
312adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return hostName;
313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
314adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
315adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
3164f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * Returns the fully qualified hostname corresponding to this IP address.
317adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
318adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getCanonicalHostName() {
319adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
3204f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return getHostByAddrImpl(this).hostName;
3214f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        } catch (UnknownHostException ex) {
3224f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return getHostAddress();
323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
325adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
326adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
327ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * Returns an {@code InetAddress} for the local host if possible, or the
328ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * loopback address otherwise. This method works by getting the hostname,
329ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * performing a DNS lookup, and then taking the first returned address.
330ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * For devices with multiple network interfaces and/or multiple addresses
331ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * per interface, this does not necessarily return the {@code InetAddress}
332ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * you want.
333f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
334ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>Multiple interface/address configurations were relatively rare
335ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * when this API was designed, but multiple interfaces are the default for
336ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * modern mobile devices (with separate wifi and radio interfaces), and
337ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * the need to support both IPv4 and IPv6 has made multiple addresses
338ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * commonplace. New code should thus avoid this method except where it's
339ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * basically being used to get a loopback address or equivalent.
340ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
341ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>There are two main ways to get a more specific answer:
342ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <ul>
343ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <li>If you have a connected socket, you should probably use
344ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@link Socket#getLocalAddress} instead: that will give you the address
345ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * that's actually in use for that connection. (It's not possible to ask
346ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * the question "what local address would a connection to a given remote
347ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * address use?"; you have to actually make the connection and see.)</li>
348ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <li>For other use cases, see {@link NetworkInterface}, which lets you
349ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * enumerate all available network interfaces and their addresses.</li>
350ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * </ul>
351ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
352ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * <p>Note that if the host doesn't have a hostname set&nbsp;&ndash; as
353ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * Android devices typically don't&nbsp;&ndash; this method will
354ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * effectively return the loopback address, albeit by getting the name
355ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@code localhost} and then doing a lookup to translate that to
356ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * {@code 127.0.0.1}.
357ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     *
358ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * @return an {@code InetAddress} representing the local host, or the
359ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2Elliott Hughes     * loopback address.
360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws UnknownHostException
361adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if the address lookup fails.
362adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
363adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public static InetAddress getLocalHost() throws UnknownHostException {
3649b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        String host = Libcore.os.uname().nodename;
365ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti        return lookupHostByName(host)[0];
366adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
367adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
368adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
369adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Gets the hashcode of the represented IP address.
370f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
371adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the appropriate hashcode value.
372adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
373adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
374adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int hashCode() {
375051128862ae7c5c031b8ddb763848ed264a63746Lorenzo Colitti        return Arrays.hashCode(ipaddress);
376adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
377adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
378ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti    /**
379048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes     * Resolves a hostname to its IP addresses using a cache.
380ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     *
381ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @param host the hostname to resolve.
382ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     * @return the IP addresses of the host.
383ca8f5b66c546d8545e41f71b6fb852424c681881Lorenzo Colitti     */
384048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes    private static InetAddress[] lookupHostByName(String host) throws UnknownHostException {
3857983be972905950b4a4e7d66df908f083c81ee29Brad Fitzpatrick        BlockGuard.getThreadPolicy().onNetwork();
386048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes        // Do we have a result cached?
387fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        Object cachedResult = addressCache.get(host);
388048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes        if (cachedResult != null) {
389fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            if (cachedResult instanceof InetAddress[]) {
390048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes                // A cached positive result.
391fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes                return (InetAddress[]) cachedResult;
392048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            } else {
393048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes                // A cached negative result.
394fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes                throw new UnknownHostException((String) cachedResult);
395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
396adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
3981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            StructAddrinfo hints = new StructAddrinfo();
3991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_flags = AI_ADDRCONFIG;
4001c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_family = AF_UNSPEC;
4011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // If we don't specify a socket type, every address will appear twice, once
4021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // for SOCK_STREAM and one for SOCK_DGRAM. Since we do not return the family
4031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // anyway, just pick one.
4041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            hints.ai_socktype = SOCK_STREAM;
4051c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            InetAddress[] addresses = Libcore.os.getaddrinfo(host, hints);
4061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // TODO: should getaddrinfo set the hostname of the InetAddresses it returns?
4071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            for (InetAddress address : addresses) {
4081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes                address.hostName = host;
4091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            }
410048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            addressCache.put(host, addresses);
411048303b64df9c987ae2f57b6bf88ff5ac1b5cca0Elliott Hughes            return addresses;
4121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } catch (GaiException gaiException) {
41375cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            // If the failure appears to have been a lack of INTERNET permission, throw a clear
41475cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            // SecurityException to aid in debugging this common mistake.
41575cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            // http://code.google.com/p/android/issues/detail?id=15722
41675cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            if (gaiException.getCause() instanceof ErrnoException) {
41775cf14944d476670f6f915e5efd849e238a16250Elliott Hughes                if (((ErrnoException) gaiException.getCause()).errno == EACCES) {
41875cf14944d476670f6f915e5efd849e238a16250Elliott Hughes                    throw new SecurityException("Permission denied (missing INTERNET permission?)", gaiException);
41975cf14944d476670f6f915e5efd849e238a16250Elliott Hughes                }
42075cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            }
42175cf14944d476670f6f915e5efd849e238a16250Elliott Hughes            // Otherwise, throw an UnknownHostException.
4221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            String detailMessage = "Unable to resolve host \"" + host + "\": " + Libcore.os.gai_strerror(gaiException.error);
423fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            addressCache.putUnknownHost(host, detailMessage);
4241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            throw gaiException.rethrowAsUnknownHostException(detailMessage);
425adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
426adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
427adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
428adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
429b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * Removes all entries from the VM's DNS cache. This does not affect the C library's DNS
430b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * cache, nor any caching DNS servers between you and the canonical server.
431b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     * @hide
432b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes     */
433b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes    public static void clearDnsCache() {
434b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes        addressCache.clear();
435b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes    }
436b744a7edf23c14216698ad69ea59151e07cc50b8Elliott Hughes
4374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    private static InetAddress getHostByAddrImpl(InetAddress address) throws UnknownHostException {
4387983be972905950b4a4e7d66df908f083c81ee29Brad Fitzpatrick        BlockGuard.getThreadPolicy().onNetwork();
4394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        try {
4404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            String hostname = Libcore.os.getnameinfo(address, NI_NAMEREQD);
4414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            return makeInetAddress(address.ipaddress.clone(), hostname);
4424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        } catch (GaiException gaiException) {
4434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            throw gaiException.rethrowAsUnknownHostException();
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
447adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
44810914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes     * Returns a string containing the host name (if available) and host address.
44910914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes     * For example: {@code "www.google.com/74.125.224.115"} or {@code "/127.0.0.1"}.
450f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
45110914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes     * <p>IPv6 addresses may additionally include an interface name or scope id.
45210914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes     * For example: {@code "www.google.com/2001:4860:4001:803::1013%eth0"} or
45310914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes     * {@code "/2001:4860:4001:803::1013%2"}.
454adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
45510914811ea02b4acc7ab1dc5a0ada1b54cdf2203Elliott Hughes    @Override public String toString() {
45638607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        return (hostName == null ? "" : hostName) + "/" + getHostAddress();
457adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
458adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
459adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
4600d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * Returns true if the string is a valid numeric IPv4 or IPv6 address (such as "192.168.0.1").
4610d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * This copes with all forms of address that Java supports, detailed in the {@link InetAddress}
4620d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     * class documentation.
4630d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes     *
464f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @hide used by frameworks/base to ensure that a getAllByName won't cause a DNS lookup.
465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
4660d93c38cc3c7a5001aece8a18cafc6d1fc7551f3Elliott Hughes    public static boolean isNumeric(String address) {
467fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        InetAddress inetAddress = parseNumericAddressNoThrow(address);
468fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        return inetAddress != null && disallowDeprecatedFormats(address, inetAddress) != null;
469adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
470adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
472f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * Returns an InetAddress corresponding to the given numeric address (such
473f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}).
474f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * This method will never do a DNS lookup. Non-numeric addresses are errors.
475f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     *
476f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @hide used by frameworks/base's NetworkUtils.numericToInetAddress
477f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     * @throws IllegalArgumentException if {@code numericAddress} is not a numeric address
478f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes     */
479f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    public static InetAddress parseNumericAddress(String numericAddress) {
48046bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        if (numericAddress == null || numericAddress.isEmpty()) {
481ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes            return Inet6Address.LOOPBACK;
48246bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        }
4831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        InetAddress result = parseNumericAddressNoThrow(numericAddress);
484fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        result = disallowDeprecatedFormats(numericAddress, result);
4851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (result == null) {
486f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes            throw new IllegalArgumentException("Not a numeric address: " + numericAddress);
487f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes        }
4881c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return result;
489f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    }
490f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes
49146bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes    private static InetAddress[] loopbackAddresses() {
492ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes        return new InetAddress[] { Inet6Address.LOOPBACK, Inet4Address.LOOPBACK };
49346bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes    }
49446bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes
495f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    /**
4965d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     * Returns the IPv6 loopback address {@code ::1} or the IPv4 loopback address {@code 127.0.0.1}.
4975d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     * @since 1.7
4985d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes     */
4995d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    public static InetAddress getLoopbackAddress() {
500ae394a866dd86df8819b652dfe00b3d2c7ee204cElliott Hughes        return Inet6Address.LOOPBACK;
5015d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    }
5025d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes
5035d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    /**
504a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this is the IPv6 unspecified wildcard address {@code ::}
505a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * or the IPv4 "any" address, {@code 0.0.0.0}.
506adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
507a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isAnyLocalAddress() {
508adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
510adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
511adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
512a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a link-local address or not.
513a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
51492cb29d71ca8fadc9b738a33e63ca39807647463Elliott Hughes     * <p>Valid IPv6 link-local addresses have the prefix {@code fe80::/10}.
515a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
516a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p><a href="http://www.ietf.org/rfc/rfc3484.txt">RFC 3484</a>
517a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * "Default Address Selection for Internet Protocol Version 6 (IPv6)" states
518a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * that both IPv4 auto-configuration addresses (prefix {@code 169.254/16}) and
519a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * IPv4 loopback addresses (prefix {@code 127/8}) have link-local scope, but
520a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * {@link Inet4Address} only considers the auto-configuration addresses
521a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * to have link-local scope. That is: the IPv4 loopback address returns false.
522adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
523adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isLinkLocalAddress() {
524adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
525adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
526adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
527adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
528a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a loopback address or not.
529f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
530a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 loopback addresses have the prefix {@code 127/8}.
531a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
532a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>The only valid IPv6 loopback address is {@code ::1}.
533adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
534a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isLoopbackAddress() {
535adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
537adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
538adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
539a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a global multicast address or not.
540a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
541a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 global multicast addresses have the prefix {@code ffxe::/16},
542a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * where {@code x} is a set of flags and the additional 112 bits make
543a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * up the global multicast address space.
544f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
545a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 global multicast addresses are the range of addresses
546a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * from {@code 224.0.1.0} to {@code 238.255.255.255}.
547adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
548adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCGlobal() {
549adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
550adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
551adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
552adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
553a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a link-local multicast address or not.
554a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
555a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 link-local multicast addresses have the prefix {@code ffx2::/16},
556a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * where x is a set of flags and the additional 112 bits make up the link-local multicast
557a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * address space.
558a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
559a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 link-local multicast addresses have the prefix {@code 224.0.0/24}.
560a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     */
561a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isMCLinkLocal() {
562a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        return false;
563a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    }
564a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
565a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    /**
566adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a node-local multicast address or not.
567a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
568a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 node-local multicast addresses have the prefix {@code ffx1::/16},
569a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * where x is a set of flags and the additional 112 bits make up the link-local multicast
570adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
571f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
572a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>There are no valid IPv4 node-local multicast addresses.
573adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
574adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCNodeLocal() {
575adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
576adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
577adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
578adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
579a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a organization-local multicast address or not.
580a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
581a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 organization-local multicast addresses have the prefix {@code ffx8::/16},
582a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * where x is a set of flags and the additional 112 bits make up the link-local multicast
583adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
584f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
585a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 organization-local multicast addresses have the prefix {@code 239.192/14}.
586adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
587a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isMCOrgLocal() {
588adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
589adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
590adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
591adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
592adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns whether this address is a site-local multicast address or not.
593a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
594a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 site-local multicast addresses have the prefix {@code ffx5::/16},
595a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * where x is a set of flags and the additional 112 bits make up the link-local multicast
596adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * address space.
597f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
598a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 site-local multicast addresses have the prefix {@code 239.255/16}.
599adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
600adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isMCSiteLocal() {
601adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
602adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
603adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
604adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
605a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a multicast address or not.
606a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
607a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv6 multicast addresses have the prefix {@code ff::/8}.
608a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
609a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>Valid IPv4 multicast addresses have the prefix {@code 224/4}.
610adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
611a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isMulticastAddress() {
612adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
613adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
614adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
615adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
616a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * Returns whether this address is a site-local address or not.
617f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
618a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p>For the purposes of this method, valid IPv6 site-local addresses have
619a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * the deprecated prefix {@code fec0::/10} from
620a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <a href="http://www.ietf.org/rfc/rfc1884.txt">RFC 1884</a>,
621a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <i>not</i> the modern prefix {@code fc00::/7} from
622a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <a href="http://www.ietf.org/rfc/rfc4193.txt">RFC 4193</a>.
623a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
624a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * <p><a href="http://www.ietf.org/rfc/rfc3484.txt">RFC 3484</a>
625a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * "Default Address Selection for Internet Protocol Version 6 (IPv6)" states
626a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * that IPv4 private addresses have the prefix {@code 10/8}, {@code 172.16/12},
627a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * or {@code 192.168/16}.
628a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     *
629a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes     * @return {@code true} if this instance represents a site-local address,
630adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
631adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
632a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public boolean isSiteLocalAddress() {
633adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return false;
634adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
635adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
636adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
637adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Tries to reach this {@code InetAddress}. This method first tries to use
6389b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes     * ICMP <i>(ICMP ECHO REQUEST)</i>, falling back to a TCP connection
6399b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes     * on port 7 (Echo) of the remote host.
640f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
641adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param timeout
642adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            timeout in milliseconds before the test fails if no connection
643adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            could be established.
644adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this address is reachable, {@code false}
645adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         otherwise.
646adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
647adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if an error occurs during an I/O operation.
648adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IllegalArgumentException
649adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if timeout is less than zero.
650adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
651adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isReachable(int timeout) throws IOException {
652adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return isReachable(null, 0, timeout);
653adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
654adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
655adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
656adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Tries to reach this {@code InetAddress}. This method first tries to use
6579b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes     * ICMP <i>(ICMP ECHO REQUEST)</i>, falling back to a TCP connection
6589b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes     * on port 7 (Echo) of the remote host.
659f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
660d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes     * @param networkInterface
661adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the network interface on which to connection should be
662adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            established.
663adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param ttl
664adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the maximum count of hops (time-to-live).
665adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param timeout
666adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            timeout in milliseconds before the test fails if no connection
667adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            could be established.
668adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this address is reachable, {@code false}
669adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         otherwise.
670adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
671adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if an error occurs during an I/O operation.
672adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IllegalArgumentException
673adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if ttl or timeout is less than zero.
674adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
6759b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes    public boolean isReachable(NetworkInterface networkInterface, final int ttl, final int timeout) throws IOException {
676d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        if (ttl < 0 || timeout < 0) {
67738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            throw new IllegalArgumentException("ttl < 0 || timeout < 0");
678adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
6799b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes
6809b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        // The simple case.
681d2af45a6fd008ceb958ac74e5a50e582b8419e9cElliott Hughes        if (networkInterface == null) {
6829b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes            return isReachable(this, null, timeout);
683adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
684adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
6859b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        // Try each NetworkInterface in parallel.
6869b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        // Use a thread pool Executor?
6879b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        List<InetAddress> sourceAddresses = Collections.list(networkInterface.getInetAddresses());
6889b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        if (sourceAddresses.isEmpty()) {
689adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
690adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
6919b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        final InetAddress destinationAddress = this;
6929b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        final CountDownLatch latch = new CountDownLatch(sourceAddresses.size());
6939b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        final AtomicBoolean isReachable = new AtomicBoolean(false);
6949b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        for (final InetAddress sourceAddress : sourceAddresses) {
695adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            new Thread() {
69638607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                @Override public void run() {
69738607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson                    try {
6989b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                        if (isReachable(destinationAddress, sourceAddress, timeout)) {
6999b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                            isReachable.set(true);
7009b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                            // Wake the main thread so it can return success without
7019b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                            // waiting for any other threads to time out.
7029b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                            while (latch.getCount() > 0) {
7039b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                                latch.countDown();
704adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                            }
705adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                        }
7069b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                    } catch (IOException ignored) {
707adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    }
7089b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes                    latch.countDown();
709adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
710adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }.start();
711adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
7129b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        try {
7139b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes            latch.await();
7149b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        } catch (InterruptedException ignored) {
7159b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes            Thread.currentThread().interrupt(); // Leave the interrupted bit set.
716adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
7179b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        return isReachable.get();
718adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
719adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
7209b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes    private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException {
7219b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        // TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106)
7220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes        FileDescriptor fd = IoBridge.socket(true);
723adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        boolean reached = false;
724adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
725b46dab348e2007bc08abaf7ecae34d89a2474e50Elliott Hughes            if (source != null) {
7260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes                IoBridge.bind(fd, source, 0);
727adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
7280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes            IoBridge.connect(fd, destination, 7, timeout);
729adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            reached = true;
730adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } catch (IOException e) {
731ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes            if (e.getCause() instanceof ErrnoException) {
732ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes                // "Connection refused" means the IP address was reachable.
733ad5d31103472d57f5744ff5a03c3bc38dcb34740Elliott Hughes                reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
734adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
735adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
736adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
7370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes        IoBridge.closeSocket(fd);
738adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
739adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return reached;
740adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
741adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
742adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
7434728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes     * Equivalent to {@code getByAddress(null, ipAddress)}. Handy for addresses with
7444f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * no associated hostname.
745adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
7464f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    public static InetAddress getByAddress(byte[] ipAddress) throws UnknownHostException {
7474728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes        return getByAddress(null, ipAddress, 0);
748adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
749adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
750adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
7514f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * Returns an {@code InetAddress} corresponding to the given network-order
7524f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * bytes {@code ipAddress} and {@code scopeId}.
753f5597e626ecf7949d249dea08c1a2964d890ec11Jesse Wilson     *
7544728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes     * <p>For an IPv4 address, the byte array must be of length 4.
7554f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * For IPv6, the byte array must be of length 16. Any other length will cause an {@code
7564f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * UnknownHostException}.
7574f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
7584f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * <p>No reverse lookup is performed. The given {@code hostName} (which may be null) is
7594f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * associated with the new {@code InetAddress} with no validation done.
7604f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
7614f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * <p>(Note that numeric addresses such as {@code "127.0.0.1"} are names for the
7624f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * purposes of this API. Most callers probably want {@link #getAllByName} instead.)
7634f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     *
7644f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes     * @throws UnknownHostException if {@code ipAddress} is null or the wrong length.
765adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
7664728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    public static InetAddress getByAddress(String hostName, byte[] ipAddress) throws UnknownHostException {
7674728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes        return getByAddress(hostName, ipAddress, 0);
7684728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    }
7694728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes
7704728a015bcd3f432e000d1547c668e804015dc04Elliott Hughes    private static InetAddress getByAddress(String hostName, byte[] ipAddress, int scopeId) throws UnknownHostException {
77155b49708e6a5f5f5047513b67094257b32ff9e93Lorenzo Colitti        if (ipAddress == null) {
77238607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            throw new UnknownHostException("ipAddress == null");
773adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
774fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        if (ipAddress.length == 4) {
7751f0c4ff5de23e466032f1810172f16ad0077fa65Jesse Wilson            return new Inet4Address(ipAddress.clone(), hostName);
776fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else if (ipAddress.length == 16) {
777fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // First check to see if the address is an IPv6-mapped
778fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // IPv4 address. If it is, then we can make it a IPv4
779fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            // address, otherwise, we'll create an IPv6 address.
780fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            if (isIPv4MappedAddress(ipAddress)) {
7811f0c4ff5de23e466032f1810172f16ad0077fa65Jesse Wilson                return new Inet4Address(ipv4MappedToIPv4(ipAddress), hostName);
782fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            } else {
7834f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes                return new Inet6Address(ipAddress.clone(), hostName, scopeId);
784fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            }
785fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        } else {
786fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes            throw badAddressLength(ipAddress);
787adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
788adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
789adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
790fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    private static UnknownHostException badAddressLength(byte[] bytes) throws UnknownHostException {
791fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes        throw new UnknownHostException("Address is neither 4 or 16 bytes: " + Arrays.toString(bytes));
792fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes    }
793fbbae9740d65620b417b85576aa0d6c7daf4ba34Elliott Hughes
7944f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static boolean isIPv4MappedAddress(byte[] ipAddress) {
7954f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // Check if the address matches ::FFFF:d.d.d.d
7964f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // The first 10 bytes are 0. The next to are -1 (FF).
7974f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        // The last 4 bytes are varied.
7984f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        if (ipAddress == null || ipAddress.length != 16) {
7994f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return false;
8004f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8014f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        for (int i = 0; i < 10; i++) {
8024f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            if (ipAddress[i] != 0) {
8034f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes                return false;
8044f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            }
8054f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8064f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        if (ipAddress[10] != -1 || ipAddress[11] != -1) {
8074f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            return false;
8084f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8094f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        return true;
8104f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    }
8114f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes
8124f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    private static byte[] ipv4MappedToIPv4(byte[] mappedAddress) {
8134f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        byte[] ipv4Address = new byte[4];
8140d4daefcf389b6433a0af481ef44a84a2546541aElliott Hughes        for (int i = 0; i < 4; i++) {
8154f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes            ipv4Address[i] = mappedAddress[12 + i];
8164f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        }
8174f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes        return ipv4Address;
8184f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes    }
8194f9ffffd8a2835c30647f9785afb48fa96a0f045Elliott Hughes
820adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private static final ObjectStreamField[] serialPersistentFields = {
821e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes        new ObjectStreamField("address", int.class),
822e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes        new ObjectStreamField("family", int.class),
82328eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("hostName", String.class),
82428eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes    };
825adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
826adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private void writeObject(ObjectOutputStream stream) throws IOException {
827adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ObjectOutputStream.PutField fields = stream.putFields();
828adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (ipaddress == null) {
82938607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson            fields.put("address", 0);
830adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } else {
831f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes            fields.put("address", Memory.peekInt(ipaddress, 0, ByteOrder.BIG_ENDIAN));
832adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
83338607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        fields.put("family", family);
83438607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        fields.put("hostName", hostName);
835adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
836adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        stream.writeFields();
837adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
838adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
8390eb70e31581a977afa5df3292d1c96e42e548821Elliott Hughes    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
840adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ObjectInputStream.GetField fields = stream.readFields();
84138607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        int addr = fields.get("address", 0);
842adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ipaddress = new byte[4];
843f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        Memory.pokeInt(ipaddress, 0, addr, ByteOrder.BIG_ENDIAN);
84438607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        hostName = (String) fields.get("hostName", null);
84538607710cdc82cb1a0e81c2fc5c78278b435e4fcJesse Wilson        family = fields.get("family", 2);
846adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
847adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
8485839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson    /*
8495839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson     * The spec requires that if we encounter a generic InetAddress in
8504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes     * serialized form then we should interpret it as an Inet4Address.
8515839b909d9528b7726e678a4b696ed37df15d897Jesse Wilson     */
852adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private Object readResolve() throws ObjectStreamException {
853adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return new Inet4Address(ipaddress, hostName);
854adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
855adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
856