# HG changeset patch # User mullan # Date 1355409449 18000 # Node ID 164b4548e3bab0988861ee758d37285c60accdd5 # Parent 37b3f525004f91240fa399a2f264f6cfce3bb021# Parent 2ecc993a8e6759fd7ce06573a9118ef83f5fc98f Merge diff -r 37b3f525004f -r 164b4548e3ba jdk/src/share/classes/java/net/Inet6Address.java --- a/jdk/src/share/classes/java/net/Inet6Address.java Thu Dec 13 09:35:35 2012 -0500 +++ b/jdk/src/share/classes/java/net/Inet6Address.java Thu Dec 13 09:37:29 2012 -0500 @@ -25,9 +25,9 @@ package java.net; -import java.io.ObjectInputStream; import java.io.IOException; import java.io.InvalidObjectException; +import java.io.ObjectInputStream; import java.util.Enumeration; /** @@ -116,7 +116,8 @@ *

Special IPv6 address

* *
- * + *
IPv4-mapped address
+ * * *
IPv4-mapped addressOf the form::ffff:w.x.y.z, this IPv6 address is used to * represent an IPv4 address. It allows the native program to * use the same address data structure and also the same @@ -130,35 +131,40 @@ * address.
*

- *

Textual representation of IPv6 scoped addresses

- *

- * The textual representation of IPv6 addresses as described above can be extended - * to specify IPv6 scoped addresses. This extension to the basic addressing architecture - * is described in [draft-ietf-ipngwg-scoping-arch-04.txt]. - *

- * Because link-local and site-local addresses are non-global, it is possible that different hosts - * may have the same destination address and may be reachable through different interfaces on the - * same originating system. In this case, the originating system is said to be connected - * to multiple zones of the same scope. In order to disambiguate which is the intended destination - * zone, it is possible to append a zone identifier (or scope_id) to an IPv6 address. - *

- * The general format for specifying the scope_id is the following: + *

Textual representation of IPv6 scoped addresses

+ * + *

The textual representation of IPv6 addresses as described above can be + * extended to specify IPv6 scoped addresses. This extension to the basic + * addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt]. + * + *

Because link-local and site-local addresses are non-global, it is possible + * that different hosts may have the same destination address and may be + * reachable through different interfaces on the same originating system. In + * this case, the originating system is said to be connected to multiple zones + * of the same scope. In order to disambiguate which is the intended destination + * zone, it is possible to append a zone identifier (or scope_id) to an + * IPv6 address. + * + *

The general format for specifying the scope_id is the following: + * *

IPv6-address%scope_id
*

