Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / java / net / NetworkInterface.java
blob47b1c67cae7812234099902229f201c502c23ce7
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 java.util.Collection;
42 import java.util.Collections;
43 import java.util.Enumeration;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Vector;
49 /**
50 * This class models a network interface on the host computer. A network
51 * interface contains a name (typically associated with a specific
52 * hardware adapter) and a list of addresses that are bound to it.
53 * For example, an ethernet interface may be named "eth0" and have the
54 * address 192.168.1.101 assigned to it.
56 * @author Michael Koch (konqueror@gmx.de)
57 * @since 1.4
59 public final class NetworkInterface
61 private String name;
62 private Vector inetAddresses;
64 NetworkInterface(String name, InetAddress address)
66 this.name = name;
67 this.inetAddresses = new Vector(1, 1);
68 this.inetAddresses.add(address);
71 NetworkInterface(String name, InetAddress[] addresses)
73 this.name = name;
74 this.inetAddresses = new Vector(addresses.length, 1);
76 for (int i = 0; i < addresses.length; i++)
77 this.inetAddresses.add(addresses[i]);
80 /**
81 * Returns the name of the network interface
83 * @return The name of the interface.
85 public String getName()
87 return name;
90 /**
91 * Returns all available addresses of the network interface
93 * If a @see SecurityManager is available all addresses are checked
94 * with @see SecurityManager::checkConnect() if they are available.
95 * Only <code>InetAddresses</code> are returned where the security manager
96 * doesn't throw an exception.
98 * @return An enumeration of all addresses.
100 public Enumeration getInetAddresses()
102 SecurityManager s = System.getSecurityManager();
104 if (s == null)
105 return inetAddresses.elements();
107 Vector tmpInetAddresses = new Vector(1, 1);
109 for (Enumeration addresses = inetAddresses.elements();
110 addresses.hasMoreElements();)
112 InetAddress addr = (InetAddress) addresses.nextElement();
115 s.checkConnect(addr.getHostAddress(), 58000);
116 tmpInetAddresses.add(addr);
118 catch (SecurityException e)
120 // Ignore.
124 return tmpInetAddresses.elements();
128 * Returns the display name of the interface
130 * @return The display name of the interface
132 public String getDisplayName()
134 return name;
138 * Returns an network interface by name
140 * @param name The name of the interface to return
142 * @return a <code>NetworkInterface</code> object representing the interface,
143 * or null if there is no interface with that name.
145 * @exception SocketException If an error occurs
146 * @exception NullPointerException If the specified name is null
148 public static NetworkInterface getByName(String name)
149 throws SocketException
151 for (Enumeration e = getNetworkInterfaces(); e.hasMoreElements();)
153 NetworkInterface tmp = (NetworkInterface) e.nextElement();
155 if (name.equals(tmp.getName()))
156 return tmp;
159 // No interface with the given name found.
160 return null;
164 * Return a network interface by its address
166 * @param addr The address of the interface to return
168 * @return the interface, or <code>null</code> if none found
170 * @exception SocketException If an error occurs
171 * @exception NullPointerException If the specified addess is null
173 public static NetworkInterface getByInetAddress(InetAddress addr)
174 throws SocketException
176 for (Enumeration interfaces = getNetworkInterfaces();
177 interfaces.hasMoreElements();)
179 NetworkInterface tmp = (NetworkInterface) interfaces.nextElement();
181 for (Enumeration addresses = tmp.inetAddresses.elements();
182 addresses.hasMoreElements();)
184 if (addr.equals((InetAddress) addresses.nextElement()))
185 return tmp;
189 throw new SocketException("no network interface is bound to such an IP address");
192 static private Collection condense(Collection interfaces)
194 final Map condensed = new HashMap();
196 final Iterator interfs = interfaces.iterator();
197 while (interfs.hasNext()) {
199 final NetworkInterface face = (NetworkInterface) interfs.next();
200 final String name = face.getName();
202 if (condensed.containsKey(name))
204 final NetworkInterface conface = (NetworkInterface) condensed.get(name);
205 if (!conface.inetAddresses.containsAll(face.inetAddresses))
207 final Iterator faceAddresses = face.inetAddresses.iterator();
208 while (faceAddresses.hasNext())
210 final InetAddress faceAddress = (InetAddress) faceAddresses.next();
211 if (!conface.inetAddresses.contains(faceAddress))
213 conface.inetAddresses.add(faceAddress);
218 else
220 condensed.put(name, face);
224 return condensed.values();
228 * Return an <code>Enumeration</code> of all available network interfaces
230 * @return all interfaces
232 * @exception SocketException If an error occurs
234 public static Enumeration getNetworkInterfaces() throws SocketException
236 Vector networkInterfaces = VMNetworkInterface.getInterfaces();
238 if (networkInterfaces.isEmpty())
239 return null;
241 Collection condensed = condense(networkInterfaces);
243 return Collections.enumeration(condensed);
247 * Checks if the current instance is equal to obj
249 * @param obj The object to compare with
251 * @return <code>true</code> if equal, <code>false</code> otherwise
253 public boolean equals(Object obj)
255 if (! (obj instanceof NetworkInterface))
256 return false;
258 NetworkInterface tmp = (NetworkInterface) obj;
260 return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses));
264 * Returns the hashcode of the current instance
266 * @return the hashcode
268 public int hashCode()
270 // FIXME: hash correctly
271 return name.hashCode() + inetAddresses.hashCode();
275 * Returns a string representation of the interface
277 * @return the string
279 public String toString()
281 // FIXME: check if this is correct
282 String result;
283 String separator = System.getProperty("line.separator");
285 result =
286 "name: " + getDisplayName() + " (" + getName() + ") addresses:"
287 + separator;
289 for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();)
291 InetAddress address = (InetAddress) e.nextElement();
292 result += address.toString() + ";" + separator;
295 return result;