Class InetAddresses
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDeprecated.The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023 -
Method Summary
Modifier and TypeMethodDescriptionstatic intDeprecated.Returns an integer representing an IPv4 address regardless of whether the supplied argument is an IPv4 address or not.static InetAddressDeprecated.Returns theInetAddresshaving the given string representation.static InetAddressforUriString(String hostAddr) Deprecated.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.static Inet4AddressfromInteger(int address) Deprecated.Returns an Inet4Address having the integer value specified by the argument.static InetAddressfromLittleEndianByteArray(byte[] addr) Deprecated.Returns an address from a little-endian ordered byte array (the opposite of whatInetAddress.getByAddress(java.lang.String, byte[])expects).static Inet4AddressDeprecated.Returns the IPv4 address embedded in a 6to4 address.static Inet4AddressDeprecated.Coerces an IPv6 address into an IPv4 address.static Inet4AddressDeprecated.Returns the IPv4 address embedded in an IPv4 compatible address.static Inet4AddressDeprecated.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.static Inet4AddressDeprecated.Returns the IPv4 address embedded in an ISATAP address.static InetAddresses.TeredoInfoDeprecated.Returns the Teredo information embedded in a Teredo address.static booleanDeprecated.Examines the Inet6Address to determine if it is an IPv6 address of one of the specified address types that contain an embedded IPv4 address.static InetAddressincrement(InetAddress address) Deprecated.Returns a new InetAddress that is one more than the passed in address.static booleanDeprecated.Evaluates whether the argument is a 6to4 address.static booleanDeprecated.Evaluates whether the argument is an IPv6 "compat" address.static booleanisInetAddress(String ipString) Deprecated.Returnstrueif the supplied string is a valid IP string literal,falseotherwise.static booleanDeprecated.Evaluates whether the argument is an ISATAP address.static booleanisMappedIPv4Address(String ipString) Deprecated.Evaluates whether the argument is an "IPv4 mapped" IPv6 address.static booleanisMaximum(InetAddress address) Deprecated.Returns true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6.static booleanDeprecated.Evaluates whether the argument is a Teredo address.static booleanisUriInetAddress(String ipString) Deprecated.Returnstrueif the supplied string is a valid URI IP string literal,falseotherwise.static StringDeprecated.Returns the string representation of anInetAddress.static StringDeprecated.Returns the string representation of anInetAddresssuitable for inclusion in a URI.
-
Method Details
-
forString
Deprecated.Returns theInetAddresshaving the given string representation.This deliberately avoids all nameservice lookups (e.g. no DNS).
- Parameters:
ipString-Stringcontaining an IPv4 or IPv6 string literal, e.g."192.168.0.1"or"2001:db8::1"- Returns:
InetAddressrepresenting the argument- Throws:
IllegalArgumentException- if the argument is not a valid IP string literal
-
isInetAddress
Deprecated.Returnstrueif the supplied string is a valid IP string literal,falseotherwise.- Parameters:
ipString-Stringto evaluated as an IP string literal- Returns:
trueif the argument is a valid IP string literal
-
toAddrString
Deprecated.Returns the string representation of anInetAddress.For IPv4 addresses, this is identical to
InetAddress.getHostAddress(), but for IPv6 addresses, the output follows RFC 5952 section 4. The main difference is that this method uses "::" for zero compression, while Java's version uses the uncompressed form.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.
- Parameters:
ip-InetAddressto be converted to an address string- Returns:
Stringcontaining the text-formatted IP address- Since:
- 10.0
-
toUriString
Deprecated.Returns the string representation of anInetAddresssuitable for inclusion in a URI.For IPv4 addresses, this is identical to
InetAddress.getHostAddress(), but for IPv6 addresses it compresses zeroes and surrounds the text with square brackets; for example"[2001:db8::1]".Per section 3.2.2 of http://tools.ietf.org/html/rfc3986, a URI containing an IPv6 string literal is of the form
"http://[2001:db8::1]:8888/index.html".Use of either
toAddrString(java.net.InetAddress),InetAddress.getHostAddress(), or this method is recommended overInetAddress.toString()when an IP address string literal is desired. This is becauseInetAddress.toString()prints the hostname and the IP address string joined by a "/".- Parameters:
ip-InetAddressto be converted to URI string literal- Returns:
Stringcontaining URI-safe string literal
-
forUriString
Deprecated.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 function is similar to
forString(String), however, it requires that IPv6 addresses are surrounded by square brackets.This function is the inverse of
toUriString(java.net.InetAddress).- Parameters:
hostAddr- A RFC 3986 section 3.2.2 encoded IPv4 or IPv6 address- Returns:
- an InetAddress representing the address in
hostAddr - Throws:
IllegalArgumentException- ifhostAddris not a valid IPv4 address, or IPv6 address surrounded by square brackets
-
isUriInetAddress
Deprecated.Returnstrueif the supplied string is a valid URI IP string literal,falseotherwise.- Parameters:
ipString-Stringto evaluated as an IP URI host string literal- Returns:
trueif the argument is a valid IP URI host
-
isCompatIPv4Address
Deprecated.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
"::192.168.0.1", though"::c0a8:1"is also considered an IPv4 compatible address (and equivalent to"::192.168.0.1").For more on IPv4 compatible addresses see section 2.5.5.1 of http://tools.ietf.org/html/rfc4291
NOTE: This method is different from
Inet6Address.isIPv4CompatibleAddress()in that it more correctly classifies"::"and"::1"as proper IPv6 addresses (which they are), NOT IPv4 compatible addresses (which they are generally NOT considered to be).- Parameters:
ip-Inet6Addressto be examined for embedded IPv4 compatible address format- Returns:
trueif the argument is a valid "compat" address
-
getCompatIPv4Address
Deprecated.Returns the IPv4 address embedded in an IPv4 compatible address.- Parameters:
ip-Inet6Addressto be examined for an embedded IPv4 address- Returns:
Inet4Addressof the embedded IPv4 address- Throws:
IllegalArgumentException- if the argument is not a valid IPv4 compatible address
-
is6to4Address
Deprecated.Evaluates whether the argument is a 6to4 address.6to4 addresses begin with the
"2002::/16"prefix. The next 32 bits are the IPv4 address of the host to which IPv6-in-IPv4 tunneled packets should be routed.For more on 6to4 addresses see section 2 of http://tools.ietf.org/html/rfc3056
- Parameters:
ip-Inet6Addressto be examined for 6to4 address format- Returns:
trueif the argument is a 6to4 address
-
get6to4IPv4Address
Deprecated.Returns the IPv4 address embedded in a 6to4 address.- Parameters:
ip-Inet6Addressto be examined for embedded IPv4 in 6to4 address- Returns:
Inet4Addressof embedded IPv4 in 6to4 address- Throws:
IllegalArgumentException- if the argument is not a valid IPv6 6to4 address
-
isTeredoAddress
Deprecated.Evaluates whether the argument is a Teredo address.Teredo addresses begin with the
"2001::/32"prefix.- Parameters:
ip-Inet6Addressto be examined for Teredo address format- Returns:
trueif the argument is a Teredo address
-
getTeredoInfo
Deprecated.Returns the Teredo information embedded in a Teredo address.- Parameters:
ip-Inet6Addressto be examined for embedded Teredo information- Returns:
- extracted
TeredoInfo - Throws:
IllegalArgumentException- if the argument is not a valid IPv6 Teredo address
-
isIsatapAddress
Deprecated.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 http://tools.ietf.org/html/rfc5214
- Parameters:
ip-Inet6Addressto be examined for ISATAP address format- Returns:
trueif the argument is an ISATAP address
-
getIsatapIPv4Address
Deprecated.Returns the IPv4 address embedded in an ISATAP address.- Parameters:
ip-Inet6Addressto be examined for embedded IPv4 in ISATAP address- Returns:
Inet4Addressof embedded IPv4 in an ISATAP address- Throws:
IllegalArgumentException- if the argument is not a valid IPv6 ISATAP address
-
hasEmbeddedIPv4ClientAddress
Deprecated.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.
- Parameters:
ip-Inet6Addressto be examined for embedded IPv4 client address- Returns:
trueif there is an embedded IPv4 client address- Since:
- 7.0
-
getEmbeddedIPv4ClientAddress
Deprecated.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.
- Parameters:
ip-Inet6Addressto be examined for embedded IPv4 client address- Returns:
Inet4Addressof embedded IPv4 client address- Throws:
IllegalArgumentException- if the argument does not have a valid embedded IPv4 address
-
isMappedIPv4Address
Deprecated.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
Stringargument becauseInetAddressautomatically collapses mapped addresses to IPv4. (It is actually possible to avoid this using one of the obscureInet6Addressmethods, but it would be unwise to depend on such a poorly-documented feature.)- Parameters:
ipString-Stringto be examined for embedded IPv4-mapped IPv6 address format- Returns:
trueif the argument is a valid "mapped" address- Since:
- 10.0
-
getCoercedIPv4Address
Deprecated.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 function does so by hashing the upper 64 bits into
224.0.0.0/3(64 bits into 29 bits).A "coerced" IPv4 address is equivalent to itself.
NOTE: This function 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.
- Parameters:
ip-InetAddressto "coerce"- Returns:
Inet4Addressrepresented "coerced" address- Since:
- 7.0
-
coerceToInteger
Deprecated.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
InetAddresses. They cannot be used as real addresses for the purposes of network communication.- Parameters:
ip-InetAddressto convert- Returns:
int, "coerced" if ip is not an IPv4 address- Since:
- 7.0
-
fromInteger
Deprecated.Returns an Inet4Address having the integer value specified by the argument.- Parameters:
address-int, the 32bit integer address to be converted- Returns:
Inet4Addressequivalent of the argument
-
fromLittleEndianByteArray
Deprecated.Returns an address from a little-endian ordered byte array (the opposite of whatInetAddress.getByAddress(java.lang.String, byte[])expects).IPv4 address byte array must be 4 bytes long and IPv6 byte array must be 16 bytes long.
- Parameters:
addr- the raw IP address in little-endian byte order- Returns:
- an InetAddress object created from the raw IP address
- Throws:
UnknownHostException- if IP address is of illegal length
-
increment
Deprecated.Returns a new InetAddress that is one more than the passed in address. This method works for both IPv4 and IPv6 addresses.- Parameters:
address- the InetAddress to increment- Returns:
- a new InetAddress that is one more than the passed in address
- Throws:
IllegalArgumentException- if InetAddress is at the end of its range- Since:
- 10.0
-
isMaximum
Deprecated.Returns true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6.- Returns:
- true if the InetAddress is either 255.255.255.255 for IPv4 or ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6
- Since:
- 10.0
-