Class InetAddresses

java.lang.Object
org.glassfish.jersey.internal.guava.InetAddresses

public final class InetAddresses extends Object
Static utility methods pertaining to InetAddress instances.

Important note: Unlike InetAddress.getByName(), the methods of this class never cause DNS services to be accessed. For this reason, you should prefer these methods as much as possible over their JDK equivalents whenever you are expecting to handle only IP address string literals -- there is no blocking DNS penalty for a malformed string.

When dealing with Inet4Address and Inet6Address objects as byte arrays (vis. InetAddress.getAddress()) they are 4 and 16 bytes in length, respectively, and represent the address in network byte order.

Examples of IP addresses and their byte representations:

  • The IPv4 loopback address, "127.0.0.1".
    7f 00 00 01

  • The IPv6 loopback address, "::1".
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01

  • From the IPv6 reserved documentation prefix (2001:db8::/32), "2001:db8::1".
    20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01

  • An IPv6 "IPv4 compatible" (or "compat") address, "::192.168.0.1".
    00 00 00 00 00 00 00 00 00 00 00 00 c0 a8 00 01

  • An IPv6 "IPv4 mapped" address, "::ffff:192.168.0.1".
    00 00 00 00 00 00 00 00 00 00 ff ff c0 a8 00 01

A few notes about IPv6 "IPv4 mapped" addresses and their observed use in Java.

"IPv4 mapped" addresses were originally a representation of IPv4 addresses for use on an IPv6 socket that could receive both IPv4 and IPv6 connections (by disabling the IPV6_V6ONLY socket option on an IPv6 socket). Yes, it's confusing. Nevertheless, these "mapped" addresses were never supposed to be seen on the wire. That assumption was dropped, some say mistakenly, in later RFCs with the apparent aim of making IPv4-to-IPv6 transition simpler.

Technically one can create a 128bit IPv6 address with the wire format of a "mapped" address, as shown above, and transmit it in an IPv6 packet header. However, Java's InetAddress creation methods appear to adhere doggedly to the original intent of the "mapped" address: all "mapped" addresses return Inet4Address objects.

For added safety, it is common for IPv6 network operators to filter all packets where either the source or destination address appears to be a "compat" or "mapped" address. Filtering suggestions usually recommend discarding any packets with source or destination addresses in the invalid range ::/3, which includes both of these bizarre address formats. For more information on "bogons", including lists of IPv6 bogon space, see:

Since:
5.0
Author:
Erik Kline
  • Method Details

    • isUriInetAddress

      public static boolean isUriInetAddress(String ipString)
      Returns true if the supplied string is a valid URI IP string literal, false otherwise.
      Parameters:
      ipString - String to evaluated as an IP URI host string literal
      Returns:
      true if the argument is a valid IP URI host
    • isMappedIPv4Address

      public static boolean isMappedIPv4Address(String ipString)
      Evaluates whether the argument is an "IPv4 mapped" IPv6 address.

      An "IPv4 mapped" address is anything in the range ::ffff:0:0/96 (sometimes written as ::ffff:0.0.0.0/96), with the last 32 bits interpreted as an IPv4 address.

      For more on IPv4 mapped addresses see section 2.5.5.2 of http://tools.ietf.org/html/rfc4291

      Note: This method takes a String argument because InetAddress automatically collapses mapped addresses to IPv4. (It is actually possible to avoid this using one of the obscure Inet6Address methods, but it would be unwise to depend on such a poorly-documented feature.)

      Parameters:
      ipString - String to be examined for embedded IPv4-mapped IPv6 address format
      Returns:
      true if the argument is a valid "mapped" address
      Since:
      10.0