1 /* NetworkInterface.java
2 Copyright (C) 2002 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., 59 Temple Place, Suite 330, 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. */
40 import java.util.Enumeration;
41 import java.util.Vector;
44 * @author Michael Koch <konqueror@gmx.de>
47 public final class NetworkInterface
49 private static Vector networkInterfaces;
53 private Vector inetAddresses;
55 private NetworkInterface (String name, InetAddress address)
58 this.inetAddresses = new Vector (1, 1);
59 this.inetAddresses.add (address);
62 private native static Vector getRealNetworkInterfaces ()
63 throws SocketException;
66 * Returns the name of the network interface
68 public String getName ()
74 * Returns all available addresses of the network interface
76 * If a @see SecurityManager is available all addresses are checked
77 * with @see SecurityManager::checkConnect() if they are available.
78 * Only InetAddresses are returned where the security manager doesn't
79 * thrown an exception.
81 * @return An enumeration of all addresses.
83 public Enumeration getInetAddresses ()
85 SecurityManager s = System.getSecurityManager ();
88 return inetAddresses.elements ();
90 Vector tmpInetAddresses = new Vector (1, 1);
92 for (Enumeration addresses = inetAddresses.elements ();
93 addresses.hasMoreElements (); )
95 InetAddress addr = (InetAddress) addresses.nextElement ();
98 s.checkConnect (addr.getHostAddress (), 58000);
99 tmpInetAddresses.add (addr);
101 catch (SecurityException e)
106 return tmpInetAddresses.elements ();
110 * Returns the display name of the interface
112 public String getDisplayName ()
118 * Returns an network interface by name
120 * @param name The name of the interface to return
122 * @exception SocketException If an error occurs
123 * @exception NullPointerException If the specified name is null
125 public static NetworkInterface getByName (String name)
126 throws SocketException
128 if (networkInterfaces == null)
129 networkInterfaces = getRealNetworkInterfaces ();
131 for (Enumeration e = networkInterfaces.elements ();
132 e.hasMoreElements (); )
134 NetworkInterface tmp = (NetworkInterface) e.nextElement ();
136 if (name.equals (tmp.getName ()))
140 throw new SocketException ("no network interface with this name exists");
144 * Return a network interface by its address
146 * @param addr The address of the interface to return
148 * @exception SocketException If an error occurs
149 * @exception NullPointerException If the specified addess is null
151 public static NetworkInterface getByInetAddress (InetAddress addr)
152 throws SocketException
154 if (networkInterfaces == null)
155 networkInterfaces = getRealNetworkInterfaces ();
157 for (Enumeration interfaces = networkInterfaces.elements ();
158 interfaces.hasMoreElements (); )
160 NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
162 for (Enumeration addresses = tmp.inetAddresses.elements ();
163 addresses.hasMoreElements (); )
165 if (addr.equals ((InetAddress) addresses.nextElement ()))
170 throw new SocketException (
171 "no network interface is bound to such an IP address");
175 * Return an Enumeration of all available network interfaces
177 * @exception SocketException If an error occurs
179 public static Enumeration getNetworkInterfaces ()
180 throws SocketException
182 if (networkInterfaces == null)
183 networkInterfaces = getRealNetworkInterfaces ();
185 Enumeration tmp = networkInterfaces.elements ();
186 if (tmp.hasMoreElements ())
193 * Checks if the current instance is equal to obj
195 * @param obj The object to compare with
197 public boolean equals (Object obj)
199 if (!(obj instanceof NetworkInterface))
202 NetworkInterface tmp = (NetworkInterface) obj;
203 return name.equals (tmp.name) &&
204 inetAddresses.equals (tmp.inetAddresses);
208 * Returns the hashcode of the current instance
210 public int hashCode ()
212 // FIXME: hash correctly
213 return name.hashCode () + inetAddresses.hashCode ();
217 * Returns a string representation of the interface
219 public String toString ()
221 // FIXME: check if this is correct
224 result = "name: " + getDisplayName () + " (" + getName () +
227 for (Enumeration e = inetAddresses.elements ();
228 e.hasMoreElements (); )
230 InetAddress address = (InetAddress) e.nextElement ();
231 result += address.toString () + "\n";
236 } // class NetworkInterface