Skip to content

Commit

Permalink
8315767: InetAddress: constructing objects from BSD literal addresses
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, aefimov, michaelm, jpai
  • Loading branch information
sercher authored and jaikiran committed May 23, 2024
1 parent 2a11e0d commit c2180d1
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 20 deletions.
125 changes: 120 additions & 5 deletions src/java.base/share/classes/java/net/Inet4Address.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -70,9 +70,9 @@
* <p> When only one part is given, the value is stored directly in
* the network address without any byte rearrangement.
*
* <p> 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:
* <p> 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
Expand All @@ -93,8 +93,16 @@
* Inet4Address.ofLiteral("02130706689"); // ==> /127.0.1.1
* }
*
* <p> 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 <a href="https://www.ietf.org/rfc/rfc6943.html#section-3.1.1">
* <i>RFC&nbsp;6943: Issues in Identifier Comparison for Security
* Purposes</i></a>. Aside from {@code Inet4Address.ofPosixLiteral(String)}, all methods only
* support strict decimal parsing.
* <p> 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.
*
* <h3> The Scope of a Multicast Address </h3>
*
Expand All @@ -112,6 +120,8 @@
* RFC 2365: Administratively Scoped IP Multicast
* @spec https://www.rfc-editor.org/info/rfc790
* RFC 790: Assigned numbers
* @spec https://www.rfc-editor.org/rfc/rfc6943.html#section-3.1.1
* RFC 6943: Issues in Identifier Comparison for Security Purposes
* @since 1.4
*/

Expand Down Expand Up @@ -180,6 +190,72 @@ public static Inet4Address ofLiteral(String ipv4AddressLiteral) {
return parseAddressString(ipv4AddressLiteral, true);
}

/**
* Creates an {@code Inet4Address} based on the provided {@linkplain
* Inet4Address##format-posix textual representation of an IPv4 address in
* POSIX {@code inet_addr} compatible form}.
* <p> <a id="format-posix"></a> The method {@code ofPosixLiteral}
* implements <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_addr.html">
* POSIX {@code inet_addr}</a> 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
* }
* <p> If the provided IPv4 address literal cannot represent a
* valid IPv4 address in {@linkplain Inet4Address##format-posix
* POSIX form} an {@code IllegalArgumentException} is thrown.
* <p> 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
Expand Down Expand Up @@ -212,6 +288,45 @@ static Inet4Address parseAddressString(String addressLiteral, boolean throwIAE)
return new Inet4Address(null, addrBytes);
}

/**
* Parses the given string as an IPv4 address literal in
* {@linkplain Inet4Address##format-posix POSIX form.}
*
* <p> 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.
*
Expand Down
1 change: 1 addition & 0 deletions src/java.base/share/classes/java/net/InetAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,7 @@ static InetAddress[] getAllByName0 (String host, boolean check)
* @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) {
Expand Down
57 changes: 49 additions & 8 deletions src/java.base/share/classes/sun/net/util/IPAddressUtil.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -666,40 +666,81 @@ public static int digit(char ch, int radix) {
* {@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;
}

/**
Expand Down
Loading

1 comment on commit c2180d1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.