Hi all,

This commit makes java.net.SocketPermission() accept unbracketed IPv6
addresses when specified in the full uncompressed form.  The way it
does this (by intercepting the argument to the constructor) seems odd,
but I had a play with a couple of proprietary JVMs and it seems that
this is what they do.  This commit also reverts my previous commit to
NetworkInterface.getInetAddresses() which is now unnecessary.

Cheers,
Gary
Index: ChangeLog
===================================================================
RCS file: /cvsroot/classpath/classpath/ChangeLog,v
retrieving revision 1.8469
diff -u -r1.8469 ChangeLog
--- ChangeLog   28 Aug 2006 21:41:57 -0000      1.8469
+++ ChangeLog   29 Aug 2006 08:23:49 -0000
@@ -1,3 +1,12 @@
+2006-08-29  Gary Benson  <[EMAIL PROTECTED]>
+
+       * java/net/SocketPermission.java
+       (maybeBracketIPv6Address): New method.
+       (<init>): Pass the hostport argument through the above.
+
+       * java/net/NetworkInterface.java (getInetAddresses):
+       Don't bracket IPv6 addresses.
+
 2006-08-28  Roman Kennke  <[EMAIL PROTECTED]>
 
        * javax/swing/text/BoxView.java
Index: java/net/SocketPermission.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/SocketPermission.java,v
retrieving revision 1.20
diff -u -r1.20 SocketPermission.java
--- java/net/SocketPermission.java      29 Jan 2006 18:55:59 -0000      1.20
+++ java/net/SocketPermission.java      29 Aug 2006 08:23:49 -0000
@@ -164,13 +164,57 @@
    */
   public SocketPermission(String hostport, String actions)
   {
-    super(hostport);
+    super(maybeBracketIPv6Address(hostport));
 
-    setHostPort(hostport);
+    setHostPort(getName());
     setActions(actions);
   }
 
   /**
+   * IPv6 addresses in the hostport must either be enclosed by
+   * "[" and "]" or be specified in the full uncompressed form.
+   * In the latter case proprietary JVMs will quote the address
+   * with "[" and "]", so we do to.
+   */
+  private static String maybeBracketIPv6Address(String hostport)
+  {
+    if (hostport.length() == 0 || hostport.charAt(0) == '[')
+      return hostport;
+
+    int colons = 0, last_colon = 0;
+    for (int i = 0; i < hostport.length(); i++)
+      {
+       if (hostport.charAt(i) == ':')
+         {
+           if (i - last_colon == 1)
+             throw new IllegalArgumentException("Ambiguous hostport part");
+           colons++;
+           last_colon = i;
+         }
+      }
+
+    switch (colons)
+      {
+      case 0:
+      case 1:
+       // a hostname or IPv4 address
+       return hostport;
+       
+      case 7:
+       // an IPv6 address with no ports
+       return "[" + hostport + "]";
+
+      case 8:
+       // an IPv6 address with ports
+       return "[" + hostport.substring(0, last_colon) + "]"
+         + hostport.substring(last_colon);
+
+      default:
+       throw new IllegalArgumentException("Ambiguous hostport part");
+      }
+  }
+  
+  /**
    * Parse the hostport argument to the constructor.
    */
   private void setHostPort(String hostport)
Index: java/net/NetworkInterface.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/NetworkInterface.java,v
retrieving revision 1.17
diff -u -r1.17 NetworkInterface.java
--- java/net/NetworkInterface.java      24 Aug 2006 10:58:55 -0000      1.17
+++ java/net/NetworkInterface.java      29 Aug 2006 08:23:49 -0000
@@ -112,10 +112,7 @@
        InetAddress addr = (InetAddress) addresses.nextElement();
        try
          {
-           String hostAddress = addr.getHostAddress();
-           if (addr instanceof Inet6Address)
-             hostAddress = "[" + hostAddress + "]";
-           s.checkConnect(hostAddress, 58000);
+           s.checkConnect(addr.getHostAddress(), 58000);
            tmpInetAddresses.add(addr);
          }
        catch (SecurityException e)

Reply via email to