src/java.base/share/classes/java/net/InetAddress.java

Print this page

        

*** 279,289 **** InetAddressHolder holder() { return holder; } /* Used to store the name service provider */ ! private static transient NameService nameService = null; /* Used to store the best available hostname */ private transient String canonicalHostName = null; /** use serialVersionUID from JDK 1.0.2 for interoperability */ --- 279,289 ---- InetAddressHolder holder() { return holder; } /* Used to store the name service provider */ ! private static NameService nameService = null; /* Used to store the best available hostname */ private transient String canonicalHostName = null; /** use serialVersionUID from JDK 1.0.2 for interoperability */
*** 856,927 **** } /** * NameService provides host and address lookup service * */ private interface NameService { /** ! * Lookup a host mapping by name. Retrieve the IP addresses ! * associated with a host * * @param host the specified hostname ! * @return array of IP addresses for the requested host * @throws UnknownHostException * if no IP address for the {@code host} could be found */ InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException; /** ! * Lookup the host corresponding to the IP address provided * * @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 */ String getHostByAddr(byte[] addr) throws UnknownHostException; - } /** * The default NameService implementation, which delegates to the underlying * OS network libraries to resolve host address mappings. * */ private static final class PlatformNameService implements NameService { public InetAddress[] lookupAllHostAddr(String host) ! throws UnknownHostException { ! return impl.lookupAllHostAddr(host); - } public String getHostByAddr(byte[] addr) throws UnknownHostException { - return impl.getHostByAddr(addr); - } - } /** ! * <p>The HostsFileNameService provides host address mapping ! * by reading the entries in a hosts file, which is specified in the ! * property jdk.net.hosts.file * ! * <p>The file format is that which corresponds with the /etc/hosts file ! * ipaddress host alias list * ! * <p>When the file lookup is enabled it replaces the call flows to the native ! * OS ip nameservice lookup calls * ! * <p>This functionalty doesn't currently support IPV6 mappings ! * This can be provided whenever it is required */ private static final class HostsFileNameService implements NameService { private final String hostsFile; --- 856,923 ---- } /** * NameService provides host and address lookup service * + * @since 9 */ private interface NameService { /** ! * Looks up a host mapping by name. Retrieves the IP addresses ! * associated with a host. * * @param host the specified hostname ! * @return an array of IP addresses for the given host name * @throws UnknownHostException * if no IP address for the {@code host} could be found */ InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException; /** ! * Looks up the host corresponding to the given IP address. * * @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 */ String getHostByAddr(byte[] addr) throws UnknownHostException; } /** * The default NameService implementation, which delegates to the underlying * OS network libraries to resolve host address mappings. * + * @since 9 */ private static final class PlatformNameService implements NameService { public InetAddress[] lookupAllHostAddr(String host) ! throws UnknownHostException ! { return impl.lookupAllHostAddr(host); } public String getHostByAddr(byte[] addr) throws UnknownHostException { return impl.getHostByAddr(addr); } } /** ! * A HostsFileNameService provides host address mappings by reading the ! * entries in a hosts file, which is specified by the {@code ! * jdk.net.hosts.file} system property. * ! * <p> The file format is that which corresponds to the /etc/hosts file ! * IP Address host alias list. * ! * <p> When the file lookup is enabled it replaces the default NameService ! * implementation. * ! * @since 9 */ private static final class HostsFileNameService implements NameService { private final String hostsFile;
*** 933,951 **** return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "." + Byte.toString(addr[2]) + "." + Byte.toString(addr[3]); } /** ! * Lookup the host name corresponding to the IP address provided. ! * Search the configured host file a host name corresponding to * the specified IP address. * * @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 */ public String getHostByAddr(byte[] addr) throws UnknownHostException { String hostEntry; String host = null; String addrString = addrToString(addr); --- 929,948 ---- return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "." + Byte.toString(addr[2]) + "." + Byte.toString(addr[3]); } /** ! * Looks up the host name corresponding to the IP address provided. ! * Searches the configured host file for a host name corresponding to * the specified IP address. * * @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 */ + @Override public String getHostByAddr(byte[] addr) throws UnknownHostException { String hostEntry; String host = null; String addrString = addrToString(addr);
*** 977,1003 **** return host; } /** ! * <p>Lookup a host mapping by name. Retrieve the IP addresses * associated with a host. * ! * <p>Search the configured hosts file for the addresses assocaited with ! * with the specified host name. * * @param host the specified hostname * @return array of IP addresses for the requested host * @throws UnknownHostException * if no IP address for the {@code host} could be found */ ! public InetAddress[] lookupAllHostAddr(String host) ! throws UnknownHostException { String hostEntry; ! String addrStr = null; ! InetAddress[] res = null; byte addr[] = new byte[4]; ArrayList<InetAddress> inetAddresses = null; // lookup the file and create a list InetAddress for the specfied host try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) { --- 974,1001 ---- return host; } /** ! * Looks up a host mapping by name. Retrieves the IP addresses * associated with a host. * ! * <p> Searches the configured hosts file for the addresses associated ! * with with the specified host name. * * @param host the specified hostname * @return array of IP addresses for the requested host * @throws UnknownHostException * if no IP address for the {@code host} could be found */ ! @Override public InetAddress[] lookupAllHostAddr(String host) ! throws UnknownHostException ! { String hostEntry; ! String addrStr; ! InetAddress[] res; byte addr[] = new byte[4]; ArrayList<InetAddress> inetAddresses = null; // lookup the file and create a list InetAddress for the specfied host try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
*** 1040,1057 **** } return filteredEntry; } private void createAddressByteArray(String addrStr, byte[] addr) { - String[] ipAddr = addrStr.split("\\."); for (int i = 0; i < 4; i++) { addr[i] = (byte) Integer.parseInt(ipAddr[i]); } } ! // host to ip address mapping private String extractHostAddr(String hostEntry, String host) { String[] mapping = hostEntry.split("\\s+"); String hostAddr = null; if (mapping.length >= 2) { --- 1038,1054 ---- } return filteredEntry; } private void createAddressByteArray(String addrStr, byte[] addr) { String[] ipAddr = addrStr.split("\\."); for (int i = 0; i < 4; i++) { addr[i] = (byte) Integer.parseInt(ipAddr[i]); } } ! /** Host to IP address mapping. */ private String extractHostAddr(String hostEntry, String host) { String[] mapping = hostEntry.split("\\s+"); String hostAddr = null; if (mapping.length >= 2) {
*** 1063,1074 **** } } return hostAddr; } ! // ipaddress to host mapping ! // use first host alias in list private String extractHost(String hostEntry, String addrString) { String[] mapping = hostEntry.split("\\s+"); String host = null; if (mapping.length >= 2) { --- 1060,1070 ---- } } return hostAddr; } ! /** IP Address to host mapping. Uses first host alias in list. */ private String extractHost(String hostEntry, String addrString) { String[] mapping = hostEntry.split("\\s+"); String host = null; if (mapping.length >= 2) {
*** 1089,1123 **** // create name service nameService = createNameService(); } /** ! * Create an instance of the NameService interface based on ! * the setting of the system property jdk.net.hosts.file. * ! * <p>The default NameService is the PlatformNameService, which typically ! * delegates name and address resolution calls to the underlying ! * OS network libraries. ! * ! * <p>If jdk.net.hosts.file is set, and the file exists then ! * instantiate a HostsFileNameService ! * else ! * instantiate a PlatformNameService. * ! * @return the instantiated {@code NameService} */ private static NameService createNameService() { - String hostsFileName = AccessController .doPrivileged(new GetPropertyAction("jdk.net.hosts.file")); ! NameService theNameService; if (hostsFileName != null) { ! theNameService = new HostsFileNameService(hostsFileName); } else { ! theNameService = new PlatformNameService(); } - return theNameService; } /** * Creates an InetAddress based on the provided host name and IP address. * No name service is checked for the validity of the address. --- 1085,1117 ---- // create name service nameService = createNameService(); } /** ! * Creates an instance of the NameService interface based on the value of ! * the {@code jdk.net.hosts.file} system property. * ! * <p> The default NameService is the PlatformNameService, which typically ! * delegates name and address resolution calls to the underlying OS network ! * libraries. ! * ! * <p> A HostsFileNameService 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 */ private static NameService createNameService() { String hostsFileName = AccessController .doPrivileged(new GetPropertyAction("jdk.net.hosts.file")); ! if (hostsFileName != null) { ! return new HostsFileNameService(hostsFileName); } else { ! return new PlatformNameService(); } } /** * Creates an InetAddress based on the provided host name and IP address. * No name service is checked for the validity of the address.