jdk/src/share/classes/java/net/Inet6Address.java
changeset 14783 2ecc993a8e67
parent 10596 39b3a979e600
child 16870 f35b2bd19761
equal deleted inserted replaced
14782:226059d7b730 14783:2ecc993a8e67
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.net;
    26 package java.net;
    27 
    27 
    28 import java.io.ObjectInputStream;
       
    29 import java.io.IOException;
    28 import java.io.IOException;
    30 import java.io.InvalidObjectException;
    29 import java.io.InvalidObjectException;
       
    30 import java.io.ObjectInputStream;
    31 import java.util.Enumeration;
    31 import java.util.Enumeration;
    32 
    32 
    33 /**
    33 /**
    34  * This class represents an Internet Protocol version 6 (IPv6) address.
    34  * This class represents an Internet Protocol version 6 (IPv6) address.
    35  * Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">
    35  * Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">
   114  * textual data.
   114  * textual data.
   115  *
   115  *
   116  * <h4> Special IPv6 address </h4>
   116  * <h4> Special IPv6 address </h4>
   117  *
   117  *
   118  * <blockquote>
   118  * <blockquote>
   119  * <table cellspacing=2 summary="Description of IPv4-mapped address"> <tr><th valign=top><i>IPv4-mapped address</i></th>
   119  * <table cellspacing=2 summary="Description of IPv4-mapped address">
       
   120  * <tr><th valign=top><i>IPv4-mapped address</i></th>
   120  *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
   121  *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
   121  *         represent an IPv4 address. It allows the native program to
   122  *         represent an IPv4 address. It allows the native program to
   122  *         use the same address data structure and also the same
   123  *         use the same address data structure and also the same
   123  *         socket when communicating with both IPv4 and IPv6 nodes.
   124  *         socket when communicating with both IPv4 and IPv6 nodes.
   124  *
   125  *
   128  *         IPv4-mapped address as input, both in byte array and text
   129  *         IPv4-mapped address as input, both in byte array and text
   129  *         representation. However, it will be converted into an IPv4
   130  *         representation. However, it will be converted into an IPv4
   130  *         address.</td></tr>
   131  *         address.</td></tr>
   131  * </table></blockquote>
   132  * </table></blockquote>
   132  * <p>
   133  * <p>
   133  * <h4> <A NAME="scoped">Textual representation of IPv6 scoped addresses</a> </h4>
   134  * <h4><A NAME="scoped">Textual representation of IPv6 scoped addresses</a></h4>
   134  * <p>
   135  *
   135  * The textual representation of IPv6 addresses as described above can be extended
   136  * <p> The textual representation of IPv6 addresses as described above can be
   136  * to specify IPv6 scoped addresses. This extension to the basic addressing architecture
   137  * extended to specify IPv6 scoped addresses. This extension to the basic
   137  * is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
   138  * addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
   138  * <p>
   139  *
   139  * Because link-local and site-local addresses are non-global, it is possible that different hosts
   140  * <p> Because link-local and site-local addresses are non-global, it is possible
   140  * may have the same destination address and may be reachable through different interfaces on the
   141  * that different hosts may have the same destination address and may be
   141  * same originating system. In this case, the originating system is said to be connected
   142  * reachable through different interfaces on the same originating system. In
   142  * to multiple zones of the same scope. In order to disambiguate which is the intended destination
   143  * this case, the originating system is said to be connected to multiple zones
   143  * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an IPv6 address.
   144  * of the same scope. In order to disambiguate which is the intended destination
   144  * <p>
   145  * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an
   145  * The general format for specifying the <i>scope_id</i> is the following:
   146  * IPv6 address.
       
   147  *
       
   148  * <p> The general format for specifying the <i>scope_id</i> is the following:
       
   149  *
   146  * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>
   150  * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>
   147  * <p> The IPv6-address is a literal IPv6 address as described above.
   151  * <p> The IPv6-address is a literal IPv6 address as described above.
   148  * The <i>scope_id</i> refers to an interface on the local system, and it can be specified
   152  * The <i>scope_id</i> refers to an interface on the local system, and it can be
   149  * in two ways.
   153  * specified in two ways.
   150  * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer that identifies the
   154  * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer
   151  * particular interface and scope as understood by the system. Usually, the numeric
   155  * that identifies the particular interface and scope as understood by the
   152  * values can be determined through administration tools on the system. Each interface may
   156  * system. Usually, the numeric values can be determined through administration
   153  * have multiple values, one for each scope. If the scope is unspecified, then the default value
   157  * tools on the system. Each interface may have multiple values, one for each
   154  * used is zero.</li><p>
   158  * scope. If the scope is unspecified, then the default value used is zero.</li>
   155  * <li><i>As a string.</i> This must be the exact string that is returned by
   159  * <p><li><i>As a string.</i> This must be the exact string that is returned by
   156  * {@link java.net.NetworkInterface#getName()} for the particular interface in question.
   160  * {@link java.net.NetworkInterface#getName()} for the particular interface in
   157  * When an Inet6Address is created in this way, the numeric scope-id is determined at the time
   161  * question. When an Inet6Address is created in this way, the numeric scope-id
   158  * the object is created by querying the relevant NetworkInterface.</li>
   162  * is determined at the time the object is created by querying the relevant
   159  * </ol><p>
   163  * NetworkInterface.</li></ol>
   160  * Note also, that the numeric <i>scope_id</i> can be retrieved from Inet6Address instances returned from the
   164  *
   161  * NetworkInterface class. This can be used to find out the current scope ids configured on the system.
   165  * <p> Note also, that the numeric <i>scope_id</i> can be retrieved from
       
   166  * Inet6Address instances returned from the NetworkInterface class. This can be
       
   167  * used to find out the current scope ids configured on the system.
   162  * @since 1.4
   168  * @since 1.4
   163  */
   169  */
   164 
   170 
   165 public final
   171 public final
   166 class Inet6Address extends InetAddress {
   172 class Inet6Address extends InetAddress {
   167     final static int INADDRSZ = 16;
   173     final static int INADDRSZ = 16;
   168 
   174 
   169     /*
   175     /*
   170      * cached scope_id - for link-local address use only.
   176      * cached scope_id - for link-local address use only.
   171      */
   177      */
   172     private transient int cached_scope_id = 0;
   178     private transient int cached_scope_id;  // 0
   173 
   179 
   174     /**
   180     /**
   175      * Holds a 128-bit (16 bytes) IPv6 address.
   181      * Holds a 128-bit (16 bytes) IPv6 address.
   176      *
   182      *
   177      * @serial
   183      * @serial
   178      */
   184      */
   179     byte[] ipaddress;
   185     byte[] ipaddress;
   180 
   186 
   181     /**
   187     /**
   182      * scope_id. The scope specified when the object is created. If the object is created
   188      * scope_id. The scope specified when the object is created. If the object
   183      * with an interface name, then the scope_id is not determined until the time it is needed.
   189      * is created with an interface name, then the scope_id is not determined
   184      */
   190      * until the time it is needed.
   185     private int scope_id = 0;
   191      */
       
   192     private int scope_id;  // 0
   186 
   193 
   187     /**
   194     /**
   188      * This will be set to true when the scope_id field contains a valid
   195      * This will be set to true when the scope_id field contains a valid
   189      * integer scope_id.
   196      * integer scope_id.
   190      */
   197      */
   191     private boolean scope_id_set = false;
   198     private boolean scope_id_set;  // false
   192 
   199 
   193     /**
   200     /**
   194      * scoped interface. scope_id is derived from this as the scope_id of the first
   201      * scoped interface. scope_id is derived from this as the scope_id of the first
   195      * address whose scope is the same as this address for the named interface.
   202      * address whose scope is the same as this address for the named interface.
   196      */
   203      */
   197     private transient NetworkInterface scope_ifname = null;
   204     private transient NetworkInterface scope_ifname;  // null
   198 
       
   199     /**
       
   200      * set if the object is constructed with a scoped interface instead of a
       
   201      * numeric scope id.
       
   202      */
       
   203     private boolean scope_ifname_set = false;
       
   204 
   205 
   205     private static final long serialVersionUID = 6880410070516793377L;
   206     private static final long serialVersionUID = 6880410070516793377L;
   206 
   207 
   207     /*
   208     // Perform native initialization
   208      * Perform initializations.
   209     static { init(); }
   209      */
       
   210     static {
       
   211         init();
       
   212     }
       
   213 
   210 
   214     Inet6Address() {
   211     Inet6Address() {
   215         super();
   212         super();
   216         hostName = null;
   213         hostName = null;
   217         ipaddress = new byte[INADDRSZ];
   214         ipaddress = new byte[INADDRSZ];
   237         try {
   234         try {
   238             initif (hostName, addr, null);
   235             initif (hostName, addr, null);
   239         } catch (UnknownHostException e) {} /* cant happen if ifname is null */
   236         } catch (UnknownHostException e) {} /* cant happen if ifname is null */
   240     }
   237     }
   241 
   238 
   242     Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException {
   239     Inet6Address (String hostName, byte addr[], NetworkInterface nif)
       
   240         throws UnknownHostException
       
   241     {
   243         initif (hostName, addr, nif);
   242         initif (hostName, addr, nif);
   244     }
   243     }
   245 
   244 
   246     Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException {
   245     Inet6Address (String hostName, byte addr[], String ifname)
       
   246         throws UnknownHostException
       
   247     {
   247         initstr (hostName, addr, ifname);
   248         initstr (hostName, addr, ifname);
   248     }
   249     }
   249 
   250 
   250     /**
   251     /**
   251      * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
   252      * Create an Inet6Address in the exact manner of {@link
   252      * except that the IPv6 scope_id is set to the value corresponding to the given interface
   253      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
   253      * for the address type specified in <code>addr</code>.
   254      * set to the value corresponding to the given interface for the address
   254      * The call will fail with an UnknownHostException if the given interface does not have a numeric
   255      * type specified in <code>addr</code>. The call will fail with an
       
   256      * UnknownHostException if the given interface does not have a numeric
   255      * scope_id assigned for the given address type (eg. link-local or site-local).
   257      * scope_id assigned for the given address type (eg. link-local or site-local).
   256      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
   258      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
   257      * scoped addresses.
   259      * scoped addresses.
   258      *
   260      *
   259      * @param host the specified host
   261      * @param host the specified host
   260      * @param addr the raw IP address in network byte order
   262      * @param addr the raw IP address in network byte order
   261      * @param nif an interface this address must be associated with.
   263      * @param nif an interface this address must be associated with.
   262      * @return  an Inet6Address object created from the raw IP address.
   264      * @return  an Inet6Address object created from the raw IP address.
   263      * @exception  UnknownHostException  if IP address is of illegal length, or if the interface
   265      * @throws  UnknownHostException
   264      *          does not have a numeric scope_id assigned for the given address type.
   266      *          if IP address is of illegal length, or if the interface does not
       
   267      *          have a numeric scope_id assigned for the given address type.
   265      *
   268      *
   266      * @since 1.5
   269      * @since 1.5
   267      */
   270      */
   268 
   271     public static Inet6Address getByAddress(String host, byte[] addr,
   269     public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif)
   272                                             NetworkInterface nif)
   270         throws UnknownHostException {
   273         throws UnknownHostException
       
   274     {
   271         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
   275         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
   272             if (host.charAt(host.length()-1) == ']') {
   276             if (host.charAt(host.length()-1) == ']') {
   273                 host = host.substring(1, host.length() -1);
   277                 host = host.substring(1, host.length() -1);
   274             }
   278             }
   275         }
   279         }
   280         }
   284         }
   281         throw new UnknownHostException("addr is of illegal length");
   285         throw new UnknownHostException("addr is of illegal length");
   282     }
   286     }
   283 
   287 
   284     /**
   288     /**
   285      * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
   289      * Create an Inet6Address in the exact manner of {@link
   286      * except that the IPv6 scope_id is set to the given numeric value.
   290      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
   287      * The scope_id is not checked to determine if it corresponds to any interface on the system.
   291      * set to the given numeric value. The scope_id is not checked to determine
       
   292      * if it corresponds to any interface on the system.
   288      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
   293      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
   289      * scoped addresses.
   294      * scoped addresses.
   290      *
   295      *
   291      * @param host the specified host
   296      * @param host the specified host
   292      * @param addr the raw IP address in network byte order
   297      * @param addr the raw IP address in network byte order
   293      * @param scope_id the numeric scope_id for the address.
   298      * @param scope_id the numeric scope_id for the address.
   294      * @return  an Inet6Address object created from the raw IP address.
   299      * @return  an Inet6Address object created from the raw IP address.
   295      * @exception  UnknownHostException  if IP address is of illegal length.
   300      * @throws  UnknownHostException  if IP address is of illegal length.
   296      *
   301      *
   297      * @since 1.5
   302      * @since 1.5
   298      */
   303      */
   299 
   304     public static Inet6Address getByAddress(String host, byte[] addr,
   300     public static Inet6Address getByAddress(String host, byte[] addr, int scope_id)
   305                                             int scope_id)
   301         throws UnknownHostException {
   306         throws UnknownHostException
       
   307     {
   302         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
   308         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
   303             if (host.charAt(host.length()-1) == ']') {
   309             if (host.charAt(host.length()-1) == ']') {
   304                 host = host.substring(1, host.length() -1);
   310                 host = host.substring(1, host.length() -1);
   305             }
   311             }
   306         }
   312         }
   310             }
   316             }
   311         }
   317         }
   312         throw new UnknownHostException("addr is of illegal length");
   318         throw new UnknownHostException("addr is of illegal length");
   313     }
   319     }
   314 
   320 
   315     private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException {
   321     private void initstr(String hostName, byte addr[], String ifname)
       
   322         throws UnknownHostException
       
   323     {
   316         try {
   324         try {
   317             NetworkInterface nif = NetworkInterface.getByName (ifname);
   325             NetworkInterface nif = NetworkInterface.getByName (ifname);
   318             if (nif == null) {
   326             if (nif == null) {
   319                 throw new UnknownHostException ("no such interface " + ifname);
   327                 throw new UnknownHostException ("no such interface " + ifname);
   320             }
   328             }
   322         } catch (SocketException e) {
   330         } catch (SocketException e) {
   323             throw new UnknownHostException ("SocketException thrown" + ifname);
   331             throw new UnknownHostException ("SocketException thrown" + ifname);
   324         }
   332         }
   325     }
   333     }
   326 
   334 
   327     private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException {
   335     private void initif(String hostName, byte addr[],NetworkInterface nif)
       
   336         throws UnknownHostException
       
   337     {
   328         this.hostName = hostName;
   338         this.hostName = hostName;
   329         if (addr.length == INADDRSZ) { // normal IPv6 address
   339         if (addr.length == INADDRSZ) { // normal IPv6 address
   330             family = IPv6;
   340             family = IPv6;
   331             ipaddress = addr.clone();
   341             ipaddress = addr.clone();
   332         }
   342         }
   333         if (nif != null) {
   343         if (nif != null) {
   334             this.scope_ifname = nif;
   344             scope_ifname = nif;
   335             scope_ifname_set = true;
   345             scope_id = deriveNumericScope(nif);
   336             scope_id = deriveNumericScope (nif);
       
   337             scope_id_set = true;
   346             scope_id_set = true;
   338         }
   347         }
   339     }
   348     }
   340 
   349 
   341     /* check the two Ipv6 addresses and return false if they are both
   350     /* check the two Ipv6 addresses and return false if they are both
   342      * non global address types, but not the same.
   351      * non global address types, but not the same.
   343      * (ie. one is sitelocal and the other linklocal)
   352      * (ie. one is sitelocal and the other linklocal)
   344      * return true otherwise.
   353      * return true otherwise.
   345      */
   354      */
   346     private boolean differentLocalAddressTypes(Inet6Address other) {
   355     private boolean differentLocalAddressTypes(Inet6Address other) {
   347 
   356         if (isLinkLocalAddress() && !other.isLinkLocalAddress())
   348         if (isLinkLocalAddress() && !other.isLinkLocalAddress()) {
       
   349             return false;
   357             return false;
   350         }
   358         if (isSiteLocalAddress() && !other.isSiteLocalAddress())
   351         if (isSiteLocalAddress() && !other.isSiteLocalAddress()) {
       
   352             return false;
   359             return false;
   353         }
       
   354         return true;
   360         return true;
   355     }
   361     }
   356 
   362 
   357     private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
   363     private int deriveNumericScope(NetworkInterface ifc)
       
   364         throws UnknownHostException
       
   365     {
   358         Enumeration<InetAddress> addresses = ifc.getInetAddresses();
   366         Enumeration<InetAddress> addresses = ifc.getInetAddresses();
   359         while (addresses.hasMoreElements()) {
   367         while (addresses.hasMoreElements()) {
   360             InetAddress addr = addresses.nextElement();
   368             InetAddress addr = addresses.nextElement();
   361             if (!(addr instanceof Inet6Address)) {
   369             if (!(addr instanceof Inet6Address)) {
   362                 continue;
   370                 continue;
   371             return ia6_addr.scope_id;
   379             return ia6_addr.scope_id;
   372         }
   380         }
   373         throw new UnknownHostException ("no scope_id found");
   381         throw new UnknownHostException ("no scope_id found");
   374     }
   382     }
   375 
   383 
   376     private int deriveNumericScope (String ifname) throws UnknownHostException {
   384     private int deriveNumericScope(String ifname) throws UnknownHostException {
   377         Enumeration<NetworkInterface> en;
   385         Enumeration<NetworkInterface> en;
   378         try {
   386         try {
   379             en = NetworkInterface.getNetworkInterfaces();
   387             en = NetworkInterface.getNetworkInterfaces();
   380         } catch (SocketException e) {
   388         } catch (SocketException e) {
   381             throw new UnknownHostException ("could not enumerate local network interfaces");
   389             throw new UnknownHostException(
       
   390                     "could not enumerate local network interfaces");
   382         }
   391         }
   383         while (en.hasMoreElements()) {
   392         while (en.hasMoreElements()) {
   384             NetworkInterface ifc = en.nextElement();
   393             NetworkInterface ifc = en.nextElement();
   385             if (ifc.getName().equals (ifname)) {
   394             if (ifc.getName().equals(ifname)) {
   386                 Enumeration<InetAddress> addresses = ifc.getInetAddresses();
   395                 Enumeration<InetAddress> addresses = ifc.getInetAddresses();
   387                 while (addresses.hasMoreElements()) {
   396                 while (addresses.hasMoreElements()) {
   388                     InetAddress addr = addresses.nextElement();
   397                     InetAddress addr = addresses.nextElement();
   389                     if (!(addr instanceof Inet6Address)) {
   398                     if (!(addr instanceof Inet6Address)) {
   390                         continue;
   399                         continue;
   398                     /* found a matching address - return its scope_id */
   407                     /* found a matching address - return its scope_id */
   399                     return ia6_addr.scope_id;
   408                     return ia6_addr.scope_id;
   400                 }
   409                 }
   401             }
   410             }
   402         }
   411         }
   403         throw new UnknownHostException ("No matching address found for interface : " +ifname);
   412         throw new UnknownHostException(
       
   413                 "No matching address found for interface : " +ifname);
   404     }
   414     }
   405 
   415 
   406     /**
   416     /**
   407      * restore the state of this object from stream
   417      * restore the state of this object from stream
   408      * including the scope information, only if the
   418      * including the scope information, only if the
   409      * scoped interface name is valid on this system
   419      * scoped interface name is valid on this system
   410      */
   420      */
   411     private void readObject(ObjectInputStream s)
   421     private void readObject(ObjectInputStream s)
   412         throws IOException, ClassNotFoundException {
   422         throws IOException, ClassNotFoundException {
   413         scope_ifname = null;
       
   414         scope_ifname_set = false;
       
   415         s.defaultReadObject();
   423         s.defaultReadObject();
   416 
   424 
   417         if (ifname != null && !"".equals (ifname)) {
   425         if (ifname != null && !ifname.equals("")) {
   418             try {
   426             try {
   419                 scope_ifname = NetworkInterface.getByName(ifname);
   427                 scope_ifname = NetworkInterface.getByName(ifname);
   420                 if (scope_ifname == null) {
   428                 if (scope_ifname != null) {
   421                     /* the interface does not exist on this system, so we clear
       
   422                      * the scope information completely */
       
   423                     scope_id_set = false;
       
   424                     scope_ifname_set = false;
       
   425                     scope_id = 0;
       
   426                 } else {
       
   427                     try {
   429                     try {
   428                         scope_id = deriveNumericScope (scope_ifname);
   430                         scope_id = deriveNumericScope(scope_ifname);
   429                     } catch (UnknownHostException e) {
   431                     } catch (UnknownHostException e) {
   430                         // typically should not happen, but it may be that
   432                         // typically should not happen, but it may be that
   431                         // the machine being used for deserialization has
   433                         // the machine being used for deserialization has
   432                         // the same interface name but without IPv6 configured.
   434                         // the same interface name but without IPv6 configured.
   433                     }
   435                     }
   453     /**
   455     /**
   454      * Utility routine to check if the InetAddress is an IP multicast
   456      * Utility routine to check if the InetAddress is an IP multicast
   455      * address. 11111111 at the start of the address identifies the
   457      * address. 11111111 at the start of the address identifies the
   456      * address as being a multicast address.
   458      * address as being a multicast address.
   457      *
   459      *
   458      * @return a <code>boolean</code> indicating if the InetAddress is
   460      * @return a {@code boolean} indicating if the InetAddress is an IP
   459      * an IP multicast address
   461      *         multicast address
       
   462      *
   460      * @since JDK1.1
   463      * @since JDK1.1
   461      */
   464      */
   462     @Override
   465     @Override
   463     public boolean isMulticastAddress() {
   466     public boolean isMulticastAddress() {
   464         return ((ipaddress[0] & 0xff) == 0xff);
   467         return ((ipaddress[0] & 0xff) == 0xff);
   465     }
   468     }
   466 
   469 
   467     /**
   470     /**
   468      * Utility routine to check if the InetAddress in a wildcard address.
   471      * Utility routine to check if the InetAddress in a wildcard address.
   469      * @return a <code>boolean</code> indicating if the Inetaddress is
   472      *
       
   473      * @return a {@code boolean} indicating if the Inetaddress is
   470      *         a wildcard address.
   474      *         a wildcard address.
       
   475      *
   471      * @since 1.4
   476      * @since 1.4
   472      */
   477      */
   473     @Override
   478     @Override
   474     public boolean isAnyLocalAddress() {
   479     public boolean isAnyLocalAddress() {
   475         byte test = 0x00;
   480         byte test = 0x00;
   480     }
   485     }
   481 
   486 
   482     /**
   487     /**
   483      * Utility routine to check if the InetAddress is a loopback address.
   488      * Utility routine to check if the InetAddress is a loopback address.
   484      *
   489      *
   485      * @return a <code>boolean</code> indicating if the InetAddress is
   490      * @return a {@code boolean} indicating if the InetAddress is a loopback
   486      * a loopback address; or false otherwise.
   491      *         address; or false otherwise.
       
   492      *
   487      * @since 1.4
   493      * @since 1.4
   488      */
   494      */
   489     @Override
   495     @Override
   490     public boolean isLoopbackAddress() {
   496     public boolean isLoopbackAddress() {
   491         byte test = 0x00;
   497         byte test = 0x00;
   496     }
   502     }
   497 
   503 
   498     /**
   504     /**
   499      * Utility routine to check if the InetAddress is an link local address.
   505      * Utility routine to check if the InetAddress is an link local address.
   500      *
   506      *
   501      * @return a <code>boolean</code> indicating if the InetAddress is
   507      * @return a {@code boolean} indicating if the InetAddress is a link local
   502      * a link local address; or false if address is not a link local unicast address.
   508      *         address; or false if address is not a link local unicast address.
       
   509      *
   503      * @since 1.4
   510      * @since 1.4
   504      */
   511      */
   505     @Override
   512     @Override
   506     public boolean isLinkLocalAddress() {
   513     public boolean isLinkLocalAddress() {
   507         return ((ipaddress[0] & 0xff) == 0xfe
   514         return ((ipaddress[0] & 0xff) == 0xfe
   509     }
   516     }
   510 
   517 
   511     /**
   518     /**
   512      * Utility routine to check if the InetAddress is a site local address.
   519      * Utility routine to check if the InetAddress is a site local address.
   513      *
   520      *
   514      * @return a <code>boolean</code> indicating if the InetAddress is
   521      * @return a {@code boolean} indicating if the InetAddress is a site local
   515      * a site local address; or false if address is not a site local unicast address.
   522      *         address; or false if address is not a site local unicast address.
       
   523      *
   516      * @since 1.4
   524      * @since 1.4
   517      */
   525      */
   518     @Override
   526     @Override
   519     public boolean isSiteLocalAddress() {
   527     public boolean isSiteLocalAddress() {
   520         return ((ipaddress[0] & 0xff) == 0xfe
   528         return ((ipaddress[0] & 0xff) == 0xfe
   522     }
   530     }
   523 
   531 
   524     /**
   532     /**
   525      * Utility routine to check if the multicast address has global scope.
   533      * Utility routine to check if the multicast address has global scope.
   526      *
   534      *
   527      * @return a <code>boolean</code> indicating if the address has
   535      * @return a {@code boolean} indicating if the address has is a multicast
   528      *         is a multicast address of global scope, false if it is not
   536      *         address of global scope, false if it is not of global scope or
   529      *         of global scope or it is not a multicast address
   537      *         it is not a multicast address
       
   538      *
   530      * @since 1.4
   539      * @since 1.4
   531      */
   540      */
   532     @Override
   541     @Override
   533     public boolean isMCGlobal() {
   542     public boolean isMCGlobal() {
   534         return ((ipaddress[0] & 0xff) == 0xff
   543         return ((ipaddress[0] & 0xff) == 0xff
   536     }
   545     }
   537 
   546 
   538     /**
   547     /**
   539      * Utility routine to check if the multicast address has node scope.
   548      * Utility routine to check if the multicast address has node scope.
   540      *
   549      *
   541      * @return a <code>boolean</code> indicating if the address has
   550      * @return a {@code boolean} indicating if the address has is a multicast
   542      *         is a multicast address of node-local scope, false if it is not
   551      *         address of node-local scope, false if it is not of node-local
   543      *         of node-local scope or it is not a multicast address
   552      *         scope or it is not a multicast address
       
   553      *
   544      * @since 1.4
   554      * @since 1.4
   545      */
   555      */
   546     @Override
   556     @Override
   547     public boolean isMCNodeLocal() {
   557     public boolean isMCNodeLocal() {
   548         return ((ipaddress[0] & 0xff) == 0xff
   558         return ((ipaddress[0] & 0xff) == 0xff
   550     }
   560     }
   551 
   561 
   552     /**
   562     /**
   553      * Utility routine to check if the multicast address has link scope.
   563      * Utility routine to check if the multicast address has link scope.
   554      *
   564      *
   555      * @return a <code>boolean</code> indicating if the address has
   565      * @return a {@code boolean} indicating if the address has is a multicast
   556      *         is a multicast address of link-local scope, false if it is not
   566      *         address of link-local scope, false if it is not of link-local
   557      *         of link-local scope or it is not a multicast address
   567      *         scope or it is not a multicast address
       
   568      *
   558      * @since 1.4
   569      * @since 1.4
   559      */
   570      */
   560     @Override
   571     @Override
   561     public boolean isMCLinkLocal() {
   572     public boolean isMCLinkLocal() {
   562         return ((ipaddress[0] & 0xff) == 0xff
   573         return ((ipaddress[0] & 0xff) == 0xff
   564     }
   575     }
   565 
   576 
   566     /**
   577     /**
   567      * Utility routine to check if the multicast address has site scope.
   578      * Utility routine to check if the multicast address has site scope.
   568      *
   579      *
   569      * @return a <code>boolean</code> indicating if the address has
   580      * @return a {@code boolean} indicating if the address has is a multicast
   570      *         is a multicast address of site-local scope, false if it is not
   581      *         address of site-local scope, false if it is not  of site-local
   571      *         of site-local scope or it is not a multicast address
   582      *         scope or it is not a multicast address
       
   583      *
   572      * @since 1.4
   584      * @since 1.4
   573      */
   585      */
   574     @Override
   586     @Override
   575     public boolean isMCSiteLocal() {
   587     public boolean isMCSiteLocal() {
   576         return ((ipaddress[0] & 0xff) == 0xff
   588         return ((ipaddress[0] & 0xff) == 0xff
   578     }
   590     }
   579 
   591 
   580     /**
   592     /**
   581      * Utility routine to check if the multicast address has organization scope.
   593      * Utility routine to check if the multicast address has organization scope.
   582      *
   594      *
   583      * @return a <code>boolean</code> indicating if the address has
   595      * @return a {@code boolean} indicating if the address has is a multicast
   584      *         is a multicast address of organization-local scope,
   596      *         address of organization-local scope, false if it is not of
   585      *         false if it is not of organization-local scope
   597      *         organization-local scope or it is not a multicast address
   586      *         or it is not a multicast address
   598      *
   587      * @since 1.4
   599      * @since 1.4
   588      */
   600      */
   589     @Override
   601     @Override
   590     public boolean isMCOrgLocal() {
   602     public boolean isMCOrgLocal() {
   591         return ((ipaddress[0] & 0xff) == 0xff
   603         return ((ipaddress[0] & 0xff) == 0xff
   592                 && (ipaddress[1] & 0x0f) == 0x08);
   604                 && (ipaddress[1] & 0x0f) == 0x08);
   593     }
   605     }
   594 
   606 
   595     /**
   607     /**
   596      * Returns the raw IP address of this <code>InetAddress</code>
   608      * Returns the raw IP address of this {@code InetAddress} object. The result
   597      * object. The result is in network byte order: the highest order
   609      * is in network byte order: the highest order byte of the address is in
   598      * byte of the address is in <code>getAddress()[0]</code>.
   610      * {@code getAddress()[0]}.
   599      *
   611      *
   600      * @return  the raw IP address of this object.
   612      * @return  the raw IP address of this object.
   601      */
   613      */
   602     @Override
   614     @Override
   603     public byte[] getAddress() {
   615     public byte[] getAddress() {
   607     /**
   619     /**
   608      * Returns the numeric scopeId, if this instance is associated with
   620      * Returns the numeric scopeId, if this instance is associated with
   609      * an interface. If no scoped_id is set, the returned value is zero.
   621      * an interface. If no scoped_id is set, the returned value is zero.
   610      *
   622      *
   611      * @return the scopeId, or zero if not set.
   623      * @return the scopeId, or zero if not set.
       
   624      *
   612      * @since 1.5
   625      * @since 1.5
   613      */
   626      */
   614      public int getScopeId () {
   627      public int getScopeId() {
   615         return scope_id;
   628         return scope_id;
   616      }
   629      }
   617 
   630 
   618     /**
   631     /**
   619      * Returns the scoped interface, if this instance was created with
   632      * Returns the scoped interface, if this instance was created with
   620      * with a scoped interface.
   633      * with a scoped interface.
   621      *
   634      *
   622      * @return the scoped interface, or null if not set.
   635      * @return the scoped interface, or null if not set.
   623      * @since 1.5
   636      * @since 1.5
   624      */
   637      */
   625      public NetworkInterface getScopedInterface () {
   638      public NetworkInterface getScopedInterface() {
   626         return scope_ifname;
   639         return scope_ifname;
   627      }
   640      }
   628 
   641 
   629     /**
   642     /**
   630      * Returns the IP address string in textual presentation. If the instance was created
   643      * Returns the IP address string in textual presentation. If the instance
   631      * specifying a scope identifier then the scope id is appended to the IP address preceded by
   644      * was created specifying a scope identifier then the scope id is appended
   632      * a "%" (per-cent) character. This can be either a numeric value or a string, depending on which
   645      * to the IP address preceded by a "%" (per-cent) character. This can be
   633      * was used to createthe instance.
   646      * either a numeric value or a string, depending on which was used to create
       
   647      * the instance.
   634      *
   648      *
   635      * @return  the raw IP address in a string format.
   649      * @return  the raw IP address in a string format.
   636      */
   650      */
   637     @Override
   651     @Override
   638     public String getHostAddress() {
   652     public String getHostAddress() {
   639         String s = numericToTextFormat(ipaddress);
   653         String s = numericToTextFormat(ipaddress);
   640         if (scope_ifname_set) { /* must check this first */
   654         if (scope_ifname != null) { /* must check this first */
   641             s = s + "%" + scope_ifname.getName();
   655             s = s + "%" + scope_ifname.getName();
   642         } else if (scope_id_set) {
   656         } else if (scope_id_set) {
   643             s = s + "%" + scope_id;
   657             s = s + "%" + scope_id;
   644         }
   658         }
   645         return s;
   659         return s;
   672             return 0;
   686             return 0;
   673         }
   687         }
   674     }
   688     }
   675 
   689 
   676     /**
   690     /**
   677      * Compares this object against the specified object.
   691      * Compares this object against the specified object. The result is {@code
   678      * The result is <code>true</code> if and only if the argument is
   692      * true} if and only if the argument is not {@code null} and it represents
   679      * not <code>null</code> and it represents the same IP address as
   693      * the same IP address as this object.
   680      * this object.
   694      *
   681      * <p>
   695      * <p> Two instances of {@code InetAddress} represent the same IP address
   682      * Two instances of <code>InetAddress</code> represent the same IP
   696      * if the length of the byte arrays returned by {@code getAddress} is the
   683      * address if the length of the byte arrays returned by
   697      * same for both, and each of the array components is the same for the byte
   684      * <code>getAddress</code> is the same for both, and each of the
   698      * arrays.
   685      * array components is the same for the byte arrays.
       
   686      *
   699      *
   687      * @param   obj   the object to compare against.
   700      * @param   obj   the object to compare against.
   688      * @return  <code>true</code> if the objects are the same;
   701      *
   689      *          <code>false</code> otherwise.
   702      * @return  {@code true} if the objects are the same; {@code false} otherwise.
       
   703      *
   690      * @see     java.net.InetAddress#getAddress()
   704      * @see     java.net.InetAddress#getAddress()
   691      */
   705      */
   692     @Override
   706     @Override
   693     public boolean equals(Object obj) {
   707     public boolean equals(Object obj) {
   694         if (obj == null ||
   708         if (obj == null || !(obj instanceof Inet6Address))
   695             !(obj instanceof Inet6Address))
       
   696             return false;
   709             return false;
   697 
   710 
   698         Inet6Address inetAddr = (Inet6Address)obj;
   711         Inet6Address inetAddr = (Inet6Address)obj;
   699 
       
   700         for (int i = 0; i < INADDRSZ; i++) {
   712         for (int i = 0; i < INADDRSZ; i++) {
   701             if (ipaddress[i] != inetAddr.ipaddress[i])
   713             if (ipaddress[i] != inetAddr.ipaddress[i])
   702                 return false;
   714                 return false;
   703         }
   715         }
   704 
   716 
   707 
   719 
   708     /**
   720     /**
   709      * Utility routine to check if the InetAddress is an
   721      * Utility routine to check if the InetAddress is an
   710      * IPv4 compatible IPv6 address.
   722      * IPv4 compatible IPv6 address.
   711      *
   723      *
   712      * @return a <code>boolean</code> indicating if the InetAddress is
   724      * @return a {@code boolean} indicating if the InetAddress is an IPv4
   713      * an IPv4 compatible IPv6 address; or false if address is IPv4 address.
   725      *         compatible IPv6 address; or false if address is IPv4 address.
       
   726      *
   714      * @since 1.4
   727      * @since 1.4
   715      */
   728      */
   716     public boolean isIPv4CompatibleAddress() {
   729     public boolean isIPv4CompatibleAddress() {
   717         if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
   730         if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
   718             (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
   731             (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
   725         return false;
   738         return false;
   726     }
   739     }
   727 
   740 
   728     // Utilities
   741     // Utilities
   729     private final static int INT16SZ = 2;
   742     private final static int INT16SZ = 2;
       
   743 
   730     /*
   744     /*
   731      * Convert IPv6 binary address into presentation (printable) format.
   745      * Convert IPv6 binary address into presentation (printable) format.
   732      *
   746      *
   733      * @param src a byte array representing the IPv6 numeric address
   747      * @param src a byte array representing the IPv6 numeric address
   734      * @return a String representing an IPv6 address in
   748      * @return a String representing an IPv6 address in
   735      *         textual representation format
   749      *         textual representation format
   736      * @since 1.4
   750      * @since 1.4
   737      */
   751      */
   738     static String numericToTextFormat(byte[] src)
   752     static String numericToTextFormat(byte[] src) {
   739     {
   753         StringBuilder sb = new StringBuilder(39);
   740         StringBuffer sb = new StringBuffer(39);
       
   741         for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
   754         for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
   742             sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
   755             sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
   743                                           | (src[(i<<1)+1] & 0xff)));
   756                                           | (src[(i<<1)+1] & 0xff)));
   744             if (i < (INADDRSZ / INT16SZ) -1 ) {
   757             if (i < (INADDRSZ / INT16SZ) -1 ) {
   745                sb.append(":");
   758                sb.append(":");
   764      * which is not serializable
   777      * which is not serializable
   765      */
   778      */
   766     private synchronized void writeObject(java.io.ObjectOutputStream s)
   779     private synchronized void writeObject(java.io.ObjectOutputStream s)
   767         throws IOException
   780         throws IOException
   768     {
   781     {
   769         if (scope_ifname_set) {
   782         if (scope_ifname != null)
   770             ifname = scope_ifname.getName();
   783             ifname = scope_ifname.getName();
   771         }
       
   772         s.defaultWriteObject();
   784         s.defaultWriteObject();
   773     }
   785     }
   774 }
   786 }