source: trunk/gcc/libjava/java/net/NetworkInterface.java

Last change on this file was 1477, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1476,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.7 KB
Line 
1/* NetworkInterface.java
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.net;
39
40import java.util.Enumeration;
41import java.util.Vector;
42
43/**
44 * @author Michael Koch <konqueror@gmx.de>
45 * @since 1.4
46 */
47public final class NetworkInterface
48{
49 private static Vector networkInterfaces;
50
51 private String name;
52
53 private Vector inetAddresses;
54
55 private NetworkInterface (String name, InetAddress address)
56 {
57 this.name = name;
58 this.inetAddresses = new Vector (1, 1);
59 this.inetAddresses.add (address);
60 }
61
62 private native static Vector getRealNetworkInterfaces ()
63 throws SocketException;
64
65 /**
66 * Returns the name of the network interface
67 */
68 public String getName ()
69 {
70 return name;
71 }
72
73 /**
74 * Returns all available addresses of the network interface
75 *
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.
80 *
81 * @return An enumeration of all addresses.
82 */
83 public Enumeration getInetAddresses ()
84 {
85 SecurityManager s = System.getSecurityManager ();
86
87 if (s == null)
88 return inetAddresses.elements ();
89
90 Vector tmpInetAddresses = new Vector (1, 1);
91
92 for (Enumeration addresses = inetAddresses.elements ();
93 addresses.hasMoreElements (); )
94 {
95 InetAddress addr = (InetAddress) addresses.nextElement ();
96 try
97 {
98 s.checkConnect (addr.getHostAddress (), 58000);
99 tmpInetAddresses.add (addr);
100 }
101 catch (SecurityException e)
102 {
103 }
104 }
105
106 return tmpInetAddresses.elements ();
107 }
108
109 /**
110 * Returns the display name of the interface
111 */
112 public String getDisplayName ()
113 {
114 return name;
115 }
116
117 /**
118 * Returns an network interface by name
119 *
120 * @param name The name of the interface to return
121 *
122 * @return a <code>NetworkInterface</code> object representing the interface,
123 * or null if there is no interface with that name.
124 *
125 * @exception SocketException If an error occurs
126 * @exception NullPointerException If the specified name is null
127 */
128 public static NetworkInterface getByName (String name)
129 throws SocketException
130 {
131 if (networkInterfaces == null)
132 networkInterfaces = getRealNetworkInterfaces ();
133
134 for (Enumeration e = networkInterfaces.elements ();
135 e.hasMoreElements (); )
136 {
137 NetworkInterface tmp = (NetworkInterface) e.nextElement ();
138
139 if (name.equals (tmp.getName ()))
140 return tmp;
141 }
142
143 // No interface with the given name found.
144 return null;
145 }
146
147 /**
148 * Return a network interface by its address
149 *
150 * @param addr The address of the interface to return
151 *
152 * @exception SocketException If an error occurs
153 * @exception NullPointerException If the specified addess is null
154 */
155 public static NetworkInterface getByInetAddress (InetAddress addr)
156 throws SocketException
157 {
158 if (networkInterfaces == null)
159 networkInterfaces = getRealNetworkInterfaces ();
160
161 for (Enumeration interfaces = networkInterfaces.elements ();
162 interfaces.hasMoreElements (); )
163 {
164 NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
165
166 for (Enumeration addresses = tmp.inetAddresses.elements ();
167 addresses.hasMoreElements (); )
168 {
169 if (addr.equals ((InetAddress) addresses.nextElement ()))
170 return tmp;
171 }
172 }
173
174 throw new SocketException (
175 "no network interface is bound to such an IP address");
176 }
177
178 /**
179 * Return an Enumeration of all available network interfaces
180 *
181 * @exception SocketException If an error occurs
182 */
183 public static Enumeration getNetworkInterfaces ()
184 throws SocketException
185 {
186 if (networkInterfaces == null)
187 networkInterfaces = getRealNetworkInterfaces ();
188
189 Enumeration tmp = networkInterfaces.elements ();
190 if (tmp.hasMoreElements ())
191 return tmp;
192
193 return null;
194 }
195
196 /**
197 * Checks if the current instance is equal to obj
198 *
199 * @param obj The object to compare with
200 */
201 public boolean equals (Object obj)
202 {
203 if (!(obj instanceof NetworkInterface))
204 return false;
205
206 NetworkInterface tmp = (NetworkInterface) obj;
207
208 return (name.equals (tmp.name)
209 && inetAddresses.equals (tmp.inetAddresses));
210 }
211
212 /**
213 * Returns the hashcode of the current instance
214 */
215 public int hashCode ()
216 {
217 // FIXME: hash correctly
218 return name.hashCode () + inetAddresses.hashCode ();
219 }
220
221 /**
222 * Returns a string representation of the interface
223 */
224 public String toString ()
225 {
226 // FIXME: check if this is correct
227 String result;
228 String separator = System.getProperty ("line.separator");
229
230 result = "name: " + getDisplayName () + " (" + getName () +
231 ") addresses:" + separator;
232
233 for (Enumeration e = inetAddresses.elements ();
234 e.hasMoreElements (); )
235 {
236 InetAddress address = (InetAddress) e.nextElement ();
237 result += address.toString () + separator;
238 }
239
240 return result;
241 }
242} // class NetworkInterface
Note: See TracBrowser for help on using the repository browser.