FYI: NetworkInterface.getInetAddresses() fix
Gary Benson
gbenson@redhat.com
Fri Aug 25 13:17:00 GMT 2006
Tom Tromey wrote:
> >>>>> "Gary" == Gary Benson <gbenson@redhat.com> writes:
>
> Gary> This commit makes NetworkInterface.getInetAddresses()
> Gary> bracket IPv6 addresses before calling SecurityManager.
> Gary> checkConnect() with them. This fixes
> Gary> http://gcc.gnu.org/ml/java/2006-08/msg00082.html.
>
> I'm curious about one aspect of this patch...
>
> Gary> + String hostAddress = addr.getHostAddress();
> Gary> + if (addr instanceof Inet6Address)
> Gary> + hostAddress = "[" + hostAddress + "]";
>
> This is un-OO-like, which raises a warning flag for me.
> I was wondering whether the brackets ought to be added in
> Inet6Address.getHostAddress... ?
Ok, I missed the line in the SocketPermission javadoc that says "The
full uncompressed form of an IPv6 literal address is also valid", so
adding the brackets should not be necessary here (assuming that
Inet6Address.getHostAddress() always returns addresses in the full
uncompressed form of course ;)).
I had a play with a couple of proprietary JVMs and it seems that they
handle unbracketed IPv6 addresses by bracketing them at <init> time,
so I wrote a patch that makes us do that too. Does this seem ok?
Cheers,
Gary
-------------- next part --------------
Index: classpath/ChangeLog.gcj
===================================================================
--- classpath/ChangeLog.gcj (revision 116398)
+++ classpath/ChangeLog.gcj (working copy)
@@ -1,3 +1,12 @@
+2006-08-25 Gary Benson <gbenson@redhat.com>
+
+ * java/net/SocketPermission.java
+ (maybeBracketIPv6Address): New method.
+ (<init>): Pass the hostport argument through the above.
+
+ * java/net/NetworkInterface.java (getInetAddresses):
+ Revert the previous change.
+
2006-08-24 Gary Benson <gbenson@redhat.com>
* java/net/NetworkInterface.java (getInetAddresses): Bracket IPv6
Index: classpath/java/net/SocketPermission.java
===================================================================
--- classpath/java/net/SocketPermission.java (revision 116398)
+++ classpath/java/net/SocketPermission.java (working copy)
@@ -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: classpath/java/net/NetworkInterface.java
===================================================================
--- classpath/java/net/NetworkInterface.java (revision 116398)
+++ classpath/java/net/NetworkInterface.java (working copy)
@@ -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)
More information about the Java-patches
mailing list