FileDocCategorySizeDatePackage
NetworkInterface.javaAPI DocJava SE 6 API15384Tue Jun 10 00:25:40 BST 2008java.net

NetworkInterface

public final class NetworkInterface extends Object
This class represents a Network Interface made up of a name, and a list of IP addresses assigned to this interface. It is used to identify the local interface on which a multicast group is joined. Interfaces are normally known by names such as "le0".
since
1.4

Fields Summary
private String
name
private String
displayName
private int
index
private InetAddress[]
addrs
private InterfaceAddress[]
bindings
private NetworkInterface[]
childs
private NetworkInterface
parent
private boolean
virtual
Constructors Summary
NetworkInterface()
Returns an NetworkInterface object with index set to 0 and name to null. Setting such an interface on a MulticastSocket will cause the kernel to choose one interface for sending multicast packets.


     
	AccessController.doPrivileged(new LoadLibraryAction("net"));
	init();
    
    
NetworkInterface(String name, int index, InetAddress[] addrs)

	this.name = name;
	this.index = index;
	this.addrs = addrs;
    
Methods Summary
public booleanequals(java.lang.Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and it represents the same NetworkInterface as this object.

Two instances of NetworkInterface represent the same NetworkInterface if both name and addrs are the same for both.

param
obj the object to compare against.
return
true if the objects are the same; false otherwise.
see
java.net.InetAddress#getAddress()

	if ((obj == null) || !(obj instanceof NetworkInterface)) {
	    return false;
	}
	NetworkInterface netIF = (NetworkInterface)obj;
	if (name != null ) {
	    if (netIF.getName() != null) {
		if (!name.equals(netIF.getName())) {
		    return false;
		}
	    } else {
		return false;
	    }
	} else {
	    if (netIF.getName() != null) {
		return false;
	    }
	}
	Enumeration newAddrs = netIF.getInetAddresses();
	int i = 0;
	for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++);
	if (addrs == null) {
	    if (i != 0) {
		return false;
	    }
	} else {
	    /* 
	     * Compare number of addresses (in the checked subset)
	     */
	    int count = 0;
	    Enumeration e = getInetAddresses();
	    for (; e.hasMoreElements(); count++) {
		e.nextElement();
	    }
	    if (i != count) {
	  	return false;
	    }
    	}
	newAddrs = netIF.getInetAddresses();
	for (; newAddrs.hasMoreElements();) {
	    boolean equal = false;
	    Enumeration thisAddrs = getInetAddresses();
	    InetAddress newAddr = (InetAddress)newAddrs.nextElement();
	    for (; thisAddrs.hasMoreElements();) {
		InetAddress thisAddr = (InetAddress)thisAddrs.nextElement();
		if (thisAddr.equals(newAddr)) {
		    equal = true;
		}
	    }
	    if (!equal) {
		return false;
	    }
	}
	return true;
    
private static native java.net.NetworkInterface[]getAll()

private static native java.net.Inet4AddressgetBroadcast0(java.lang.String name, int ind)

static native java.net.NetworkInterfacegetByIndex(int index)
Get a network interface given its index.

param
index an integer, the index of the interface
return
the NetworkInterface obtained from its index
exception
SocketException if an I/O error occurs.

public static java.net.NetworkInterfacegetByInetAddress(java.net.InetAddress addr)
Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.

If the specified IP address is bound to multiple network interfaces it is not defined which network interface is returned.

param
addr The InetAddress to search with.
return
A NetworkInterface or null if there is no network interface with the specified IP address.
throws
SocketException If an I/O error occurs.
throws
NullPointerException If the specified address is null.

	if (addr == null)
	    throw new NullPointerException();
	return getByInetAddress0(addr);
    
private static native java.net.NetworkInterfacegetByInetAddress0(java.net.InetAddress addr)

public static java.net.NetworkInterfacegetByName(java.lang.String name)
Searches for the network interface with the specified name.

param
name The name of the network interface.
return
A NetworkInterface with the specified name, or null if there is no network interface with the specified name.
throws
SocketException If an I/O error occurs.
throws
NullPointerException If the specified name is null.

	if (name == null) 
	    throw new NullPointerException();
	return getByName0(name);
    
private static native java.net.NetworkInterfacegetByName0(java.lang.String name)

public java.lang.StringgetDisplayName()
Get the display name of this network interface. A display name is a human readable String describing the network device.

return
the display name of this network interface, or null if no display name is available.

	return displayName;
    
public byte[]getHardwareAddress()
Returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.

return
a byte array containing the address or null if the address doesn't exist or is not accessible.
exception
SocketException if an I/O error occurs.
since
1.6

	for (InetAddress addr : addrs) {
	    if (addr instanceof Inet4Address) {
		return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
	    }
	}
	return getMacAddr0(null, name, index);
    
intgetIndex()
Get the index of this network interface.

return
the index of this network interface

	return index;
    
public java.util.EnumerationgetInetAddresses()
Convenience method to return an Enumeration with all or a subset of the InetAddresses bound to this network interface.

If there is a security manager, its checkConnect method is called for each InetAddress. Only InetAddresses where the checkConnect doesn't throw a SecurityException will be returned in the Enumeration.

return
an Enumeration object with all or a subset of the InetAddresses bound to this network interface


	class checkedAddresses implements Enumeration<InetAddress> {
    
	    private int i=0, count=0;
	    private InetAddress local_addrs[];
    
	    checkedAddresses() {
		local_addrs = new InetAddress[addrs.length];
    	    
		SecurityManager sec = System.getSecurityManager();
		for (int j=0; j<addrs.length; j++) {
		    try {
			if (sec != null) {
			    sec.checkConnect(addrs[j].getHostAddress(), -1);
			}
			local_addrs[count++] = addrs[j];
		    } catch (SecurityException e) { }
		}
    	
	    }
    
	    public InetAddress nextElement() {
		if (i < count) {
		    return local_addrs[i++];
		} else {
		    throw new NoSuchElementException();
		}
	    }
	
	    public boolean hasMoreElements() {
		return (i < count);
	    }
	}
	return new checkedAddresses();

    
public java.util.ListgetInterfaceAddresses()
Get a List of all or a subset of the InterfaceAddresses of this network interface.

If there is a security manager, its checkConnect method is called with the InetAddress for each InterfaceAddress. Only InterfaceAddresses where the checkConnect doesn't throw a SecurityException will be returned in the List.

return
a List object with all or a subset of the InterfaceAddresss of this network interface
since
1.6

	java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1);
	SecurityManager sec = System.getSecurityManager();
	for (int j=0; j<bindings.length; j++) {
	    try {
		if (sec != null) {
		    sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
		}
		lst.add(bindings[j]);
	    } catch (SecurityException e) { }
	}
    	return lst;
    
public intgetMTU()
Returns the Maximum Transmission Unit (MTU) of this interface.

return
the value of the MTU for that interface.
exception
SocketException if an I/O error occurs.
since
1.6

	return getMTU0(name, index);
    
private static native intgetMTU0(java.lang.String name, int ind)

private static native byte[]getMacAddr0(byte[] inAddr, java.lang.String name, int ind)

public java.lang.StringgetName()
Get the name of this network interface.

return
the name of this network interface

	    return name;
    
public static java.util.EnumerationgetNetworkInterfaces()
Returns all the interfaces on this machine. Returns null if no network interfaces could be found on this machine. NOTE: can use getNetworkInterfaces()+getInetAddresses() to obtain all IP addresses for this node

return
an Enumeration of NetworkInterfaces found on this machine
exception
SocketException if an I/O error occurs.

	final NetworkInterface[] netifs = getAll();

	// specified to return null if no network interfaces
	if (netifs == null) 
	    return null;
	
	return new Enumeration<NetworkInterface>() {
	    private int i = 0;
	    public NetworkInterface nextElement() {
		if (netifs != null && i < netifs.length) {
		    NetworkInterface netif = netifs[i++];
		    return netif;
		} else {
		    throw new NoSuchElementException();
		}
	    }

	    public boolean hasMoreElements() {
		return (netifs != null && i < netifs.length);
	    }
	};
    
public java.net.NetworkInterfacegetParent()
Returns the parent NetworkInterface of this interface if this is a subinterface, or null if it is a physical (non virtual) interface or has no parent.

return
The NetworkInterface this interface is attached to.
since
1.6

	return parent;
    
public java.util.EnumerationgetSubInterfaces()
Get an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface.

For instance eth0:1 will be a subinterface to eth0.

return
an Enumeration object with all of the subinterfaces of this network interface
since
1.6

	class subIFs implements Enumeration<NetworkInterface> {
    
	    private int i=0;
    
	    subIFs() {
	    }
    	    
	    public NetworkInterface nextElement() {
		if (i < childs.length) {
		    return childs[i++];
		} else {
		    throw new NoSuchElementException();
		}
	    }
	
	    public boolean hasMoreElements() {
		return (i < childs.length);
	    }
	}
	return new subIFs();

    
private static native longgetSubnet0(java.lang.String name, int ind)

public inthashCode()

	int count = 0;
	if (addrs != null) {
	    for (int i = 0; i < addrs.length; i++) {
		count += addrs[i].hashCode();
	    }
	}
	return count;
    
private static native voidinit()

public booleanisLoopback()
Returns whether a network interface is a loopback interface.

return
true if the interface is a loopback interface.
exception
SocketException if an I/O error occurs.
since
1.6

	return isLoopback0(name, index);
    
private static native booleanisLoopback0(java.lang.String name, int ind)

private static native booleanisP2P0(java.lang.String name, int ind)

public booleanisPointToPoint()
Returns whether a network interface is a point to point interface. A typical point to point interface would be a PPP connection through a modem.

return
true if the interface is a point to point interface.
exception
SocketException if an I/O error occurs.
since
1.6

	return isP2P0(name, index);
    
public booleanisUp()
Returns whether a network interface is up and running.

return
true if the interface is up and running.
exception
SocketException if an I/O error occurs.
since
1.6

	return isUp0(name, index);
    
private static native booleanisUp0(java.lang.String name, int ind)

public booleanisVirtual()
Returns whether this interface is a virtual interface (also called subinterface). Virtual interfaces are, on some systems, interfaces created as a child of a physical interface and given different settings (like address or MTU). Usually the name of the interface will the name of the parent followed by a colon (:) and a number identifying the child since there can be several virtual interfaces attached to a single physical interface.

return
true if this interface is a virtual interface.
since
1.6

	return virtual;
    
public booleansupportsMulticast()
Returns whether a network interface supports multicasting or not.

return
true if the interface supports Multicasting.
exception
SocketException if an I/O error occurs.
since
1.6

	return supportsMulticast0(name, index);
    
private static native booleansupportsMulticast0(java.lang.String name, int ind)

public java.lang.StringtoString()

	String result = "name:";
	result += name == null? "null": name;
	if (displayName != null) {
	    result += " (" + displayName + ")";
	}
	result += " index: "+index+" addresses:\n";
	for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) {
	    InetAddress addr = (InetAddress)e.nextElement();
	    result += addr+";\n";
	}
	return result;