Merge from the pain train
[official-gcc.git] / libjava / java / net / NetworkInterface.java
blob2b4da7392e69400973b44da3083478bdc509bf9e
1 /* NetworkInterface.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package java.net;
41 import gnu.classpath.Configuration;
43 import java.util.Enumeration;
44 import java.util.Vector;
46 /**
47 * This class models a network interface on the host computer. A network
48 * interface contains a name (typically associated with a specific
49 * hardware adapter) and a list of addresses that are bound to it.
50 * For example, an ethernet interface may be named "eth0" and have the
51 * address 192.168.1.101 assigned to it.
53 * @author Michael Koch (konqueror@gmx.de)
54 * @since 1.4
56 public final class NetworkInterface
58 static
60 if (Configuration.INIT_LOAD_LIBRARY)
61 System.loadLibrary("javanet");
64 private String name;
65 private Vector inetAddresses;
67 private NetworkInterface(String name, InetAddress address)
69 this.name = name;
70 this.inetAddresses = new Vector(1, 1);
71 this.inetAddresses.add(address);
74 private static native Vector getRealNetworkInterfaces()
75 throws SocketException;
77 /**
78 * Returns the name of the network interface
80 * @return The name of the interface.
82 public String getName()
84 return name;
87 /**
88 * Returns all available addresses of the network interface
90 * If a @see SecurityManager is available all addresses are checked
91 * with @see SecurityManager::checkConnect() if they are available.
92 * Only <code>InetAddresses</code> are returned where the security manager
93 * doesn't throw an exception.
95 * @return An enumeration of all addresses.
97 public Enumeration getInetAddresses()
99 SecurityManager s = System.getSecurityManager();
101 if (s == null)
102 return inetAddresses.elements();
104 Vector tmpInetAddresses = new Vector(1, 1);
106 for (Enumeration addresses = inetAddresses.elements();
107 addresses.hasMoreElements();)
109 InetAddress addr = (InetAddress) addresses.nextElement();
112 s.checkConnect(addr.getHostAddress(), 58000);
113 tmpInetAddresses.add(addr);
115 catch (SecurityException e)
117 // Ignore.
121 return tmpInetAddresses.elements();
125 * Returns the display name of the interface
127 * @return The display name of the interface
129 public String getDisplayName()
131 return name;
135 * Returns an network interface by name
137 * @param name The name of the interface to return
139 * @return a <code>NetworkInterface</code> object representing the interface,
140 * or null if there is no interface with that name.
142 * @exception SocketException If an error occurs
143 * @exception NullPointerException If the specified name is null
145 public static NetworkInterface getByName(String name)
146 throws SocketException
148 Vector networkInterfaces = getRealNetworkInterfaces();
150 for (Enumeration e = networkInterfaces.elements(); e.hasMoreElements();)
152 NetworkInterface tmp = (NetworkInterface) e.nextElement();
154 if (name.equals(tmp.getName()))
155 return tmp;
158 // No interface with the given name found.
159 return null;
163 * Return a network interface by its address
165 * @param addr The address of the interface to return
167 * @return the interface, or <code>null</code> if none found
169 * @exception SocketException If an error occurs
170 * @exception NullPointerException If the specified addess is null
172 public static NetworkInterface getByInetAddress(InetAddress addr)
173 throws SocketException
175 Vector networkInterfaces = getRealNetworkInterfaces();
177 for (Enumeration interfaces = networkInterfaces.elements();
178 interfaces.hasMoreElements();)
180 NetworkInterface tmp = (NetworkInterface) interfaces.nextElement();
182 for (Enumeration addresses = tmp.inetAddresses.elements();
183 addresses.hasMoreElements();)
185 if (addr.equals((InetAddress) addresses.nextElement()))
186 return tmp;
190 throw new SocketException("no network interface is bound to such an IP address");
194 * Return an <code>Enumeration</code> of all available network interfaces
196 * @return all interfaces
198 * @exception SocketException If an error occurs
200 public static Enumeration getNetworkInterfaces() throws SocketException
202 Vector networkInterfaces = getRealNetworkInterfaces();
204 if (networkInterfaces.isEmpty())
205 return null;
207 return networkInterfaces.elements();
211 * Checks if the current instance is equal to obj
213 * @param obj The object to compare with
215 * @return <code>true</code> if equal, <code>false</code> otherwise
217 public boolean equals(Object obj)
219 if (! (obj instanceof NetworkInterface))
220 return false;
222 NetworkInterface tmp = (NetworkInterface) obj;
224 return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses));
228 * Returns the hashcode of the current instance
230 * @return the hashcode
232 public int hashCode()
234 // FIXME: hash correctly
235 return name.hashCode() + inetAddresses.hashCode();
239 * Returns a string representation of the interface
241 * @return the string
243 public String toString()
245 // FIXME: check if this is correct
246 String result;
247 String separator = System.getProperty("line.separator");
249 result =
250 "name: " + getDisplayName() + " (" + getName() + ") addresses:"
251 + separator;
253 for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();)
255 InetAddress address = (InetAddress) e.nextElement();
256 result += address.toString() + ";" + separator;
259 return result;