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:
"127.0.0.1".
7f 00 00 01
"::1".
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
2001:db8::/32), "2001:db8::1".
20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01
"::192.168.0.1".
00 00 00 00 00 00 00 00 00 00 00 00 c0 a8 00 01
"::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:
| Modifier and Type | Class and Description |
|---|---|
| public static class | InetAddresses.
A simple immutable data class to encapsulate the information to be found in a Teredo address. |
| Modifier and Type | Field and Description |
|---|---|
| private static final Inet4Address | |
| private static final char | |
| private static final CharMatcher | |
| private static final int | |
| private static final char | |
| private static final CharMatcher | |
| private static final int | |
| private static final Inet4Address |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static InetAddress | Returns: an InetAddress object created from the raw IP addressthe raw 4-byte or 16-byte IP address in big-endian order addr)Convert a byte array into an InetAddress. |
| public static int | Returns: int, "coerced" if ip is not an IPv4 addressInetAddress to convertReturns an integer representing an IPv4 address regardless of whether the supplied argument is an IPv4 address or not. |
| private static void | compressLongestRunOfZeroes(int[]
int[] mutable array of eight 16-bit hextetsIdentify and mark the longest run of zeroes in an IPv6 address. |
| private static String | |
| public static InetAddress | Returns: a new InetAddress that is one less than the passed in addressthe InetAddress to decrement address)Returns a new InetAddress that is one less than the passed in address. |
| private static IllegalArgumentException | |
| public static InetAddress | Returns: InetAddress representing the argumentString containing an IPv4 or IPv6 string literal, e.g. "192.168.0.1" or "2001:db8::1"Returns the |
| public static InetAddress | Returns: an InetAddress representing the address inhostAddran RFC 3986 section 3.2.2 encoded IPv4 or IPv6 address hostAddr)Returns an InetAddress representing the literal IPv4 or IPv6 host portion of a URL, encoded in the format specified by RFC 3986 section 3.2.2. |
| private static InetAddress | |
| private static InetAddress | Returns: the BigInteger converted to an addressthe address represented as a big integer address, boolean whether the created address should be IPv4 or IPv6 isIpv6)Converts a BigInteger to either an IPv4 or IPv6 address. |
| public static Inet4Address | Returns: Inet4Address equivalent of the argumentint, the 32bit integer address to be convertedReturns an Inet4Address having the integer value specified by the argument. |
| public static Inet4Address | Returns: Inet4Address representation of the given BigIntegerBigInteger representing the IPv4 address address)Returns the |
| public static Inet6Address | Returns: Inet6Address representation of the given BigIntegerBigInteger representing the IPv6 address address)Returns the |
| public static InetAddress | Returns: an InetAddress object created from the raw IP addressthe raw IP address in little-endian byte order addr)Returns an address from a little-endian ordered byte array (the opposite of what |
| public static Inet4Address | Returns: Inet4Address of embedded IPv4 in 6to4 addressInet6Address to be examined for embedded IPv4 in 6to4 addressReturns the IPv4 address embedded in a 6to4 address. |
| public static Inet4Address | Returns: Inet4Address represented "coerced" addressInetAddress to "coerce"Coerces an IPv6 address into an IPv4 address. |
| public static Inet4Address | Returns: Inet4Address of the embedded IPv4 addressInet6Address to be examined for an embedded IPv4 addressReturns the IPv4 address embedded in an IPv4 compatible address. |
| public static Inet4Address | Returns: Inet4Address of embedded IPv4 client addressInet6Address to be examined for embedded IPv4 client addressExamines the Inet6Address to extract the embedded IPv4 client address if the InetAddress is an IPv6 address of one of the specified address types that contain an embedded IPv4 address. |
| private static Inet4Address | Returns: Inet4Address corresponding to the supplied byte arraybyte array representing an IPv4 address (should be of length 4) bytes)Returns an |
| public static Inet4Address | Returns: Inet4Address of embedded IPv4 in an ISATAP addressInet6Address to be examined for embedded IPv4 in ISATAP addressReturns the IPv4 address embedded in an ISATAP address. |
| public static InetAddresses. | Returns: extractedTeredoInfoInet6Address to be examined for embedded Teredo informationReturns the Teredo information embedded in a Teredo address. |
| public static boolean | Returns: true if there is an embedded IPv4 client addressInet6Address to be examined for embedded IPv4 client addressExamines the Inet6Address to determine if it is an IPv6 address of one of the specified address types that contain an embedded IPv4 address. |
| private static String | hextetsToIPv6String(int[]
int[] array of eight 16-bit hextets, or -1sConvert a list of hextets into a human-readable IPv6 address. |
| public static InetAddress | Returns: a new InetAddress that is one more than the passed in addressthe InetAddress to increment address)Returns a new InetAddress that is one more than the passed in address. |
| private static byte[] | |
| public static boolean | Returns: true if the argument is a 6to4 addressInet6Address to be examined for 6to4 address formatEvaluates whether the argument is a 6to4 address. |
| public static boolean | Returns: true if the argument is a valid "compat" addressInet6Address to be examined for embedded IPv4 compatible address formatEvaluates whether the argument is an IPv6 "compat" address. |
| public static boolean | Returns: true if the argument is a valid IP string literalString to evaluated as an IP string literalReturns |
| public static boolean | Returns: true if the argument is an ISATAP addressInet6Address to be examined for ISATAP address formatEvaluates whether the argument is an ISATAP address. |
| public static boolean | Returns: true if the argument is a valid "mapped" addressString to be examined for embedded IPv4-mapped IPv6 address formatEvaluates whether the argument is an "IPv4 mapped" IPv6 address. |
| public static boolean | Returns: true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6Returns true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6. |
| public static boolean | Returns: true if the argument is a Teredo addressInet6Address to be examined for Teredo address formatEvaluates whether the argument is a Teredo address. |
| public static boolean | Returns: true if the argument is a valid IP URI hostString to evaluated as an IP URI host string literalReturns |
| private static short | |
| private static byte | |
| private static byte[] | |
| private static byte[] | |
| public static String | Returns: String containing the text-formatted IP addressInetAddress to be converted to an address stringReturns the string representation of an |
| public static BigInteger | Returns: BigInteger representation of the addressInetAddress to convertReturns a BigInteger representing the address. |
| public static String | Returns: String containing URI-safe string literalInetAddress to be converted to URI string literalReturns the string representation of an |
| ANY4 | back to summary |
|---|---|
| private static final Inet4Address ANY4 | |
| IPV4_DELIMITER | back to summary |
|---|---|
| private static final char IPV4_DELIMITER | |
| IPV4_DELIMITER_MATCHER | back to summary |
|---|---|
| private static final CharMatcher IPV4_DELIMITER_MATCHER | |
| IPV4_PART_COUNT | back to summary |
|---|---|
| private static final int IPV4_PART_COUNT | |
| IPV6_DELIMITER | back to summary |
|---|---|
| private static final char IPV6_DELIMITER | |
| IPV6_DELIMITER_MATCHER | back to summary |
|---|---|
| private static final CharMatcher IPV6_DELIMITER_MATCHER | |
| IPV6_PART_COUNT | back to summary |
|---|---|
| private static final int IPV6_PART_COUNT | |
| LOOPBACK4 | back to summary |
|---|---|
| private static final Inet4Address LOOPBACK4 | |
| InetAddresses | back to summary |
|---|---|
| private InetAddresses() | |
| bytesToInetAddress | back to summary |
|---|---|
| private static InetAddress bytesToInetAddress(byte[] addr) Convert a byte array into an InetAddress.
| |
| coerceToInteger | back to summary |
|---|---|
| public static int coerceToInteger(InetAddress ip) Returns an integer representing an IPv4 address regardless of whether the supplied argument is an IPv4 address or not. IPv6 addresses are coerced to IPv4 addresses before being converted to integers. As long as there are applications that assume that all IP addresses are IPv4 addresses and can therefore be converted safely to integers (for whatever purpose) this function can be used to handle IPv6 addresses as well until the application is suitably fixed. Note an IPv6 address coerced to an IPv4 address can only be used for such purposes as
rudimentary identification or indexing into a collection of real
| |
| compressLongestRunOfZeroes | back to summary |
|---|---|
| private static void compressLongestRunOfZeroes(int[] hextets) Identify and mark the longest run of zeroes in an IPv6 address. Only runs of two or more hextets are considered. In case of a tie, the leftmost run wins. If a qualifying run is found, its hextets are replaced by the sentinel value -1.
| |
| convertDottedQuadToHex | back to summary |
|---|---|
private static String convertDottedQuadToHex(String ipString)
| |
| decrement | back to summary |
|---|---|
| public static InetAddress decrement(InetAddress address) Returns a new InetAddress that is one less than the passed in address. This method works for both IPv4 and IPv6 addresses.
| |
| formatIllegalArgumentException | back to summary |
|---|---|
| private static IllegalArgumentException formatIllegalArgumentException(String format, Object... args) | |
| forString | back to summary |
|---|---|
| public static InetAddress forString(String ipString) Returns the This deliberately avoids all nameservice lookups (e.g. no DNS). Anything after a This method accepts non-ASCII digits, for example
| |
| forUriString | back to summary |
|---|---|
| public static InetAddress forUriString(String hostAddr) Returns an InetAddress representing the literal IPv4 or IPv6 host portion of a URL, encoded in the format specified by RFC 3986 section 3.2.2. This method is similar to This method is the inverse of This method accepts non-ASCII digits, for example
| |
| forUriStringNoThrow | back to summary |
|---|---|
private static InetAddress forUriStringNoThrow(String hostAddr)
| |
| fromBigInteger | back to summary |
|---|---|
| private static InetAddress fromBigInteger(BigInteger address, boolean isIpv6) Converts a BigInteger to either an IPv4 or IPv6 address. If the IP is IPv4, it must be constrained to 32 bits, otherwise it is constrained to 128 bits.
| |
| fromInteger | back to summary |
|---|---|
| public static Inet4Address fromInteger(int address) Returns an Inet4Address having the integer value specified by the argument.
| |
| fromIPv4BigInteger | back to summary |
|---|---|
| public static Inet4Address fromIPv4BigInteger(BigInteger address) Returns the
| |
| fromIPv6BigInteger | back to summary |
|---|---|
| public static Inet6Address fromIPv6BigInteger(BigInteger address) Returns the
| |
| fromLittleEndianByteArray | back to summary |
|---|---|
| public static InetAddress fromLittleEndianByteArray(byte[] addr) throws UnknownHostException Returns an address from a little-endian ordered byte array (the opposite of what IPv4 address byte array must be 4 bytes long and IPv6 byte array must be 16 bytes long.
| |
| get6to4IPv4Address | back to summary |
|---|---|
| public static Inet4Address get6to4IPv4Address(Inet6Address ip) Returns the IPv4 address embedded in a 6to4 address.
| |
| getCoercedIPv4Address | back to summary |
|---|---|
| public static Inet4Address getCoercedIPv4Address(InetAddress ip) Coerces an IPv6 address into an IPv4 address. HACK: As long as applications continue to use IPv4 addresses for indexing into tables,
accounting, et cetera, it may be necessary to coerce IPv6 addresses into IPv4 addresses.
This method does so by hashing 64 bits of the IPv6 address into
A "coerced" IPv4 address is equivalent to itself. Note This method is failsafe for security purposes: ALL IPv6 addresses (except localhost (::1)) are hashed to avoid the security risk associated with extracting an embedded IPv4 address that might permit elevated privileges.
| |
| getCompatIPv4Address | back to summary |
|---|---|
| public static Inet4Address getCompatIPv4Address(Inet6Address ip) Returns the IPv4 address embedded in an IPv4 compatible address.
| |
| getEmbeddedIPv4ClientAddress | back to summary |
|---|---|
| public static Inet4Address getEmbeddedIPv4ClientAddress(Inet6Address ip) Examines the Inet6Address to extract the embedded IPv4 client address if the InetAddress is an IPv6 address of one of the specified address types that contain an embedded IPv4 address. Note ISATAP addresses are explicitly excluded from this method due to their trivial spoofability. With other transition addresses spoofing involves (at least) infection of one's BGP routing table.
| |
| getInet4Address | back to summary |
|---|---|
| private static Inet4Address getInet4Address(byte[] bytes) Returns an
| |
| getIsatapIPv4Address | back to summary |
|---|---|
| public static Inet4Address getIsatapIPv4Address(Inet6Address ip) Returns the IPv4 address embedded in an ISATAP address.
| |
| getTeredoInfo | back to summary |
|---|---|
| public static InetAddresses. Returns the Teredo information embedded in a Teredo address.
| |
| hasEmbeddedIPv4ClientAddress | back to summary |
|---|---|
| public static boolean hasEmbeddedIPv4ClientAddress(Inet6Address ip) Examines the Inet6Address to determine if it is an IPv6 address of one of the specified address types that contain an embedded IPv4 address. Note ISATAP addresses are explicitly excluded from this method due to their trivial spoofability. With other transition addresses spoofing involves (at least) infection of one's BGP routing table.
| |
| hextetsToIPv6String | back to summary |
|---|---|
| private static String hextetsToIPv6String(int[] hextets) Convert a list of hextets into a human-readable IPv6 address. In order for "::" compression to work, the input should contain negative sentinel values in place of the elided zeroes.
| |
| increment | back to summary |
|---|---|
| public static InetAddress increment(InetAddress address) Returns a new InetAddress that is one more than the passed in address. This method works for both IPv4 and IPv6 addresses.
| |
| ipStringToBytes | back to summary |
|---|---|
| private static byte[] ipStringToBytes(String ipStringParam) Returns
| |
| is6to4Address | back to summary |
|---|---|
| public static boolean is6to4Address(Inet6Address ip) Evaluates whether the argument is a 6to4 address. 6to4 addresses begin with the For more on 6to4 addresses see section 2 of RFC 3056.
| |
| isCompatIPv4Address | back to summary |
|---|---|
| public static boolean isCompatIPv4Address(Inet6Address ip) Evaluates whether the argument is an IPv6 "compat" address. An "IPv4 compatible", or "compat", address is one with 96 leading bits of zero, with the
remaining 32 bits interpreted as an IPv4 address. These are conventionally represented in
string literals as For more on IPv4 compatible addresses see section 2.5.5.1 of RFC 4291. Note This method is different from
| |
| isInetAddress | back to summary |
|---|---|
| public static boolean isInetAddress(String ipString) Returns This method accepts non-ASCII digits, for example
| |
| isIsatapAddress | back to summary |
|---|---|
| public static boolean isIsatapAddress(Inet6Address ip) Evaluates whether the argument is an ISATAP address. From RFC 5214: "ISATAP interface identifiers are constructed in Modified EUI-64 format [...] by concatenating the 24-bit IANA OUI (00-00-5E), the 8-bit hexadecimal value 0xFE, and a 32-bit IPv4 address in network byte order [...]" For more on ISATAP addresses see section 6.1 of RFC 5214.
| |
| isMappedIPv4Address | back to summary |
|---|---|
| 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 RFC 4291. Note This method takes a This method accepts non-ASCII digits. That is consistent with
| |
| isMaximum | back to summary |
|---|---|
| public static boolean isMaximum(InetAddress address) Returns true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6.
| |
| isTeredoAddress | back to summary |
|---|---|
| public static boolean isTeredoAddress(Inet6Address ip) Evaluates whether the argument is a Teredo address. Teredo addresses begin with the
| |
| isUriInetAddress | back to summary |
|---|---|
| public static boolean isUriInetAddress(String ipString) Returns This method accepts non-ASCII digits, for example
| |
| parseHextet | back to summary |
|---|---|
| private static short parseHextet(String ipString, int start, int end) | |
| parseOctet | back to summary |
|---|---|
| private static byte parseOctet(String ipString, int start, int end) | |
| textToNumericFormatV4 | back to summary |
|---|---|
private static byte[] textToNumericFormatV4(String ipString)
| |
| textToNumericFormatV6 | back to summary |
|---|---|
private static byte[] textToNumericFormatV6(String ipString)
| |
| toAddrString | back to summary |
|---|---|
| public static String toAddrString(InetAddress ip) Returns the string representation of an For IPv4 addresses, this is identical to This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses such as "::c000:201". The output does not include a Scope ID.
| |
| toBigInteger | back to summary |
|---|---|
| public static BigInteger toBigInteger(InetAddress address) Returns a BigInteger representing the address. Unlike
| |
| toUriString | back to summary |
|---|---|
| public static String toUriString(InetAddress ip) Returns the string representation of an For IPv4 addresses, this is identical to Per section 3.2.2 of RFC 3986, a URI containing an IPv6
string literal is of the form Use of either
| |
All of the fields in this class are encoded in various portions of the IPv6 address as part of the protocol. More protocols details can be found at: http://en.wikipedia. org/wiki/Teredo_tunneling.
The RFC can be found here: RFC 4380.
| Modifier and Type | Field and Description |
|---|---|
| private final Inet4Address | |
| private final int | |
| private final int | |
| private final Inet4Address |
| Access | Constructor and Description |
|---|---|
| public | TeredoInfo(Inet4Address server, Inet4Address client, int port, int flags)
Constructs a TeredoInfo instance. |
| Modifier and Type | Method and Description |
|---|---|
| public Inet4Address | |
| public int | |
| public int | |
| public Inet4Address |
| client | back to summary |
|---|---|
| private final Inet4Address client | |
| flags | back to summary |
|---|---|
| private final int flags | |
| port | back to summary |
|---|---|
| private final int port | |
| server | back to summary |
|---|---|
| private final Inet4Address server | |
| TeredoInfo | back to summary |
|---|---|
| public TeredoInfo(Inet4Address server, Inet4Address client, int port, int flags) Constructs a TeredoInfo instance. Both server and client can be
| |
| getClient | back to summary |
|---|---|
| public Inet4Address getClient() | |
| getFlags | back to summary |
|---|---|
| public int getFlags() | |
| getPort | back to summary |
|---|---|
| public int getPort() | |
| getServer | back to summary |
|---|---|
| public Inet4Address getServer() | |