jdk/src/share/classes/java/net/InetAddress.java
author chegar
Fri, 21 Jan 2011 17:02:57 +0000
changeset 7995 8168047d48c2
parent 7668 d4a77089c587
child 9035 1255eb81cc2f
permissions -rw-r--r--
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi Reviewed-by: alanb, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6670
diff changeset
     2
 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.LinkedHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
    38
import java.io.ObjectInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.net.InetAddressCachePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.net.spi.nameservice.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class represents an Internet Protocol (IP) address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p> An IP address is either a 32-bit or 128-bit unsigned number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * used by IP, a lower-level protocol on which protocols like UDP and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * TCP are built. The IP address architecture is defined by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC&nbsp;790:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Assigned Numbers</i></a>, <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC&nbsp;1918:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Address Allocation for Private Internets</i></a>, <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Administratively Scoped IP Multicast</i></a>, and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Version 6 Addressing Architecture</i></a>. An instance of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * InetAddress consists of an IP address and possibly its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * corresponding host name (depending on whether it is constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * with a host name or whether it has already done reverse host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * resolution).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <h4> Address types </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <blockquote><table cellspacing=2 summary="Description of unicast and multicast address types">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   <tr><th valign=top><i>unicast</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       <td>An identifier for a single interface. A packet sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *         a unicast address is delivered to the interface identified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         that address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         <p> The Unspecified Address -- Also called anylocal or wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         address. It must never be assigned to any node. It indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         absence of an address. One example of its use is as the target of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         bind, which allows a server to accept a client connection on any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *         interface, in case the server host has multiple interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *         <p> The <i>unspecified</i> address must not be used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *         the destination address of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         <p> The <i>Loopback</i> Addresses -- This is the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *         assigned to the loopback interface. Anything sent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         IP address loops around and becomes IP input on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *         host. This address is often used when testing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         client.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   <tr><th valign=top><i>multicast</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *       <td>An identifier for a set of interfaces (typically belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         to different nodes). A packet sent to a multicast address is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         delivered to all interfaces identified by that address.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <h4> IP address scope </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> <i>Link-local</i> addresses are designed to be used for addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * on a single link for purposes such as auto-address configuration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * neighbor discovery, or when no routers are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p> <i>Site-local</i> addresses are designed to be used for addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * inside of a site without the need for a global prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p> <i>Global</i> addresses are unique across the internet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <h4> Textual representation of IP addresses </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * The textual representation of an IP address is address family specific.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * For IPv4 address format, please refer to <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * HREF="Inet4Address.html#format">Inet4Address#format</A>; For IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * address format, please refer to <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * HREF="Inet6Address.html#format">Inet6Address#format</A>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <P>There is a <a href="doc-files/net-properties.html#Ipv4IPv6">couple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * System Properties</a> affecting how IPv4 and IPv6 addresses are used.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <h4> Host Name Resolution </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Host name-to-IP address <i>resolution</i> is accomplished through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * the use of a combination of local machine configuration information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * and network naming services such as the Domain Name System (DNS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * and Network Information Service(NIS). The particular naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * services(s) being used is by default the local machine configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * one. For any host name, its corresponding IP address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p> <i>Reverse name resolution</i> means that for any IP address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * the host associated with the IP address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p> The InetAddress class provides methods to resolve host names to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * their IP addresses and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <h4> InetAddress Caching </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * The InetAddress class has a cache to store successful as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * unsuccessful host name resolutions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p> By default, when a security manager is installed, in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * protect against DNS spoofing attacks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * the result of positive host name resolutions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * cached forever. When a security manager is not installed, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * behavior is to cache entries for a finite (implementation dependent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * period of time. The result of unsuccessful host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * name resolution is cached for a very short period of time (10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * seconds) to improve performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <p> If the default behavior is not desired, then a Java security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * can be set to a different Time-to-live (TTL) value for positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * caching. Likewise, a system admin can configure a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * negative caching TTL value when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <p> Two Java security properties control the TTL values used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *  positive and negative host name resolution caching:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <dt><b>networkaddress.cache.ttl</b></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <dd>Indicates the caching policy for successful name lookups from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * the name service. The value is specified as as integer to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * the number of seconds to cache the successful lookup. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * setting is to cache for an implementation specific period of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * A value of -1 indicates "cache forever".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <dt><b>networkaddress.cache.negative.ttl</b> (default: 10)</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <dd>Indicates the caching policy for un-successful name lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * from the name service. The value is specified as as integer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * indicate the number of seconds to cache the failure for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * un-successful lookups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * A value of 0 indicates "never cache".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * A value of -1 indicates "cache forever".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * @author  Chris Warth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * @see     java.net.InetAddress#getByAddress(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * @see     java.net.InetAddress#getByAddress(java.lang.String, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * @see     java.net.InetAddress#getAllByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * @see     java.net.InetAddress#getByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * @see     java.net.InetAddress#getLocalHost()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
class InetAddress implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Specify the address family: Internet Protocol, Version 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static final int IPv4 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Specify the address family: Internet Protocol, Version 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static final int IPv6 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* Specify address family preference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    static transient boolean preferIPv6Address = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    String hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Holds a 32-bit IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    int address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Specifies the address family type, for instance, '1' for IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * addresses, and '2' for IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    int family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* Used to store the name service provider */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static List<NameService> nameServices = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /* Used to store the best available hostname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private transient String canonicalHostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private static final long serialVersionUID = 3286316764910316507L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Load net library into runtime, and perform initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        preferIPv6Address = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        AccessController.doPrivileged(new LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Constructor for the Socket.accept() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * This creates an empty InetAddress, which is filled in by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the accept() method.  This InetAddress, however, is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * put in the address cache, since it is not created by name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    InetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Replaces the de-serialized object with an Inet4Address object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the alternate object to the de-serialized object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // will replace the deserialized 'this' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return new Inet4Address(this.hostName, this.address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * IP multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * an IP multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean isMulticastAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Utility routine to check if the InetAddress in a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return a <code>boolean</code> indicating if the Inetaddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public boolean isAnyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * a link local address; or false if address is not a link local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * a site local address; or false if address is not a site local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *         of global scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Test whether that address is reachable. Best effort is made by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * implementation to try to reach the host, but firewalls and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * configuration may block requests resulting in a unreachable status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * while some specific ports may be accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * A typical implementation will use ICMP ECHO REQUESTs if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * privilege can be obtained, otherwise it will try to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * a TCP connection on port 7 (Echo) of the destination host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The timeout value, in milliseconds, indicates the maximum amount of time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * the try should take. If the operation times out before getting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * answer, the host is deemed unreachable. A negative value will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * in an IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param   timeout the time, in milliseconds, before the call aborts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return a <code>boolean</code> indicating if the address is reachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @throws IOException if a network error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws  IllegalArgumentException if <code>timeout</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public boolean isReachable(int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return isReachable(null, 0 , timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Test whether that address is reachable. Best effort is made by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * implementation to try to reach the host, but firewalls and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * configuration may block requests resulting in a unreachable status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * while some specific ports may be accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * A typical implementation will use ICMP ECHO REQUESTs if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * privilege can be obtained, otherwise it will try to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * a TCP connection on port 7 (Echo) of the destination host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * The <code>network interface</code> and <code>ttl</code> parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * let the caller specify which network interface the test will go through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * and the maximum number of hops the packets should go through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * A negative value for the <code>ttl</code> will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * The timeout value, in milliseconds, indicates the maximum amount of time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * the try should take. If the operation times out before getting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * answer, the host is deemed unreachable. A negative value will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * in an IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param   netif   the NetworkInterface through which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *                    test will be done, or null for any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param   ttl     the maximum numbers of hops to try or 0 for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *                  default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param   timeout the time, in milliseconds, before the call aborts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @throws  IllegalArgumentException if either <code>timeout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *                          or <code>ttl</code> are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return a <code>boolean</code>indicating if the address is reachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @throws IOException if a network error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public boolean isReachable(NetworkInterface netif, int ttl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                               int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (ttl < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw new IllegalArgumentException("ttl can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return impl.isReachable(this, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Gets the host name for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <p>If this InetAddress was created with a host name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * this host name will be remembered and returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * otherwise, a reverse name lookup will be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * and the result will be returned based on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * configured name lookup service. If a lookup of the name service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * is required, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * {@link #getCanonicalHostName() getCanonicalHostName}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <code>checkConnect</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * with the hostname and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @see InetAddress#getCanonicalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public String getHostName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return getHostName(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns the hostname for this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * If the host is equal to null, then this address refers to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * of the local machine's available network addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * this is package private so SocketPermission can make calls into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * here without a security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * calls its <code>checkConnect</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * with the hostname and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param check make security check if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    String getHostName(boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (hostName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            hostName = InetAddress.getHostFromNameService(this, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Gets the fully qualified domain name for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Best effort method, meaning we may not be able to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * the FQDN depending on the underlying system configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * calls its <code>checkConnect</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * with the hostname and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return  the fully qualified domain name for this IP address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *    or if the operation is not allowed by the security check,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *    the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public String getCanonicalHostName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (canonicalHostName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            canonicalHostName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                InetAddress.getHostFromNameService(this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return canonicalHostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Returns the hostname for this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * calls its <code>checkConnect</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * with the hostname and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param check make security check if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private static String getHostFromNameService(InetAddress addr, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        String host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        for (NameService nameService : nameServices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // first lookup the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                host = nameService.getHostByAddr(addr.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                /* check to see if calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                 * the hostname for this IP address, ie, connect to the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        sec.checkConnect(host, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                /* now get all the IP addresses for this hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                 * and make sure one of them matches the original IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                 * address. We do this to try and prevent spoofing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                InetAddress[] arr = InetAddress.getAllByName0(host, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if(arr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    for(int i = 0; !ok && i < arr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        ok = addr.equals(arr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                //XXX: if it looks a spoof just return the address?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                // let next provider resolve the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Returns the raw IP address of this <code>InetAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * object. The result is in network byte order: the highest order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * byte of the address is in <code>getAddress()[0]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public byte[] getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns the IP address string in textual presentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @return  the raw IP address in a string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @since   JDK1.0.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * not <code>null</code> and it represents the same IP address as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Two instances of <code>InetAddress</code> represent the same IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * address if the length of the byte arrays returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <code>getAddress</code> is the same for both, and each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Converts this IP address to a <code>String</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * string returned is of the form: hostname / literal IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * If the host name is unresolved, no reverse name service lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * is performed. The hostname part will be represented by an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return  a string representation of this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return ((hostName != null) ? hostName : "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            + "/" + getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Cached addresses - our own litle nis, not!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private static Cache addressCache = new Cache(Cache.Type.Positive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private static Cache negativeCache = new Cache(Cache.Type.Negative);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private static boolean addressCacheInit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    static InetAddress[]    unknown_array; // put THIS in cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    static InetAddressImpl  impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
   680
    private static final HashMap<String, Void> lookupTable = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Represents a cache entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    static final class CacheEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   687
        CacheEntry(InetAddress[] addresses, long expiration) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   688
            this.addresses = addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            this.expiration = expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   692
        InetAddress[] addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        long expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * A cache that manages entries based on a policy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * at creation time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    static final class Cache {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   701
        private LinkedHashMap<String, CacheEntry> cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        private Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        enum Type {Positive, Negative};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * Create cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        public Cache(Type type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            this.type = type;
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   711
            cache = new LinkedHashMap<String, CacheEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        private int getPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (type == Type.Positive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                return InetAddressCachePolicy.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                return InetAddressCachePolicy.getNegative();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * Add an entry to the cache. If there's already an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * entry then for this host then the entry will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   727
        public Cache put(String host, InetAddress[] addresses) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            int policy = getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            if (policy == InetAddressCachePolicy.NEVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // purge any expired entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if (policy != InetAddressCachePolicy.FOREVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                // As we iterate in insertion order we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // terminate when a non-expired entry is found.
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
   739
                LinkedList<String> expired = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                long now = System.currentTimeMillis();
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   741
                for (String key : cache.keySet()) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   742
                    CacheEntry entry = cache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    if (entry.expiration >= 0 && entry.expiration < now) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        expired.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   751
                for (String key : expired) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   752
                    cache.remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // create new entry and add it to the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            // -- as a HashMap replaces existing entries we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            //    don't need to explicitly check if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            //    already an entry for this host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            long expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            if (policy == InetAddressCachePolicy.FOREVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                expiration = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                expiration = System.currentTimeMillis() + (policy * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   766
            CacheEntry entry = new CacheEntry(addresses, expiration);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            cache.put(host, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
         * Query the cache for the specific host. If found then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         * return its CacheEntry, or null if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        public CacheEntry get(String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            int policy = getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (policy == InetAddressCachePolicy.NEVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   780
            CacheEntry entry = cache.get(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            // check if entry has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (entry != null && policy != InetAddressCachePolicy.FOREVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (entry.expiration >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    entry.expiration < System.currentTimeMillis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    cache.remove(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Initialize cache and insert anyLocalAddress into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * unknown array with no expiry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    private static void cacheInitIfNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        assert Thread.holdsLock(addressCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (addressCacheInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        unknown_array = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        unknown_array[0] = impl.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        addressCache.put(impl.anyLocalAddress().getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                         unknown_array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        addressCacheInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /*
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   814
     * Cache the given hostname and addresses.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   816
    private static void cacheAddresses(String hostname,
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   817
                                       InetAddress[] addresses,
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   818
                                       boolean success) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        hostname = hostname.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        synchronized (addressCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            cacheInitIfNeeded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (success) {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   823
                addressCache.put(hostname, addresses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } else {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   825
                negativeCache.put(hostname, addresses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Lookup hostname in cache (positive & negative cache). If
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   832
     * found return addresses, null if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   834
    private static InetAddress[] getCachedAddresses(String hostname) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        hostname = hostname.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // search both positive & negative caches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        synchronized (addressCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            cacheInitIfNeeded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   842
            CacheEntry entry = addressCache.get(hostname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                entry = negativeCache.get(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if (entry != null) {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   848
                return entry.addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    private static NameService createNSProvider(String provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        NameService nameService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (provider.equals("default")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            // initialize the default name service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            nameService = new NameService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                public InetAddress[] lookupAllHostAddr(String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    return impl.lookupAllHostAddr(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                public String getHostByAddr(byte[] addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    return impl.getHostByAddr(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            final String providerName = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                nameService = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    new java.security.PrivilegedExceptionAction<NameService>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        public NameService run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                            Iterator itr = Service.providers(NameServiceDescriptor.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                            while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                NameServiceDescriptor nsd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                    = (NameServiceDescriptor)itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                if (providerName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                    equalsIgnoreCase(nsd.getType()+","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                        +nsd.getProviderName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                        return nsd.createNameService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                        System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                                            "Cannot create name service:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                             +providerName+": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return nameService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // create the impl
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   910
        impl = InetAddressImplFactory.create();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // get name service if provided and requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        String provider = null;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        String propPrefix = "sun.net.spi.nameservice.provider.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        nameServices = new ArrayList<NameService>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        provider = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                new GetPropertyAction(propPrefix + n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        while (provider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            NameService ns = createNSProvider(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            if (ns != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                nameServices.add(ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            provider = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    new GetPropertyAction(propPrefix + n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // if not designate any name services provider,
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   930
        // create a default one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if (nameServices.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            NameService ns = createNSProvider("default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            nameServices.add(ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   938
     * Creates an InetAddress based on the provided host name and IP address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * No name service is checked for the validity of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <p> The host name can either be a machine name, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * "<code>java.sun.com</code>", or a textual representation of its IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * <p> No validity checking is done on the host name either.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <p> If addr specifies an IPv4 address an instance of Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * will be returned; otherwise, an instance of Inet6Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * must be 16 bytes long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @return  an InetAddress object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @exception  UnknownHostException  if IP address is of illegal length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    public static InetAddress getByAddress(String host, byte[] addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (addr.length == Inet4Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                return new Inet4Address(host, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            } else if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                byte[] newAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    = IPAddressUtil.convertFromIPv4MappedAddress(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                if (newAddr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    return new Inet4Address(host, newAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    return new Inet6Address(host, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Determines the IP address of a host, given the host's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * <p> The host name can either be a machine name, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * "<code>java.sun.com</code>", or a textual representation of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * IP address. If a literal IP address is supplied, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * validity of the address format is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * <p> For <code>host</code> specified in literal IPv6 address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * either the form defined in RFC 2732 or the literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * <p> If the host is <tt>null</tt> then an <tt>InetAddress</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * representing an address of the loopback interface is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * section&nbsp;2.5.3. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @param      host   the specified host, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @return     an IP address for the given host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @exception  UnknownHostException  if no IP address for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *               <code>host</code> could be found, or if a scope_id was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *               for a global IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @exception  SecurityException if a security manager exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *             and its checkConnect method doesn't allow the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public static InetAddress getByName(String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        return InetAddress.getAllByName(host)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * Given the name of a host, returns an array of its IP addresses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * based on the configured name service on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <p> The host name can either be a machine name, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * "<code>java.sun.com</code>", or a textual representation of its IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * address. If a literal IP address is supplied, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * validity of the address format is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * <p> For <code>host</code> specified in <i>literal IPv6 address</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * either the form defined in RFC 2732 or the literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * format defined in RFC 2373 is accepted. A literal IPv6 address may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * also be qualified by appending a scoped zone identifier or scope_id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * The syntax and usage of scope_ids is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * <a href="Inet6Address.html#scoped">here</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * <p> If the host is <tt>null</tt> then an <tt>InetAddress</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * representing an address of the loopback interface is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * section&nbsp;2.5.3. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * <p> If there is a security manager and <code>host</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * null and <code>host.length() </code> is not equal to zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * with the hostname and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @param      host   the name of the host, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @return     an array of all the IP addresses for a given host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @exception  UnknownHostException  if no IP address for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *               <code>host</code> could be found, or if a scope_id was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *               for a global IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *               <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    public static InetAddress[] getAllByName(String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (host == null || host.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            InetAddress[] ret = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            ret[0] = impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        boolean ipv6Expected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (host.charAt(0) == '[') {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1066
            // This is supposed to be an IPv6 literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            if (host.length() > 2 && host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                ipv6Expected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                // This was supposed to be a IPv6 address, but it's not!
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1072
                throw new UnknownHostException(host + ": invalid IPv6 address");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        // if host is an IP address, we won't do further lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (Character.digit(host.charAt(0), 16) != -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            || (host.charAt(0) == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            byte[] addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            int numericZone = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            String ifname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // see if it is IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            addr = IPAddressUtil.textToNumericFormatV4(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                // see if it is IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                // Check if a numeric or string zone id is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                if ((pos=host.indexOf ("%")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    numericZone = checkNumericZone (host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    if (numericZone == -1) { /* remainder of string must be an ifname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        ifname = host.substring (pos+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                addr = IPAddressUtil.textToNumericFormatV6(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            } else if (ipv6Expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                // Means an IPv4 litteral between brackets!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                throw new UnknownHostException("["+host+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            InetAddress[] ret = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if(addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                if (addr.length == Inet4Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    ret[0] = new Inet4Address(null, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    if (ifname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        ret[0] = new Inet6Address(null, addr, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                        ret[0] = new Inet6Address(null, addr, numericZone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            } else if (ipv6Expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                // We were expecting an IPv6 Litteral, but got something else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                throw new UnknownHostException("["+host+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        return getAllByName0(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Returns the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * The InetAddress returned will represent the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * loopback address, 127.0.0.1, or the IPv6 loopback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * address, ::1. The IPv4 loopback address returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * is only one of many in the form 127.*.*.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * @return  the InetAddress loopback instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public static InetAddress getLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        return impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * check if the literal address string has %nn appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * returns -1 if not, or the numeric value otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * %nn may also be a string that represents the displayName of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * a currently available NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    private static int checkNumericZone (String s) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        int percent = s.indexOf ('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        int digit, zone=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if (percent == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        for (int i=percent+1; i<slen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (c == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                if (i == percent+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    /* empty per-cent field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if ((digit = Character.digit (c, 10)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            zone = (zone * 10) + digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        return zone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    private static InetAddress[] getAllByName0 (String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        return getAllByName0(host, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * package private so SocketPermission can call it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    static InetAddress[] getAllByName0 (String host, boolean check)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        throws UnknownHostException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        /* If it gets here it is presumed to be a hostname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        /* Cache.get can return: null, unknownAddress, or InetAddress[] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        /* make sure the connection to the host is allowed, before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
         * give out a hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                security.checkConnect(host, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1190
        InetAddress[] addresses = getCachedAddresses(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        /* If no entry in cache, then do the host lookup */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1193
        if (addresses == null) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1194
            addresses = getAddressesFromNameService(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1197
        if (addresses == unknown_array)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            throw new UnknownHostException(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1200
        return addresses.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1203
    private static InetAddress[] getAddressesFromNameService(String host)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1206
        InetAddress[] addresses = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        UnknownHostException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        // Check whether the host is in the lookupTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // 1) If the host isn't in the lookupTable when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        //    checkLookupTable() is called, checkLookupTable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        //    would add the host in the lookupTable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        //    return null. So we will do the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        // 2) If the host is in the lookupTable when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        //    checkLookupTable() is called, the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        //    would be blocked until the host is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        //    from the lookupTable. Then this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        //    should try to look up the addressCache.
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1220
        //     i) if it found the addresses in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        //        addressCache, checkLookupTable()  would
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1222
        //        return the addresses.
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1223
        //     ii) if it didn't find the addresses in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        //         addressCache for any reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        //         it should add the host in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        //         lookupTable and return null so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        //         following code would do  a lookup itself.
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1228
        if ((addresses = checkLookupTable(host)) == null) {
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1229
            try {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1230
                // This is the first thread which looks up the addresses
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1231
                // this host or the cache entry for this host has been
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1232
                // expired so this thread should do the lookup.
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1233
                for (NameService nameService : nameServices) {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1234
                    try {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1235
                        /*
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1236
                         * Do not put the call to lookup() inside the
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1237
                         * constructor.  if you do you will still be
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1238
                         * allocating space when the lookup fails.
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1239
                         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1241
                        addresses = nameService.lookupAllHostAddr(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                        success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                        break;
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1244
                    } catch (UnknownHostException uhe) {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1245
                        if (host.equalsIgnoreCase("localhost")) {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1246
                            InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1247
                            addresses = local;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1248
                            success = true;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1249
                            break;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1250
                        }
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1251
                        else {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1252
                            addresses = unknown_array;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1253
                            success = false;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1254
                            ex = uhe;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1255
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                }
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1258
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1259
                // Cache the addresses.
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1260
                cacheAddresses(host, addresses, success);
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1261
                if (!success && ex != null)
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1262
                    throw ex;
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1263
            } finally {
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1264
                // Delete host from the lookupTable and notify
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1265
                // all threads waiting on the lookupTable monitor.
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1266
                updateLookupTable(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1270
        return addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1274
    private static InetAddress[] checkLookupTable(String host) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        synchronized (lookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            // If the host isn't in the lookupTable, add it in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            // lookuptable and return null. The caller should do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            // the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            if (lookupTable.containsKey(host) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                lookupTable.put(host, null);
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1281
                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            // If the host is in the lookupTable, it means that another
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1285
            // thread is trying to look up the addresses of this host.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            // This thread should wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            while (lookupTable.containsKey(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    lookupTable.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1295
        // The other thread has finished looking up the addresses of
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1296
        // the host. This thread should retry to get the addresses
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1297
        // from the addressCache. If it doesn't get the addresses from
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1298
        // the cache, it will try to look up the addresses itself.
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1299
        InetAddress[] addresses = getCachedAddresses(host);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1300
        if (addresses == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            synchronized (lookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                lookupTable.put(host, null);
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1303
                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1307
        return addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    private static void updateLookupTable(String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        synchronized (lookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            lookupTable.remove(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            lookupTable.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * Returns an <code>InetAddress</code> object given the raw IP address .
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * The argument is in network byte order: the highest order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * byte of the address is in <code>getAddress()[0]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <p> This method doesn't block, i.e. no reverse name service lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * must be 16 bytes long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * @return  an InetAddress object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @exception  UnknownHostException  if IP address is of illegal length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    public static InetAddress getByAddress(byte[] addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        return getByAddress(null, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    private static InetAddress cachedLocalHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    private static long cacheTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    private static final long maxCacheTime = 5000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    private static final Object cacheLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * Returns the address of the local host. This is achieved by retrieving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * the name of the host from the system, then resolving that name into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * an <code>InetAddress</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <P>Note: The resolved address may be cached for a short period of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * </P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * <p>If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * with the local host name and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * If the operation is not allowed, an InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * the loopback address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @return     the address of the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @exception  UnknownHostException  if the local host name could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *             be resolved into an address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @see java.net.InetAddress#getByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    public static InetAddress getLocalHost() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            String local = impl.getLocalHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                security.checkConnect(local, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (local.equals("localhost")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                return impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            InetAddress ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            synchronized (cacheLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                if (cachedLocalHost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    if ((now - cacheTime) < maxCacheTime) // Less than 5s old?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                        ret = cachedLocalHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                        cachedLocalHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1390
                // we are calling getAddressesFromNameService directly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                // to avoid getting localHost from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                    InetAddress[] localAddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                        localAddrs =
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1396
                            InetAddress.getAddressesFromNameService(local);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    } catch (UnknownHostException uhe) {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1398
                        // Rethrow with a more informative error message.
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1399
                        UnknownHostException uhe2 =
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1400
                            new UnknownHostException(local + ": " +
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1401
                                                     uhe.getMessage());
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1402
                        uhe2.initCause(uhe);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1403
                        throw uhe2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    cachedLocalHost = localAddrs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    cacheTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    ret = localAddrs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        } catch (java.lang.SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            return impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * Returns the InetAddress representing anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * (typically 0.0.0.0 or ::0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    static InetAddress anyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return impl.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * Load and instantiate an underlying impl class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1433
    static InetAddressImpl loadImpl(String implName) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1434
        Object impl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         * Property "impl.prefix" will be prepended to the classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
         * of the implementation object we instantiate, to which we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
         * delegate the real work (like native methods).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         * property can vary across implementations of the java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         * classes.  The default is an empty String "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        String prefix = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                      new GetPropertyAction("impl.prefix", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            impl = Class.forName("java.net." + prefix + implName).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            System.err.println("Class not found: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            System.err.println("Could not instantiate: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            System.err.println("Cannot access class: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        if (impl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                impl = Class.forName(implName).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                throw new Error("System property impl.prefix incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1469
        return (InetAddressImpl) impl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1471
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1472
    private void readObjectNoData (ObjectInputStream s) throws
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1473
                         IOException, ClassNotFoundException {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1474
        if (getClass().getClassLoader() != null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1475
            throw new SecurityException ("invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1476
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1477
    }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1478
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1479
    private void readObject (ObjectInputStream s) throws
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1480
                         IOException, ClassNotFoundException {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1481
        s.defaultReadObject ();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1482
        if (getClass().getClassLoader() != null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1483
            hostName = null;
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1484
            address = 0;
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1485
            throw new SecurityException ("invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1486
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1487
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
 * Simple factory to create the impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
class InetAddressImplFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    static InetAddressImpl create() {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1496
        return InetAddress.loadImpl(isIPv6Supported() ?
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1497
                                    "Inet6AddressImpl" : "Inet4AddressImpl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    static native boolean isIPv6Supported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
}