1 /* NetworkInterface.java --
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 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)
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
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
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. */
41 import gnu.classpath.SystemProperties;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Enumeration;
46 import java.util.HashMap;
47 import java.util.Iterator;
49 import java.util.Vector;
52 * This class models a network interface on the host computer. A network
53 * interface contains a name (typically associated with a specific
54 * hardware adapter) and a list of addresses that are bound to it.
55 * For example, an ethernet interface may be named "eth0" and have the
56 * address 192.168.1.101 assigned to it.
58 * @author Michael Koch (konqueror@gmx.de)
61 public final class NetworkInterface
63 private final VMNetworkInterface netif;
65 private NetworkInterface(VMNetworkInterface netif)
70 /** Creates an NetworkInterface instance which
71 * represents any interface in the system. Its only
72 * address is <code>0.0.0.0/0.0.0.0</code>. This
73 * method is needed by {@link MulticastSocket#getNetworkInterface}
75 static NetworkInterface createAnyInterface()
77 return new NetworkInterface(new VMNetworkInterface());
81 * Returns the name of the network interface
83 * @return The name of the interface.
85 public String getName()
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<InetAddress> getInetAddresses()
102 SecurityManager s = System.getSecurityManager();
103 Vector inetAddresses = new Vector(netif.addresses);
106 return inetAddresses.elements();
108 Vector<InetAddress> tmpInetAddresses = new Vector<InetAddress>(1, 1);
110 for (Enumeration<InetAddress> addresses = inetAddresses.elements();
111 addresses.hasMoreElements();)
113 InetAddress addr = addresses.nextElement();
116 s.checkConnect(addr.getHostAddress(), -1);
117 tmpInetAddresses.add(addr);
119 catch (SecurityException e)
125 return tmpInetAddresses.elements();
129 * Returns the display name of the interface
131 * @return The display name of the interface
133 public String getDisplayName()
139 * Returns an network interface by name
141 * @param name The name of the interface to return
143 * @return a <code>NetworkInterface</code> object representing the interface,
144 * or null if there is no interface with that name.
146 * @exception SocketException If an error occurs
147 * @exception NullPointerException If the specified name is null
149 public static NetworkInterface getByName(String name)
150 throws SocketException
153 throw new NullPointerException();
154 VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
155 for (int i = 0; i < netifs.length; i++)
157 if (netifs[i].name.equals(name))
158 return new NetworkInterface(netifs[i]);
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
177 throw new NullPointerException();
178 VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
179 for (int i = 0; i < netifs.length; i++)
181 if (netifs[i].addresses.contains(addr))
182 return new NetworkInterface(netifs[i]);
188 * Return an <code>Enumeration</code> of all available network interfaces
190 * @return all interfaces
192 * @exception SocketException If an error occurs
194 public static Enumeration<NetworkInterface> getNetworkInterfaces()
195 throws SocketException
197 VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
198 Vector<NetworkInterface> networkInterfaces =
199 new Vector<NetworkInterface>(netifs.length);
200 for (int i = 0; i < netifs.length; i++)
202 if (!netifs[i].addresses.isEmpty())
203 networkInterfaces.add(new NetworkInterface(netifs[i]));
205 return networkInterfaces.elements();
209 * Checks if the current instance is equal to obj
211 * @param obj The object to compare with
213 * @return <code>true</code> if equal, <code>false</code> otherwise
215 public boolean equals(Object obj)
217 if (! (obj instanceof NetworkInterface))
220 NetworkInterface tmp = (NetworkInterface) obj;
222 if (netif.name == null)
223 return tmp.netif.name == null;
225 return (netif.name.equals(tmp.netif.name)
226 && (netif.addresses.equals(tmp.netif.addresses)));
230 * Returns the hashcode of the current instance
232 * @return the hashcode
234 public int hashCode()
236 // FIXME: hash correctly
237 int hc = netif.addresses.hashCode();
239 if (netif.name != null)
240 hc += netif.name.hashCode();
246 * Returns a string representation of the interface
250 public String toString()
252 // FIXME: check if this is correct
254 String separator = SystemProperties.getProperty("line.separator");
256 result = new StringBuffer();
258 result.append("name: ");
259 result.append(getDisplayName());
260 result.append(" (").append(getName()).append(") addresses:");
261 result.append(separator);
263 for (Iterator it = netif.addresses.iterator(); it.hasNext(); )
265 InetAddress address = (InetAddress) it.next();
266 result.append(address.toString()).append(";").append(separator);
269 return result.toString();