0N/A/*
3909N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
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;
4638N/A private static final NetworkInterface defaultInterface;
4638N/A private static final int defaultIndex; /* index of defaultInterface */
0N/A
0N/A static {
0N/A AccessController.doPrivileged(new LoadLibraryAction("net"));
0N/A init();
4638N/A defaultInterface = DefaultInterface.getDefault();
4638N/A if (defaultInterface != null) {
4638N/A defaultIndex = defaultInterface.getIndex();
4638N/A } else {
4638N/A defaultIndex = 0;
4638N/A }
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
2906N/A * will be returned in the Enumeration. However, if the caller has the
2906N/A * {@link NetPermission}("getNetworkInformation") permission, then all
2906N/A * InetAddresses are returned.
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];
2906N/A boolean trusted = true;
0N/A
0N/A SecurityManager sec = System.getSecurityManager();
2906N/A if (sec != null) {
2906N/A try {
2906N/A sec.checkPermission(new NetPermission("getNetworkInformation"));
2906N/A } catch (SecurityException e) {
2906N/A trusted = false;
2906N/A }
2906N/A }
0N/A for (int j=0; j<addrs.length; j++) {
0N/A try {
2906N/A if (sec != null && !trusted) {
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 /**
509N/A * Returns the index of this network interface. The index is an integer greater
509N/A * or equal to zero, or {@code -1} for unknown. This is a system specific value
509N/A * and interfaces with the same name can have different indexes on different
509N/A * machines.
0N/A *
509N/A * @return the index of this network interface or {@code -1} if the index is
509N/A * unknown
509N/A * @see #getByIndex(int)
509N/A * @since 1.7
0N/A */
509N/A public 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 *
2303N/A * @return a non-empty string representing the display name of this network
2303N/A * interface, or null if no display name is available.
0N/A */
0N/A public String getDisplayName() {
2303N/A /* strict TCK conformance */
2303N/A return "".equals(displayName) ? null : 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
509N/A * @return the NetworkInterface obtained from its index, or {@code null} if
509N/A * there is no interface with such an index on the system
509N/A * @throws SocketException if an I/O error occurs.
509N/A * @throws IllegalArgumentException if index has a negative value
509N/A * @see #getIndex()
509N/A * @since 1.7
0N/A */
509N/A public static NetworkInterface getByIndex(int index) throws SocketException {
509N/A if (index < 0)
509N/A throw new IllegalArgumentException("Interface index can't be negative");
509N/A return getByIndex0(index);
509N/A }
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 {
2259N/A if (addr == null) {
0N/A throw new NullPointerException();
2259N/A }
2259N/A if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
2259N/A throw new IllegalArgumentException ("invalid address type");
2259N/A }
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
509N/A private native static NetworkInterface getByIndex0(int index)
509N/A throws SocketException;
509N/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.
2906N/A * If a security manager is set, then the caller must have
2906N/A * the permission {@link NetPermission}("getNetworkInformation").
0N/A *
2906N/A * @return a byte array containing the address, or <code>null</code> if
2906N/A * the address doesn't exist, is not accessible or a security
2906N/A * manager is set and the caller does not have the permission
2906N/A * NetPermission("getNetworkInformation")
2906N/A *
0N/A * @exception SocketException if an I/O error occurs.
0N/A * @since 1.6
0N/A */
0N/A public byte[] getHardwareAddress() throws SocketException {
2906N/A SecurityManager sec = System.getSecurityManager();
2906N/A if (sec != null) {
2906N/A try {
2906N/A sec.checkPermission(new NetPermission("getNetworkInformation"));
2906N/A } catch (SecurityException e) {
2906N/A if (!getInetAddresses().hasMoreElements()) {
2906N/A // don't have connect permission to any local address
2906N/A return null;
2906N/A }
2906N/A }
2906N/A }
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 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) {
3397N/A if (!(obj instanceof NetworkInterface)) {
0N/A return false;
0N/A }
3397N/A NetworkInterface that = (NetworkInterface)obj;
3397N/A if (this.name != null ) {
3397N/A if (!this.name.equals(that.name)) {
0N/A return false;
0N/A }
0N/A } else {
3397N/A if (that.name != null) {
0N/A return false;
0N/A }
0N/A }
3397N/A
3397N/A if (this.addrs == null) {
3397N/A return that.addrs == null;
3397N/A } else if (that.addrs == null) {
3397N/A return false;
3397N/A }
3397N/A
3397N/A /* Both addrs not null. Compare number of addresses */
3397N/A
3397N/A if (this.addrs.length != that.addrs.length) {
3397N/A return false;
3397N/A }
3397N/A
3397N/A InetAddress[] thatAddrs = that.addrs;
3397N/A int count = thatAddrs.length;
3397N/A
3397N/A for (int i=0; i<count; i++) {
3397N/A boolean found = false;
3397N/A for (int j=0; j<count; j++) {
3397N/A if (addrs[i].equals(thatAddrs[j])) {
3397N/A found = true;
3397N/A break;
0N/A }
0N/A }
3397N/A if (!found) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public int hashCode() {
3397N/A return name == null? 0: name.hashCode();
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 return result;
0N/A }
4299N/A
0N/A private static native void init();
4638N/A
4638N/A /**
4638N/A * Returns the default network interface of this system
4638N/A *
4638N/A * @return the default interface
4638N/A */
4638N/A static NetworkInterface getDefault() {
4638N/A return defaultInterface;
4638N/A }
0N/A}