src/java.base/share/classes/java/net/InetAddress.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 52902 e3398b2e1ab0
child 53133 bf2533105a26
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52157
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
     2
 * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    28
import java.util.NavigableSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33674
diff changeset
    30
import java.util.Objects;
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
    31
import java.util.Scanner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
    33
import java.io.File;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
    34
import java.io.FileNotFoundException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectStreamException;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
    36
import java.io.ObjectStreamField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
52157
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
    38
import java.io.InvalidObjectException;
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
    39
import java.io.ObjectInputStream;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
    40
import java.io.ObjectInputStream.GetField;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
    41
import java.io.ObjectOutputStream;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
    42
import java.io.ObjectOutputStream.PutField;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
    43
import java.lang.annotation.Native;
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    44
import java.util.concurrent.ConcurrentHashMap;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    45
import java.util.concurrent.ConcurrentMap;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    46
import java.util.concurrent.ConcurrentSkipListSet;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    47
import java.util.concurrent.atomic.AtomicLong;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
    48
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52220
diff changeset
    49
import jdk.internal.access.JavaNetInetAddressAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52220
diff changeset
    50
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.net.InetAddressCachePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This class represents an Internet Protocol (IP) address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> An IP address is either a 32-bit or 128-bit unsigned number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * used by IP, a lower-level protocol on which protocols like UDP and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * TCP are built. The IP address architecture is defined by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC&nbsp;790:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Assigned Numbers</i></a>, <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC&nbsp;1918:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Address Allocation for Private Internets</i></a>, <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Administratively Scoped IP Multicast</i></a>, and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Version 6 Addressing Architecture</i></a>. An instance of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * InetAddress consists of an IP address and possibly its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * corresponding host name (depending on whether it is constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * with a host name or whether it has already done reverse host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * resolution).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 16870
diff changeset
    74
 * <h3> Address types </h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
    76
 * <table class="striped" style="margin-left:2em">
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 41771
diff changeset
    77
 *   <caption style="display:none">Description of unicast and multicast address types</caption>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
    78
 *   <thead>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
    79
 *   <tr><th scope="col">Address Type</th><th scope="col">Description</th></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
    80
 *   </thead>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 41771
diff changeset
    81
 *   <tbody>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
    82
 *   <tr><th scope="row" style="vertical-align:top">unicast</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *       <td>An identifier for a single interface. A packet sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *         a unicast address is delivered to the interface identified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         that address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *         <p> The Unspecified Address -- Also called anylocal or wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         address. It must never be assigned to any node. It indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         absence of an address. One example of its use is as the target of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *         bind, which allows a server to accept a client connection on any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         interface, in case the server host has multiple interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         <p> The <i>unspecified</i> address must not be used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *         the destination address of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *         <p> The <i>Loopback</i> Addresses -- This is the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *         assigned to the loopback interface. Anything sent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         IP address loops around and becomes IP input on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *         host. This address is often used when testing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *         client.</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   101
 *   <tr><th scope="row" style="vertical-align:top">multicast</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *       <td>An identifier for a set of interfaces (typically belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *         to different nodes). A packet sent to a multicast address is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *         delivered to all interfaces identified by that address.</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 41771
diff changeset
   105
 * </tbody>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   106
 * </table>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <h4> IP address scope </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p> <i>Link-local</i> addresses are designed to be used for addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * on a single link for purposes such as auto-address configuration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * neighbor discovery, or when no routers are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p> <i>Site-local</i> addresses are designed to be used for addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * inside of a site without the need for a global prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p> <i>Global</i> addresses are unique across the internet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <h4> Textual representation of IP addresses </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * The textual representation of an IP address is address family specific.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * For IPv4 address format, please refer to <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * HREF="Inet4Address.html#format">Inet4Address#format</A>; For IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * address format, please refer to <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * HREF="Inet6Address.html#format">Inet6Address#format</A>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <P>There is a <a href="doc-files/net-properties.html#Ipv4IPv6">couple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * System Properties</a> affecting how IPv4 and IPv6 addresses are used.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <h4> Host Name Resolution </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Host name-to-IP address <i>resolution</i> is accomplished through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * the use of a combination of local machine configuration information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * and network naming services such as the Domain Name System (DNS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * and Network Information Service(NIS). The particular naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * services(s) being used is by default the local machine configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * one. For any host name, its corresponding IP address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p> <i>Reverse name resolution</i> means that for any IP address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * the host associated with the IP address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <p> The InetAddress class provides methods to resolve host names to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * their IP addresses and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <h4> InetAddress Caching </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * The InetAddress class has a cache to store successful as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * unsuccessful host name resolutions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p> By default, when a security manager is installed, in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * protect against DNS spoofing attacks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * the result of positive host name resolutions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * cached forever. When a security manager is not installed, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * behavior is to cache entries for a finite (implementation dependent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * period of time. The result of unsuccessful host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * name resolution is cached for a very short period of time (10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * seconds) to improve performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <p> If the default behavior is not desired, then a Java security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * can be set to a different Time-to-live (TTL) value for positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * caching. Likewise, a system admin can configure a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * negative caching TTL value when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <p> Two Java security properties control the TTL values used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *  positive and negative host name resolution caching:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   170
 * <dl style="margin-left:2em">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <dt><b>networkaddress.cache.ttl</b></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <dd>Indicates the caching policy for successful name lookups from
23927
225d01ae6469 8038821: Fix typos in java.net
chegar
parents: 21635
diff changeset
   173
 * the name service. The value is specified as an integer to indicate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * the number of seconds to cache the successful lookup. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * setting is to cache for an implementation specific period of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * A value of -1 indicates "cache forever".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <dt><b>networkaddress.cache.negative.ttl</b> (default: 10)</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * <dd>Indicates the caching policy for un-successful name lookups
23927
225d01ae6469 8038821: Fix typos in java.net
chegar
parents: 21635
diff changeset
   181
 * from the name service. The value is specified as an integer to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * indicate the number of seconds to cache the failure for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * un-successful lookups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * A value of 0 indicates "never cache".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * A value of -1 indicates "cache forever".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * @author  Chris Warth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * @see     java.net.InetAddress#getByAddress(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * @see     java.net.InetAddress#getByAddress(java.lang.String, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * @see     java.net.InetAddress#getAllByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * @see     java.net.InetAddress#getByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * @see     java.net.InetAddress#getLocalHost()
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   196
 * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
