1 /* NetworkInterface.java
2 Copyright (C) 2002, 2003 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 gnu
.classpath
.Configuration
;
41 import java
.util
.Enumeration
;
42 import java
.util
.Vector
;
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.
51 * @author Michael Koch <konqueror@gmx.de>
54 public final class NetworkInterface
58 if (Configuration
.INIT_LOAD_LIBRARY
)
60 System
.loadLibrary ("javanet");
66 private Vector inetAddresses
;
68 private NetworkInterface (String name
, InetAddress address
)
71 this.inetAddresses
= new Vector (1, 1);
72 this.inetAddresses
.add (address
);
75 private native static Vector
getRealNetworkInterfaces ()
76 throws SocketException
;
79 * Returns the name of the network interface
81 * @return The name of the interface.
83 public String
getName ()
89 * Returns all available addresses of the network interface
91 * If a @see SecurityManager is available all addresses are checked
92 * with @see SecurityManager::checkConnect() if they are available.
93 * Only <code>InetAddresses</code> are returned where the security manager
94 * doesn't throw an exception.
96 * @return An enumeration of all addresses.
98 public Enumeration
getInetAddresses ()
100 SecurityManager s
= System
.getSecurityManager ();
103 return inetAddresses
.elements ();
105 Vector tmpInetAddresses
= new Vector (1, 1);
107 for (Enumeration addresses
= inetAddresses
.elements ();
108 addresses
.hasMoreElements (); )
110 InetAddress addr
= (InetAddress
) addresses
.nextElement ();
113 s
.checkConnect (addr
.getHostAddress (), 58000);
114 tmpInetAddresses
.add (addr
);
116 catch (SecurityException e
)
121 return tmpInetAddresses
.elements ();
125 * Returns the display name of the interface
127 * @return The display name of the interface
129 public String
getDisplayName ()
135 * Returns an network interface by name
137 * @param name The name of the interface to return
139 * @exception SocketException If an error occurs
140 * @exception NullPointerException If the specified name is null
142 public static NetworkInterface
getByName (String name
)
143 throws SocketException
145 Vector networkInterfaces
= getRealNetworkInterfaces ();
147 for (Enumeration e
= networkInterfaces
.elements ();
148 e
.hasMoreElements (); )
150 NetworkInterface tmp
= (NetworkInterface
) e
.nextElement ();
152 if (name
.equals (tmp
.getName ()))
156 throw new SocketException ("no network interface with this name exists");
160 * Return a network interface by its address
162 * @param addr The address of the interface to return
164 * @exception SocketException If an error occurs
165 * @exception NullPointerException If the specified addess is null
167 public static NetworkInterface
getByInetAddress (InetAddress addr
)
168 throws SocketException
170 Vector networkInterfaces
= getRealNetworkInterfaces ();
172 for (Enumeration interfaces
= networkInterfaces
.elements ();
173 interfaces
.hasMoreElements (); )
175 NetworkInterface tmp
= (NetworkInterface
) interfaces
.nextElement ();
177 for (Enumeration addresses
= tmp
.inetAddresses
.elements ();
178 addresses
.hasMoreElements (); )
180 if (addr
.equals ((InetAddress
) addresses
.nextElement ()))
185 throw new SocketException (
186 "no network interface is bound to such an IP address");
190 * Return an <code>Enumeration</code> of all available network interfaces
192 * @exception SocketException If an error occurs
194 public static Enumeration
getNetworkInterfaces ()
195 throws SocketException
197 Vector networkInterfaces
= getRealNetworkInterfaces();
199 if (networkInterfaces
.isEmpty())
202 return networkInterfaces
.elements();
206 * Checks if the current instance is equal to obj
208 * @param obj The object to compare with
210 public boolean equals (Object obj
)
212 if (!(obj
instanceof NetworkInterface
))
215 NetworkInterface tmp
= (NetworkInterface
) obj
;
217 return (name
.equals (tmp
.name
)
218 && inetAddresses
.equals (tmp
.inetAddresses
));
222 * Returns the hashcode of the current instance
224 public int hashCode ()
226 // FIXME: hash correctly
227 return name
.hashCode () + inetAddresses
.hashCode ();
231 * Returns a string representation of the interface
233 public String
toString ()
235 // FIXME: check if this is correct
237 String separator
= System
.getProperty ("line.separator");
239 result
= "name: " + getDisplayName () + " (" + getName () +
240 ") addresses:" + separator
;
242 for (Enumeration e
= inetAddresses
.elements ();
243 e
.hasMoreElements (); )
245 InetAddress address
= (InetAddress
) e
.nextElement ();
246 result
+= address
.toString () + ";" + separator
;
251 } // class NetworkInterface