NetworkInterface.java revision 0
0N/A/*
0N/A * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.net.SocketException;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport sun.security.action.*;
0N/Aimport java.security.AccessController;
0N/A
0N/A/**
0N/A * This class represents a Network Interface made up of a name,
0N/A * and a list of IP addresses assigned to this interface.
0N/A * It is used to identify the local interface on which a multicast group
0N/A * is joined.
0N/A *
0N/A * Interfaces are normally known by names such as "le0".
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic final class NetworkInterface {
0N/A private String name;
0N/A private String displayName;
0N/A private int index;
0N/A private InetAddress addrs[];
0N/A private InterfaceAddress bindings[];
0N/A private NetworkInterface childs[];
0N/A private NetworkInterface parent = null;
0N/A private boolean virtual = false;
0N/A
0N/A static {
0N/A AccessController.doPrivileged(new LoadLibraryAction("net"));
0N/A init();
0N/A }
0N/A
0N/A /**
0N/A * Returns an NetworkInterface object with index set to 0 and name to null.
0N/A * Setting such an interface on a MulticastSocket will cause the
0N/A * kernel to choose one interface for sending multicast packets.
0N/A *
0N/A */
0N/A NetworkInterface() {
0N/A }
0N/A
0N/A NetworkInterface(String name, int index, InetAddress[] addrs) {
0N/A this.name = name;
0N/A this.index = index;
0N/A this.addrs = addrs;
0N/A }
0N/A
0N/A /**
0N/A * Get the name of this network interface.
0N/A *
0N/A * @return the name of this network interface
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Convenience method to return an Enumeration with all or a
0N/A * subset of the InetAddresses bound to this network interface.
0N/A * <p>
0N/A * If there is a security manager, its <code>checkConnect</code>
0N/A * method is called for each InetAddress. Only InetAddresses where
0N/A * the <code>checkConnect</code> doesn't throw a SecurityException
0N/A * will be returned in the Enumeration.
0N/A * @return an Enumeration object with all or a subset of the InetAddresses
0N/A * bound to this network interface
0N/A */
0N/A public Enumeration<InetAddress> getInetAddresses() {
0N/A
0N/A class checkedAddresses implements Enumeration<InetAddress> {
0N/A
0N/A private int i=0, count=0;
0N/A private InetAddress local_addrs[];
0N/A
0N/A checkedAddresses() {
0N/A local_addrs = new InetAddress[addrs.length];
0N/A
0N/A SecurityManager sec = System.getSecurityManager();
0N/A for (int j=0; j<addrs.length; j++) {
0N/A try {
0N/A if (sec != null) {
0N/A sec.checkConnect(addrs[j].getHostAddress(), -1);
0N/A }
0N/A local_addrs[count++] = addrs[j];
0N/A } catch (SecurityException e) { }
0N/A }
0N/A
0N/A }
0N/A
0N/A public InetAddress nextElement() {
0N/A if (i < count) {
0N/A return local_addrs[i++];
0N/A } else {
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return (i < count);
0N/A }
0N/A }
0N/A return new checkedAddresses();
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Get a List of all or a subset of the <code>InterfaceAddresses</code>
0N/A * of this network interface.
0N/A * <p>
0N/A * If there is a security manager, its <code>checkConnect</code>
0N/A * method is called with the InetAddress for each InterfaceAddress.
0N/A * Only InterfaceAddresses where the <code>checkConnect</code> doesn't throw
0N/A * a SecurityException will be returned in the List.
0N/A *
0N/A * @return a <code>List</code> object with all or a subset of the
0N/A * InterfaceAddresss of this network interface
0N/A * @since 1.6
0N/A */
0N/A public java.util.List<InterfaceAddress> getInterfaceAddresses() {
0N/A java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1);
0N/A SecurityManager sec = System.getSecurityManager();
0N/A for (int j=0; j<bindings.length; j++) {
0N/A try {
0N/A if (sec != null) {
0N/A sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
0N/A }
0N/A lst.add(bindings[j]);
0N/A } catch (SecurityException e) { }
0N/A }
0N/A return lst;
0N/A }
0N/A
0N/A /**
0N/A * Get an Enumeration with all the subinterfaces (also known as virtual
0N/A * interfaces) attached to this network interface.
0N/A * <p>
0N/A * For instance eth0:1 will be a subinterface to eth0.
0N/A *
0N/A * @return an Enumeration object with all of the subinterfaces
0N/A * of this network interface
0N/A * @since 1.6
0N/A */
0N/A public Enumeration<NetworkInterface> getSubInterfaces() {
0N/A class subIFs implements Enumeration<NetworkInterface> {
0N/A
0N/A private int i=0;
0N/A
0N/A subIFs() {
0N/A }
0N/A
0N/A public NetworkInterface nextElement() {
0N/A if (i < childs.length) {
0N/A return childs[i++];
0N/A } else {
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return (i < childs.length);
0N/A }
0N/A }
0N/A return new subIFs();
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the parent NetworkInterface of this interface if this is
0N/A * a subinterface, or <code>null</code> if it is a physical
0N/A * (non virtual) interface or has no parent.
0N/A *
0N/A * @return The <code>NetworkInterface</code> this interface is attached to.
0N/A * @since 1.6
0N/A */
0N/A public NetworkInterface getParent() {
0N/A return parent;
0N/A }
0N/A
0N/A /**
0N/A * Get the index of this network interface.
0N/A *
0N/A * @return the index of this network interface
0N/A */
0N/A int getIndex() {
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Get the display name of this network interface.
0N/A * A display name is a human readable String describing the network
0N/A * device.
0N/A *
0N/A * @return the display name of this network interface,
0N/A * or null if no display name is available.
0N/A */
0N/A public String getDisplayName() {
0N/A return displayName;
0N/A }
0N/A
0N/A /**
0N/A * Searches for the network interface with the specified name.
0N/A *
0N/A * @param name
0N/A * The name of the network interface.
0N/A *
0N/A * @return A <tt>NetworkInterface</tt> with the specified name,
0N/A * or <tt>null</tt> if there is no network interface
0N/A * with the specified name.
0N/A *
0N/A * @throws SocketException
0N/A * If an I/O error occurs.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the specified name is <tt>null</tt>.
0N/A */
0N/A public static NetworkInterface getByName(String name) throws SocketException {
0N/A if (name == null)
0N/A throw new NullPointerException();
0N/A return getByName0(name);
0N/A }
0N/A
0N/A /**
0N/A * Get a network interface given its index.
0N/A *
0N/A * @param index an integer, the index of the interface
0N/A * @return the NetworkInterface obtained from its index
0N/A * @exception SocketException if an I/O error occurs.
0N/A */
0N/A native static NetworkInterface getByIndex(int index)
0N/A throws SocketException;
0N/A
0N/A /**
0N/A * Convenience method to search for a network interface that
0N/A * has the specified Internet Protocol (IP) address bound to
0N/A * it.
0N/A * <p>
0N/A * If the specified IP address is bound to multiple network
0N/A * interfaces it is not defined which network interface is
0N/A * returned.
0N/A *
0N/A * @param addr
0N/A * The <tt>InetAddress</tt> to search with.
0N/A *
0N/A * @return A <tt>NetworkInterface</tt>
0N/A * or <tt>null</tt> if there is no network interface
0N/A * with the specified IP address.
0N/A *
0N/A * @throws SocketException
0N/A * If an I/O error occurs.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the specified address is <tt>null</tt>.
0N/A */
0N/A public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
0N/A if (addr == null)
0N/A throw new NullPointerException();
0N/A return getByInetAddress0(addr);
0N/A }
0N/A
0N/A /**
0N/A * Returns all the interfaces on this machine. Returns null if no
0N/A * network interfaces could be found on this machine.
0N/A *
0N/A * NOTE: can use getNetworkInterfaces()+getInetAddresses()
0N/A * to obtain all IP addresses for this node
0N/A *
0N/A * @return an Enumeration of NetworkInterfaces found on this machine
0N/A * @exception SocketException if an I/O error occurs.
0N/A */
0N/A
0N/A public static Enumeration<NetworkInterface> getNetworkInterfaces()
0N/A throws SocketException {
0N/A final NetworkInterface[] netifs = getAll();
0N/A
0N/A // specified to return null if no network interfaces
0N/A if (netifs == null)
0N/A return null;
0N/A
0N/A return new Enumeration<NetworkInterface>() {
0N/A private int i = 0;
0N/A public NetworkInterface nextElement() {
0N/A if (netifs != null && i < netifs.length) {
0N/A NetworkInterface netif = netifs[i++];
0N/A return netif;
0N/A } else {
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return (netifs != null && i < netifs.length);
0N/A }
0N/A };
0N/A }
0N/A
0N/A private native static NetworkInterface[] getAll()
0N/A throws SocketException;
0N/A
0N/A private native static NetworkInterface getByName0(String name)
0N/A throws SocketException;
0N/A
0N/A private native static NetworkInterface getByInetAddress0(InetAddress addr)
0N/A throws SocketException;
0N/A
0N/A /**
0N/A * Returns whether a network interface is up and running.
0N/A *
0N/A * @return <code>true</code> if the interface is up and running.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A
0N/A public boolean isUp() throws SocketException {
0N/A return isUp0(name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns whether a network interface is a loopback interface.
0N/A *
0N/A * @return <code>true</code> if the interface is a loopback interface.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A
0N/A public boolean isLoopback() throws SocketException {
0N/A return isLoopback0(name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns whether a network interface is a point to point interface.
0N/A * A typical point to point interface would be a PPP connection through
0N/A * a modem.
0N/A *
0N/A * @return <code>true</code> if the interface is a point to point
0N/A * interface.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A
0N/A public boolean isPointToPoint() throws SocketException {
0N/A return isP2P0(name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns whether a network interface supports multicasting or not.
0N/A *
0N/A * @return <code>true</code> if the interface supports Multicasting.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A
0N/A public boolean supportsMulticast() throws SocketException {
0N/A return supportsMulticast0(name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hardware address (usually MAC) of the interface if it
0N/A * has one and if it can be accessed given the current privileges.
0N/A *
0N/A * @return a byte array containing the address or <code>null</code> if
0N/A * the address doesn't exist or is not accessible.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A public byte[] getHardwareAddress() throws SocketException {
0N/A for (InetAddress addr : addrs) {
0N/A if (addr instanceof Inet4Address) {
0N/A return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
0N/A }
0N/A }
0N/A return getMacAddr0(null, name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the Maximum Transmission Unit (MTU) of this interface.
0N/A *
0N/A * @return the value of the MTU for that interface.
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A public int getMTU() throws SocketException {
0N/A return getMTU0(name, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this interface is a virtual interface (also called
0N/A * subinterface).
0N/A * Virtual interfaces are, on some systems, interfaces created as a child
0N/A * of a physical interface and given different settings (like address or
0N/A * MTU). Usually the name of the interface will the name of the parent
0N/A * followed by a colon (:) and a number identifying the child since there
0N/A * can be several virtual interfaces attached to a single physical
0N/A * interface.
0N/A *
0N/A * @return <code>true</code> if this interface is a virtual interface.
0N/A * @since 1.6
0N/A */
0N/A public boolean isVirtual() {
0N/A return virtual;
0N/A }
0N/A
0N/A private native static long getSubnet0(String name, int ind) throws SocketException;
0N/A private native static Inet4Address getBroadcast0(String name, int ind) throws SocketException;
0N/A private native static boolean isUp0(String name, int ind) throws SocketException;
0N/A private native static boolean isLoopback0(String name, int ind) throws SocketException;
0N/A private native static boolean supportsMulticast0(String name, int ind) throws SocketException;
0N/A private native static boolean isP2P0(String name, int ind) throws SocketException;
0N/A private native static byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException;
0N/A private native static int getMTU0(String name, int ind) throws SocketException;
0N/A
0N/A /**
0N/A * Compares this object against the specified object.
0N/A * The result is <code>true</code> if and only if the argument is
0N/A * not <code>null</code> and it represents the same NetworkInterface
0N/A * as this object.
0N/A * <p>
0N/A * Two instances of <code>NetworkInterface</code> represent the same
0N/A * NetworkInterface if both name and addrs are the same for both.
0N/A *
0N/A * @param obj the object to compare against.
0N/A * @return <code>true</code> if the objects are the same;
0N/A * <code>false</code> otherwise.
0N/A * @see java.net.InetAddress#getAddress()
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if ((obj == null) || !(obj instanceof NetworkInterface)) {
0N/A return false;
0N/A }
0N/A NetworkInterface netIF = (NetworkInterface)obj;
0N/A if (name != null ) {
0N/A if (netIF.getName() != null) {
0N/A if (!name.equals(netIF.getName())) {
0N/A return false;
0N/A }
0N/A } else {
0N/A return false;
0N/A }
0N/A } else {
0N/A if (netIF.getName() != null) {
0N/A return false;
0N/A }
0N/A }
0N/A Enumeration newAddrs = netIF.getInetAddresses();
0N/A int i = 0;
0N/A for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++);
0N/A if (addrs == null) {
0N/A if (i != 0) {
0N/A return false;
0N/A }
0N/A } else {
0N/A /*
0N/A * Compare number of addresses (in the checked subset)
0N/A */
0N/A int count = 0;
0N/A Enumeration e = getInetAddresses();
0N/A for (; e.hasMoreElements(); count++) {
0N/A e.nextElement();
0N/A }
0N/A if (i != count) {
0N/A return false;
0N/A }
0N/A }
0N/A newAddrs = netIF.getInetAddresses();
0N/A for (; newAddrs.hasMoreElements();) {
0N/A boolean equal = false;
0N/A Enumeration thisAddrs = getInetAddresses();
0N/A InetAddress newAddr = (InetAddress)newAddrs.nextElement();
0N/A for (; thisAddrs.hasMoreElements();) {
0N/A InetAddress thisAddr = (InetAddress)thisAddrs.nextElement();
0N/A if (thisAddr.equals(newAddr)) {
0N/A equal = true;
0N/A }
0N/A }
0N/A if (!equal) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public int hashCode() {
0N/A int count = 0;
0N/A if (addrs != null) {
0N/A for (int i = 0; i < addrs.length; i++) {
0N/A count += addrs[i].hashCode();
0N/A }
0N/A }
0N/A return count;
0N/A }
0N/A
0N/A public String toString() {
0N/A String result = "name:";
0N/A result += name == null? "null": name;
0N/A if (displayName != null) {
0N/A result += " (" + displayName + ")";
0N/A }
0N/A result += " index: "+index+" addresses:\n";
0N/A for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) {
0N/A InetAddress addr = (InetAddress)e.nextElement();
0N/A result += addr+";\n";
0N/A }
0N/A return result;
0N/A }
0N/A private static native void init();
0N/A
0N/A}