class InetAddress implements java.io.Serializable {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   200
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   201
    @Native static final int PREFER_IPV4_VALUE = 0;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   202
    @Native static final int PREFER_IPV6_VALUE = 1;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   203
    @Native static final int PREFER_SYSTEM_VALUE = 2;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   204
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Specify the address family: Internet Protocol, Version 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41596
diff changeset
   209
    @Native static final int IPv4 = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Specify the address family: Internet Protocol, Version 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41596
diff changeset
   215
    @Native static final int IPv6 = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /* Specify address family preference */
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   218
    static transient final int preferIPv6Address;
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   219
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   220
    static class InetAddressHolder {
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   221
        /**
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   222
         * Reserve the original application specified hostname.
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   223
         *
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   224
         * The original hostname is useful for domain-based endpoint
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   225
         * identification (see RFC 2818 and RFC 6125).  If an address
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   226
         * was created with a raw IP address, a reverse name lookup
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   227
         * may introduce endpoint identification security issue via
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   228
         * DNS forging.
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   229
         *
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   230
         * Oracle JSSE provider is using this original hostname, via
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31918
diff changeset
   231
         * jdk.internal.misc.JavaNetAccess, for SSL/TLS endpoint identification.
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   232
         *
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   233
         * Note: May define a new public method in the future if necessary.
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   234
         */
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents: 32834
diff changeset
   235
        String originalHostName;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   236
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   237
        InetAddressHolder() {}
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   238
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   239
        InetAddressHolder(String hostName, int address, int family) {
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   240
            this.originalHostName = hostName;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   241
            this.hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   242
            this.address = address;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   243
            this.family = family;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   244
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   245
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   246
        void init(String hostName, int family) {
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   247
            this.originalHostName = hostName;
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   248
            this.hostName = hostName;
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   249
            if (family != -1) {
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   250
                this.family = family;
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   251
            }
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   252
        }
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   253
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   254
        String hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   255
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   256
        String getHostName() {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   257
            return hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   258
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   259
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   260
        String getOriginalHostName() {
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   261
            return originalHostName;
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   262
        }
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 29986
diff changeset
   263
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   264
        /**
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   265
         * Holds a 32-bit IPv4 address.
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   266
         */
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   267
        int address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   269
        int getAddress() {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   270
            return address;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   271
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   272
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   273
        /**
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   274
         * Specifies the address family type, for instance, '1' for IPv4
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   275
         * addresses, and '2' for IPv6 addresses.
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   276
         */
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   277
        int family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   279
        int getFamily() {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   280
            return family;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   281
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   282
    }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   283
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   284
    /* Used to store the serializable fields of InetAddress */
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   285
    final transient InetAddressHolder holder;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   286
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   287
    InetAddressHolder holder() {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   288
        return holder;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   289
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /* Used to store the name service provider */
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   292
    private static transient NameService nameService = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
49531
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   294
    /**
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   295
     * Used to store the best available hostname.
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   296
     * Lazily initialized via a data race; safe because Strings are immutable.
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   297
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private transient String canonicalHostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static final long serialVersionUID = 3286316764910316507L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Load net library into runtime, and perform initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   307
        String str = java.security.AccessController.doPrivileged(
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   308
                new GetPropertyAction("java.net.preferIPv6Addresses"));
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   309
        if (str == null) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   310
            preferIPv6Address = PREFER_IPV4_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   311
        } else if (str.equalsIgnoreCase("true")) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   312
            preferIPv6Address = PREFER_IPV6_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   313
        } else if (str.equalsIgnoreCase("false")) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   314
            preferIPv6Address = PREFER_IPV4_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   315
        } else if (str.equalsIgnoreCase("system")) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   316
            preferIPv6Address = PREFER_SYSTEM_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   317
        } else {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   318
            preferIPv6Address = PREFER_IPV4_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37782
diff changeset
   319
        }
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   320
        AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26199
diff changeset
   321
            new java.security.PrivilegedAction<>() {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   322
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   323
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   324
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   325
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11119
diff changeset
   326
            });
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31918
diff changeset
   327
        SharedSecrets.setJavaNetInetAddressAccess(
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31918
diff changeset
   328
                new JavaNetInetAddressAccess() {
31918
1f13aceb00d7 8132705: Refactor SharedSecrets in sun.misc.JavaNetAccess
rriggs
parents: 31687
diff changeset
   329
                    public String getOriginalHostName(InetAddress ia) {
1f13aceb00d7 8132705: Refactor SharedSecrets in sun.misc.JavaNetAccess
rriggs
parents: 31687
diff changeset
   330
                        return ia.holder.getOriginalHostName();
1f13aceb00d7 8132705: Refactor SharedSecrets in sun.misc.JavaNetAccess
rriggs
parents: 31687
diff changeset
   331
                    }
41596
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   332
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   333
                    public InetAddress getByName(String hostName,
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   334
                                                 InetAddress hostAddress)
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   335
                            throws UnknownHostException
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   336
                    {
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   337
                        return InetAddress.getByName(hostName, hostAddress);
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 40118
diff changeset
   338
                    }
31918
1f13aceb00d7 8132705: Refactor SharedSecrets in sun.misc.JavaNetAccess
rriggs
parents: 31687
diff changeset
   339
                }
1f13aceb00d7 8132705: Refactor SharedSecrets in sun.misc.JavaNetAccess
rriggs
parents: 31687
diff changeset
   340
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Constructor for the Socket.accept() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This creates an empty InetAddress, which is filled in by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * the accept() method.  This InetAddress, however, is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * put in the address cache, since it is not created by name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    InetAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   351
        holder = new InetAddressHolder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Replaces the de-serialized object with an Inet4Address object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return the alternate object to the de-serialized object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // will replace the deserialized 'this' object
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   364
        return new Inet4Address(holder().getHostName(), holder().getAddress());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * IP multicast address.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   370
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * an IP multicast address
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   372
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public boolean isMulticastAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
40118
f63c761ec2d2 8162819: fix minor Javadoc issues and remove warnings in java.net.Socket
clanger
parents: 38552
diff changeset
   379
     * Utility routine to check if the InetAddress is a wildcard address.
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
   380
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public boolean isAnyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   391
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   402
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * a link local address; or false if address is not a link local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   413
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * a site local address; or false if address is not a site local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   424
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *         of global scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   436
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   448
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   460
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   472
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Test whether that address is reachable. Best effort is made by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * implementation to try to reach the host, but firewalls and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * configuration may block requests resulting in a unreachable status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * while some specific ports may be accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * A typical implementation will use ICMP ECHO REQUESTs if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * privilege can be obtained, otherwise it will try to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * a TCP connection on port 7 (Echo) of the destination host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * The timeout value, in milliseconds, indicates the maximum amount of time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the try should take. If the operation times out before getting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * answer, the host is deemed unreachable. A negative value will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * in an IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param   timeout the time, in milliseconds, before the call aborts
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   498
     * @return a {@code boolean} indicating if the address is reachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @throws IOException if a network error occurs
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   500
     * @throws  IllegalArgumentException if {@code timeout} is negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public boolean isReachable(int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return isReachable(null, 0 , timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Test whether that address is reachable. Best effort is made by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * implementation to try to reach the host, but firewalls and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * configuration may block requests resulting in a unreachable status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * while some specific ports may be accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * A typical implementation will use ICMP ECHO REQUESTs if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * privilege can be obtained, otherwise it will try to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * a TCP connection on port 7 (Echo) of the destination host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   516
     * The {@code network interface} and {@code ttl} parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * let the caller specify which network interface the test will go through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * and the maximum number of hops the packets should go through.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   519
     * A negative value for the {@code ttl} will result in an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * The timeout value, in milliseconds, indicates the maximum amount of time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * the try should take. If the operation times out before getting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * answer, the host is deemed unreachable. A negative value will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * in an IllegalArgumentException being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param   netif   the NetworkInterface through which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *                    test will be done, or null for any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param   ttl     the maximum numbers of hops to try or 0 for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *                  default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param   timeout the time, in milliseconds, before the call aborts
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   532
     * @throws  IllegalArgumentException if either {@code timeout}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   533
     *                          or {@code ttl} are negative.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   534
     * @return a {@code boolean}indicating if the address is reachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @throws IOException if a network error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public boolean isReachable(NetworkInterface netif, int ttl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                               int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (ttl < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throw new IllegalArgumentException("ttl can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return impl.isReachable(this, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Gets the host name for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <p>If this InetAddress was created with a host name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * this host name will be remembered and returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * otherwise, a reverse name lookup will be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * and the result will be returned based on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * configured name lookup service. If a lookup of the name service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * is required, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * {@link #getCanonicalHostName() getCanonicalHostName}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <p>If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   560
     * {@code checkConnect} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   561
     * with the hostname and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @see InetAddress#getCanonicalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public String getHostName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return getHostName(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the hostname for this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * If the host is equal to null, then this address refers to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * of the local machine's available network addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * this is package private so SocketPermission can make calls into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * here without a security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   585
     * calls its {@code checkConnect} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   586
     * with the hostname and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param check make security check if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    String getHostName(boolean check) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   601
        if (holder().getHostName() == null) {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   602
            holder().hostName = InetAddress.getHostFromNameService(this, check);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   604
        return holder().getHostName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Gets the fully qualified domain name for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Best effort method, meaning we may not be able to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * the FQDN depending on the underlying system configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   613
     * calls its {@code checkConnect} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   614
     * with the hostname and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return  the fully qualified domain name for this IP address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *    or if the operation is not allowed by the security check,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *    the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public String getCanonicalHostName() {
49531
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   629
        String value = canonicalHostName;
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   630
        if (value == null)
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   631
            canonicalHostName = value =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                InetAddress.getHostFromNameService(this, true);
49531
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47747
diff changeset
   633
        return value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Returns the hostname for this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   640
     * calls its {@code checkConnect} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   641
     * with the hostname and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * as its arguments to see if the calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * the hostname for this IP address, i.e., to connect to the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * If the operation is not allowed, it will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * the textual representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @return  the host name for this IP address, or if the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *    is not allowed by the security check, the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *    representation of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param check make security check if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private static String getHostFromNameService(InetAddress addr, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        String host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                // first lookup the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                host = nameService.getHostByAddr(addr.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                /* check to see if calling code is allowed to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                 * the hostname for this IP address, ie, connect to the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        sec.checkConnect(host, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                /* now get all the IP addresses for this hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                 * and make sure one of them matches the original IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                 * address. We do this to try and prevent spoofing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                InetAddress[] arr = InetAddress.getAllByName0(host, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if(arr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    for(int i = 0; !ok && i < arr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        ok = addr.equals(arr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                //XXX: if it looks a spoof just return the address?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                host = addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                // let next provider resolve the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   700
     * Returns the raw IP address of this {@code InetAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * object. The result is in network byte order: the highest order
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   702
     * byte of the address is in {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public byte[] getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Returns the IP address string in textual presentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @return  the raw IP address in a string format.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   714
     * @since   1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        return null;
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
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   731
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   732
     * not {@code null} and it represents the same IP address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   735
     * Two instances of {@code InetAddress} represent the same IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * address if the length of the byte arrays returned by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   737
     * {@code getAddress} is the same for both, and each of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   741
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   742
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   750
     * Converts this IP address to a {@code String}. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * string returned is of the form: hostname / literal IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * If the host name is unresolved, no reverse name service lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * is performed. The hostname part will be represented by an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return  a string representation of this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public String toString() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   760
        String hostName = holder().getHostName();
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33674
diff changeset
   761
        return Objects.toString(hostName, "")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            + "/" + getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   765
    // mapping from host name to Addresses - either NameServiceAddresses (while
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   766
    // still being looked-up by NameService(s)) or CachedAddresses when cached
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   767
    private static final ConcurrentMap<String, Addresses> cache =
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   768
        new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   770
    // CachedAddresses that have to expire are kept ordered in this NavigableSet
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   771
    // which is scanned on each access
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   772
    private static final NavigableSet<CachedAddresses> expirySet =
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   773
        new ConcurrentSkipListSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   775
    // common interface
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   776
    private interface Addresses {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   777
        InetAddress[] get() throws UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   780
    // a holder for cached addresses with required metadata
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   781
    private static final class CachedAddresses  implements Addresses, Comparable<CachedAddresses> {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   782
        private static final AtomicLong seq = new AtomicLong();
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   783
        final String host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   784
        final InetAddress[] inetAddresses;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   785
        final long expiryTime; // time of expiry (in terms of System.nanoTime())
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   786
        final long id = seq.incrementAndGet(); // each instance is unique
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   788
        CachedAddresses(String host, InetAddress[] inetAddresses, long expiryTime) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   789
            this.host = host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   790
            this.inetAddresses = inetAddresses;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   791
            this.expiryTime = expiryTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   794
        @Override
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   795
        public InetAddress[] get() throws UnknownHostException {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   796
            if (inetAddresses == null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   797
                throw new UnknownHostException(host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   799
            return inetAddresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   802
        @Override
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   803
        public int compareTo(CachedAddresses other) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   804
            // natural order is expiry time -
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   805
            // compare difference of expiry times rather than
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   806
            // expiry times directly, to avoid possible overflow.
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   807
            // (see System.nanoTime() recommendations...)
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   808
            long diff = this.expiryTime - other.expiryTime;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   809
            if (diff < 0L) return -1;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   810
            if (diff > 0L) return 1;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   811
            // ties are broken using unique id
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   812
            return Long.compare(this.id, other.id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   816
    // a name service lookup based Addresses implementation which replaces itself
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   817
    // in cache when the result is obtained
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   818
    private static final class NameServiceAddresses implements Addresses {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   819
        private final String host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   820
        private final InetAddress reqAddr;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   821
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   822
        NameServiceAddresses(String host, InetAddress reqAddr) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   823
            this.host = host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   824
            this.reqAddr = reqAddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   827
        @Override
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   828
        public InetAddress[] get() throws UnknownHostException {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   829
            Addresses addresses;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   830
            // only one thread is doing lookup to name service
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   831
            // for particular host at any time.
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   832
            synchronized (this) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   833
                // re-check that we are still us + re-install us if slot empty
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   834
                addresses = cache.putIfAbsent(host, this);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   835
                if (addresses == null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   836
                    // this can happen when we were replaced by CachedAddresses in
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   837
                    // some other thread, then CachedAddresses expired and were
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   838
                    // removed from cache while we were waiting for lock...
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   839
                    addresses = this;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   840
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   841
                // still us ?
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   842
                if (addresses == this) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   843
                    // lookup name services
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   844
                    InetAddress[] inetAddresses;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   845
                    UnknownHostException ex;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   846
                    int cachePolicy;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   847
                    try {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   848
                        inetAddresses = getAddressesFromNameService(host, reqAddr);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   849
                        ex = null;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   850
                        cachePolicy = InetAddressCachePolicy.get();
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   851
                    } catch (UnknownHostException uhe) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   852
                        inetAddresses = null;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   853
                        ex = uhe;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   854
                        cachePolicy = InetAddressCachePolicy.getNegative();
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   855
                    }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   856
                    // remove or replace us with cached addresses according to cachePolicy
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   857
                    if (cachePolicy == InetAddressCachePolicy.NEVER) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   858
                        cache.remove(host, this);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   859
                    } else {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   860
                        CachedAddresses cachedAddresses = new CachedAddresses(
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   861
                            host,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   862
                            inetAddresses,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   863
                            cachePolicy == InetAddressCachePolicy.FOREVER
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   864
                            ? 0L
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   865
                            // cachePolicy is in [s] - we need [ns]
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   866
                            : System.nanoTime() + 1000_000_000L * cachePolicy
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   867
                        );
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   868
                        if (cache.replace(host, this, cachedAddresses) &&
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   869
                            cachePolicy != InetAddressCachePolicy.FOREVER) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   870
                            // schedule expiry
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   871
                            expirySet.add(cachedAddresses);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   872
                        }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   873
                    }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   874
                    if (inetAddresses == null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   875
                        throw ex == null ? new UnknownHostException(host) : ex;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   876
                    }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   877
                    return inetAddresses;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   878
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   879
                // else addresses != this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   881
            // delegate to different addresses when we are already replaced
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   882
            // but outside of synchronized block to avoid any chance of dead-locking
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
   883
            return addresses.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   887
    /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   888
     * NameService provides host and address lookup service
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   889
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   890
     * @since 9
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   891
     */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   892
    private interface NameService {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   893
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   894
        /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   895
         * Lookup a host mapping by name. Retrieve the IP addresses
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   896
         * associated with a host
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   897
         *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   898
         * @param host the specified hostname
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   899
         * @return array of IP addresses for the requested host
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   900
         * @throws UnknownHostException
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   901
         *             if no IP address for the {@code host} could be found
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   902
         */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   903
        InetAddress[] lookupAllHostAddr(String host)
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   904
                throws UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   906
        /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   907
         * Lookup the host corresponding to the IP address provided
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   908
         *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   909
         * @param addr byte array representing an IP address
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   910
         * @return {@code String} representing the host name mapping
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   911
         * @throws UnknownHostException
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   912
         *             if no host found for the specified IP address
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   913
         */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   914
        String getHostByAddr(byte[] addr) throws UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   916
    }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   917
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   918
    /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   919
     * The default NameService implementation, which delegates to the underlying
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   920
     * OS network libraries to resolve host address mappings.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   921
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   922
     * @since 9
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   923
     */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   924
    private static final class PlatformNameService implements NameService {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   925
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   926
        public InetAddress[] lookupAllHostAddr(String host)
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   927
            throws UnknownHostException
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   928
        {
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   929
            return impl.lookupAllHostAddr(host);
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   930
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   931
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   932
        public String getHostByAddr(byte[] addr)
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   933
            throws UnknownHostException
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   934
        {
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   935
            return impl.getHostByAddr(addr);
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36967
diff changeset
   936
        }
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   937
    }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   938
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   939
    /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   940
     * The HostsFileNameService provides host address mapping
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   941
     * by reading the entries in a hosts file, which is specified by
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   942
     * {@code jdk.net.hosts.file} system property
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   943
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   944
     * <p>The file format is that which corresponds with the /etc/hosts file
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   945
     * IP Address host alias list.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   946
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   947
     * <p>When the file lookup is enabled it replaces the default NameService
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   948
     * implementation
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   949
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   950
     * @since 9
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   951
     */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   952
    private static final class HostsFileNameService implements NameService {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   953
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   954
        private final String hostsFile;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   955
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   956
        public HostsFileNameService (String hostsFileName) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   957
            this.hostsFile = hostsFileName;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   958
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   959
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   960
        private  String addrToString(byte addr[]) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   961
          String stringifiedAddress = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   962
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   963
            if (addr.length == Inet4Address.INADDRSZ) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   964
                stringifiedAddress = Inet4Address.numericToTextFormat(addr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   965
            } else { // treat as an IPV6 jobby
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   966
                byte[] newAddr
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   967
                    = IPAddressUtil.convertFromIPv4MappedAddress(addr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   968
                if (newAddr != null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   969
                   stringifiedAddress = Inet4Address.numericToTextFormat(addr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   970
                } else {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   971
                    stringifiedAddress = Inet6Address.numericToTextFormat(addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                }
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   973
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   974
            return stringifiedAddress;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   975
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   976
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   977
        /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   978
         * Lookup the host name  corresponding to the IP address provided.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   979
         * Search the configured host file a host name corresponding to
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   980
         * the specified IP address.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   981
         *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   982
         * @param addr byte array representing an IP address
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   983
         * @return {@code String} representing the host name mapping
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   984
         * @throws UnknownHostException
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   985
         *             if no host found for the specified IP address
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   986
         */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   987
        @Override
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   988
        public String getHostByAddr(byte[] addr) throws UnknownHostException {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   989
            String hostEntry;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   990
            String host = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   991
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   992
            String addrString = addrToString(addr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   993
            try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   994
                while (hostsFileScanner.hasNextLine()) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   995
                    hostEntry = hostsFileScanner.nextLine();
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   996
                    if (!hostEntry.startsWith("#")) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   997
                        hostEntry = removeComments(hostEntry);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   998
                        if (hostEntry.contains(addrString)) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
   999
                            host = extractHost(hostEntry, addrString);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1000
                            if (host != null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1001
                                break;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1002
                            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1003
                        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1004
                    }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1005
                }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1006
            } catch (FileNotFoundException e) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1007
                throw new UnknownHostException("Unable to resolve address "
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1008
                        + addrString + " as hosts file " + hostsFile
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1009
                        + " not found ");
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1010
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1011
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52700
diff changeset
  1012
            if ((host == null) || (host.isEmpty()) || (host.equals(" "))) {
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1013
                throw new UnknownHostException("Requested address "
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1014
                        + addrString
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1015
                        + " resolves to an invalid entry in hosts file "
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1016
                        + hostsFile);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1017
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1018
            return host;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1019
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1020
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1021
        /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1022
         * <p>Lookup a host mapping by name. Retrieve the IP addresses
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1023
         * associated with a host.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1024
         *
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
  1025
         * <p>Search the configured hosts file for the addresses associated
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1026
         * with the specified host name.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1027
         *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1028
         * @param host the specified hostname
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1029
         * @return array of IP addresses for the requested host
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1030
         * @throws UnknownHostException
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1031
         *             if no IP address for the {@code host} could be found
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1032
         */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1033
        public InetAddress[] lookupAllHostAddr(String host)
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1034
                throws UnknownHostException {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1035
            String hostEntry;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1036
            String addrStr = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1037
            InetAddress[] res = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1038
            byte addr[] = new byte[4];
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1039
            ArrayList<InetAddress> inetAddresses = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1040
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
  1041
            // lookup the file and create a list InetAddress for the specified host
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1042
            try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1043
                while (hostsFileScanner.hasNextLine()) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1044
                    hostEntry = hostsFileScanner.nextLine();
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1045
                    if (!hostEntry.startsWith("#")) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1046
                        hostEntry = removeComments(hostEntry);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1047
                        if (hostEntry.contains(host)) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1048
                            addrStr = extractHostAddr(hostEntry, host);
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52700
diff changeset
  1049
                            if ((addrStr != null) && (!addrStr.isEmpty())) {
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1050
                                addr = createAddressByteArray(addrStr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1051
                                if (inetAddresses == null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1052
                                    inetAddresses = new ArrayList<>(1);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1053
                                }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1054
                                if (addr != null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1055
                                    inetAddresses.add(InetAddress.getByAddress(host, addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    }
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1060
                }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1061
            } catch (FileNotFoundException e) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1062
                throw new UnknownHostException("Unable to resolve host " + host
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1063
                        + " as hosts file " + hostsFile + " not found ");
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1064
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1065
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1066
            if (inetAddresses != null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1067
                res = inetAddresses.toArray(new InetAddress[inetAddresses.size()]);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1068
            } else {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1069
                throw new UnknownHostException("Unable to resolve host " + host
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1070
                        + " in hosts file " + hostsFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1072
            return res;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1073
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1074
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1075
        private String removeComments(String hostsEntry) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1076
            String filteredEntry = hostsEntry;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1077
            int hashIndex;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1078
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1079
            if ((hashIndex = hostsEntry.indexOf("#")) != -1) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1080
                filteredEntry = hostsEntry.substring(0, hashIndex);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1081
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1082
            return filteredEntry;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1083
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1084
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1085
        private byte [] createAddressByteArray(String addrStr) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1086
            byte[] addrArray;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1087
            // check if IPV4 address - most likely
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1088
            addrArray = IPAddressUtil.textToNumericFormatV4(addrStr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1089
            if (addrArray == null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1090
                addrArray = IPAddressUtil.textToNumericFormatV6(addrStr);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1091
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1092
            return addrArray;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1095
        /** host to ip address mapping */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1096
        private String extractHostAddr(String hostEntry, String host) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1097
            String[] mapping = hostEntry.split("\\s+");
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1098
            String hostAddr = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1099
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1100
            if (mapping.length >= 2) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1101
                // look at the host aliases
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1102
                for (int i = 1; i < mapping.length; i++) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1103
                    if (mapping[i].equalsIgnoreCase(host)) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1104
                        hostAddr = mapping[0];
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1105
                    }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1106
                }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1107
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1108
            return hostAddr;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1109
        }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1110
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1111
        /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1112
         * IP Address to host mapping
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1113
         * use first host alias in list
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1114
         */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1115
        private String extractHost(String hostEntry, String addrString) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1116
            String[] mapping = hostEntry.split("\\s+");
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1117
            String host = null;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1118
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1119
            if (mapping.length >= 2) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1120
                if (mapping[0].equalsIgnoreCase(addrString)) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1121
                    host = mapping[1];
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1122
                }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1123
            }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1124
            return host;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1125
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1128
    static final InetAddressImpl  impl;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // create the impl
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1132
        impl = InetAddressImplFactory.create();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1134
        // create name service
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1135
        nameService = createNameService();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1138
    /**
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1139
     * Create an instance of the NameService interface based on
47747
84e7c1515661 8189631: Missing space in the javadoc for InetAddress.createNameService()
igerasim
parents: 47216
diff changeset
  1140
     * the setting of the {@code jdk.net.hosts.file} system property.
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1141
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1142
     * <p>The default NameService is the PlatformNameService, which typically
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1143
     * delegates name and address resolution calls to the underlying
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1144
     * OS network libraries.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1145
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1146
     * <p> A HostsFileNameService is created if the {@code jdk.net.hosts.file}
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1147
     * system property is set. If the specified file doesn't exist, the name or
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1148
     * address lookup will result in an UnknownHostException. Thus, non existent
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1149
     * hosts file is handled as if the file is empty.
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1150
     *
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1151
     * @return a NameService
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1152
     */
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1153
    private static NameService createNameService() {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1154
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
  1155
        String hostsFileName =
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
  1156
                GetPropertyAction.privilegedGetProperty("jdk.net.hosts.file");
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1157
        NameService theNameService;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1158
        if (hostsFileName != null) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1159
            theNameService = new HostsFileNameService(hostsFileName);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1160
        } else {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1161
            theNameService = new PlatformNameService();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 33675
diff changeset
  1163
        return theNameService;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /**
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1167
     * Creates an InetAddress based on the provided host name and IP address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * No name service is checked for the validity of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <p> The host name can either be a machine name, such as
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52499
diff changeset
  1171
     * "{@code www.example.com}", or a textual representation of its IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <p> No validity checking is done on the host name either.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <p> If addr specifies an IPv4 address an instance of Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * will be returned; otherwise, an instance of Inet6Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * must be 16 bytes long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @return  an InetAddress object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @exception  UnknownHostException  if IP address is of illegal length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    public static InetAddress getByAddress(String host, byte[] addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        throws UnknownHostException {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
  1190
        if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            if (addr.length == Inet4Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                return new Inet4Address(host, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            } else if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                byte[] newAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    = IPAddressUtil.convertFromIPv4MappedAddress(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                if (newAddr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    return new Inet4Address(host, newAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                    return new Inet6Address(host, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * Determines the IP address of a host, given the host's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * <p> The host name can either be a machine name, such as
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52499
diff changeset
  1216
     * "{@code www.example.com}", or a textual representation of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * IP address. If a literal IP address is supplied, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * validity of the address format is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1220
     * <p> For {@code host} specified in literal IPv6 address,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * either the form defined in RFC 2732 or the literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
49926
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1226
     * <p> If the host is {@code null} or {@code host.length()} is equal
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1227
     * to zero, then an {@code InetAddress} representing an address of the
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1228
     * loopback interface is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
49926
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1231
     * section&nbsp;2.5.3.
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1232
     *
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1233
     * <p> If there is a security manager, and {@code host} is not {@code null}
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1234
     * or {@code host.length() } is not equal to zero, the security manager's
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1235
     * {@code checkConnect} method is called with the hostname and {@code -1}
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1236
     * as its arguments to determine if the operation is allowed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1238
     * @param      host   the specified host, or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @return     an IP address for the given host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @exception  UnknownHostException  if no IP address for the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1241
     *               {@code host} could be found, or if a scope_id was specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *               for a global IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * @exception  SecurityException if a security manager exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *             and its checkConnect method doesn't allow the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public static InetAddress getByName(String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        return InetAddress.getAllByName(host)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1251
    // called from deployment cache manager
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1252
    private static InetAddress getByName(String host, InetAddress reqAddr)
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1253
        throws UnknownHostException {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1254
        return InetAddress.getAllByName(host, reqAddr)[0];
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1255
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Given the name of a host, returns an array of its IP addresses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * based on the configured name service on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * <p> The host name can either be a machine name, such as
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52499
diff changeset
  1262
     * "{@code www.example.com}", or a textual representation of its IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * address. If a literal IP address is supplied, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * validity of the address format is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1266
     * <p> For {@code host} specified in <i>literal IPv6 address</i>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * either the form defined in RFC 2732 or the literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * format defined in RFC 2373 is accepted. A literal IPv6 address may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * also be qualified by appending a scoped zone identifier or scope_id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * The syntax and usage of scope_ids is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * <a href="Inet6Address.html#scoped">here</a>.
49926
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1272
     *
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1273
     * <p> If the host is {@code null} or {@code host.length()} is equal
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1274
     * to zero, then an {@code InetAddress} representing an address of the
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1275
     * loopback interface is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * section&nbsp;2.5.3. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
49926
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1280
     * <p> If there is a security manager, and {@code host} is not {@code null}
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1281
     * or {@code host.length() } is not equal to zero, the security manager's
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1282
     * {@code checkConnect} method is called with the hostname and {@code -1}
b7c2996d690b 8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
chegar
parents: 49531
diff changeset
  1283
     * as its arguments to determine if the operation is allowed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1285
     * @param      host   the name of the host, or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @return     an array of all the IP addresses for a given host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * @exception  UnknownHostException  if no IP address for the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1289
     *               {@code host} could be found, or if a scope_id was specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *               for a global IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1292
     *               {@code checkConnect} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    public static InetAddress[] getAllByName(String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        throws UnknownHostException {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1298
        return getAllByName(host, null);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1299
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1300
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1301
    private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1302
        throws UnknownHostException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
  1304
        if (host == null || host.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            InetAddress[] ret = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            ret[0] = impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        boolean ipv6Expected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (host.charAt(0) == '[') {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1312
            // This is supposed to be an IPv6 literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            if (host.length() > 2 && host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                ipv6Expected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                // This was supposed to be a IPv6 address, but it's not!
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1318
                throw new UnknownHostException(host + ": invalid IPv6 address");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        // if host is an IP address, we won't do further lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        if (Character.digit(host.charAt(0), 16) != -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            || (host.charAt(0) == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            byte[] addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            int numericZone = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            String ifname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            // see if it is IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            addr = IPAddressUtil.textToNumericFormatV4(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            if (addr == null) {
21630
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1331
                // This is supposed to be an IPv6 literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                // Check if a numeric or string zone id is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                int pos;
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23927
diff changeset
  1334
                if ((pos=host.indexOf ('%')) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    numericZone = checkNumericZone (host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    if (numericZone == -1) { /* remainder of string must be an ifname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        ifname = host.substring (pos+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                }
21635
b938024653ca 8028074: InetAddress.getByName fails with UHE "invalid IPv6 address" if host name starts with a-f
alanb
parents: 21630
diff changeset
  1340
                if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) {
21630
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1341
                    throw new UnknownHostException(host + ": invalid IPv6 address");
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1342
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            } else if (ipv6Expected) {
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
  1344
                // Means an IPv4 literal between brackets!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                throw new UnknownHostException("["+host+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            InetAddress[] ret = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            if(addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                if (addr.length == Inet4Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    ret[0] = new Inet4Address(null, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    if (ifname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                        ret[0] = new Inet6Address(null, addr, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                        ret[0] = new Inet6Address(null, addr, numericZone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            }
21630
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1360
        } else if (ipv6Expected) {
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
  1361
            // We were expecting an IPv6 Literal, but got something else
21630
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1362
            throw new UnknownHostException("["+host+"]");
58a025b37bff 8019834: InetAddress.getByName hangs for bad IPv6 literals
chegar
parents: 20848
diff changeset
  1363
        }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1364
        return getAllByName0(host, reqAddr, true, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * Returns the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * The InetAddress returned will represent the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * loopback address, 127.0.0.1, or the IPv6 loopback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * address, ::1. The IPv4 loopback address returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * is only one of many in the form 127.*.*.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @return  the InetAddress loopback instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    public static InetAddress getLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        return impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * check if the literal address string has %nn appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * returns -1 if not, or the numeric value otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * %nn may also be a string that represents the displayName of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * a currently available NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    private static int checkNumericZone (String s) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        int percent = s.indexOf ('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        int digit, zone=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        if (percent == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        for (int i=percent+1; i<slen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            if (c == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                if (i == percent+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    /* empty per-cent field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            if ((digit = Character.digit (c, 10)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            zone = (zone * 10) + digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        return zone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    private static InetAddress[] getAllByName0 (String host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        return getAllByName0(host, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * package private so SocketPermission can call it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    static InetAddress[] getAllByName0 (String host, boolean check)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        throws UnknownHostException  {
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1425
        return getAllByName0 (host, null, check, true);
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1426
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1427
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1428
    /**
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1429
     * Designated lookup method.
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1430
     *
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1431
     * @param host host name to look up
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1432
     * @param reqAddr requested address to be the 1st in returned array
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1433
     * @param check perform security check
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1434
     * @param useCache use cached value if not expired else always
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1435
     *                 perform name service lookup (and cache the result)
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1436
     * @return array of InetAddress(es)
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1437
     * @throws UnknownHostException if host name is not found
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1438
     */
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1439
    private static InetAddress[] getAllByName0(String host,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1440
                                               InetAddress reqAddr,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1441
                                               boolean check,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1442
                                               boolean useCache)
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1443
        throws UnknownHostException  {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1444
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        /* If it gets here it is presumed to be a hostname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        /* make sure the connection to the host is allowed, before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
         * give out a hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                security.checkConnect(host, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1457
        // remove expired addresses from cache - expirySet keeps them ordered
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1458
        // by expiry time so we only need to iterate the prefix of the NavigableSet...
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1459
        long now = System.nanoTime();
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1460
        for (CachedAddresses caddrs : expirySet) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1461
            // compare difference of time instants rather than
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1462
            // time instants directly, to avoid possible overflow.
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1463
            // (see System.nanoTime() recommendations...)
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1464
            if ((caddrs.expiryTime - now) < 0L) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1465
                // ConcurrentSkipListSet uses weakly consistent iterator,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1466
                // so removing while iterating is OK...
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1467
                if (expirySet.remove(caddrs)) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1468
                    // ... remove from cache
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1469
                    cache.remove(caddrs.host, caddrs);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1470
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1471
            } else {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1472
                // we encountered 1st element that expires in future
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1473
                break;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1474
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1477
        // look-up or remove from cache
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1478
        Addresses addrs;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1479
        if (useCache) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1480
            addrs = cache.get(host);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1481
        } else {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1482
            addrs = cache.remove(host);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1483
            if (addrs != null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1484
                if (addrs instanceof CachedAddresses) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1485
                    // try removing from expirySet too if CachedAddresses
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1486
                    expirySet.remove(addrs);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1487
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1488
                addrs = null;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1489
            }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1490
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1492
        if (addrs == null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1493
            // create a NameServiceAddresses instance which will look up
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1494
            // the name service and install it within cache...
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1495
            Addresses oldAddrs = cache.putIfAbsent(
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1496
                host,
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1497
                addrs = new NameServiceAddresses(host, reqAddr)
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1498
            );
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1499
            if (oldAddrs != null) { // lost putIfAbsent race
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1500
                addrs = oldAddrs;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1501
            }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1502
        }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1503
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1504
        // ask Addresses to get an array of InetAddress(es) and clone it
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1505
        return addrs.get().clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1508
    static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1511
        InetAddress[] addresses = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        UnknownHostException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 7668
diff changeset
  1514
            try {
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1515
                addresses = nameService.lookupAllHostAddr(host);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1516
            } catch (UnknownHostException uhe) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1517
                if (host.equalsIgnoreCase("localhost")) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1518
                    addresses = new InetAddress[] { impl.loopbackAddress() };
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1519
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1520
                else {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1521
                    ex = uhe;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1522
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1523
            }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1524
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1525
        if (addresses == null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1526
            throw ex == null ? new UnknownHostException(host) : ex;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1527
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1529
        // More to do?
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1530
        if (reqAddr != null && addresses.length > 1 && !addresses[0].equals(reqAddr)) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1531
            // Find it?
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1532
            int i = 1;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1533
            for (; i < addresses.length; i++) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1534
                if (addresses[i].equals(reqAddr)) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1535
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1537
            }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1538
            // Rotate
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1539
            if (i < addresses.length) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1540
                InetAddress tmp, tmp2 = reqAddr;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1541
                for (int j = 0; j < i; j++) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1542
                    tmp = addresses[j];
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1543
                    addresses[j] = tmp2;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1544
                    tmp2 = tmp;
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1545
                }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1546
                addresses[i] = tmp2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1550
        return addresses;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1554
     * Returns an {@code InetAddress} object given the raw IP address .
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * The argument is in network byte order: the highest order
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1556
     * byte of the address is in {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * <p> This method doesn't block, i.e. no reverse name service lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * must be 16 bytes long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @return  an InetAddress object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * @exception  UnknownHostException  if IP address is of illegal length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    public static InetAddress getByAddress(byte[] addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        return getByAddress(null, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1574
    private static final class CachedLocalHost {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1575
        final String host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1576
        final InetAddress addr;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1577
        final long expiryTime = System.nanoTime() + 5000_000_000L; // now + 5s;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1578
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1579
        CachedLocalHost(String host, InetAddress addr) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1580
            this.host = host;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1581
            this.addr = addr;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1582
        }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1583
    }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1584
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1585
    private static volatile CachedLocalHost cachedLocalHost;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * Returns the address of the local host. This is achieved by retrieving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * the name of the host from the system, then resolving that name into
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1590
     * an {@code InetAddress}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * <P>Note: The resolved address may be cached for a short period of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * </P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * <p>If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1596
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
  1597
     * with the local host name and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * If the operation is not allowed, an InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * the loopback address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * @return     the address of the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * @exception  UnknownHostException  if the local host name could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *             be resolved into an address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @see java.net.InetAddress#getByName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    public static InetAddress getLocalHost() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        try {
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1614
            // is cached data still valid?
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1615
            CachedLocalHost clh = cachedLocalHost;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1616
            if (clh != null && (clh.expiryTime - System.nanoTime()) >= 0L) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1617
                if (security != null) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1618
                    security.checkConnect(clh.host, -1);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1619
                }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1620
                return clh.addr;
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1621
            }
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1622
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            String local = impl.getLocalHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                security.checkConnect(local, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1629
            InetAddress localAddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            if (local.equals("localhost")) {
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1631
                // shortcut for "localhost" host name
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1632
                localAddr = impl.loopbackAddress();
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1633
            } else {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1634
                // call getAllByName0 without security checks and
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1635
                // without using cached data
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1636
                try {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1637
                    localAddr = getAllByName0(local, null, false, false)[0];
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1638
                } catch (UnknownHostException uhe) {
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1639
                    // Rethrow with a more informative error message.
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1640
                    UnknownHostException uhe2 =
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1641
                        new UnknownHostException(local + ": " +
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1642
                                                 uhe.getMessage());
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1643
                    uhe2.initCause(uhe);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1644
                    throw uhe2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            }
26199
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1647
            cachedLocalHost = new CachedLocalHost(local, localAddr);
f30d82a2c06f 8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents: 25859
diff changeset
  1648
            return localAddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        } catch (java.lang.SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            return impl.loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * Returns the InetAddress representing anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * (typically 0.0.0.0 or ::0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    static InetAddress anyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        return impl.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * Load and instantiate an underlying impl class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     */
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1671
    static InetAddressImpl loadImpl(String implName) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1672
        Object impl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         * Property "impl.prefix" will be prepended to the classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * of the implementation object we instantiate, to which we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         * delegate the real work (like native methods).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         * property can vary across implementations of the java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         * classes.  The default is an empty String "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         */
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
  1681
        String prefix = GetPropertyAction.privilegedGetProperty("impl.prefix", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1683
            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1684
            Object tmp = Class.forName("java.net." + prefix + implName).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1685
            impl = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            System.err.println("Class not found: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            System.err.println("Could not instantiate: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            System.err.println("Cannot access class: java.net." + prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                               implName + ":\ncheck impl.prefix property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                               "in your properties file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        if (impl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1702
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1703
                Object tmp = Class.forName(implName).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1704
                impl = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                throw new Error("System property impl.prefix incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1710
        return (InetAddressImpl) impl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1712
51181
01b8120f867a 8208060: Additional corrections of serial-related declarations
darcy
parents: 50846
diff changeset
  1713
    private void readObjectNoData () {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1714
        if (getClass().getClassLoader() != null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1715
            throw new SecurityException ("invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1716
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1717
    }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1718
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45437
diff changeset
  1719
    private static final jdk.internal.misc.Unsafe UNSAFE
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45437
diff changeset
  1720
            = jdk.internal.misc.Unsafe.getUnsafe();
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45437
diff changeset
  1721
    private static final long FIELDS_OFFSET
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45437
diff changeset
  1722
            = UNSAFE.objectFieldOffset(InetAddress.class, "holder");
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1723
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1724
    private void readObject (ObjectInputStream s) throws
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1725
                         IOException, ClassNotFoundException {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1726
        if (getClass().getClassLoader() != null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1727
            throw new SecurityException ("invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1728
        }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1729
        GetField gf = s.readFields();
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1730
        String host = (String)gf.get("hostName", null);
52157
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
  1731
        int address = gf.get("address", 0);
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
  1732
        int family = gf.get("family", 0);
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
  1733
        if (family != IPv4 && family != IPv6) {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
  1734
            throw new InvalidObjectException("invalid address family type: " + family);
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 51181
diff changeset
  1735
        }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1736
        InetAddressHolder h = new InetAddressHolder(host, address, family);
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 52157
diff changeset
  1737
        UNSAFE.putReference(this, FIELDS_OFFSET, h);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1738
    }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1739
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1740
    /* needed because the serializable fields no longer exist */
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1741
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1742
    /**
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1743
     * @serialField hostName String
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1744
     * @serialField address int
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1745
     * @serialField family int
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1746
     */
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1747
    private static final ObjectStreamField[] serialPersistentFields = {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1748
        new ObjectStreamField("hostName", String.class),
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1749
        new ObjectStreamField("address", int.class),
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1750
        new ObjectStreamField("family", int.class),
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1751
    };
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1752
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1753
    private void writeObject (ObjectOutputStream s) throws
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1754
                        IOException {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1755
        if (getClass().getClassLoader() != null) {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1756
            throw new SecurityException ("invalid address type");
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1757
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1758
        PutField pf = s.putFields();
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1759
        pf.put("hostName", holder().getHostName());
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1760
        pf.put("address", holder().getAddress());
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1761
        pf.put("family", holder().getFamily());
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1762
        s.writeFields();
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
  1763
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
 * Simple factory to create the impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
class InetAddressImplFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    static InetAddressImpl create() {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1772
        return InetAddress.loadImpl(isIPv6Supported() ?
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
  1773
                                    "Inet6AddressImpl" : "Inet4AddressImpl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    static native boolean isIPv6Supported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
}