The IPv6-address is a literal IPv6 address as described above. - * The scope_id refers to an interface on the local system, and it can be specified - * in two ways. - *

  1. As a numeric identifier. This must be a positive integer that identifies the - * particular interface and scope as understood by the system. Usually, the numeric - * values can be determined through administration tools on the system. Each interface may - * have multiple values, one for each scope. If the scope is unspecified, then the default value - * used is zero.
  2. - *

  3. As a string. This must be the exact string that is returned by - * {@link java.net.NetworkInterface#getName()} for the particular interface in question. - * When an Inet6Address is created in this way, the numeric scope-id is determined at the time - * the object is created by querying the relevant NetworkInterface.
  4. - *

- * Note also, that the numeric scope_id can be retrieved from Inet6Address instances returned from the - * NetworkInterface class. This can be used to find out the current scope ids configured on the system. + * The scope_id refers to an interface on the local system, and it can be + * specified in two ways. + *

  1. As a numeric identifier. This must be a positive integer + * that identifies the particular interface and scope as understood by the + * system. Usually, the numeric values can be determined through administration + * tools on the system. Each interface may have multiple values, one for each + * scope. If the scope is unspecified, then the default value used is zero.
  2. + *

  3. As a string. This must be the exact string that is returned by + * {@link java.net.NetworkInterface#getName()} for the particular interface in + * question. When an Inet6Address is created in this way, the numeric scope-id + * is determined at the time the object is created by querying the relevant + * NetworkInterface.
+ * + *

Note also, that the numeric scope_id can be retrieved from + * Inet6Address instances returned from the NetworkInterface class. This can be + * used to find out the current scope ids configured on the system. * @since 1.4 */ @@ -169,7 +175,7 @@ /* * cached scope_id - for link-local address use only. */ - private transient int cached_scope_id = 0; + private transient int cached_scope_id; // 0 /** * Holds a 128-bit (16 bytes) IPv6 address. @@ -179,37 +185,28 @@ byte[] ipaddress; /** - * scope_id. The scope specified when the object is created. If the object is created - * with an interface name, then the scope_id is not determined until the time it is needed. + * scope_id. The scope specified when the object is created. If the object + * is created with an interface name, then the scope_id is not determined + * until the time it is needed. */ - private int scope_id = 0; + private int scope_id; // 0 /** * This will be set to true when the scope_id field contains a valid * integer scope_id. */ - private boolean scope_id_set = false; + private boolean scope_id_set; // false /** * scoped interface. scope_id is derived from this as the scope_id of the first * address whose scope is the same as this address for the named interface. */ - private transient NetworkInterface scope_ifname = null; - - /** - * set if the object is constructed with a scoped interface instead of a - * numeric scope id. - */ - private boolean scope_ifname_set = false; + private transient NetworkInterface scope_ifname; // null private static final long serialVersionUID = 6880410070516793377L; - /* - * Perform initializations. - */ - static { - init(); - } + // Perform native initialization + static { init(); } Inet6Address() { super(); @@ -239,19 +236,24 @@ } catch (UnknownHostException e) {} /* cant happen if ifname is null */ } - Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException { + Inet6Address (String hostName, byte addr[], NetworkInterface nif) + throws UnknownHostException + { initif (hostName, addr, nif); } - Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException { + Inet6Address (String hostName, byte addr[], String ifname) + throws UnknownHostException + { initstr (hostName, addr, ifname); } /** - * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} - * except that the IPv6 scope_id is set to the value corresponding to the given interface - * for the address type specified in addr. - * The call will fail with an UnknownHostException if the given interface does not have a numeric + * Create an Inet6Address in the exact manner of {@link + * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is + * set to the value corresponding to the given interface for the address + * type specified in addr. The call will fail with an + * UnknownHostException if the given interface does not have a numeric * scope_id assigned for the given address type (eg. link-local or site-local). * See here for a description of IPv6 * scoped addresses. @@ -260,14 +262,16 @@ * @param addr the raw IP address in network byte order * @param nif an interface this address must be associated with. * @return an Inet6Address object created from the raw IP address. - * @exception UnknownHostException if IP address is of illegal length, or if the interface - * does not have a numeric scope_id assigned for the given address type. + * @throws UnknownHostException + * if IP address is of illegal length, or if the interface does not + * have a numeric scope_id assigned for the given address type. * * @since 1.5 */ - - public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) - throws UnknownHostException { + public static Inet6Address getByAddress(String host, byte[] addr, + NetworkInterface nif) + throws UnknownHostException + { if (host != null && host.length() > 0 && host.charAt(0) == '[') { if (host.charAt(host.length()-1) == ']') { host = host.substring(1, host.length() -1); @@ -282,9 +286,10 @@ } /** - * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} - * except that the IPv6 scope_id is set to the given numeric value. - * The scope_id is not checked to determine if it corresponds to any interface on the system. + * Create an Inet6Address in the exact manner of {@link + * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is + * set to the given numeric value. The scope_id is not checked to determine + * if it corresponds to any interface on the system. * See here for a description of IPv6 * scoped addresses. * @@ -292,13 +297,14 @@ * @param addr the raw IP address in network byte order * @param scope_id the numeric scope_id for the address. * @return an Inet6Address object created from the raw IP address. - * @exception UnknownHostException if IP address is of illegal length. + * @throws UnknownHostException if IP address is of illegal length. * * @since 1.5 */ - - public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) - throws UnknownHostException { + public static Inet6Address getByAddress(String host, byte[] addr, + int scope_id) + throws UnknownHostException + { if (host != null && host.length() > 0 && host.charAt(0) == '[') { if (host.charAt(host.length()-1) == ']') { host = host.substring(1, host.length() -1); @@ -312,7 +318,9 @@ throw new UnknownHostException("addr is of illegal length"); } - private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException { + private void initstr(String hostName, byte addr[], String ifname) + throws UnknownHostException + { try { NetworkInterface nif = NetworkInterface.getByName (ifname); if (nif == null) { @@ -324,16 +332,17 @@ } } - private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException { + private void initif(String hostName, byte addr[],NetworkInterface nif) + throws UnknownHostException + { this.hostName = hostName; if (addr.length == INADDRSZ) { // normal IPv6 address family = IPv6; ipaddress = addr.clone(); } if (nif != null) { - this.scope_ifname = nif; - scope_ifname_set = true; - scope_id = deriveNumericScope (nif); + scope_ifname = nif; + scope_id = deriveNumericScope(nif); scope_id_set = true; } } @@ -344,17 +353,16 @@ * return true otherwise. */ private boolean differentLocalAddressTypes(Inet6Address other) { - - if (isLinkLocalAddress() && !other.isLinkLocalAddress()) { + if (isLinkLocalAddress() && !other.isLinkLocalAddress()) return false; - } - if (isSiteLocalAddress() && !other.isSiteLocalAddress()) { + if (isSiteLocalAddress() && !other.isSiteLocalAddress()) return false; - } return true; } - private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException { + private int deriveNumericScope(NetworkInterface ifc) + throws UnknownHostException + { Enumeration addresses = ifc.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); @@ -373,16 +381,17 @@ throw new UnknownHostException ("no scope_id found"); } - private int deriveNumericScope (String ifname) throws UnknownHostException { + private int deriveNumericScope(String ifname) throws UnknownHostException { Enumeration en; try { en = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { - throw new UnknownHostException ("could not enumerate local network interfaces"); + throw new UnknownHostException( + "could not enumerate local network interfaces"); } while (en.hasMoreElements()) { NetworkInterface ifc = en.nextElement(); - if (ifc.getName().equals (ifname)) { + if (ifc.getName().equals(ifname)) { Enumeration addresses = ifc.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); @@ -400,7 +409,8 @@ } } } - throw new UnknownHostException ("No matching address found for interface : " +ifname); + throw new UnknownHostException( + "No matching address found for interface : " +ifname); } /** @@ -410,22 +420,14 @@ */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { - scope_ifname = null; - scope_ifname_set = false; s.defaultReadObject(); - if (ifname != null && !"".equals (ifname)) { + if (ifname != null && !ifname.equals("")) { try { scope_ifname = NetworkInterface.getByName(ifname); - if (scope_ifname == null) { - /* the interface does not exist on this system, so we clear - * the scope information completely */ - scope_id_set = false; - scope_ifname_set = false; - scope_id = 0; - } else { + if (scope_ifname != null) { try { - scope_id = deriveNumericScope (scope_ifname); + scope_id = deriveNumericScope(scope_ifname); } catch (UnknownHostException e) { // typically should not happen, but it may be that // the machine being used for deserialization has @@ -455,8 +457,9 @@ * address. 11111111 at the start of the address identifies the * address as being a multicast address. * - * @return a boolean indicating if the InetAddress is - * an IP multicast address + * @return a {@code boolean} indicating if the InetAddress is an IP + * multicast address + * * @since JDK1.1 */ @Override @@ -466,8 +469,10 @@ /** * Utility routine to check if the InetAddress in a wildcard address. - * @return a boolean indicating if the Inetaddress is + * + * @return a {@code boolean} indicating if the Inetaddress is * a wildcard address. + * * @since 1.4 */ @Override @@ -482,8 +487,9 @@ /** * Utility routine to check if the InetAddress is a loopback address. * - * @return a boolean indicating if the InetAddress is - * a loopback address; or false otherwise. + * @return a {@code boolean} indicating if the InetAddress is a loopback + * address; or false otherwise. + * * @since 1.4 */ @Override @@ -498,8 +504,9 @@ /** * Utility routine to check if the InetAddress is an link local address. * - * @return a boolean indicating if the InetAddress is - * a link local address; or false if address is not a link local unicast address. + * @return a {@code boolean} indicating if the InetAddress is a link local + * address; or false if address is not a link local unicast address. + * * @since 1.4 */ @Override @@ -511,8 +518,9 @@ /** * Utility routine to check if the InetAddress is a site local address. * - * @return a boolean indicating if the InetAddress is - * a site local address; or false if address is not a site local unicast address. + * @return a {@code boolean} indicating if the InetAddress is a site local + * address; or false if address is not a site local unicast address. + * * @since 1.4 */ @Override @@ -524,9 +532,10 @@ /** * Utility routine to check if the multicast address has global scope. * - * @return a boolean indicating if the address has - * is a multicast address of global scope, false if it is not - * of global scope or it is not a multicast address + * @return a {@code boolean} indicating if the address has is a multicast + * address of global scope, false if it is not of global scope or + * it is not a multicast address + * * @since 1.4 */ @Override @@ -538,9 +547,10 @@ /** * Utility routine to check if the multicast address has node scope. * - * @return a boolean indicating if the address has - * is a multicast address of node-local scope, false if it is not - * of node-local scope or it is not a multicast address + * @return a {@code boolean} indicating if the address has is a multicast + * address of node-local scope, false if it is not of node-local + * scope or it is not a multicast address + * * @since 1.4 */ @Override @@ -552,9 +562,10 @@ /** * Utility routine to check if the multicast address has link scope. * - * @return a boolean indicating if the address has - * is a multicast address of link-local scope, false if it is not - * of link-local scope or it is not a multicast address + * @return a {@code boolean} indicating if the address has is a multicast + * address of link-local scope, false if it is not of link-local + * scope or it is not a multicast address + * * @since 1.4 */ @Override @@ -566,9 +577,10 @@ /** * Utility routine to check if the multicast address has site scope. * - * @return a boolean indicating if the address has - * is a multicast address of site-local scope, false if it is not - * of site-local scope or it is not a multicast address + * @return a {@code boolean} indicating if the address has is a multicast + * address of site-local scope, false if it is not of site-local + * scope or it is not a multicast address + * * @since 1.4 */ @Override @@ -580,10 +592,10 @@ /** * Utility routine to check if the multicast address has organization scope. * - * @return a boolean indicating if the address has - * is a multicast address of organization-local scope, - * false if it is not of organization-local scope - * or it is not a multicast address + * @return a {@code boolean} indicating if the address has is a multicast + * address of organization-local scope, false if it is not of + * organization-local scope or it is not a multicast address + * * @since 1.4 */ @Override @@ -593,9 +605,9 @@ } /** - * Returns the raw IP address of this InetAddress - * object. The result is in network byte order: the highest order - * byte of the address is in getAddress()[0]. + * Returns the raw IP address of this {@code InetAddress} object. The result + * is in network byte order: the highest order byte of the address is in + * {@code getAddress()[0]}. * * @return the raw IP address of this object. */ @@ -609,9 +621,10 @@ * an interface. If no scoped_id is set, the returned value is zero. * * @return the scopeId, or zero if not set. + * * @since 1.5 */ - public int getScopeId () { + public int getScopeId() { return scope_id; } @@ -622,22 +635,23 @@ * @return the scoped interface, or null if not set. * @since 1.5 */ - public NetworkInterface getScopedInterface () { + public NetworkInterface getScopedInterface() { return scope_ifname; } /** - * Returns the IP address string in textual presentation. If the instance was created - * specifying a scope identifier then the scope id is appended to the IP address preceded by - * a "%" (per-cent) character. This can be either a numeric value or a string, depending on which - * was used to createthe instance. + * Returns the IP address string in textual presentation. If the instance + * was created specifying a scope identifier then the scope id is appended + * to the IP address preceded by a "%" (per-cent) character. This can be + * either a numeric value or a string, depending on which was used to create + * the instance. * * @return the raw IP address in a string format. */ @Override public String getHostAddress() { String s = numericToTextFormat(ipaddress); - if (scope_ifname_set) { /* must check this first */ + if (scope_ifname != null) { /* must check this first */ s = s + "%" + scope_ifname.getName(); } else if (scope_id_set) { s = s + "%" + scope_id; @@ -674,29 +688,27 @@ } /** - * Compares this object against the specified object. - * The result is true if and only if the argument is - * not null and it represents the same IP address as - * this object. - *

- * Two instances of InetAddress represent the same IP - * address if the length of the byte arrays returned by - * getAddress is the same for both, and each of the - * array components is the same for the byte arrays. + * Compares this object against the specified object. The result is {@code + * true} if and only if the argument is not {@code null} and it represents + * the same IP address as this object. + * + *

Two instances of {@code InetAddress} represent the same IP address + * if the length of the byte arrays returned by {@code getAddress} is the + * same for both, and each of the array components is the same for the byte + * arrays. * * @param obj the object to compare against. - * @return true if the objects are the same; - * false otherwise. + * + * @return {@code true} if the objects are the same; {@code false} otherwise. + * * @see java.net.InetAddress#getAddress() */ @Override public boolean equals(Object obj) { - if (obj == null || - !(obj instanceof Inet6Address)) + if (obj == null || !(obj instanceof Inet6Address)) return false; Inet6Address inetAddr = (Inet6Address)obj; - for (int i = 0; i < INADDRSZ; i++) { if (ipaddress[i] != inetAddr.ipaddress[i]) return false; @@ -709,8 +721,9 @@ * Utility routine to check if the InetAddress is an * IPv4 compatible IPv6 address. * - * @return a boolean indicating if the InetAddress is - * an IPv4 compatible IPv6 address; or false if address is IPv4 address. + * @return a {@code boolean} indicating if the InetAddress is an IPv4 + * compatible IPv6 address; or false if address is IPv4 address. + * * @since 1.4 */ public boolean isIPv4CompatibleAddress() { @@ -727,6 +740,7 @@ // Utilities private final static int INT16SZ = 2; + /* * Convert IPv6 binary address into presentation (printable) format. * @@ -735,9 +749,8 @@ * textual representation format * @since 1.4 */ - static String numericToTextFormat(byte[] src) - { - StringBuffer sb = new StringBuffer(39); + static String numericToTextFormat(byte[] src) { + StringBuilder sb = new StringBuilder(39); for (int i = 0; i < (INADDRSZ / INT16SZ); i++) { sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00) | (src[(i<<1)+1] & 0xff))); @@ -766,9 +779,8 @@ private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException { - if (scope_ifname_set) { + if (scope_ifname != null) ifname = scope_ifname.getName(); - } s.defaultWriteObject(); } } diff -r 37b3f525004f -r 164b4548e3ba jdk/src/share/native/java/net/Inet6Address.c --- a/jdk/src/share/native/java/net/Inet6Address.c Thu Dec 13 09:35:35 2012 -0500 +++ b/jdk/src/share/native/java/net/Inet6Address.c Thu Dec 13 09:37:29 2012 -0500 @@ -38,7 +38,6 @@ jfieldID ia6_cachedscopeidID; jfieldID ia6_scopeidsetID; jfieldID ia6_scopeifnameID; -jfieldID ia6_scopeifnamesetID; jmethodID ia6_ctrID; /* @@ -62,8 +61,6 @@ CHECK_NULL(ia6_scopeidID); ia6_scopeifnameID = (*env)->GetFieldID(env, ia6_class, "scope_ifname", "Ljava/net/NetworkInterface;"); CHECK_NULL(ia6_scopeifnameID); - ia6_scopeifnamesetID = (*env)->GetFieldID(env, ia6_class, "scope_ifname_set", "Z"); - CHECK_NULL(ia6_scopeifnamesetID); ia6_ctrID = (*env)->GetMethodID(env, ia6_class, "", "()V"); CHECK_NULL(ia6_ctrID); } diff -r 37b3f525004f -r 164b4548e3ba jdk/test/java/net/Inet6Address/StringScope.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/net/Inet6Address/StringScope.java Thu Dec 13 09:37:29 2012 -0500 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2012, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8004675 + * @summary Inet6Address.getHostAddress should use string scope + * identifier where available + */ + +import java.net.*; +import java.util.Enumeration; + +public class StringScope { + + public static void main(String args[]) throws Exception { + Enumeration e = NetworkInterface.getNetworkInterfaces(); + while (e.hasMoreElements()) { + NetworkInterface iface = e.nextElement(); + Enumeration iadrs = iface.getInetAddresses(); + while (iadrs.hasMoreElements()) { + InetAddress iadr = iadrs.nextElement(); + if (iadr instanceof Inet6Address) { + Inet6Address i6adr = (Inet6Address) iadr; + NetworkInterface nif = i6adr.getScopedInterface(); + if (nif == null) + continue; + + String nifName = nif.getName(); + String i6adrHostAddress = i6adr.getHostAddress(); + int index = i6adrHostAddress.indexOf('%'); + String i6adrScopeName = i6adrHostAddress.substring(index+1); + + if (!nifName.equals(i6adrScopeName)) + throw new RuntimeException("Expected nifName [" + + nifName + "], to equal i6adrScopeName [" + + i6adrScopeName + "] "); + } + } + } + } +} +