author | michaelm |
Thu, 22 Jul 2010 16:33:12 +0100 | |
changeset 6872 | b90e4fee471f |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.util.Enumeration; |
|
29 |
import java.util.NoSuchElementException; |
|
30 |
import sun.security.action.*; |
|
31 |
import java.security.AccessController; |
|
32 |
||
33 |
/** |
|
34 |
* This class represents a Network Interface made up of a name, |
|
35 |
* and a list of IP addresses assigned to this interface. |
|
36 |
* It is used to identify the local interface on which a multicast group |
|
37 |
* is joined. |
|
38 |
* |
|
39 |
* Interfaces are normally known by names such as "le0". |
|
40 |
* |
|
41 |
* @since 1.4 |
|
42 |
*/ |
|
43 |
public final class NetworkInterface { |
|
44 |
private String name; |
|
45 |
private String displayName; |
|
46 |
private int index; |
|
47 |
private InetAddress addrs[]; |
|
48 |
private InterfaceAddress bindings[]; |
|
49 |
private NetworkInterface childs[]; |
|
50 |
private NetworkInterface parent = null; |
|
51 |
private boolean virtual = false; |
|
52 |
||
53 |
static { |
|
54 |
AccessController.doPrivileged(new LoadLibraryAction("net")); |
|
55 |
init(); |
|
56 |
} |
|
57 |
||
58 |
/** |
|
59 |
* Returns an NetworkInterface object with index set to 0 and name to null. |
|
60 |
* Setting such an interface on a MulticastSocket will cause the |
|
61 |
* kernel to choose one interface for sending multicast packets. |
|
62 |
* |
|
63 |
*/ |
|
64 |
NetworkInterface() { |
|
65 |
} |
|
66 |
||
67 |
NetworkInterface(String name, int index, InetAddress[] addrs) { |
|
68 |
this.name = name; |
|
69 |
this.index = index; |
|
70 |
this.addrs = addrs; |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Get the name of this network interface. |
|
75 |
* |
|
76 |
* @return the name of this network interface |
|
77 |
*/ |
|
78 |
public String getName() { |
|
79 |
return name; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Convenience method to return an Enumeration with all or a |
|
84 |
* subset of the InetAddresses bound to this network interface. |
|
85 |
* <p> |
|
86 |
* If there is a security manager, its <code>checkConnect</code> |
|
87 |
* method is called for each InetAddress. Only InetAddresses where |
|
88 |
* the <code>checkConnect</code> doesn't throw a SecurityException |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
89 |
* will be returned in the Enumeration. However, if the caller has the |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
90 |
* {@link NetPermission}("getNetworkInformation") permission, then all |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
91 |
* InetAddresses are returned. |
2 | 92 |
* @return an Enumeration object with all or a subset of the InetAddresses |
93 |
* bound to this network interface |
|
94 |
*/ |
|
95 |
public Enumeration<InetAddress> getInetAddresses() { |
|
96 |
||
97 |
class checkedAddresses implements Enumeration<InetAddress> { |
|
98 |
||
99 |
private int i=0, count=0; |
|
100 |
private InetAddress local_addrs[]; |
|
101 |
||
102 |
checkedAddresses() { |
|
103 |
local_addrs = new InetAddress[addrs.length]; |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
104 |
boolean trusted = true; |
2 | 105 |
|
106 |
SecurityManager sec = System.getSecurityManager(); |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
107 |
if (sec != null) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
108 |
try { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
109 |
sec.checkPermission(new NetPermission("getNetworkInformation")); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
110 |
} catch (SecurityException e) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
111 |
trusted = false; |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
112 |
} |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
113 |
} |
2 | 114 |
for (int j=0; j<addrs.length; j++) { |
115 |
try { |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
116 |
if (sec != null && !trusted) { |
2 | 117 |
sec.checkConnect(addrs[j].getHostAddress(), -1); |
118 |
} |
|
119 |
local_addrs[count++] = addrs[j]; |
|
120 |
} catch (SecurityException e) { } |
|
121 |
} |
|
122 |
||
123 |
} |
|
124 |
||
125 |
public InetAddress nextElement() { |
|
126 |
if (i < count) { |
|
127 |
return local_addrs[i++]; |
|
128 |
} else { |
|
129 |
throw new NoSuchElementException(); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
public boolean hasMoreElements() { |
|
134 |
return (i < count); |
|
135 |
} |
|
136 |
} |
|
137 |
return new checkedAddresses(); |
|
138 |
||
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Get a List of all or a subset of the <code>InterfaceAddresses</code> |
|
143 |
* of this network interface. |
|
144 |
* <p> |
|
145 |
* If there is a security manager, its <code>checkConnect</code> |
|
146 |
* method is called with the InetAddress for each InterfaceAddress. |
|
147 |
* Only InterfaceAddresses where the <code>checkConnect</code> doesn't throw |
|
148 |
* a SecurityException will be returned in the List. |
|
149 |
* |
|
150 |
* @return a <code>List</code> object with all or a subset of the |
|
151 |
* InterfaceAddresss of this network interface |
|
152 |
* @since 1.6 |
|
153 |
*/ |
|
154 |
public java.util.List<InterfaceAddress> getInterfaceAddresses() { |
|
155 |
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1); |
|
156 |
SecurityManager sec = System.getSecurityManager(); |
|
157 |
for (int j=0; j<bindings.length; j++) { |
|
158 |
try { |
|
159 |
if (sec != null) { |
|
160 |
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1); |
|
161 |
} |
|
162 |
lst.add(bindings[j]); |
|
163 |
} catch (SecurityException e) { } |
|
164 |
} |
|
165 |
return lst; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Get an Enumeration with all the subinterfaces (also known as virtual |
|
170 |
* interfaces) attached to this network interface. |
|
171 |
* <p> |
|
172 |
* For instance eth0:1 will be a subinterface to eth0. |
|
173 |
* |
|
174 |
* @return an Enumeration object with all of the subinterfaces |
|
175 |
* of this network interface |
|
176 |
* @since 1.6 |
|
177 |
*/ |
|
178 |
public Enumeration<NetworkInterface> getSubInterfaces() { |
|
179 |
class subIFs implements Enumeration<NetworkInterface> { |
|
180 |
||
181 |
private int i=0; |
|
182 |
||
183 |
subIFs() { |
|
184 |
} |
|
185 |
||
186 |
public NetworkInterface nextElement() { |
|
187 |
if (i < childs.length) { |
|
188 |
return childs[i++]; |
|
189 |
} else { |
|
190 |
throw new NoSuchElementException(); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
public boolean hasMoreElements() { |
|
195 |
return (i < childs.length); |
|
196 |
} |
|
197 |
} |
|
198 |
return new subIFs(); |
|
199 |
||
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns the parent NetworkInterface of this interface if this is |
|
204 |
* a subinterface, or <code>null</code> if it is a physical |
|
205 |
* (non virtual) interface or has no parent. |
|
206 |
* |
|
207 |
* @return The <code>NetworkInterface</code> this interface is attached to. |
|
208 |
* @since 1.6 |
|
209 |
*/ |
|
210 |
public NetworkInterface getParent() { |
|
211 |
return parent; |
|
212 |
} |
|
213 |
||
214 |
/** |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
215 |
* Returns the index of this network interface. The index is an integer greater |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
216 |
* or equal to zero, or {@code -1} for unknown. This is a system specific value |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
217 |
* and interfaces with the same name can have different indexes on different |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
218 |
* machines. |
2 | 219 |
* |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
220 |
* @return the index of this network interface or {@code -1} if the index is |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
221 |
* unknown |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
222 |
* @see #getByIndex(int) |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
223 |
* @since 1.7 |
2 | 224 |
*/ |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
225 |
public int getIndex() { |
2 | 226 |
return index; |
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Get the display name of this network interface. |
|
231 |
* A display name is a human readable String describing the network |
|
232 |
* device. |
|
233 |
* |
|
5293
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
234 |
* @return a non-empty string representing the display name of this network |
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
235 |
* interface, or null if no display name is available. |
2 | 236 |
*/ |
237 |
public String getDisplayName() { |
|
5293
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
238 |
/* strict TCK conformance */ |
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
239 |
return "".equals(displayName) ? null : displayName; |
2 | 240 |
} |
241 |
||
242 |
/** |
|
243 |
* Searches for the network interface with the specified name. |
|
244 |
* |
|
245 |
* @param name |
|
246 |
* The name of the network interface. |
|
247 |
* |
|
248 |
* @return A <tt>NetworkInterface</tt> with the specified name, |
|
249 |
* or <tt>null</tt> if there is no network interface |
|
250 |
* with the specified name. |
|
251 |
* |
|
252 |
* @throws SocketException |
|
253 |
* If an I/O error occurs. |
|
254 |
* |
|
255 |
* @throws NullPointerException |
|
256 |
* If the specified name is <tt>null</tt>. |
|
257 |
*/ |
|
258 |
public static NetworkInterface getByName(String name) throws SocketException { |
|
259 |
if (name == null) |
|
260 |
throw new NullPointerException(); |
|
261 |
return getByName0(name); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Get a network interface given its index. |
|
266 |
* |
|
267 |
* @param index an integer, the index of the interface |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
268 |
* @return the NetworkInterface obtained from its index, or {@code null} if |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
269 |
* there is no interface with such an index on the system |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
270 |
* @throws SocketException if an I/O error occurs. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
271 |
* @throws IllegalArgumentException if index has a negative value |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
272 |
* @see #getIndex() |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
273 |
* @since 1.7 |
2 | 274 |
*/ |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
275 |
public static NetworkInterface getByIndex(int index) throws SocketException { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
276 |
if (index < 0) |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
277 |
throw new IllegalArgumentException("Interface index can't be negative"); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
278 |
return getByIndex0(index); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
279 |
} |
2 | 280 |
|
281 |
/** |
|
282 |
* Convenience method to search for a network interface that |
|
283 |
* has the specified Internet Protocol (IP) address bound to |
|
284 |
* it. |
|
285 |
* <p> |
|
286 |
* If the specified IP address is bound to multiple network |
|
287 |
* interfaces it is not defined which network interface is |
|
288 |
* returned. |
|
289 |
* |
|
290 |
* @param addr |
|
291 |
* The <tt>InetAddress</tt> to search with. |
|
292 |
* |
|
293 |
* @return A <tt>NetworkInterface</tt> |
|
294 |
* or <tt>null</tt> if there is no network interface |
|
295 |
* with the specified IP address. |
|
296 |
* |
|
297 |
* @throws SocketException |
|
298 |
* If an I/O error occurs. |
|
299 |
* |
|
300 |
* @throws NullPointerException |
|
301 |
* If the specified address is <tt>null</tt>. |
|
302 |
*/ |
|
303 |
public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException { |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
304 |
if (addr == null) { |
2 | 305 |
throw new NullPointerException(); |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
306 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
307 |
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
308 |
throw new IllegalArgumentException ("invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
309 |
} |
2 | 310 |
return getByInetAddress0(addr); |
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Returns all the interfaces on this machine. Returns null if no |
|
315 |
* network interfaces could be found on this machine. |
|
316 |
* |
|
317 |
* NOTE: can use getNetworkInterfaces()+getInetAddresses() |
|
318 |
* to obtain all IP addresses for this node |
|
319 |
* |
|
320 |
* @return an Enumeration of NetworkInterfaces found on this machine |
|
321 |
* @exception SocketException if an I/O error occurs. |
|
322 |
*/ |
|
323 |
||
324 |
public static Enumeration<NetworkInterface> getNetworkInterfaces() |
|
325 |
throws SocketException { |
|
326 |
final NetworkInterface[] netifs = getAll(); |
|
327 |
||
328 |
// specified to return null if no network interfaces |
|
329 |
if (netifs == null) |
|
330 |
return null; |
|
331 |
||
332 |
return new Enumeration<NetworkInterface>() { |
|
333 |
private int i = 0; |
|
334 |
public NetworkInterface nextElement() { |
|
335 |
if (netifs != null && i < netifs.length) { |
|
336 |
NetworkInterface netif = netifs[i++]; |
|
337 |
return netif; |
|
338 |
} else { |
|
339 |
throw new NoSuchElementException(); |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
public boolean hasMoreElements() { |
|
344 |
return (netifs != null && i < netifs.length); |
|
345 |
} |
|
346 |
}; |
|
347 |
} |
|
348 |
||
349 |
private native static NetworkInterface[] getAll() |
|
350 |
throws SocketException; |
|
351 |
||
352 |
private native static NetworkInterface getByName0(String name) |
|
353 |
throws SocketException; |
|
354 |
||
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
355 |
private native static NetworkInterface getByIndex0(int index) |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
356 |
throws SocketException; |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
357 |
|
2 | 358 |
private native static NetworkInterface getByInetAddress0(InetAddress addr) |
359 |
throws SocketException; |
|
360 |
||
361 |
/** |
|
362 |
* Returns whether a network interface is up and running. |
|
363 |
* |
|
364 |
* @return <code>true</code> if the interface is up and running. |
|
365 |
* @exception SocketException if an I/O error occurs. |
|
366 |
* @since 1.6 |
|
367 |
*/ |
|
368 |
||
369 |
public boolean isUp() throws SocketException { |
|
370 |
return isUp0(name, index); |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns whether a network interface is a loopback interface. |
|
375 |
* |
|
376 |
* @return <code>true</code> if the interface is a loopback interface. |
|
377 |
* @exception SocketException if an I/O error occurs. |
|
378 |
* @since 1.6 |
|
379 |
*/ |
|
380 |
||
381 |
public boolean isLoopback() throws SocketException { |
|
382 |
return isLoopback0(name, index); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Returns whether a network interface is a point to point interface. |
|
387 |
* A typical point to point interface would be a PPP connection through |
|
388 |
* a modem. |
|
389 |
* |
|
390 |
* @return <code>true</code> if the interface is a point to point |
|
391 |
* interface. |
|
392 |
* @exception SocketException if an I/O error occurs. |
|
393 |
* @since 1.6 |
|
394 |
*/ |
|
395 |
||
396 |
public boolean isPointToPoint() throws SocketException { |
|
397 |
return isP2P0(name, index); |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Returns whether a network interface supports multicasting or not. |
|
402 |
* |
|
403 |
* @return <code>true</code> if the interface supports Multicasting. |
|
404 |
* @exception SocketException if an I/O error occurs. |
|
405 |
* @since 1.6 |
|
406 |
*/ |
|
407 |
||
408 |
public boolean supportsMulticast() throws SocketException { |
|
409 |
return supportsMulticast0(name, index); |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Returns the hardware address (usually MAC) of the interface if it |
|
414 |
* has one and if it can be accessed given the current privileges. |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
415 |
* If a security manager is set, then the caller must have |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
416 |
* the permission {@link NetPermission}("getNetworkInformation"). |
2 | 417 |
* |
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
418 |
* @return a byte array containing the address, or <code>null</code> if |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
419 |
* the address doesn't exist, is not accessible or a security |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
420 |
* manager is set and the caller does not have the permission |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
421 |
* NetPermission("getNetworkInformation") |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
422 |
* |
2 | 423 |
* @exception SocketException if an I/O error occurs. |
424 |
* @since 1.6 |
|
425 |
*/ |
|
426 |
public byte[] getHardwareAddress() throws SocketException { |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
427 |
SecurityManager sec = System.getSecurityManager(); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
428 |
if (sec != null) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
429 |
try { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
430 |
sec.checkPermission(new NetPermission("getNetworkInformation")); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
431 |
} catch (SecurityException e) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
432 |
if (!getInetAddresses().hasMoreElements()) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
433 |
// don't have connect permission to any local address |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
434 |
return null; |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
435 |
} |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
436 |
} |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
437 |
} |
2 | 438 |
for (InetAddress addr : addrs) { |
439 |
if (addr instanceof Inet4Address) { |
|
440 |
return getMacAddr0(((Inet4Address)addr).getAddress(), name, index); |
|
441 |
} |
|
442 |
} |
|
443 |
return getMacAddr0(null, name, index); |
|
444 |
} |
|
445 |
||
446 |
/** |
|
447 |
* Returns the Maximum Transmission Unit (MTU) of this interface. |
|
448 |
* |
|
449 |
* @return the value of the MTU for that interface. |
|
450 |
* @exception SocketException if an I/O error occurs. |
|
451 |
* @since 1.6 |
|
452 |
*/ |
|
453 |
public int getMTU() throws SocketException { |
|
454 |
return getMTU0(name, index); |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Returns whether this interface is a virtual interface (also called |
|
459 |
* subinterface). |
|
460 |
* Virtual interfaces are, on some systems, interfaces created as a child |
|
461 |
* of a physical interface and given different settings (like address or |
|
462 |
* MTU). Usually the name of the interface will the name of the parent |
|
463 |
* followed by a colon (:) and a number identifying the child since there |
|
464 |
* can be several virtual interfaces attached to a single physical |
|
465 |
* interface. |
|
466 |
* |
|
467 |
* @return <code>true</code> if this interface is a virtual interface. |
|
468 |
* @since 1.6 |
|
469 |
*/ |
|
470 |
public boolean isVirtual() { |
|
471 |
return virtual; |
|
472 |
} |
|
473 |
||
474 |
private native static boolean isUp0(String name, int ind) throws SocketException; |
|
475 |
private native static boolean isLoopback0(String name, int ind) throws SocketException; |
|
476 |
private native static boolean supportsMulticast0(String name, int ind) throws SocketException; |
|
477 |
private native static boolean isP2P0(String name, int ind) throws SocketException; |
|
478 |
private native static byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException; |
|
479 |
private native static int getMTU0(String name, int ind) throws SocketException; |
|
480 |
||
481 |
/** |
|
482 |
* Compares this object against the specified object. |
|
483 |
* The result is <code>true</code> if and only if the argument is |
|
484 |
* not <code>null</code> and it represents the same NetworkInterface |
|
485 |
* as this object. |
|
486 |
* <p> |
|
487 |
* Two instances of <code>NetworkInterface</code> represent the same |
|
488 |
* NetworkInterface if both name and addrs are the same for both. |
|
489 |
* |
|
490 |
* @param obj the object to compare against. |
|
491 |
* @return <code>true</code> if the objects are the same; |
|
492 |
* <code>false</code> otherwise. |
|
493 |
* @see java.net.InetAddress#getAddress() |
|
494 |
*/ |
|
495 |
public boolean equals(Object obj) { |
|
496 |
if ((obj == null) || !(obj instanceof NetworkInterface)) { |
|
497 |
return false; |
|
498 |
} |
|
499 |
NetworkInterface netIF = (NetworkInterface)obj; |
|
500 |
if (name != null ) { |
|
501 |
if (netIF.getName() != null) { |
|
502 |
if (!name.equals(netIF.getName())) { |
|
503 |
return false; |
|
504 |
} |
|
505 |
} else { |
|
506 |
return false; |
|
507 |
} |
|
508 |
} else { |
|
509 |
if (netIF.getName() != null) { |
|
510 |
return false; |
|
511 |
} |
|
512 |
} |
|
513 |
Enumeration newAddrs = netIF.getInetAddresses(); |
|
514 |
int i = 0; |
|
515 |
for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++); |
|
516 |
if (addrs == null) { |
|
517 |
if (i != 0) { |
|
518 |
return false; |
|
519 |
} |
|
520 |
} else { |
|
521 |
/* |
|
522 |
* Compare number of addresses (in the checked subset) |
|
523 |
*/ |
|
524 |
int count = 0; |
|
525 |
Enumeration e = getInetAddresses(); |
|
526 |
for (; e.hasMoreElements(); count++) { |
|
527 |
e.nextElement(); |
|
528 |
} |
|
529 |
if (i != count) { |
|
530 |
return false; |
|
531 |
} |
|
532 |
} |
|
533 |
newAddrs = netIF.getInetAddresses(); |
|
534 |
for (; newAddrs.hasMoreElements();) { |
|
535 |
boolean equal = false; |
|
536 |
Enumeration thisAddrs = getInetAddresses(); |
|
537 |
InetAddress newAddr = (InetAddress)newAddrs.nextElement(); |
|
538 |
for (; thisAddrs.hasMoreElements();) { |
|
539 |
InetAddress thisAddr = (InetAddress)thisAddrs.nextElement(); |
|
540 |
if (thisAddr.equals(newAddr)) { |
|
541 |
equal = true; |
|
542 |
} |
|
543 |
} |
|
544 |
if (!equal) { |
|
545 |
return false; |
|
546 |
} |
|
547 |
} |
|
548 |
return true; |
|
549 |
} |
|
550 |
||
551 |
public int hashCode() { |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
552 |
int count = name == null? 0: name.hashCode(); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
553 |
Enumeration<InetAddress> addrs = getInetAddresses(); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
554 |
while (addrs.hasMoreElements()) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
555 |
count += addrs.nextElement().hashCode(); |
2 | 556 |
} |
557 |
return count; |
|
558 |
} |
|
559 |
||
560 |
public String toString() { |
|
561 |
String result = "name:"; |
|
562 |
result += name == null? "null": name; |
|
563 |
if (displayName != null) { |
|
564 |
result += " (" + displayName + ")"; |
|
565 |
} |
|
566 |
result += " index: "+index+" addresses:\n"; |
|
567 |
for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) { |
|
568 |
InetAddress addr = (InetAddress)e.nextElement(); |
|
569 |
result += addr+";\n"; |
|
570 |
} |
|
571 |
return result; |
|
572 |
} |
|
573 |
private static native void init(); |
|
574 |
||
575 |
} |