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