An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built. The IP address architecture is defined by RFC 790: Assigned Numbers, RFC 1918: Address Allocation for Private Internets, RFC 2365: Administratively Scoped IP Multicast, and RFC 2373: IP Version 6 Addressing Architecture. An instance of an InetAddress consists of an IP address and possibly its corresponding host name (depending on whether it is constructed with a host name or whether it has already done reverse host name resolution).
Address Type | Description |
---|---|
unicast | An identifier for a single interface. A packet sent to
a unicast address is delivered to the interface identified by
that address.
The Unspecified Address -- Also called anylocal or wildcard address. It must never be assigned to any node. It indicates the absence of an address. One example of its use is as the target of bind, which allows a server to accept a client connection on any interface, in case the server host has multiple interfaces. The unspecified address must not be used as the destination address of an IP packet. The Loopback Addresses -- This is the address assigned to the loopback interface. Anything sent to this IP address loops around and becomes IP input on the local host. This address is often used when testing a client. |
multicast | An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address. |
Link-local addresses are designed to be used for addressing on a single link for purposes such as auto-address configuration, neighbor discovery, or when no routers are present.
Site-local addresses are designed to be used for addressing inside of a site without the need for a global prefix.
Global addresses are unique across the internet.
For IPv4 address format, please refer to Inet4Address#format; For IPv6 address format, please refer to Inet6Address#format.
There is a couple of System Properties affecting how IPv4 and IPv6 addresses are used.
The InetAddress class provides methods to resolve host names to their IP addresses and vice versa. The actual resolution is delegated to an InetAddress resolver.
Host name-to-IP address resolution maps a host name to an IP address. For any host name, its corresponding IP address is returned.
Reverse name resolution means that for any IP address, the host associated with the IP address is returned.
The built-in InetAddress resolver implementation does host name-to-IP address resolution and vice versa through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and the Lightweight Directory Access Protocol (LDAP). The particular naming services that the built-in resolver uses by default depends on the configuration of the local machine.
InetAddress
has a service provider mechanism for InetAddress resolvers
that allows a custom InetAddress resolver to be used instead of the built-in implementation.
InetAddressResolverProvider
is the service provider class. Its API docs provide all the
details on this mechanism.
By default, when a security manager is installed, in order to protect against DNS spoofing attacks, the result of positive host name resolutions are cached forever. When a security manager is not installed, the default behavior is to cache entries for a finite (implementation dependent) period of time. The result of unsuccessful host name resolution is cached for a very short period of time (10 seconds) to improve performance.
If the default behavior is not desired, then a Java security property can be set to a different Time-to-live (TTL) value for positive caching. Likewise, a system admin can configure a different negative caching TTL value when needed or extend the usage of the stale data.
Three Java security properties control the TTL values used for positive and negative host name resolution caching:
A value of -1 indicates "cache forever".
If the value of this property is larger than "networkaddress.cache.ttl" then "networkaddress.cache.ttl" will be used as a refresh interval of the name in the cache. For example, if this property is set to 1 day and "networkaddress.cache.ttl" is set to 30 seconds, then the positive response will be cached for 1 day but an attempt to refresh it will be done every 30 seconds.
A value of 0 (zero) or if the property is not set means do not use stale names. Negative values are ignored.
A value of 0 indicates "never cache". A value of -1 indicates "cache forever".
java.net.InetAddress#getByAddress(byte[])
, java.net.InetAddress#getByAddress(java.lang.String, byte[])
, java.net.InetAddress#getAllByName(java.lang.String)
, java.net.InetAddress#getByName(java.lang.String)
, java.net.InetAddress#getLocalHost()
Modifier and Type | Class and Description |
---|---|
private static interface | |
private static class | |
private static class | InetAddress.
A cached result of a name service lookup. |
private static class | InetAddress.
The HostsFileResolver provides host address mapping
by reading the entries in a hosts file, which is specified by
The file format is that which corresponds with the /etc/hosts file IP Address host alias list. |
pack-priv static class | |
private static class | |
private static class | InetAddress.
The default InetAddressResolver implementation, which delegates to the underlying OS network libraries to resolve host address mappings. |
private static class | InetAddress.
A cached valid lookup containing addresses whose validity may be temporarily extended by an additional stale period pending the mapping being refreshed or updated. |
Modifier and Type | Field and Description |
---|---|
private static volatile InetAddressResolver | |
private static final InetAddressResolver | |
private static final ConcurrentMap | |
private static volatile InetAddress. | |
private transient String | canonicalHostName
Used to store the best available hostname. |
private static final NavigableSet | |
private static final long | |
pack-priv final transient InetAddress. | |
private static final String | |
pack-priv static final InetAddressImpl | |
private static final RuntimePermission | INET_ADDRESS_RESOLVER_PERMISSION
The |
pack-priv static final int | IPv4
Specify the address family: Internet Protocol, Version 4 |
pack-priv static final int | IPv6
Specify the address family: Internet Protocol, Version 6 |
pack-priv static final InetAddressResolver. | PLATFORM_LOOKUP_POLICY
Platform-wide |
private static final String | |
private static final String | |
private static volatile InetAddressResolver | |
private static final ReentrantLock | |
private static final ObjectStreamField[] | |
private static final long | serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability |
private static final Unsafe |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
pack-priv static InetAddress | |
private static InetAddressResolverProvider. | |
private static InetAddressResolver | Returns: an InetAddressResolverCreate an instance of the InetAddressResolver interface based on
the setting of the |
public boolean | Returns: true if the objects are the same;
false otherwise.the object to compare against. obj)Overrides java. Compares this object against the specified object. |
public byte[] | Returns: the raw IP address of this object.Returns the raw IP address of this |
pack-priv static InetAddress[] | |
public static InetAddress[] | Returns: an array of all the IP addresses for a given host name.the name of the host, or host)null .Given the name of a host, returns an array of its IP addresses, based on the system-wide resolver. |
pack-priv static InetAddress[] | |
private static InetAddress[] | Returns: array of InetAddress(es)host name to look up host, boolean perform security check check, boolean use cached value if not expired else always
perform name service lookup (and cache the result) useCache)Designated lookup method. |
public static InetAddress | Returns: an InetAddress object created from the raw IP address.the specified host host, byte[] the raw IP address in network byte order addr)Creates an InetAddress based on the provided host name and IP address. |
public static InetAddress | Returns: an InetAddress object created from the raw IP address.the raw IP address in network byte order addr)Returns an |
public static InetAddress | |
public String | Returns: the fully qualified domain name for this IP address. If either the operation is not allowed by the security check or the system-wide resolver wasn't able to determine the fully qualified domain name for the IP address, the textual representation of the IP address is returned instead.Gets the fully qualified domain name for this IP address using the system-wide resolver. |
public String | Returns: the raw IP address in a string format.Returns the IP address string in textual presentation. |
private static String | Returns: the fully qualified domain name for the given IP address. If either the operation is not allowed by the security check or the system-wide resolver wasn't able to determine the fully qualified domain name for the IP address, the textual representation of the IP address is returned instead.make security check if true check)Returns the fully qualified domain name for the given address. |
public String | Returns: the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.Gets the host name for this IP address. |
pack-priv String | Returns: the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.make security check if true check)Returns the hostname for this address. |
public static InetAddress | |
public static InetAddress | |
public int | Returns: a hash code value for this IP address.Overrides java. Returns a hashcode for this IP address. |
pack-priv InetAddress. | |
private static native void | |
private static final InetAddressResolver. | initializePlatformLookupPolicy()
Creates an address lookup policy from |
private static UnknownHostException | |
pack-priv static boolean | |
pack-priv static boolean | |
public boolean | Returns: aboolean indicating if the InetAddress is
a wildcard address.Utility routine to check if the InetAddress is a wildcard address. |
private static native boolean | |
private static native boolean | |
public boolean | Returns: aboolean indicating if the InetAddress is
a link local address; or false if address is not a link local unicast address.Utility routine to check if the InetAddress is a link local address. |
public boolean | Returns: aboolean indicating if the InetAddress is
a loopback address; or false otherwise.Utility routine to check if the InetAddress is a loopback address. |
public boolean | Returns: aboolean indicating if the address has
is a multicast address of global scope, false if it is not
of global scope or it is not a multicast addressUtility routine to check if the multicast address has global scope. |
public boolean | Returns: aboolean indicating if the address has
is a multicast address of link-local scope, false if it is not
of link-local scope or it is not a multicast addressUtility routine to check if the multicast address has link scope. |
public boolean | Returns: aboolean indicating if the address has
is a multicast address of node-local scope, false if it is not
of node-local scope or it is not a multicast addressUtility routine to check if the multicast address has node scope. |
public boolean | Returns: aboolean indicating if the address has
is a multicast address of organization-local scope,
false if it is not of organization-local scope
or it is not a multicast addressUtility routine to check if the multicast address has organization scope. |
public boolean | Returns: aboolean indicating if the address has
is a multicast address of site-local scope, false if it is not
of site-local scope or it is not a multicast addressUtility routine to check if the multicast address has site scope. |
public boolean | Returns: aboolean indicating if the InetAddress is
an IP multicast addressUtility routine to check if the InetAddress is an IP multicast address. |
public boolean | Returns: aboolean indicating if the address is reachable.the time, in milliseconds, before the call aborts timeout)Test whether that address is reachable. |
public boolean | Returns: aboolean indicating if the address is reachable.the NetworkInterface through which the
test will be done, or null for any interface netif, int the maximum numbers of hops to try or 0 for the
default ttl, int the time, in milliseconds, before the call aborts timeout)Test whether that address is reachable. |
public boolean | Returns: aboolean indicating if the InetAddress is
a site local address; or false if address is not a site local unicast address.Utility routine to check if the InetAddress is a site local address. |
private static InetAddressResolver | |
public static InetAddress | Returns: anInetAddress object with no hostname set, and constructed
from the provided IP address literal.the textual representation of an IP address. ipAddressLiteral)Creates an |
private void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readRestores the state of this object from the stream. |
private Object | Returns: the alternate object to the de-serialized object.Replaces the de-serialized object with an Inet4Address object. |
private static InetAddressResolver | |
pack-priv static boolean | |
public String | Returns: a string representation of this IP address.Overrides java. Converts this IP address to a |
private static void | |
private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenWrites the state of this object to the stream. |