GNU Classpath (0.17) | ||
Frames | No Frames |
1: /* NetworkInterface.java -- 2: Copyright (C) 2002, 2003, 2004, 2005 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 java.util.Enumeration; 42: import java.util.Vector; 43: 44: /** 45: * This class models a network interface on the host computer. A network 46: * interface contains a name (typically associated with a specific 47: * hardware adapter) and a list of addresses that are bound to it. 48: * For example, an ethernet interface may be named "eth0" and have the 49: * address 192.168.1.101 assigned to it. 50: * 51: * @author Michael Koch (konqueror@gmx.de) 52: * @since 1.4 53: */ 54: public final class NetworkInterface 55: { 56: private String name; 57: private Vector inetAddresses; 58: 59: NetworkInterface(String name, InetAddress address) 60: { 61: this.name = name; 62: this.inetAddresses = new Vector(1, 1); 63: this.inetAddresses.add(address); 64: } 65: 66: NetworkInterface(String name, InetAddress[] addresses) 67: { 68: this.name = name; 69: this.inetAddresses = new Vector(addresses.length, 1); 70: 71: for (int i = 0; i < addresses.length; i++) 72: this.inetAddresses.add(addresses[i]); 73: } 74: 75: /** 76: * Returns the name of the network interface 77: * 78: * @return The name of the interface. 79: */ 80: public String getName() 81: { 82: return name; 83: } 84: 85: /** 86: * Returns all available addresses of the network interface 87: * 88: * If a @see SecurityManager is available all addresses are checked 89: * with @see SecurityManager::checkConnect() if they are available. 90: * Only <code>InetAddresses</code> are returned where the security manager 91: * doesn't throw an exception. 92: * 93: * @return An enumeration of all addresses. 94: */ 95: public Enumeration getInetAddresses() 96: { 97: SecurityManager s = System.getSecurityManager(); 98: 99: if (s == null) 100: return inetAddresses.elements(); 101: 102: Vector tmpInetAddresses = new Vector(1, 1); 103: 104: for (Enumeration addresses = inetAddresses.elements(); 105: addresses.hasMoreElements();) 106: { 107: InetAddress addr = (InetAddress) addresses.nextElement(); 108: try 109: { 110: s.checkConnect(addr.getHostAddress(), 58000); 111: tmpInetAddresses.add(addr); 112: } 113: catch (SecurityException e) 114: { 115: // Ignore. 116: } 117: } 118: 119: return tmpInetAddresses.elements(); 120: } 121: 122: /** 123: * Returns the display name of the interface 124: * 125: * @return The display name of the interface 126: */ 127: public String getDisplayName() 128: { 129: return name; 130: } 131: 132: /** 133: * Returns an network interface by name 134: * 135: * @param name The name of the interface to return 136: * 137: * @return a <code>NetworkInterface</code> object representing the interface, 138: * or null if there is no interface with that name. 139: * 140: * @exception SocketException If an error occurs 141: * @exception NullPointerException If the specified name is null 142: */ 143: public static NetworkInterface getByName(String name) 144: throws SocketException 145: { 146: Vector networkInterfaces = VMNetworkInterface.getInterfaces(); 147: 148: for (Enumeration e = networkInterfaces.elements(); e.hasMoreElements();) 149: { 150: NetworkInterface tmp = (NetworkInterface) e.nextElement(); 151: 152: if (name.equals(tmp.getName())) 153: return tmp; 154: } 155: 156: // No interface with the given name found. 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: Vector networkInterfaces = VMNetworkInterface.getInterfaces(); 174: 175: for (Enumeration interfaces = networkInterfaces.elements(); 176: interfaces.hasMoreElements();) 177: { 178: NetworkInterface tmp = (NetworkInterface) interfaces.nextElement(); 179: 180: for (Enumeration addresses = tmp.inetAddresses.elements(); 181: addresses.hasMoreElements();) 182: { 183: if (addr.equals((InetAddress) addresses.nextElement())) 184: return tmp; 185: } 186: } 187: 188: throw new SocketException("no network interface is bound to such an IP address"); 189: } 190: 191: /** 192: * Return an <code>Enumeration</code> of all available network interfaces 193: * 194: * @return all interfaces 195: * 196: * @exception SocketException If an error occurs 197: */ 198: public static Enumeration getNetworkInterfaces() throws SocketException 199: { 200: Vector networkInterfaces = VMNetworkInterface.getInterfaces(); 201: 202: if (networkInterfaces.isEmpty()) 203: return null; 204: 205: return networkInterfaces.elements(); 206: } 207: 208: /** 209: * Checks if the current instance is equal to obj 210: * 211: * @param obj The object to compare with 212: * 213: * @return <code>true</code> if equal, <code>false</code> otherwise 214: */ 215: public boolean equals(Object obj) 216: { 217: if (! (obj instanceof NetworkInterface)) 218: return false; 219: 220: NetworkInterface tmp = (NetworkInterface) obj; 221: 222: return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses)); 223: } 224: 225: /** 226: * Returns the hashcode of the current instance 227: * 228: * @return the hashcode 229: */ 230: public int hashCode() 231: { 232: // FIXME: hash correctly 233: return name.hashCode() + inetAddresses.hashCode(); 234: } 235: 236: /** 237: * Returns a string representation of the interface 238: * 239: * @return the string 240: */ 241: public String toString() 242: { 243: // FIXME: check if this is correct 244: String result; 245: String separator = System.getProperty("line.separator"); 246: 247: result = 248: "name: " + getDisplayName() + " (" + getName() + ") addresses:" 249: + separator; 250: 251: for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();) 252: { 253: InetAddress address = (InetAddress) e.nextElement(); 254: result += address.toString() + ";" + separator; 255: } 256: 257: return result; 258: } 259: }
GNU Classpath (0.17) |