diff --git a/src/java.base/share/classes/java/lang/RuntimePermission.java b/src/java.base/share/classes/java/lang/RuntimePermission.java index db985101f7c..7df4c5fac5b 100644 --- a/src/java.base/share/classes/java/lang/RuntimePermission.java +++ b/src/java.base/share/classes/java/lang/RuntimePermission.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -378,6 +378,16 @@ import java.lang.module.ModuleFinder; * {@linkplain ModuleFinder#ofSystem system modules} in the runtime image. * * + *
There is a couple of - * System Properties affecting how IPv4 and IPv6 addresses are used.
+ *There is a couple of + * System Properties affecting how IPv4 and IPv6 addresses are used. * - *
The InetAddress class provides methods to resolve host names to + * their IP addresses and vice versa. The actual resolution is delegated to an + * {@linkplain InetAddressResolver InetAddress resolver}. + * + *
Host name-to-IP address resolution maps a host name to an IP address. + * For any host name, its corresponding IP address is returned. * *
Reverse name resolution means that for any IP address, * the host associated with the IP address is returned. * - *
The InetAddress class provides methods to resolve host names to - * their IP addresses and vice versa. + *
The built-in InetAddress resolver implementation does + * host name-to-IP address resolution and vice versa through the use of + * a combination of local machine configuration information and network + * naming services such as the Domain Name System (DNS) and the Lightweight Directory + * Access Protocol (LDAP). + * The particular naming services that the built-in resolver uses by default + * depends on the configuration of the local machine. * - *
{@code InetAddress} has a service provider mechanism for InetAddress resolvers + * that allows a custom InetAddress resolver to be used instead of the built-in implementation. + * {@link InetAddressResolverProvider} is the service provider class. Its API docs provide all the + * details on this mechanism. + * + *
The file format is that which corresponds with the /etc/hosts file * IP Address host alias list. * - *
When the file lookup is enabled it replaces the default NameService + *
When the file lookup is enabled it replaces the default InetAddressResolver
* implementation
*
* @since 9
*/
- private static final class HostsFileNameService implements NameService {
-
- private static final InetAddress[] EMPTY_ARRAY = new InetAddress[0];
-
- // Specify if only IPv4 addresses should be returned by HostsFileService implementation
- private static final boolean preferIPv4Stack = Boolean.parseBoolean(
- GetPropertyAction.privilegedGetProperty("java.net.preferIPv4Stack"));
+ private static final class HostsFileResolver implements InetAddressResolver {
private final String hostsFile;
- public HostsFileNameService(String hostsFileName) {
+ public HostsFileResolver(String hostsFileName) {
this.hostsFile = hostsFileName;
}
@@ -974,17 +1086,22 @@ public class InetAddress implements java.io.Serializable {
*
* @param addr byte array representing an IP address
* @return {@code String} representing the host name mapping
- * @throws UnknownHostException
- * if no host found for the specified IP address
+ * @throws UnknownHostException if no host found for the specified IP address
+ * @throws IllegalArgumentException if IP address is of illegal length
+ * @throws NullPointerException if addr is {@code null}
*/
@Override
- public String getHostByAddr(byte[] addr) throws UnknownHostException {
+ public String lookupByAddress(byte[] addr) throws UnknownHostException {
String hostEntry;
String host = null;
+ Objects.requireNonNull(addr);
+ // Check the length of the address array
+ if (addr.length != Inet4Address.INADDRSZ && addr.length != Inet6Address.INADDRSZ) {
+ throw new IllegalArgumentException("Invalid address length");
+ }
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile),
- UTF_8.INSTANCE))
- {
+ UTF_8.INSTANCE)) {
while (hostsFileScanner.hasNextLine()) {
hostEntry = hostsFileScanner.nextLine();
if (!hostEntry.startsWith("#")) {
@@ -1020,22 +1137,31 @@ public class InetAddress implements java.io.Serializable {
* with the specified host name.
*
* @param host the specified hostname
- * @return array of IP addresses for the requested host
+ * @param lookupPolicy IP addresses lookup policy which specifies addresses
+ * family and their order
+ * @return stream of IP addresses for the requested host
+ * @throws NullPointerException if either parameter is {@code null}
* @throws UnknownHostException
* if no IP address for the {@code host} could be found
*/
- public InetAddress[] lookupAllHostAddr(String host)
+ public Stream The default NameService is the PlatformNameService, which typically
+ * The default InetAddressResolver is the PlatformResolver, which typically
* delegates name and address resolution calls to the underlying
* OS network libraries.
*
- * A HostsFileNameService is created if the {@code jdk.net.hosts.file}
+ * A HostsFileResolver is created if the {@code jdk.net.hosts.file}
* system property is set. If the specified file doesn't exist, the name or
* address lookup will result in an UnknownHostException. Thus, non existent
* hosts file is handled as if the file is empty.
*
- * @return a NameService
+ * @return an InetAddressResolver
*/
- private static NameService createNameService() {
-
- String hostsFileName =
- GetPropertyAction.privilegedGetProperty("jdk.net.hosts.file");
- NameService theNameService;
- if (hostsFileName != null) {
- theNameService = new HostsFileNameService(hostsFileName);
+ private static InetAddressResolver createBuiltinInetAddressResolver() {
+ InetAddressResolver theResolver;
+ if (HOSTS_FILE_NAME != null) {
+ theResolver = new HostsFileResolver(HOSTS_FILE_NAME);
} else {
- theNameService = new PlatformNameService();
+ theResolver = new PlatformResolver();
}
- return theNameService;
+ return theResolver;
}
/**
* Creates an InetAddress based on the provided host name and IP address.
- * No name service is checked for the validity of the address.
+ * The system-wide {@linkplain InetAddressResolver resolver} is not used to check
+ * the validity of the address.
*
* The host name can either be a machine name, such as
* "{@code www.example.com}", or a textual representation of its IP
@@ -1251,15 +1389,9 @@ public class InetAddress implements java.io.Serializable {
return InetAddress.getAllByName(host)[0];
}
- // called from deployment cache manager
- private static InetAddress getByName(String host, InetAddress reqAddr)
- throws UnknownHostException {
- return InetAddress.getAllByName(host, reqAddr)[0];
- }
-
/**
* Given the name of a host, returns an array of its IP addresses,
- * based on the configured name service on the system.
+ * based on the configured system {@linkplain InetAddressResolver resolver}.
*
* The host name can either be a machine name, such as
* "{@code www.example.com}", or a textual representation of its IP
@@ -1298,11 +1430,6 @@ public class InetAddress implements java.io.Serializable {
*/
public static InetAddress[] getAllByName(String host)
throws UnknownHostException {
- return getAllByName(host, null);
- }
-
- private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
- throws UnknownHostException {
if (host == null || host.isEmpty()) {
InetAddress[] ret = new InetAddress[1];
@@ -1364,7 +1491,7 @@ public class InetAddress implements java.io.Serializable {
// We were expecting an IPv6 Literal, but got something else
throw new UnknownHostException("["+host+"]");
}
- return getAllByName0(host, reqAddr, true, true);
+ return getAllByName0(host, true, true);
}
/**
@@ -1414,25 +1541,18 @@ public class InetAddress implements java.io.Serializable {
return zone;
}
- private static InetAddress[] getAllByName0 (String host)
- throws UnknownHostException
- {
- return getAllByName0(host, true);
- }
-
/**
* package private so SocketPermission can call it
*/
static InetAddress[] getAllByName0 (String host, boolean check)
throws UnknownHostException {
- return getAllByName0 (host, null, check, true);
+ return getAllByName0(host, check, true);
}
/**
* Designated lookup method.
*
* @param host host name to look up
- * @param reqAddr requested address to be the 1st in returned array
* @param check perform security check
* @param useCache use cached value if not expired else always
* perform name service lookup (and cache the result)
@@ -1440,7 +1560,6 @@ public class InetAddress implements java.io.Serializable {
* @throws UnknownHostException if host name is not found
*/
private static InetAddress[] getAllByName0(String host,
- InetAddress reqAddr,
boolean check,
boolean useCache)
throws UnknownHostException {
@@ -1498,7 +1617,7 @@ public class InetAddress implements java.io.Serializable {
// the name service and install it within cache...
Addresses oldAddrs = cache.putIfAbsent(
host,
- addrs = new NameServiceAddresses(host, reqAddr)
+ addrs = new NameServiceAddresses(host)
);
if (oldAddrs != null) { // lost putIfAbsent race
addrs = oldAddrs;
@@ -1509,47 +1628,30 @@ public class InetAddress implements java.io.Serializable {
return addrs.get().clone();
}
- static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
+ static InetAddress[] getAddressesFromNameService(String host)
throws UnknownHostException {
- InetAddress[] addresses = null;
+ Stream This method doesn't block, i.e. no reverse name service lookup
- * is performed.
+ * This method doesn't block, i.e. no reverse lookup is performed.
*
* IPv4 address byte array must be 4 bytes long and IPv6 byte array
* must be 16 bytes long
@@ -1637,7 +1738,7 @@ public class InetAddress implements java.io.Serializable {
// call getAllByName0 without security checks and
// without using cached data
try {
- localAddr = getAllByName0(local, null, false, false)[0];
+ localAddr = getAllByName0(local, false, false)[0];
} catch (UnknownHostException uhe) {
// Rethrow with a more informative error message.
UnknownHostException uhe2 =
diff --git a/src/java.base/share/classes/java/net/InetAddressImpl.java b/src/java.base/share/classes/java/net/InetAddressImpl.java
index a2f8ea01052..f0364ffaedf 100644
--- a/src/java.base/share/classes/java/net/InetAddressImpl.java
+++ b/src/java.base/share/classes/java/net/InetAddressImpl.java
@@ -24,7 +24,10 @@
*/
package java.net;
+
import java.io.IOException;
+import java.net.spi.InetAddressResolver.LookupPolicy;
+
/*
* Package private interface to "implementation" used by
* {@link InetAddress}.
@@ -38,7 +41,7 @@ sealed interface InetAddressImpl permits Inet4AddressImpl, Inet6AddressImpl {
String getLocalHostName() throws UnknownHostException;
InetAddress[]
- lookupAllHostAddr(String hostname) throws UnknownHostException;
+ lookupAllHostAddr(String hostname, LookupPolicy lookupPolicy) throws UnknownHostException;
String getHostByAddr(byte[] addr) throws UnknownHostException;
InetAddress anyLocalAddress();
diff --git a/src/java.base/share/classes/java/net/doc-files/net-properties.html b/src/java.base/share/classes/java/net/doc-files/net-properties.html
index ea0311b7161..0c2d3e232da 100644
--- a/src/java.base/share/classes/java/net/doc-files/net-properties.html
+++ b/src/java.base/share/classes/java/net/doc-files/net-properties.html
@@ -1,6 +1,6 @@