public class LocalAddressUtils extends Object
LocalAddressUtils.warmUp()
during the application's startup phase
so that results are readily available when needed.
The main method is
getMostLikelyLocalInetAddress()
.
The other methods essentially exist to provide input to this method but are
exposed nevertheless if anyone wants them.
These utility methods are in particular relevant in relation to
the PAC helper method myIpAddress()
. However, the class may indeed
be used for any use case.
Note, that there is no single correct answer to the question about determining the local host's IP address in an environment with multiple network interfaces or multiple addresses on each network interface.
Modifier and Type | Method and Description |
---|---|
static List<InetAddress> |
getDatagramLocalInetAddress(IpAddressUtils.IpTypePreference ipTypePref)
Returns the address of the local host, found by using UDP method.
|
static InetAddress |
getLocalHost()
Returns the address of the local host.
|
static InetAddress[] |
getLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref)
Returns the addresses of the local host.
|
static InetAddress |
getLoopbackAddress(IpAddressUtils.IpTypePreference ipTypePref)
Returns the loopback address.
|
static InetAddress |
getMostLikelyLocalInetAddress(IpAddressUtils.IpTypePreference ipTypePref)
Returns the host's IP address.
|
static InetAddress[] |
getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference ipTypePref)
Returns the host's IP addresses.
|
static List<InetAddress> |
getPrioritizedLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref)
Returns a prioritized list of local host addresses.
|
static boolean |
isSoftwareVirtualAdapter(NetworkInterface nif)
Tries to guess if the network interface is a virtual adapter
by one of the makers of virtualization solutions (e.g.
|
static void |
refreshNetworkInfo(boolean await)
Refreshes the cached network information.
|
static void |
warmUp()
Starts collecting network information in a background task.
|
public static void warmUp()
public static void refreshNetworkInfo(boolean await)
await
- if true, waits for the refresh to complete@NonNull public static InetAddress getLocalHost() throws UnknownHostException
This method returns a cached result of calling
InetAddress.getLocalHost()
and is therefore likely not to
block (unlike the underlying method) unless this is the first time
this class is being referenced.
Note that InetAddress#getLocalHost()
is known to return
unpredictable results for hosts with multiple network adapters. The
getMostLikelyLocalInetAddresses()
method is much more likely to return an acceptable result.
UnknownHostException
LocalAddressUtils.getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference)
@NonNull public static InetAddress[] getLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref) throws UnknownHostException
This is achieved by retrieving the
name-of-the-host
from the system, then resolving that name into a list of InetAddress
es.
This method returns a cached result and is therefore likely not to block unless this is the first time this class is being referenced.
ipTypePref
- filterUnknownHostException
- if no IP address for the host name could be foundnetwork.hname.HostnameUtils
,
InetAddress.getAllByName(java.lang.String)
@NonNull public static List<InetAddress> getPrioritizedLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref)
Prioritization is done on the following basis:
look
like a software virtual adapters
are prioritized lower than addresses
belonging to interfaces which don't look software virtual adapters.The method returns a cached result and is therefore likely not to block, unless this is the first time this class is being referenced.
ipTypePref
- filter@NonNull public static List<InetAddress> getDatagramLocalInetAddress(IpAddressUtils.IpTypePreference ipTypePref)
This method works by creating a 'connected' UDP socket. Upon the connect operation, the Berkeley sockets API will populate the local endpoint (own host) according to the host's routing information. Hence we can use this for finding the IP of the local host. Note, that there is never any actual UDP connection created. We only use the socket to see how its metadata has been populated.
This method is known not to work on Mac OSX. It will most likely return an empty list if on Mac OSX.
This method returns a cached result and is therefore likely not to block unless this is the first time this class is being referenced.
ipTypePref
- IP protocol filterLocalAddressUtils.getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference)
@NonNull public static InetAddress[] getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference ipTypePref)
InetAddress.getLocalHost()
,
in particular on hosts with multiple network interfaces or hosts
that are virtualized or operating in a PaaS environment.
The method uses the following prioritization for determining what to return, by continously moving to the next step if the previous step yielded an empty result:
getLocalHostAddresses()
(List A) is compared to
getPrioritizedLocalHostAddresses()
(List B),
picking the ones from List B list which is also on List A.
LocalAddressUtils.getLocalHost()
if it matches the
ipTypePref
filter.
getDatagramLocalInetAddress()
if it matches the
ipTypePref
filter.
getLoopbackAddress()
.
The method uses the other methods in the class and is therefore likely not to block, unless this is the first time this class is being referenced.
ipTypePref
- IP protocol filterLocalAddressUtils.getMostLikelyLocalInetAddress(IpAddressUtils.IpTypePreference)
@NonNull public static InetAddress getMostLikelyLocalInetAddress(IpAddressUtils.IpTypePreference ipTypePref)
getMostLikelyLocalInetAddresses()
but only returns a single IP address.ipTypePref
- IP protocol filterLocalAddressUtils.getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference)
@NonNull public static InetAddress getLoopbackAddress(IpAddressUtils.IpTypePreference ipTypePref)
This method is similar to InetAddress.getLoopbackAddress()
except that the preference for IPv4 vs IPv6 can be explicitly
stated.
For IPv4 the returned address is always 127.0.0.1
and for
IPv6 it is ::1
.
ipTypePref
- IPv4 vs IP4v6 preferencepublic static boolean isSoftwareVirtualAdapter(NetworkInterface nif)
nif
- network interface