GNU Classpath (0.95) | |
Frames | No Frames |
1: /* NetworkInterface.java -- 2: Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.net; 40: 41: import gnu.classpath.SystemProperties; 42: 43: import java.util.Enumeration; 44: import java.util.Iterator; 45: import java.util.Vector; 46: 47: /** 48: * This class models a network interface on the host computer. A network 49: * interface contains a name (typically associated with a specific 50: * hardware adapter) and a list of addresses that are bound to it. 51: * For example, an ethernet interface may be named "eth0" and have the 52: * address 192.168.1.101 assigned to it. 53: * 54: * @author Michael Koch (konqueror@gmx.de) 55: * @since 1.4 56: */ 57: public final class NetworkInterface 58: { 59: private final VMNetworkInterface netif; 60: 61: private NetworkInterface(VMNetworkInterface netif) 62: { 63: this.netif = netif; 64: } 65: 66: /** Creates an NetworkInterface instance which 67: * represents any interface in the system. Its only 68: * address is <code>0.0.0.0/0.0.0.0</code>. This 69: * method is needed by {@link MulticastSocket#getNetworkInterface} 70: */ 71: static NetworkInterface createAnyInterface() 72: { 73: return new NetworkInterface(new VMNetworkInterface()); 74: } 75: 76: /** 77: * Returns the name of the network interface 78: * 79: * @return The name of the interface. 80: */ 81: public String getName() 82: { 83: return netif.name; 84: } 85: 86: /** 87: * Returns all available addresses of the network interface 88: * 89: * If a @see SecurityManager is available all addresses are checked 90: * with @see SecurityManager::checkConnect() if they are available. 91: * Only <code>InetAddresses</code> are returned where the security manager 92: * doesn't throw an exception. 93: * 94: * @return An enumeration of all addresses. 95: */ 96: public Enumeration<InetAddress> getInetAddresses() 97: { 98: SecurityManager s = System.getSecurityManager(); 99: Vector<InetAddress> inetAddresses 100: = new Vector<InetAddress>(netif.addresses); 101: 102: if (s == null) 103: return inetAddresses.elements(); 104: 105: Vector<InetAddress> tmpInetAddresses = new Vector<InetAddress>(1, 1); 106: 107: for (Enumeration<InetAddress> addresses = inetAddresses.elements(); 108: addresses.hasMoreElements();) 109: { 110: InetAddress addr = addresses.nextElement(); 111: try 112: { 113: s.checkConnect(addr.getHostAddress(), -1); 114: tmpInetAddresses.add(addr); 115: } 116: catch (SecurityException e) 117: { 118: // Ignore. 119: } 120: } 121: 122: return tmpInetAddresses.elements(); 123: } 124: 125: /** 126: * Returns the display name of the interface 127: * 128: * @return The display name of the interface 129: */ 130: public String getDisplayName() 131: { 132: return netif.name; 133: } 134: 135: /** 136: * Returns an network interface by name 137: * 138: * @param name The name of the interface to return 139: * 140: * @return a <code>NetworkInterface</code> object representing the interface, 141: * or null if there is no interface with that name. 142: * 143: * @exception SocketException If an error occurs 144: * @exception NullPointerException If the specified name is null 145: */ 146: public static NetworkInterface getByName(String name) 147: throws SocketException 148: { 149: if (name == null) 150: throw new NullPointerException(); 151: VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces(); 152: for (int i = 0; i < netifs.length; i++) 153: { 154: if (netifs[i].name.equals(name)) 155: return new NetworkInterface(netifs[i]); 156: } 157: return null; 158: } 159: 160: /** 161: * Return a network interface by its address 162: * 163: * @param addr The address of the interface to return 164: * 165: * @return the interface, or <code>null</code> if none found 166: * 167: * @exception SocketException If an error occurs 168: * @exception NullPointerException If the specified addess is null 169: */ 170: public static NetworkInterface getByInetAddress(InetAddress addr) 171: throws SocketException 172: { 173: if (addr == null) 174: throw new NullPointerException(); 175: VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces(); 176: for (int i = 0; i < netifs.length; i++) 177: { 178: if (netifs[i].addresses.contains(addr)) 179: return new NetworkInterface(netifs[i]); 180: } 181: return null; 182: } 183: 184: /** 185: * Return an <code>Enumeration</code> of all available network interfaces 186: * 187: * @return all interfaces 188: * 189: * @exception SocketException If an error occurs 190: */ 191: public static Enumeration<NetworkInterface> getNetworkInterfaces() 192: throws SocketException 193: { 194: VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces(); 195: Vector<NetworkInterface> networkInterfaces = 196: new Vector<NetworkInterface>(netifs.length); 197: for (int i = 0; i < netifs.length; i++) 198: { 199: if (!netifs[i].addresses.isEmpty()) 200: networkInterfaces.add(new NetworkInterface(netifs[i])); 201: } 202: return networkInterfaces.elements(); 203: } 204: 205: /** 206: * Checks if the current instance is equal to obj 207: * 208: * @param obj The object to compare with 209: * 210: * @return <code>true</code> if equal, <code>false</code> otherwise 211: */ 212: public boolean equals(Object obj) 213: { 214: if (! (obj instanceof NetworkInterface)) 215: return false; 216: 217: NetworkInterface tmp = (NetworkInterface) obj; 218: 219: if (netif.name == null) 220: return tmp.netif.name == null; 221: 222: return (netif.name.equals(tmp.netif.name) 223: && (netif.addresses.equals(tmp.netif.addresses))); 224: } 225: 226: /** 227: * Returns the hashcode of the current instance 228: * 229: * @return the hashcode 230: */ 231: public int hashCode() 232: { 233: // FIXME: hash correctly 234: int hc = netif.addresses.hashCode(); 235: 236: if (netif.name != null) 237: hc += netif.name.hashCode(); 238: 239: return hc; 240: } 241: 242: /** 243: * Returns a string representation of the interface 244: * 245: * @return the string 246: */ 247: public String toString() 248: { 249: // FIXME: check if this is correct 250: StringBuffer result; 251: String separator = SystemProperties.getProperty("line.separator"); 252: 253: result = new StringBuffer(); 254: 255: result.append("name: "); 256: result.append(getDisplayName()); 257: result.append(" (").append(getName()).append(") addresses:"); 258: result.append(separator); 259: 260: for (Iterator it = netif.addresses.iterator(); it.hasNext(); ) 261: { 262: InetAddress address = (InetAddress) it.next(); 263: result.append(address.toString()).append(";").append(separator); 264: } 265: 266: return result.toString(); 267: } 268: }
GNU Classpath (0.95) |