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

Unresolved ipv6 InetSocketAddress is not parsed properly #74

Closed
arsentiikaptsan opened this issue Oct 7, 2021 · 3 comments
Closed

Unresolved ipv6 InetSocketAddress is not parsed properly #74

arsentiikaptsan opened this issue Oct 7, 2021 · 3 comments

Comments

@arsentiikaptsan
Copy link

Hi!

I ran into a problem, when I tried to create HostName from unresolved ipv6 InetSocketAddress. Here is a snippet:

var address = InetSocketAddress.createUnresolved("::1", 8080);
var hostName = new HostName(address);
System.out.println("Is valid: " + hostName.isValid());
System.out.println("Host: " + hostName.getHost());
System.out.println("Port: " + hostName.getPort());
System.out.println("HostName as string: " + hostName.toNormalizedString());

Output:

Is valid: true
Host: ::1:8080
Port: null
HostName as string: [0:0:0:0:0:0:1:8080]

Apparently, HostName does not properly parse port in this case. However if I create address with:
new InetSocketAddress("::1", 8080), everything is fine and I get [0:0:0:0:0:0:0:1]:8080 as the result.

@seancfoley
Copy link
Owner

Yes, this is a bug.

To trigger this bug you need an IPv6 compressed address like "::1", and you need to create the InetSocketAddress with InetSocketAddress.createUnresolved. If both those happen, then the port will become the last part of the address.

A fix for this will go into future releases.

In the meantime, the work-around choices would be:

  1. Use the constructors for InetSocketAddress and not InetSocketAddress.createUnresolved, as you have shown.
  2. Use the following code to construct the HostName from an InetSocketAddress. This code allows you to continue using InetSocketAddress.createUnresolved:
static HostNameParameters p = HostName.DEFAULT_VALIDATION_OPTIONS.toBuilder().expectPort(true).toParams();

HostName create() {
		InetSocketAddress sa = InetSocketAddress.createUnresolved("::1", 8080);
		String str = sa.getHostString() + HostName.PORT_SEPARATOR + sa.getPort();
		return new HostName(str, p);
}

@arsentiikaptsan
Copy link
Author

Understood, thank you!

@seancfoley
Copy link
Owner

seancfoley commented Mar 22, 2022

This has been fixed in the newly released version 5.3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants