RFR: 8272215: Add InetAddress methods for parsing IP address literals

Daniel Fuchs dfuchs at openjdk.org
Mon Oct 16 16:18:22 UTC 2023


On Mon, 18 Sep 2023 16:58:15 GMT, Bernd <duke at openjdk.org> wrote:

>> src/java.base/share/classes/java/net/Inet4Address.java line 148:
>> 
>>> 146:      *   <blockquote><ul style="list-style-type:none">
>>> 147:      *   <li>{@code 1.2.3.4}</li>
>>> 148:      *   <li>{@code 06.07.08.09}</li>
>> 
>> It might be clearer to transform this in a kind of JShell like snippet, showing the Inet4Adress call and what is returned? I would also expect to see an example of IPv4-compatible IPv6 address literal here.
>> 
>> Something like:
>> 
>>      * {@snippet:
>>      *     Inet4Address.ofLiteral("1.2.3.4")            ==>    /1.2.3.4
>>      *     Inet4Address.ofLiteral("06.07.08.09")        ==>    /6.7.8.9
>>      *     Inet4Address.ofLiteral(""::ffff:1020:3040")  ==>    /16.32.48.64
>>      * }
>> 
>> 
>> As a side note, I suspect that Inet4Adress.ofLiteral() accepting IPv4-compatible IPv6 addresses will be surprising for most users. I wonder if we should change that, and have Inet6Address.ofLiteral() return InetAddress instead of Inet6Address, and take care of that edge case.
>> 
>> As a user of the API I could very well see myself trying to do something like:
>> 
>> if (addr.startsWith("[") && addr.endsWith("]")) {
>>     var ipv6 = addr.substring(1, addr.length() - 1);
>>     try {
>>          Inet6Address.ofLiteral(ipv6);
>>     }  catch (...) {
>>          throw new IllegalArgumentException("Invalid IPv6 literal between "[ ]"");
>>     }
>> }
>> 
>> I'm not saying we shouldn't do what you're suggesting here, but I believe we should get more feedback.
>
>>      *     Inet4Address.ofLiteral("06.07.08.09")        ==>    /6.7.8.9
> 
> if we ignore octal encoding, we should document it explicitely.
> 
> Maybe 
> 
>> The loose IPv4 syntax of partial numbers or hex encoding ("10.0.258", "0xA000102") is not supported and causes a parsing error. Octal (zero) prefixes are ignored, i.e. interpreted decimal.
> 
> (However I would prefer to reject octal in this case)

This method behaves like `InetAddress.getByName` in that regards.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15775#discussion_r1329046350


More information about the net-dev mailing list