History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/java/net/NetworkInterface.java (1 of 18352 files). Show all

Unified · Split

java/pkg/java/net/NetworkInterface.java+554−0
@@ -0,0 +1,554 @@
1/*
2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26package java.net;
27
28import java.util.Enumeration;
29import java.util.NoSuchElementException;
30import sun.security.action.*;
31import 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 */
43public 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
89 * will be returned in the Enumeration. However, if the caller has the
90 * {@link NetPermission}("getNetworkInformation") permission, then all
91 * InetAddresses are returned.
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];
104 boolean trusted = true;
105
106 SecurityManager sec = System.getSecurityManager();
107 if (sec != null) {
108 try {
109 sec.checkPermission(new NetPermission("getNetworkInformation"));
110 } catch (SecurityException e) {
111 trusted = false;
112 }
113 }
114 for (int j=0; j<addrs.length; j++) {
115 try {
116 if (sec != null && !trusted) {
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 /**
215 * Returns the index of this network interface. The index is an integer greater
216 * or equal to zero, or {@code -1} for unknown. This is a system specific value
217 * and interfaces with the same name can have different indexes on different
218 * machines.
219 *
220 * @return the index of this network interface or {@code -1} if the index is
221 * unknown
222 * @see #getByIndex(int)
223 * @since 1.7
224 */
225 public int getIndex() {
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 *
234 * @return a non-empty string representing the display name of this network
235 * interface, or null if no display name is available.
236 */
237 public String getDisplayName() {
238 /* strict TCK conformance */
239 return "".equals(displayName) ? null : displayName;
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
268 * @return the NetworkInterface obtained from its index, or {@code null} if
269 * there is no interface with such an index on the system
270 * @throws SocketException if an I/O error occurs.
271 * @throws IllegalArgumentException if index has a negative value
272 * @see #getIndex()
273 * @since 1.7
274 */
275 public static NetworkInterface getByIndex(int index) throws SocketException {
276 if (index < 0)
277 throw new IllegalArgumentException("Interface index can't be negative");
278 return getByIndex0(index);
279 }
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 {
304 if (addr == null) {
305 throw new NullPointerException();
306 }
307 if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
308 throw new IllegalArgumentException ("invalid address type");
309 }
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
355 private native static NetworkInterface getByIndex0(int index)
356 throws SocketException;
357
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.
415 * If a security manager is set, then the caller must have
416 * the permission {@link NetPermission}("getNetworkInformation").
417 *
418 * @return a byte array containing the address, or <code>null</code> if
419 * the address doesn't exist, is not accessible or a security
420 * manager is set and the caller does not have the permission
421 * NetPermission("getNetworkInformation")
422 *
423 * @exception SocketException if an I/O error occurs.
424 * @since 1.6
425 */
426 public byte[] getHardwareAddress() throws SocketException {
427 SecurityManager sec = System.getSecurityManager();
428 if (sec != null) {
429 try {
430 sec.checkPermission(new NetPermission("getNetworkInformation"));
431 } catch (SecurityException e) {
432 if (!getInetAddresses().hasMoreElements()) {
433 // don't have connect permission to any local address
434 return null;
435 }
436 }
437 }
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 instanceof NetworkInterface)) {
497 return false;
498 }
499 NetworkInterface that = (NetworkInterface)obj;
500 if (this.name != null ) {
501 if (!this.name.equals(that.name)) {
502 return false;
503 }
504 } else {
505 if (that.name != null) {
506 return false;
507 }
508 }
509
510 if (this.addrs == null) {
511 return that.addrs == null;
512 } else if (that.addrs == null) {
513 return false;
514 }
515
516 /* Both addrs not null. Compare number of addresses */
517
518 if (this.addrs.length != that.addrs.length) {
519 return false;
520 }
521
522 InetAddress[] thatAddrs = that.addrs;
523 int count = thatAddrs.length;
524
525 for (int i=0; i<count; i++) {
526 boolean found = false;
527 for (int j=0; j<count; j++) {
528 if (addrs[i].equals(thatAddrs[j])) {
529 found = true;
530 break;
531 }
532 }
533 if (!found) {
534 return false;
535 }
536 }
537 return true;
538 }
539
540 public int hashCode() {
541 return name == null? 0: name.hashCode();
542 }
543
544 public String toString() {
545 String result = "name:";
546 result += name == null? "null": name;
547 if (displayName != null) {
548 result += " (" + displayName + ")";
549 }
550 return result;
551 }
552
553 private static native void init();
554}