< prev index next >

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

Print this page

  28 import java.util.List;
  29 import java.util.NavigableSet;
  30 import java.util.ArrayList;
  31 import java.util.Objects;
  32 import java.util.Scanner;
  33 import java.io.File;
  34 import java.io.ObjectStreamException;
  35 import java.io.ObjectStreamField;
  36 import java.io.IOException;
  37 import java.io.InvalidObjectException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectInputStream.GetField;
  40 import java.io.ObjectOutputStream;
  41 import java.io.ObjectOutputStream.PutField;
  42 import java.lang.annotation.Native;
  43 import java.util.concurrent.ConcurrentHashMap;
  44 import java.util.concurrent.ConcurrentMap;
  45 import java.util.concurrent.ConcurrentSkipListSet;
  46 import java.util.concurrent.atomic.AtomicLong;
  47 import java.util.Arrays;

  48 
  49 import jdk.internal.access.JavaNetInetAddressAccess;
  50 import jdk.internal.access.SharedSecrets;

  51 import sun.security.action.*;
  52 import sun.net.InetAddressCachePolicy;
  53 import sun.net.util.IPAddressUtil;
  54 import sun.nio.cs.UTF_8;
  55 
  56 /**
  57  * This class represents an Internet Protocol (IP) address.
  58  *
  59  * <p> An IP address is either a 32-bit or 128-bit unsigned number
  60  * used by IP, a lower-level protocol on which protocols like UDP and
  61  * TCP are built. The IP address architecture is defined by <a
  62  * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC&nbsp;790:
  63  * Assigned Numbers</i></a>, <a
  64  * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC&nbsp;1918:
  65  * Address Allocation for Private Internets</i></a>, <a
  66  * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
  67  * Administratively Scoped IP Multicast</i></a>, and <a
  68  * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
  69  * Version 6 Addressing Architecture</i></a>. An instance of an
  70  * InetAddress consists of an IP address and possibly its

 805 
 806         @Override
 807         public int compareTo(CachedAddresses other) {
 808             // natural order is expiry time -
 809             // compare difference of expiry times rather than
 810             // expiry times directly, to avoid possible overflow.
 811             // (see System.nanoTime() recommendations...)
 812             long diff = this.expiryTime - other.expiryTime;
 813             if (diff < 0L) return -1;
 814             if (diff > 0L) return 1;
 815             // ties are broken using unique id
 816             return Long.compare(this.id, other.id);
 817         }
 818     }
 819 
 820     // a name service lookup based Addresses implementation which replaces itself
 821     // in cache when the result is obtained
 822     private static final class NameServiceAddresses implements Addresses {
 823         private final String host;
 824         private final InetAddress reqAddr;

 825 
 826         NameServiceAddresses(String host, InetAddress reqAddr) {
 827             this.host = host;
 828             this.reqAddr = reqAddr;
 829         }
 830 
 831         @Override
 832         public InetAddress[] get() throws UnknownHostException {
 833             Addresses addresses;
 834             // only one thread is doing lookup to name service
 835             // for particular host at any time.
 836             synchronized (this) {

 837                 // re-check that we are still us + re-install us if slot empty
 838                 addresses = cache.putIfAbsent(host, this);
 839                 if (addresses == null) {
 840                     // this can happen when we were replaced by CachedAddresses in
 841                     // some other thread, then CachedAddresses expired and were
 842                     // removed from cache while we were waiting for lock...
 843                     addresses = this;
 844                 }
 845                 // still us ?
 846                 if (addresses == this) {
 847                     // lookup name services
 848                     InetAddress[] inetAddresses;
 849                     UnknownHostException ex;
 850                     int cachePolicy;
 851                     try {
 852                         inetAddresses = getAddressesFromNameService(host, reqAddr);
 853                         ex = null;
 854                         cachePolicy = InetAddressCachePolicy.get();
 855                     } catch (UnknownHostException uhe) {
 856                         inetAddresses = null;

 864                         CachedAddresses cachedAddresses = new CachedAddresses(
 865                             host,
 866                             inetAddresses,
 867                             cachePolicy == InetAddressCachePolicy.FOREVER
 868                             ? 0L
 869                             // cachePolicy is in [s] - we need [ns]
 870                             : System.nanoTime() + 1000_000_000L * cachePolicy
 871                         );
 872                         if (cache.replace(host, this, cachedAddresses) &&
 873                             cachePolicy != InetAddressCachePolicy.FOREVER) {
 874                             // schedule expiry
 875                             expirySet.add(cachedAddresses);
 876                         }
 877                     }
 878                     if (inetAddresses == null) {
 879                         throw ex == null ? new UnknownHostException(host) : ex;
 880                     }
 881                     return inetAddresses;
 882                 }
 883                 // else addresses != this


 884             }
 885             // delegate to different addresses when we are already replaced
 886             // but outside of synchronized block to avoid any chance of dead-locking
 887             return addresses.get();
 888         }
 889     }
 890 
 891     /**
 892      * NameService provides host and address lookup service
 893      *
 894      * @since 9
 895      */
 896     private interface NameService {
 897 
 898         /**
 899          * Lookup a host mapping by name. Retrieve the IP addresses
 900          * associated with a host
 901          *
 902          * @param host the specified hostname
 903          * @return array of IP addresses for the requested host

 913          * @param addr byte array representing an IP address
 914          * @return {@code String} representing the host name mapping
 915          * @throws UnknownHostException
 916          *             if no host found for the specified IP address
 917          */
 918         String getHostByAddr(byte[] addr) throws UnknownHostException;
 919 
 920     }
 921 
 922     /**
 923      * The default NameService implementation, which delegates to the underlying
 924      * OS network libraries to resolve host address mappings.
 925      *
 926      * @since 9
 927      */
 928     private static final class PlatformNameService implements NameService {
 929 
 930         public InetAddress[] lookupAllHostAddr(String host)
 931             throws UnknownHostException
 932         {
 933             return impl.lookupAllHostAddr(host);




 934         }
 935 
 936         public String getHostByAddr(byte[] addr)
 937             throws UnknownHostException
 938         {
 939             return impl.getHostByAddr(addr);




 940         }
 941     }
 942 
 943     /**
 944      * The HostsFileNameService provides host address mapping
 945      * by reading the entries in a hosts file, which is specified by
 946      * {@code jdk.net.hosts.file} system property
 947      *
 948      * <p>The file format is that which corresponds with the /etc/hosts file
 949      * IP Address host alias list.
 950      *
 951      * <p>When the file lookup is enabled it replaces the default NameService
 952      * implementation
 953      *
 954      * @since 9
 955      */
 956     private static final class HostsFileNameService implements NameService {
 957 
 958         private static final InetAddress[] EMPTY_ARRAY = new InetAddress[0];
 959 

  28 import java.util.List;
  29 import java.util.NavigableSet;
  30 import java.util.ArrayList;
  31 import java.util.Objects;
  32 import java.util.Scanner;
  33 import java.io.File;
  34 import java.io.ObjectStreamException;
  35 import java.io.ObjectStreamField;
  36 import java.io.IOException;
  37 import java.io.InvalidObjectException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectInputStream.GetField;
  40 import java.io.ObjectOutputStream;
  41 import java.io.ObjectOutputStream.PutField;
  42 import java.lang.annotation.Native;
  43 import java.util.concurrent.ConcurrentHashMap;
  44 import java.util.concurrent.ConcurrentMap;
  45 import java.util.concurrent.ConcurrentSkipListSet;
  46 import java.util.concurrent.atomic.AtomicLong;
  47 import java.util.Arrays;
  48 import java.util.concurrent.locks.ReentrantLock;
  49 
  50 import jdk.internal.access.JavaNetInetAddressAccess;
  51 import jdk.internal.access.SharedSecrets;
  52 import jdk.internal.misc.Blocker;
  53 import sun.security.action.*;
  54 import sun.net.InetAddressCachePolicy;
  55 import sun.net.util.IPAddressUtil;
  56 import sun.nio.cs.UTF_8;
  57 
  58 /**
  59  * This class represents an Internet Protocol (IP) address.
  60  *
  61  * <p> An IP address is either a 32-bit or 128-bit unsigned number
  62  * used by IP, a lower-level protocol on which protocols like UDP and
  63  * TCP are built. The IP address architecture is defined by <a
  64  * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC&nbsp;790:
  65  * Assigned Numbers</i></a>, <a
  66  * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC&nbsp;1918:
  67  * Address Allocation for Private Internets</i></a>, <a
  68  * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
  69  * Administratively Scoped IP Multicast</i></a>, and <a
  70  * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
  71  * Version 6 Addressing Architecture</i></a>. An instance of an
  72  * InetAddress consists of an IP address and possibly its

 807 
 808         @Override
 809         public int compareTo(CachedAddresses other) {
 810             // natural order is expiry time -
 811             // compare difference of expiry times rather than
 812             // expiry times directly, to avoid possible overflow.
 813             // (see System.nanoTime() recommendations...)
 814             long diff = this.expiryTime - other.expiryTime;
 815             if (diff < 0L) return -1;
 816             if (diff > 0L) return 1;
 817             // ties are broken using unique id
 818             return Long.compare(this.id, other.id);
 819         }
 820     }
 821 
 822     // a name service lookup based Addresses implementation which replaces itself
 823     // in cache when the result is obtained
 824     private static final class NameServiceAddresses implements Addresses {
 825         private final String host;
 826         private final InetAddress reqAddr;
 827         private final ReentrantLock lookupLock = new ReentrantLock();
 828 
 829         NameServiceAddresses(String host, InetAddress reqAddr) {
 830             this.host = host;
 831             this.reqAddr = reqAddr;
 832         }
 833 
 834         @Override
 835         public InetAddress[] get() throws UnknownHostException {
 836             Addresses addresses;
 837             // only one thread is doing lookup to name service
 838             // for particular host at any time.
 839             lookupLock.lock();
 840             try {
 841                 // re-check that we are still us + re-install us if slot empty
 842                 addresses = cache.putIfAbsent(host, this);
 843                 if (addresses == null) {
 844                     // this can happen when we were replaced by CachedAddresses in
 845                     // some other thread, then CachedAddresses expired and were
 846                     // removed from cache while we were waiting for lock...
 847                     addresses = this;
 848                 }
 849                 // still us ?
 850                 if (addresses == this) {
 851                     // lookup name services
 852                     InetAddress[] inetAddresses;
 853                     UnknownHostException ex;
 854                     int cachePolicy;
 855                     try {
 856                         inetAddresses = getAddressesFromNameService(host, reqAddr);
 857                         ex = null;
 858                         cachePolicy = InetAddressCachePolicy.get();
 859                     } catch (UnknownHostException uhe) {
 860                         inetAddresses = null;

 868                         CachedAddresses cachedAddresses = new CachedAddresses(
 869                             host,
 870                             inetAddresses,
 871                             cachePolicy == InetAddressCachePolicy.FOREVER
 872                             ? 0L
 873                             // cachePolicy is in [s] - we need [ns]
 874                             : System.nanoTime() + 1000_000_000L * cachePolicy
 875                         );
 876                         if (cache.replace(host, this, cachedAddresses) &&
 877                             cachePolicy != InetAddressCachePolicy.FOREVER) {
 878                             // schedule expiry
 879                             expirySet.add(cachedAddresses);
 880                         }
 881                     }
 882                     if (inetAddresses == null) {
 883                         throw ex == null ? new UnknownHostException(host) : ex;
 884                     }
 885                     return inetAddresses;
 886                 }
 887                 // else addresses != this
 888             } finally {
 889                 lookupLock.unlock();
 890             }
 891             // delegate to different addresses when we are already replaced
 892             // but outside of synchronized block to avoid any chance of dead-locking
 893             return addresses.get();
 894         }
 895     }
 896 
 897     /**
 898      * NameService provides host and address lookup service
 899      *
 900      * @since 9
 901      */
 902     private interface NameService {
 903 
 904         /**
 905          * Lookup a host mapping by name. Retrieve the IP addresses
 906          * associated with a host
 907          *
 908          * @param host the specified hostname
 909          * @return array of IP addresses for the requested host

 919          * @param addr byte array representing an IP address
 920          * @return {@code String} representing the host name mapping
 921          * @throws UnknownHostException
 922          *             if no host found for the specified IP address
 923          */
 924         String getHostByAddr(byte[] addr) throws UnknownHostException;
 925 
 926     }
 927 
 928     /**
 929      * The default NameService implementation, which delegates to the underlying
 930      * OS network libraries to resolve host address mappings.
 931      *
 932      * @since 9
 933      */
 934     private static final class PlatformNameService implements NameService {
 935 
 936         public InetAddress[] lookupAllHostAddr(String host)
 937             throws UnknownHostException
 938         {
 939             if (Thread.currentThread().isVirtual()) {
 940                 return Blocker.managedBlock(() -> impl.lookupAllHostAddr(host));
 941             } else {
 942                 return impl.lookupAllHostAddr(host);
 943             }
 944         }
 945 
 946         public String getHostByAddr(byte[] addr)
 947             throws UnknownHostException
 948         {
 949             if (Thread.currentThread().isVirtual()) {
 950                 return Blocker.managedBlock(() ->  impl.getHostByAddr(addr));
 951             } else {
 952                 return impl.getHostByAddr(addr);
 953             }
 954         }
 955     }
 956 
 957     /**
 958      * The HostsFileNameService provides host address mapping
 959      * by reading the entries in a hosts file, which is specified by
 960      * {@code jdk.net.hosts.file} system property
 961      *
 962      * <p>The file format is that which corresponds with the /etc/hosts file
 963      * IP Address host alias list.
 964      *
 965      * <p>When the file lookup is enabled it replaces the default NameService
 966      * implementation
 967      *
 968      * @since 9
 969      */
 970     private static final class HostsFileNameService implements NameService {
 971 
 972         private static final InetAddress[] EMPTY_ARRAY = new InetAddress[0];
 973 
< prev index next >