Package org.ow2.clif.util
Class Network
- java.lang.Object
-
- org.ow2.clif.util.Network
-
public abstract class Network extends java.lang.ObjectThis utility class provides a method to get all IPv4 addresses of local network interfaces, either randomly or in a round-robin way. If at least an IPv4 address is configured, then loopback/localhost addresses are ignored. Otherwise, if no network interface holds any configured IPv4 address, a default localhost address will be selected. IPv6 addresses are ignored anyway. The main() method provides a helper that smartly chooses and prints a local address from which a given remote address can be reached.- Author:
- Bruno Dillenseger
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.util.List<java.net.InterfaceAddress>allAddresses
-
Constructor Summary
Constructors Constructor Description Network()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanbelongsToSubnet(java.net.Inet4Address address, java.lang.String subnet)Checks if a given IPv4 address belongs to a given subnetstatic java.net.InetAddressgetInetAddress(java.lang.String subnet)static java.net.InetAddress[]getInetAddresses()Get all locally available IPv4 addresses but the loopback address.static booleanisLocalAddress(java.lang.String address)Checks that the given IP address is bound to a local network interfacestatic booleanisLocalAddress(java.net.InetAddress address)Checks that the given IP address is bound to a local network interfacestatic voidmain(java.lang.String[] args)Prints to standard output the local IP address from which the provided host IP address or name is reachable.
-
-
-
Method Detail
-
getInetAddress
public static java.net.InetAddress getInetAddress(java.lang.String subnet)
- Parameters:
subnet- if not null, this string must specify an IPv4 subnetwork address in the form b1.b2.b3.b4/maskbits (e.g. 192.168.1.0/24)- Returns:
- an arbitrary local IPv4 address belonging to the specified subnet if not null. Successive calls always return the same address.
-
getInetAddresses
public static java.net.InetAddress[] getInetAddresses()
Get all locally available IPv4 addresses but the loopback address. Nevertheless, when the loopback address is the only one configured, it is returned.- Returns:
- either all locally available IPv4 addresses different from the loopback address, or just the loopback address if it is the only one configured.
-
belongsToSubnet
public static boolean belongsToSubnet(java.net.Inet4Address address, java.lang.String subnet)Checks if a given IPv4 address belongs to a given subnet- Parameters:
address- the IPv4 addresssubnet- the subnet address, specified as b1.b2.b3.b4/maskbits (e.g. 192.168.1.0/24)- Returns:
- true if the address belongs to the subnet, false otherwise
-
isLocalAddress
public static boolean isLocalAddress(java.lang.String address)
Checks that the given IP address is bound to a local network interface- Parameters:
address- the classical String representation (xxx.xxx.xxx.xxx) of the IP address to check- Returns:
- true if the address is bound to a local network interface, false otherwise
-
isLocalAddress
public static boolean isLocalAddress(java.net.InetAddress address)
Checks that the given IP address is bound to a local network interface- Parameters:
address- the IP address to check- Returns:
- true if the address is bound to a local network interface, false otherwise
-
main
public static void main(java.lang.String[] args)
Prints to standard output the local IP address from which the provided host IP address or name is reachable. Keeps mute if no local address routes to the target host. Reports possible error messages to the standard error output. Exits with status code 0 if a correct local address was found, 1 if the target host is unreachable from any local address, or a greater value if an error occurred. The output is printed as: "network.address=ddd.ddd.ddd.ddd/nn"- Parameters:
args- 2 arguments expected: args[0] the target host IP address or name, args[1] the timeout in seconds for reachability assessment of each local address.
-
-