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

Print this page




  26 package java.net;
  27 
  28 import java.util.NavigableSet;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 import java.util.ArrayList;
  32 import java.util.ServiceLoader;
  33 import java.security.AccessController;
  34 import java.io.ObjectStreamException;
  35 import java.io.ObjectStreamField;
  36 import java.io.IOException;
  37 import java.io.ObjectInputStream;
  38 import java.io.ObjectInputStream.GetField;
  39 import java.io.ObjectOutputStream;
  40 import java.io.ObjectOutputStream.PutField;
  41 import java.util.concurrent.ConcurrentHashMap;
  42 import java.util.concurrent.ConcurrentMap;
  43 import java.util.concurrent.ConcurrentSkipListSet;
  44 import java.util.concurrent.atomic.AtomicLong;
  45 


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


 198     /**
 199      * Specify the address family: Internet Protocol, Version 6
 200      * @since 1.4
 201      */
 202     static final int IPv6 = 2;
 203 
 204     /* Specify address family preference */
 205     static transient boolean preferIPv6Address = false;
 206 
 207     static class InetAddressHolder {
 208         /**
 209          * Reserve the original application specified hostname.
 210          *
 211          * The original hostname is useful for domain-based endpoint
 212          * identification (see RFC 2818 and RFC 6125).  If an address
 213          * was created with a raw IP address, a reverse name lookup
 214          * may introduce endpoint identification security issue via
 215          * DNS forging.
 216          *
 217          * Oracle JSSE provider is using this original hostname, via
 218          * sun.misc.JavaNetAccess, for SSL/TLS endpoint identification.
 219          *
 220          * Note: May define a new public method in the future if necessary.
 221          */
 222         private String originalHostName;
 223 
 224         InetAddressHolder() {}
 225 
 226         InetAddressHolder(String hostName, int address, int family) {
 227             this.originalHostName = hostName;
 228             this.hostName = hostName;
 229             this.address = address;
 230             this.family = family;
 231         }
 232 
 233         void init(String hostName, int family) {
 234             this.originalHostName = hostName;
 235             this.hostName = hostName;
 236             if (family != -1) {
 237                 this.family = family;
 238             }


 280 
 281     /* Used to store the best available hostname */
 282     private transient String canonicalHostName = null;
 283 
 284     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 285     private static final long serialVersionUID = 3286316764910316507L;
 286 
 287     /*
 288      * Load net library into runtime, and perform initializations.
 289      */
 290     static {
 291         preferIPv6Address = java.security.AccessController.doPrivileged(
 292             new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue();
 293         AccessController.doPrivileged(
 294             new java.security.PrivilegedAction<>() {
 295                 public Void run() {
 296                     System.loadLibrary("net");
 297                     return null;
 298                 }
 299             });
 300         sun.misc.SharedSecrets.setJavaNetInetAddressAccess(
 301                 new sun.misc.JavaNetInetAddressAccess() {
 302                     public String getOriginalHostName(InetAddress ia) {
 303                         return ia.holder.getOriginalHostName();
 304                     }
 305                 }
 306         );
 307         init();
 308     }
 309 
 310     /**
 311      * Constructor for the Socket.accept() method.
 312      * This creates an empty InetAddress, which is filled in by
 313      * the accept() method.  This InetAddress, however, is not
 314      * put in the address cache, since it is not created by name.
 315      */
 316     InetAddress() {
 317         holder = new InetAddressHolder();
 318     }
 319 
 320     /**
 321      * Replaces the de-serialized object with an Inet4Address object.




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


 200     /**
 201      * Specify the address family: Internet Protocol, Version 6
 202      * @since 1.4
 203      */
 204     static final int IPv6 = 2;
 205 
 206     /* Specify address family preference */
 207     static transient boolean preferIPv6Address = false;
 208 
 209     static class InetAddressHolder {
 210         /**
 211          * Reserve the original application specified hostname.
 212          *
 213          * The original hostname is useful for domain-based endpoint
 214          * identification (see RFC 2818 and RFC 6125).  If an address
 215          * was created with a raw IP address, a reverse name lookup
 216          * may introduce endpoint identification security issue via
 217          * DNS forging.
 218          *
 219          * Oracle JSSE provider is using this original hostname, via
 220          * jdk.internal.misc.JavaNetAccess, for SSL/TLS endpoint identification.
 221          *
 222          * Note: May define a new public method in the future if necessary.
 223          */
 224         private String originalHostName;
 225 
 226         InetAddressHolder() {}
 227 
 228         InetAddressHolder(String hostName, int address, int family) {
 229             this.originalHostName = hostName;
 230             this.hostName = hostName;
 231             this.address = address;
 232             this.family = family;
 233         }
 234 
 235         void init(String hostName, int family) {
 236             this.originalHostName = hostName;
 237             this.hostName = hostName;
 238             if (family != -1) {
 239                 this.family = family;
 240             }


 282 
 283     /* Used to store the best available hostname */
 284     private transient String canonicalHostName = null;
 285 
 286     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 287     private static final long serialVersionUID = 3286316764910316507L;
 288 
 289     /*
 290      * Load net library into runtime, and perform initializations.
 291      */
 292     static {
 293         preferIPv6Address = java.security.AccessController.doPrivileged(
 294             new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue();
 295         AccessController.doPrivileged(
 296             new java.security.PrivilegedAction<>() {
 297                 public Void run() {
 298                     System.loadLibrary("net");
 299                     return null;
 300                 }
 301             });
 302         SharedSecrets.setJavaNetInetAddressAccess(
 303                 new JavaNetInetAddressAccess() {
 304                     public String getOriginalHostName(InetAddress ia) {
 305                         return ia.holder.getOriginalHostName();
 306                     }
 307                 }
 308         );
 309         init();
 310     }
 311 
 312     /**
 313      * Constructor for the Socket.accept() method.
 314      * This creates an empty InetAddress, which is filled in by
 315      * the accept() method.  This InetAddress, however, is not
 316      * put in the address cache, since it is not created by name.
 317      */
 318     InetAddress() {
 319         holder = new InetAddressHolder();
 320     }
 321 
 322     /**
 323      * Replaces the de-serialized object with an Inet4Address object.