public final class InetAddresses extends Object
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 | Method and Description |
---|---|
static boolean |
isMappedIPv4Address(String ipString)
Evaluates whether the argument is an "IPv4 mapped" IPv6 address.
|
static boolean |
isUriInetAddress(String ipString)
Returns
true if the supplied string is a valid URI IP string
literal, false otherwise. |
public static boolean isUriInetAddress(String ipString)
true
if the supplied string is a valid URI IP string
literal, false
otherwise.ipString
- String
to evaluated as an IP URI host string literaltrue
if the argument is a valid IP URI hostpublic static boolean isMappedIPv4Address(String ipString)
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.)
ipString
- String
to be examined for embedded IPv4-mapped IPv6 address formattrue
if the argument is a valid "mapped" addressCopyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.