diff --git a/src/java.base/share/classes/java/net/Inet4Address.java b/src/java.base/share/classes/java/net/Inet4Address.java index bef9fa9500d..16cea2d10d7 100644 --- a/src/java.base/share/classes/java/net/Inet4Address.java +++ b/src/java.base/share/classes/java/net/Inet4Address.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2024, 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 @@ -70,9 +70,9 @@ import java.util.Objects; *
When only one part is given, the value is stored directly in * the network address without any byte rearrangement. * - *
These forms support parts specified in decimal format only. - * For example, the following forms are supported by methods capable - * of parsing textual representations of IPv4 addresses: + *
For example, the following (decimal) forms are supported by the methods + * {@link Inet4Address#ofLiteral(String)} and {@link InetAddress#getByName(String)} + * which are capable of parsing textual representations of IPv4 addresses: * {@snippet : * // Dotted-decimal 'd.d.d.d' form with four part address literal * InetAddress.getByName("007.008.009.010"); // ==> /7.8.9.10 @@ -93,8 +93,16 @@ import java.util.Objects; * Inet4Address.ofLiteral("02130706689"); // ==> /127.0.1.1 * } * + *
The above forms adhere to "strict" decimal-only syntax. + * Additionally, the {@link Inet4Address#ofPosixLiteral(String)} + * method implements a POSIX {@code inet_addr} compatible "loose" + * parsing algorithm, allowing octal and hexadecimal address segments. + * Please refer to + * RFC 6943: Issues in Identifier Comparison for Security + * Purposes. Aside from {@code Inet4Address.ofPosixLiteral(String)}, all methods only + * support strict decimal parsing. *
For methods that return a textual representation as output - * value, the first form, i.e. a dotted-quad string, is used. + * value, the first form, i.e. a dotted-quad string in strict decimal notation, is used. * *
The method {@code ofPosixLiteral} + * implements + * POSIX {@code inet_addr} compatible parsing algorithm, allowing + * octal and hexadecimal address segments. {@code "0"} is the prefix + * for octal numbers, {@code "0x"} and {@code "0X"} are the prefixes + * for hexadecimal numbers. Non-zero address segments that start from + * non-zero digits are parsed as decimal numbers. The following + * (non-decimal) forms are supported by this method: + * {@snippet : + * // Dotted-quad 'x.x.x.x' form with four part address literal + * Inet4Address.ofPosixLiteral("0177.0.0.1"); // ==> /127.0.0.1 + * Inet4Address.ofPosixLiteral("0x7F.0.0.1"); // ==> /127.0.0.1 + * + * // Dotted-triple 'x.x.x' form with three part address literal, + * // the last part is placed in the rightmost two bytes + * // of the constructed address + * Inet4Address.ofPosixLiteral("0177.0.0402"); // ==> /127.0.1.2 + * Inet4Address.ofPosixLiteral("0x7F.0.0x102"); // ==> /127.0.1.2 + * + * // Dotted-double 'x.x' form with two part address literal, + * // the last part is placed in the rightmost three bytes + * // of the constructed address + * Inet4Address.ofPosixLiteral("0177.0201003"); // ==> /127.1.2.3 + * Inet4Address.ofPosixLiteral("0x7F.0x10203"); // ==> /127.1.2.3 + * Inet4Address.ofPosixLiteral("127.66051"); // ==> /127.1.2.3 + * + * // Dotless 'x' form with one value that is stored directly in + * // the constructed address bytes without any rearrangement + * Inet4Address.ofPosixLiteral("0100401404"); // ==> /1.2.3.4 + * Inet4Address.ofPosixLiteral("0x1020304"); // ==> /1.2.3.4 + * Inet4Address.ofPosixLiteral("16909060"); // ==> /1.2.3.4 + * } + *
If the provided IPv4 address literal cannot represent a + * valid IPv4 address in {@linkplain Inet4Address##format-posix + * POSIX form} an {@code IllegalArgumentException} is thrown. + *
This method doesn't block, i.e. no hostname lookup is performed. + * + * @apiNote + * This method produces different results compared to {@linkplain Inet4Address#ofLiteral} + * when {@code posixIPAddressLiteral} parameter contains address segments with + * leading zeroes. An address segment with a leading zero is always parsed as an octal + * number by this method, therefore {@code 0255} (octal) will be parsed as + * {@code 173} (decimal). On the other hand, {@link Inet4Address#ofLiteral + * Inet4Address.ofLiteral} ignores leading zeros, parses all numbers as decimal and produces + * {@code 255}. Where this method would parse {@code 0256.0256.0256.0256} (octal) and + * produce {@code 174.174.174.174} (decimal) in four dotted quad notation, + * {@link Inet4Address#ofLiteral Inet4Address.ofLiteral} will throw + * {@code IllegalArgumentException}. + * + * @param posixIPAddressLiteral a textual representation of an IPv4 address. + * @return an {@link Inet4Address} object with no hostname set, and constructed + * from the provided IPv4 address literal. + * @throws IllegalArgumentException if the {@code posixIPAddressLiteral} cannot be + * parsed as an IPv4 address literal. + * @throws NullPointerException if the {@code posixIPAddressLiteral} is {@code null}. + * @since 23 + */ + public static Inet4Address ofPosixLiteral(String posixIPAddressLiteral) { + Objects.requireNonNull(posixIPAddressLiteral); + return parseAddressStringPosix(posixIPAddressLiteral); + } + /** * Parses the given string as an IPv4 address literal. * If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal @@ -212,6 +288,45 @@ class Inet4Address extends InetAddress { return new Inet4Address(null, addrBytes); } + /** + * Parses the given string as an IPv4 address literal in + * {@linkplain Inet4Address##format-posix POSIX form.} + * + *
If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
+ * in POSIX form and {@code throwIAE} is {@code false}, {@code null} is returned.
+ * If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
+ * and {@code throwIAE} is {@code true}, an {@code IllegalArgumentException}
+ * is thrown.
+ *
+ * @apiNote
+ * This method produces different results compared to {@linkplain Inet4Address#parseAddressString}
+ * when {@code addressLiteral} parameter contains address segments with leading
+ * zeroes. An address segment with a leading zero is always parsed as an octal
+ * number by this method, therefore {@code 0255} (octal) will be parsed as
+ * {@code 173} (decimal). On the other hand, {@link Inet4Address#parseAddressString}
+ * ignores leading zeros, parses all numbers as decimal and produces {@code 255}.
+ * Where this method would parse {@code 0256.0256.0256.0256} (octal) and produce
+ * {@code 174.174.174.174} (decimal) in four dotted quad notation, {@linkplain
+ * Inet4Address#parseAddressString} will either throw {@code IllegalArgumentException}
+ * or return {@code null}, depending on the value of {@code throwIAE}.
+ *
+ * @param addressLiteral IPv4 address literal to parse
+ * @param throwIAE whether to throw {@code IllegalArgumentException} if the
+ * given {@code addressLiteral} string cannot be parsed as
+ * an IPv4 address literal.
+ * @return {@code Inet4Address} object constructed from the address literal;
+ * or {@code null} if the literal cannot be parsed as an IPv4 address
+ * @throws IllegalArgumentException if the given {@code addressLiteral} string
+ * cannot be parsed as an IPv4 address literal and {@code throwIAE} is {@code true}.
+ */
+ private static Inet4Address parseAddressStringPosix(String addressLiteral) {
+ byte [] parsedBytes = IPAddressUtil.parseBsdLiteralV4(addressLiteral);
+ if (parsedBytes == null) {
+ throw IPAddressUtil.invalidIpAddressLiteral(addressLiteral);
+ }
+ return new Inet4Address(null, parsedBytes);
+ }
+
/**
* Replaces the object to be serialized with an InetAddress object.
*
diff --git a/src/java.base/share/classes/java/net/InetAddress.java b/src/java.base/share/classes/java/net/InetAddress.java
index 534553a99f3..7e0c39f9111 100644
--- a/src/java.base/share/classes/java/net/InetAddress.java
+++ b/src/java.base/share/classes/java/net/InetAddress.java
@@ -1722,6 +1722,7 @@ public sealed class InetAddress implements Serializable permits Inet4Address, In
* @throws NullPointerException if the {@code ipAddressLiteral} is {@code null}.
* @see Inet4Address#ofLiteral(String)
* @see Inet6Address#ofLiteral(String)
+ * @see Inet4Address#ofPosixLiteral(String)
* @since 22
*/
public static InetAddress ofLiteral(String ipAddressLiteral) {
diff --git a/src/java.base/share/classes/sun/net/util/IPAddressUtil.java b/src/java.base/share/classes/sun/net/util/IPAddressUtil.java
index e81b6543393..ecd60a9ffc4 100644
--- a/src/java.base/share/classes/sun/net/util/IPAddressUtil.java
+++ b/src/java.base/share/classes/sun/net/util/IPAddressUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2024, 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
@@ -666,40 +666,81 @@ public class IPAddressUtil {
* {@code false} otherwise.
*/
public static boolean isBsdParsableV4(String input) {
+ return parseBsdLiteralV4(input) != null;
+ }
+
+ /**
+ * Parse String as IPv4 address literal by following
+ * POSIX-style formatting rules.
+ *
+ * @param input a String representing an IPv4 address in POSIX format
+ * @return a byte array representing the IPv4 numeric address
+ * if input string is a parsable POSIX formatted IPv4 address literal,
+ * {@code null} otherwise.
+ */
+ public static byte[] parseBsdLiteralV4(String input) {
+
+ byte[] res = new byte[]{0,0,0,0};
+
+ int len = input.length();
+ if (len == 0) {
+ return null;
+ }
char firstSymbol = input.charAt(0);
// Check if first digit is not a decimal digit
if (parseAsciiDigit(firstSymbol, DECIMAL) == -1) {
- return false;
+ return null;
}
// Last character is dot OR is not a supported digit: [0-9,A-F,a-f]
- char lastSymbol = input.charAt(input.length() - 1);
+ char lastSymbol = input.charAt(len - 1);
if (lastSymbol == '.' || parseAsciiHexDigit(lastSymbol) == -1) {
- return false;
+ return null;
}
// Parse IP address fields
CharBuffer charBuffer = CharBuffer.wrap(input);
int fieldNumber = 0;
+ long fieldValue = -1L;
while (charBuffer.hasRemaining()) {
- long fieldValue = -1L;
+ fieldValue = -1L;
// Try to parse fields in all supported radixes
for (int radix : SUPPORTED_RADIXES) {
fieldValue = parseV4FieldBsd(radix, charBuffer, fieldNumber);
if (fieldValue >= 0) {
+ if (fieldValue < 256) {
+ // Store the parsed field in the byte buffer.
+ // If the field value is greater than 255, it can only be the last field.
+ // If it is not the last one, parseV4FieldBsd enforces this limit
+ // and returns TERMINAL_PARSE_ERROR.
+ res[fieldNumber] = (byte) fieldValue;
+ }
fieldNumber++;
break;
} else if (fieldValue == TERMINAL_PARSE_ERROR) {
- return false;
+ return null;
}
}
// If field can't be parsed as one of supported radixes stop
// parsing
if (fieldValue < 0) {
- return false;
+ return null;
}
}
- return true;
+ // The last field value must be non-negative
+ if (fieldValue < 0) {
+ return null;
+ }
+ // If the last fieldValue is greater than 255 (fieldNumber < 4),
+ // it is written to the last (4 - (fieldNumber - 1)) octets
+ // in the network order
+ if (fieldNumber < 4) {
+ for (int i = 3; i >= fieldNumber - 1; --i) {
+ res[i] = (byte) (fieldValue & 255);
+ fieldValue >>= 8;
+ }
+ }
+ return res;
}
/**
diff --git a/test/jdk/java/net/InetAddress/OfLiteralTest.java b/test/jdk/java/net/InetAddress/OfLiteralTest.java
index 90148a786af..090523a9ee9 100644
--- a/test/jdk/java/net/InetAddress/OfLiteralTest.java
+++ b/test/jdk/java/net/InetAddress/OfLiteralTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2024, 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
@@ -22,8 +22,8 @@
*/
/* @test
- * @bug 8272215
- * @summary Test for ofLiteral API in InetAddress classes
+ * @bug 8272215 8315767
+ * @summary Test for ofLiteral, ofPosixLiteral APIs in InetAddress classes
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
* OfLiteralTest
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
@@ -67,11 +67,15 @@ public class OfLiteralTest {
InetAddress ofLiteralResult = switch (inetAddressClass) {
case INET_ADDRESS -> InetAddress.ofLiteral(addressLiteral);
case INET4_ADDRESS -> Inet4Address.ofLiteral(addressLiteral);
+ case INET4_ADDRESS_POSIX -> Inet4Address.ofPosixLiteral(addressLiteral);
case INET6_ADDRESS -> Inet6Address.ofLiteral(addressLiteral);
};
- InetAddress getByNameResult = InetAddress.getByName(addressLiteral);
Assert.assertArrayEquals(expectedAddressBytes, ofLiteralResult.getAddress());
- Assert.assertEquals(getByNameResult, ofLiteralResult);
+ // POSIX literals are not compatible with InetAddress.getByName()
+ if (inetAddressClass != InetAddressClass.INET4_ADDRESS_POSIX) {
+ InetAddress getByNameResult = InetAddress.getByName(addressLiteral);
+ Assert.assertEquals(getByNameResult, ofLiteralResult);
+ }
}
private static Stream