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

Reverse toString parsing for InetAddresses. #6713

Open
rhuffy opened this issue Sep 11, 2023 · 7 comments
Open

Reverse toString parsing for InetAddresses. #6713

rhuffy opened this issue Sep 11, 2023 · 7 comments

Comments

@rhuffy
Copy link

rhuffy commented Sep 11, 2023

InetAddress.toString() outputs a string in the format <hostname>/<address>. I have use cases where I need to convert this string back to an Inet4Address or Inet6Address, which I think would be useful to include in Guava's InetAddresses class.

The code I'm using currently looks like this:

  public static InetAddress forHostnameAddressString(String hostAndAddr) throws UnknownHostException {
    checkNotNull(hostAndAddr);

    if (hostAndAddr.contains("/")) {
      String[] parts = hostAndAddr.split("/", 2);
      String host = parts[0];
      String addr = parts[1];
      InetAddress inetAddress = InetAddresses.forString(addr);

      if (inetAddress instanceof Inet4Address) {
        return Inet4Address.getByAddress(host, inetAddress.getAddress());
      } else if (inetAddress instanceof Inet6Address) {
        return Inet6Address.getByAddress(host, inetAddress.getAddress(), ((Inet6Address) inetAddress).getScopeId());
      } else {
        throw new IllegalStateException("InetAddress must be an instance of Inet4Address or Inet6Address");
      }
    } else {
      return InetAddresses.forString(hostAndAddr);
    }
  }
@netdpb
Copy link
Member

netdpb commented Sep 15, 2023

Are these cases in which it's not guaranteed that the string was generated from InetAddress.toString()? Because those always have the "/".

@Shin0017
Copy link

Hi netdpb! May I take this issue?

@netdpb
Copy link
Member

netdpb commented Oct 16, 2023

Hi @Shin0017. Thanks for asking! Generally we don't want to accept new APIs until we have some more discussion of the requirements, the use cases, and specifically the API. I haven't heard back from @rhuffy about my question yet. Do you have any thoughts here?

@Shin0017
Copy link

Thanks for your response!

If the string does not contain '/', then this string was not generated from InetAddress.toString(). Otherwise, individually check the legality of each part generated by split() to ensure that this string can be generated by some InetAddress.toString(). If the verification confirms its legitimacy, return the corresponding Inet4Address or Inet6Address.

@netdpb
Copy link
Member

netdpb commented Oct 17, 2023

Thanks.

We still have questions about the usefulness of this feature. How many people need such an API? (We have seen no need for it within Google.) If you're serializing InetAddress. why is the toString() the right serialization format?

Another question for @rhuffy: The code snippet you provided passes the addr part to InetAddresses.forString(String) and uses the parsed scope ID portion—but that method is documented to drop any such scope ID from the string. Is the scope ID part important? If not, why not? If so, I think it would be the first example of an API in InetAddresses that cares about the scope ID.

@blackdurumi
Copy link

Hi @netdpb, I'd like to work on this issue, too.

I think we can create a new static method that does the exact opposite of toString(), and just ignore the other cases(let's throw an exception).
How do you think?

@netdpb
Copy link
Member

netdpb commented Jan 10, 2024

Hi @blackdurumi. Until we have a better sense of why such a feature would be actually useful to people, we won't accept it as an API addition. Additionally, there are questions about whether it's even possible (or desirable) to round-trip the scope ID.

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

No branches or pull requests

4 participants