Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HostAndPort to support parsing ip+port string #2412

Merged
merged 9 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static io.servicetalk.http.api.HttpHeaderNames.AUTHORIZATION;
import static io.servicetalk.http.api.HttpHeaderNames.HOST;
import static io.servicetalk.http.api.HttpRequestMethod.CONNECT;
import static io.servicetalk.utils.internal.NetworkUtils.isValidIpV6Address;
import static java.lang.System.lineSeparator;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
Expand All @@ -39,6 +40,7 @@
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -208,19 +210,21 @@ void testParseHttpUriAbsoluteFormWithHost() {
}

@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
void testEffectiveHostIPv6NoPort() {
createFixture("some/path?foo=bar&abc=def&foo=baz");
fixture.headers().set(HOST, "[1:2:3::5]");

assertEffectiveHostAndPort("[1:2:3::5]");
assertEffectiveHostAndPort("1:2:3::5");
}

@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
void testEffectiveHostIPv6WithPort() {
createFixture("some/path?foo=bar&abc=def&foo=baz");
fixture.headers().set(HOST, "[1:2:3::5]:8080");

assertEffectiveHostAndPort("[1:2:3::5]", 8080);
assertEffectiveHostAndPort("1:2:3::5", 8080);
}

@Test
Expand Down Expand Up @@ -839,6 +843,9 @@ private void assertEffectiveHostAndPort(String hostName, int port) {
assertNotNull(effectiveHostAndPort);
assertEquals(hostName, effectiveHostAndPort.hostName());
assertEquals(port, effectiveHostAndPort.port());
assertThat(effectiveHostAndPort.toString(), isValidIpV6Address(hostName) ?
equalTo('[' + hostName + "]:" + port) :
equalTo(hostName + ':' + port));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@
*/
package io.servicetalk.transport.api;

import javax.annotation.Nullable;

import static io.servicetalk.utils.internal.NetworkUtils.isValidIpV4Address;
import static io.servicetalk.utils.internal.NetworkUtils.isValidIpV6Address;
import static java.lang.Integer.parseInt;
import static java.util.Objects.requireNonNull;

/**
* A default immutable implementation of {@link HostAndPort}.
*/
final class DefaultHostAndPort implements HostAndPort {
/**
* {@code xxx.xxx.xxx.xxx:yyyyy}
*/
private static final int MAX_IPV4_LEN = 21;
/**
* {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} = 47 chars w/out zone id
*/
private static final int MAX_IPV6_LEN = 47 + 12 /* some limit for zone id length */;
private static final String STR_IPV6 = "_ipv6_";
private final String hostName;
@Nullable
private String toString;
private final int port;

/**
Expand All @@ -30,8 +46,86 @@ final class DefaultHostAndPort implements HostAndPort {
* @param port the port.
*/
DefaultHostAndPort(String hostName, int port) {
if (isValidIpV6Address(requireNonNull(hostName))) { // Normalize ipv6 so equals/hashCode works as expected
Copy link
Member Author

@Scottmitch Scottmitch Nov 2, 2022

Choose a reason for hiding this comment

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

@idelpivnitskiy - note that I changed IPv6 do compression to be consistent in all cases.

this.hostName = hostName.charAt(0) == '[' ?
compressIPv6(hostName, 1, hostName.length() - 1) : compressIPv6(hostName, 0, hostName.length());
this.toString = STR_IPV6;
} else {
this.hostName = hostName;
}
this.port = port;
}

DefaultHostAndPort(String hostName, int port, boolean isIPv6) {
this.hostName = requireNonNull(hostName);
this.port = port;
idelpivnitskiy marked this conversation as resolved.
Show resolved Hide resolved
this.toString = isIPv6 ? STR_IPV6 : null;
}

/**
* Parse IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} and IPv6 {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} style
* addresses.
* @param ipPort An IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} or IPv6
* {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} addresses.
* @param startIndex The index at which the address parsing starts.
* @return A {@link HostAndPort} where the hostname is the IP address and the port is parsed from the string.
*/
static HostAndPort parseFromIpPort(String ipPort, int startIndex) {
String inetAddress;
final boolean isv6;
int i;
if (ipPort.charAt(startIndex) == '[') { // check if ipv6
if (ipPort.length() - startIndex > MAX_IPV6_LEN) {
throw new IllegalArgumentException("Invalid IPv6 address: " + ipPort.substring(startIndex));
}
i = ipPort.indexOf(']');
if (i <= startIndex) {
throw new IllegalArgumentException("unable to find end ']' of IPv6 address: " +
ipPort.substring(startIndex));
}
inetAddress = ipPort.substring(startIndex + 1, i);
++i;
isv6 = true;
if (i >= ipPort.length()) {
throw new IllegalArgumentException("no port found after ']' of IPv6 address: " +
ipPort.substring(startIndex));
} else if (ipPort.charAt(i) != ':') {
throw new IllegalArgumentException("':' expected after ']' for IPv6 address: " +
ipPort.substring(startIndex));
}
} else {
if (ipPort.length() - startIndex > MAX_IPV4_LEN) {
throw new IllegalArgumentException("Invalid IPv4 address: " + ipPort.substring(startIndex));
}
i = ipPort.lastIndexOf(':');
if (i < 0) {
throw new IllegalArgumentException("no port found: " + ipPort.substring(startIndex));
}
inetAddress = ipPort.substring(startIndex, i);
isv6 = false;
}

final int port;
try {
port = parseInt(ipPort.substring(i + 1));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("invalid port " + ipPort.substring(startIndex), e);
}
if (!isValidPort(port) || ipPort.charAt(i + 1) == '+') { // parseInt allows '+' but we don't want this
throw new IllegalArgumentException("invalid port " + ipPort.substring(startIndex));
}

if (isv6) {
if (!isValidIpV6Address(inetAddress)) {
Scottmitch marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Invalid IPv6 address: " + inetAddress);
}
inetAddress = compressIPv6(inetAddress, 0, inetAddress.length());
return new DefaultHostAndPort(inetAddress, port, true);
}
if (!isValidIpV4Address(inetAddress)) {
throw new IllegalArgumentException("Invalid IPv4 address: " + inetAddress);
}
return new DefaultHostAndPort(inetAddress, port, false);
}

@Override
Expand All @@ -46,7 +140,13 @@ public int port() {

@Override
public String toString() {
return hostName + ':' + port;
String str = toString;
if (str == null) {
toString = str = hostName + ':' + port;
} else if (str == STR_IPV6) {
toString = str = '[' + hostName + "]:" + port;
}
return str;
}

@Override
Expand All @@ -62,4 +162,68 @@ public boolean equals(Object o) {
public int hashCode() {
return 31 * (31 + port) + hostName.hashCode();
}

private static boolean isValidPort(int port) {
return port >= 0 && port <= 65535;
}

private static String compressIPv6(String rawIp, int start, int end) {
if (end - start <= 0) {
throw new IllegalArgumentException("Empty IPv6 address");
}
// https://datatracker.ietf.org/doc/html/rfc5952#section-2
// JDK doesn't do IPv6 compression, or remove leading 0s. This may lead to inconsistent String representation
// which will yield different hash-codes and equals comparisons to fail when it shouldn't.
int longestZerosCount = 0;
int longestZerosBegin = -1;
int longestZerosEnd = -1;
int zerosCount = 0;
int zerosBegin = rawIp.charAt(start) != '0' ? -1 : 0;
int zerosEnd = -1;
boolean isCompressed = false;
char prevChar = '\0';
StringBuilder compressedIPv6Builder = new StringBuilder(end - start);
for (int i = start; i < end; ++i) {
final char c = rawIp.charAt(i);
switch (c) {
case '0':
if (zerosBegin < 0 || i == end - 1) {
compressedIPv6Builder.append('0');
}
break;
case ':':
if (prevChar == ':') {
isCompressed = true;
compressedIPv6Builder.append(':');
} else if (zerosBegin >= 0) {
++zerosCount;
compressedIPv6Builder.append("0:");
zerosEnd = compressedIPv6Builder.length();
} else {
compressedIPv6Builder.append(':');
zerosBegin = compressedIPv6Builder.length();
}
break;
default:
// https://datatracker.ietf.org/doc/html/rfc5952#section-4.2.3
// if there is a tie in the longest length, we must choose the first to compress.
if (zerosEnd > 0 && zerosCount > longestZerosCount) {
longestZerosCount = zerosCount;
longestZerosBegin = zerosBegin;
longestZerosEnd = zerosEnd;
}
zerosBegin = zerosEnd = -1;
zerosCount = 0;
compressedIPv6Builder.append(c);
break;
}
prevChar = c;
}
// https://datatracker.ietf.org/doc/html/rfc5952#section-4.2.2
// The symbol "::" MUST NOT be used to shorten just one 16-bit 0 field.
if (!isCompressed && longestZerosBegin >= 0 && longestZerosCount > 1) {
compressedIPv6Builder.replace(longestZerosBegin, longestZerosEnd, longestZerosBegin == 0 ? "::" : ":");
}
return compressedIPv6Builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.servicetalk.transport.api;

import java.net.Inet6Address;
import java.net.InetSocketAddress;

/**
Expand Down Expand Up @@ -55,6 +56,31 @@ static HostAndPort of(String host, int port) {
* @return the {@link HostAndPort}.
*/
static HostAndPort of(InetSocketAddress address) {
return new DefaultHostAndPort(address.getHostString(), address.getPort());
return new DefaultHostAndPort(address.getHostString(), address.getPort(),
address.getAddress() instanceof Inet6Address);
}

/**
* Parse IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} and IPv6 {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} style
* addresses.
* @param ipPort An IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} or IPv6
* {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} addresses.
* @return A {@link HostAndPort} where the hostname is the IP address and the port is parsed from the string.
* @see #ofIpPort(String, int)
*/
static HostAndPort ofIpPort(String ipPort) {
return DefaultHostAndPort.parseFromIpPort(ipPort, 0);
}

/**
* Parse IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} and IPv6 {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} style
* addresses.
* @param ipPort An IPv4 {@code xxx.xxx.xxx.xxx:yyyyy} or IPv6
* {@code [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:yyyyy} addresses.
* @param startIndex The index at which the address parsing starts.
* @return A {@link HostAndPort} where the hostname is the IP address and the port is parsed from the string.
*/
static HostAndPort ofIpPort(String ipPort, int startIndex) {
return DefaultHostAndPort.parseFromIpPort(ipPort, startIndex);
}
}
Loading