JDK-8225499 : InetSocketAddress::toString not friendly to IPv6 literal addresses
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.0,13
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2019-06-08
  • Updated: 2021-09-28
  • Resolved: 2019-10-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 14
14 b20Fixed
Related Reports
CSR :  
Relates :  
Sub Tasks
JDK-8232369 :  
Description
Reported on core-libs-dev and further discussed here:
https://mail.openjdk.java.net/pipermail/net-dev/2019-June/012741.html

The issue here seems to be that InetSocketAddress::toString is specified to call InetAddress.toString() and concatenate :<port>. This doesn't align with the implementation and is also not friendly to IPv6 literal addresses, eg:

new InetSocketAddress("::1", 8080).toString()

prints "/0:0:0:0:0:0:0:1:8080" when users will expect "[::1]:80)" (no slash).

Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/eb172a3b1c1c User: dfuchs Date: 2019-10-18 16:54:52 +0000
18-10-2019

Another silly one: $ cat T.java public class T { public static void main(String[] args) { System.out.println(new java.net.InetSocketAddress(80)); } } $jdk-12/bin/java -Djava.net.preferIPv6Addresses=true T.java ::/0:0:0:0:0:0:0:0:80 Point being, that the toString representation is in no way a tuple of host or IP-literal, and port number
30-09-2019

A note about unresolved addresses; they do not follow the InetSocketAddress::toString specification either. jshell> InetSocketAddress.createUnresolved("foo", 80) $13 ==> foo:80 jshell> InetSocketAddress.createUnresolved(":", 1) $14 ==> ::1 <<< this one is just silly ( where is the slash? )
30-09-2019

I believe more exactly that the format could be: new InetSocketAddress("::1", 8080).toString() => "/[0:0:0:0:0:0:0:1]:8080" The "/" ony means that a reverse lookup was not performed - the format is <hostname>/<IP-literal>:<port>. We only want the <IP-literal> to be enclosed in "[]" when the address is an IPv6 literal. jshell> new InetSocketAddress("::1", 8080).toString() $1 ==> "/0:0:0:0:0:0:0:1:8080" jshell> new InetSocketAddress("127.0.0.1", 8080).toString() $2 ==> "/127.0.0.1:8080"
30-09-2019