org.ow2.clif.util
Class Network

java.lang.Object
  extended by org.ow2.clif.util.Network

public abstract class Network
extends java.lang.Object

This 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.

Author:
Bruno Dillenseger

Field Summary
protected static java.net.InetAddress[] inetAddresses
           
 
Constructor Summary
Network()
           
 
Method Summary
static boolean belongsToSubnet(java.net.Inet4Address address, java.lang.String subnet)
           
static java.net.InetAddress getInetAddress(java.lang.String subnet)
           
static java.net.InetAddress[] getInetAddresses(java.lang.String subnet)
           
static java.net.InetAddress getNextInetAddress(java.lang.String subnet)
           
static java.net.InetAddress getRandomInetAddress()
           
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inetAddresses

protected static java.net.InetAddress[] inetAddresses
Constructor Detail

Network

public Network()
Method Detail

getNextInetAddress

public static java.net.InetAddress getNextInetAddress(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 IP address picked among available local IPv4 addresses, in a round-robin way, belonging to the specified subnet if not null.

getRandomInetAddress

public static java.net.InetAddress getRandomInetAddress()
Returns:
an IP address randomly picked among available local IPv4 addresses

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(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:
all available local IPv4 addresses, or null if no IPv4 address could be found. If subnet parameter is not null, only addresses belonging to the specified subnet are returned.

belongsToSubnet

public static boolean belongsToSubnet(java.net.Inet4Address address,
                                      java.lang.String subnet)

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. Exists 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.

Parameters:
args - 2 arguments expected: args[0] gives the target host IP address or name, args[1] gives the timeout in s for reachability assessment of each local address.