author | iris |
Wed, 20 Jan 2016 11:02:36 -0800 | |
changeset 35302 | e4d2275861c3 |
parent 32649 | 2ee9017c7597 |
child 41969 | e4c2f100927f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
2 |
* Copyright (c) 2000, 2015, 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 |
||
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
28 |
import java.util.Arrays; |
2 | 29 |
import java.util.Enumeration; |
30 |
import java.util.NoSuchElementException; |
|
31 |
import java.security.AccessController; |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
32 |
import java.util.Spliterator; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
33 |
import java.util.Spliterators; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
34 |
import java.util.stream.Stream; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
35 |
import java.util.stream.StreamSupport; |
2 | 36 |
|
37 |
/** |
|
38 |
* This class represents a Network Interface made up of a name, |
|
39 |
* and a list of IP addresses assigned to this interface. |
|
40 |
* It is used to identify the local interface on which a multicast group |
|
41 |
* is joined. |
|
42 |
* |
|
43 |
* Interfaces are normally known by names such as "le0". |
|
44 |
* |
|
45 |
* @since 1.4 |
|
46 |
*/ |
|
47 |
public final class NetworkInterface { |
|
48 |
private String name; |
|
49 |
private String displayName; |
|
50 |
private int index; |
|
51 |
private InetAddress addrs[]; |
|
52 |
private InterfaceAddress bindings[]; |
|
53 |
private NetworkInterface childs[]; |
|
54 |
private NetworkInterface parent = null; |
|
55 |
private boolean virtual = false; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
56 |
private static final NetworkInterface defaultInterface; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
57 |
private static final int defaultIndex; /* index of defaultInterface */ |
2 | 58 |
|
59 |
static { |
|
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
60 |
AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
61 |
new java.security.PrivilegedAction<>() { |
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
62 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
63 |
System.loadLibrary("net"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
64 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
65 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
66 |
}); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12047
diff
changeset
|
67 |
|
2 | 68 |
init(); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
69 |
defaultInterface = DefaultInterface.getDefault(); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
70 |
if (defaultInterface != null) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
71 |
defaultIndex = defaultInterface.getIndex(); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
72 |
} else { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
73 |
defaultIndex = 0; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
74 |
} |
2 | 75 |
} |
76 |
||
77 |
/** |
|
78 |
* Returns an NetworkInterface object with index set to 0 and name to null. |
|
79 |
* Setting such an interface on a MulticastSocket will cause the |
|
80 |
* kernel to choose one interface for sending multicast packets. |
|
81 |
* |
|
82 |
*/ |
|
83 |
NetworkInterface() { |
|
84 |
} |
|
85 |
||
86 |
NetworkInterface(String name, int index, InetAddress[] addrs) { |
|
87 |
this.name = name; |
|
88 |
this.index = index; |
|
89 |
this.addrs = addrs; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Get the name of this network interface. |
|
94 |
* |
|
95 |
* @return the name of this network interface |
|
96 |
*/ |
|
97 |
public String getName() { |
|
98 |
return name; |
|
99 |
} |
|
100 |
||
101 |
/** |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
102 |
* Get an Enumeration with all or a subset of the InetAddresses bound to |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
103 |
* this network interface. |
2 | 104 |
* <p> |
19069 | 105 |
* If there is a security manager, its {@code checkConnect} |
2 | 106 |
* method is called for each InetAddress. Only InetAddresses where |
19069 | 107 |
* the {@code checkConnect} doesn't throw a SecurityException |
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
108 |
* 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
|
109 |
* {@link NetPermission}("getNetworkInformation") permission, then all |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
110 |
* InetAddresses are returned. |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
111 |
* |
2 | 112 |
* @return an Enumeration object with all or a subset of the InetAddresses |
113 |
* bound to this network interface |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
114 |
* @see #inetAddresses() |
2 | 115 |
*/ |
116 |
public Enumeration<InetAddress> getInetAddresses() { |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
117 |
return enumerationFromArray(getCheckedInetAddresses()); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
118 |
} |
2 | 119 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
120 |
/** |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
121 |
* Get a Stream of all or a subset of the InetAddresses bound to this |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
122 |
* network interface. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
123 |
* <p> |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
124 |
* If there is a security manager, its {@code checkConnect} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
125 |
* method is called for each InetAddress. Only InetAddresses where |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
126 |
* the {@code checkConnect} doesn't throw a SecurityException will be |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
127 |
* returned in the Stream. However, if the caller has the |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
128 |
* {@link NetPermission}("getNetworkInformation") permission, then all |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
129 |
* InetAddresses are returned. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
130 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
131 |
* @return a Stream object with all or a subset of the InetAddresses |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
132 |
* bound to this network interface |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
32649
diff
changeset
|
133 |
* @since 9 |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
134 |
*/ |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
135 |
public Stream<InetAddress> inetAddresses() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
136 |
return streamFromArray(getCheckedInetAddresses()); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
137 |
} |
2 | 138 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
139 |
private InetAddress[] getCheckedInetAddresses() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
140 |
InetAddress[] local_addrs = new InetAddress[addrs.length]; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
141 |
boolean trusted = true; |
2 | 142 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
143 |
SecurityManager sec = System.getSecurityManager(); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
144 |
if (sec != null) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
145 |
try { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
146 |
sec.checkPermission(new NetPermission("getNetworkInformation")); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
147 |
} catch (SecurityException e) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
148 |
trusted = false; |
2 | 149 |
} |
150 |
} |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
151 |
int i = 0; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
152 |
for (int j = 0; j < addrs.length; j++) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
153 |
try { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
154 |
if (!trusted) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
155 |
sec.checkConnect(addrs[j].getHostAddress(), -1); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
156 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
157 |
local_addrs[i++] = addrs[j]; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
158 |
} catch (SecurityException e) { } |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
159 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
160 |
return Arrays.copyOf(local_addrs, i); |
2 | 161 |
} |
162 |
||
163 |
/** |
|
19069 | 164 |
* Get a List of all or a subset of the {@code InterfaceAddresses} |
2 | 165 |
* of this network interface. |
166 |
* <p> |
|
19069 | 167 |
* If there is a security manager, its {@code checkConnect} |
2 | 168 |
* method is called with the InetAddress for each InterfaceAddress. |
19069 | 169 |
* Only InterfaceAddresses where the {@code checkConnect} doesn't throw |
2 | 170 |
* a SecurityException will be returned in the List. |
171 |
* |
|
19069 | 172 |
* @return a {@code List} object with all or a subset of the |
2 | 173 |
* InterfaceAddresss of this network interface |
174 |
* @since 1.6 |
|
175 |
*/ |
|
176 |
public java.util.List<InterfaceAddress> getInterfaceAddresses() { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
177 |
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1); |
2 | 178 |
SecurityManager sec = System.getSecurityManager(); |
179 |
for (int j=0; j<bindings.length; j++) { |
|
180 |
try { |
|
181 |
if (sec != null) { |
|
182 |
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1); |
|
183 |
} |
|
184 |
lst.add(bindings[j]); |
|
185 |
} catch (SecurityException e) { } |
|
186 |
} |
|
187 |
return lst; |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Get an Enumeration with all the subinterfaces (also known as virtual |
|
192 |
* interfaces) attached to this network interface. |
|
193 |
* <p> |
|
194 |
* For instance eth0:1 will be a subinterface to eth0. |
|
195 |
* |
|
196 |
* @return an Enumeration object with all of the subinterfaces |
|
197 |
* of this network interface |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
198 |
* @see #subInterfaces() |
2 | 199 |
* @since 1.6 |
200 |
*/ |
|
201 |
public Enumeration<NetworkInterface> getSubInterfaces() { |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
202 |
return enumerationFromArray(childs); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
203 |
} |
2 | 204 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
205 |
/** |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
206 |
* Get a Stream of all subinterfaces (also known as virtual |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
207 |
* interfaces) attached to this network interface. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
208 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
209 |
* @return a Stream object with all of the subinterfaces |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
210 |
* of this network interface |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
32649
diff
changeset
|
211 |
* @since 9 |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
212 |
*/ |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
213 |
public Stream<NetworkInterface> subInterfaces() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
214 |
return streamFromArray(childs); |
2 | 215 |
} |
216 |
||
217 |
/** |
|
218 |
* Returns the parent NetworkInterface of this interface if this is |
|
19069 | 219 |
* a subinterface, or {@code null} if it is a physical |
2 | 220 |
* (non virtual) interface or has no parent. |
221 |
* |
|
19069 | 222 |
* @return The {@code NetworkInterface} this interface is attached to. |
2 | 223 |
* @since 1.6 |
224 |
*/ |
|
225 |
public NetworkInterface getParent() { |
|
226 |
return parent; |
|
227 |
} |
|
228 |
||
229 |
/** |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
230 |
* 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
|
231 |
* 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
|
232 |
* 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
|
233 |
* machines. |
2 | 234 |
* |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
235 |
* @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
|
236 |
* unknown |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
237 |
* @see #getByIndex(int) |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
238 |
* @since 1.7 |
2 | 239 |
*/ |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
240 |
public int getIndex() { |
2 | 241 |
return index; |
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Get the display name of this network interface. |
|
246 |
* A display name is a human readable String describing the network |
|
247 |
* device. |
|
248 |
* |
|
5293
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
249 |
* @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
|
250 |
* interface, or null if no display name is available. |
2 | 251 |
*/ |
252 |
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
|
253 |
/* strict TCK conformance */ |
be5f9ec7ca3b
6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents:
5180
diff
changeset
|
254 |
return "".equals(displayName) ? null : displayName; |
2 | 255 |
} |
256 |
||
257 |
/** |
|
258 |
* Searches for the network interface with the specified name. |
|
259 |
* |
|
260 |
* @param name |
|
261 |
* The name of the network interface. |
|
262 |
* |
|
19069 | 263 |
* @return A {@code NetworkInterface} with the specified name, |
264 |
* or {@code null} if there is no network interface |
|
2 | 265 |
* with the specified name. |
266 |
* |
|
267 |
* @throws SocketException |
|
268 |
* If an I/O error occurs. |
|
269 |
* |
|
270 |
* @throws NullPointerException |
|
19069 | 271 |
* If the specified name is {@code null}. |
2 | 272 |
*/ |
273 |
public static NetworkInterface getByName(String name) throws SocketException { |
|
274 |
if (name == null) |
|
275 |
throw new NullPointerException(); |
|
276 |
return getByName0(name); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Get a network interface given its index. |
|
281 |
* |
|
282 |
* @param index an integer, the index of the interface |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
283 |
* @return the NetworkInterface obtained from its index, or {@code null} if |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
284 |
* there is no interface with such an index on the system |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
285 |
* @throws SocketException if an I/O error occurs. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
286 |
* @throws IllegalArgumentException if index has a negative value |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
287 |
* @see #getIndex() |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
288 |
* @since 1.7 |
2 | 289 |
*/ |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
290 |
public static NetworkInterface getByIndex(int index) throws SocketException { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
291 |
if (index < 0) |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
292 |
throw new IllegalArgumentException("Interface index can't be negative"); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
293 |
return getByIndex0(index); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
294 |
} |
2 | 295 |
|
296 |
/** |
|
297 |
* Convenience method to search for a network interface that |
|
298 |
* has the specified Internet Protocol (IP) address bound to |
|
299 |
* it. |
|
300 |
* <p> |
|
301 |
* If the specified IP address is bound to multiple network |
|
302 |
* interfaces it is not defined which network interface is |
|
303 |
* returned. |
|
304 |
* |
|
305 |
* @param addr |
|
19069 | 306 |
* The {@code InetAddress} to search with. |
2 | 307 |
* |
19069 | 308 |
* @return A {@code NetworkInterface} |
309 |
* or {@code null} if there is no network interface |
|
2 | 310 |
* with the specified IP address. |
311 |
* |
|
312 |
* @throws SocketException |
|
313 |
* If an I/O error occurs. |
|
314 |
* |
|
315 |
* @throws NullPointerException |
|
19069 | 316 |
* If the specified address is {@code null}. |
2 | 317 |
*/ |
318 |
public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException { |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
319 |
if (addr == null) { |
2 | 320 |
throw new NullPointerException(); |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
321 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
322 |
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
323 |
throw new IllegalArgumentException ("invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1152
diff
changeset
|
324 |
} |
2 | 325 |
return getByInetAddress0(addr); |
326 |
} |
|
327 |
||
328 |
/** |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
329 |
* Returns an {@code Enumeration} of all the interfaces on this machine. The |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
330 |
* {@code Enumeration} contains at least one element, possibly representing |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
331 |
* a loopback interface that only supports communication between entities on |
13374
2a3bf352ef97
7120665: Change Java SE spec so that external networking not required
michaelm
parents:
12559
diff
changeset
|
332 |
* this machine. |
2 | 333 |
* |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
334 |
* @apiNote this method can be used in combination with |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
335 |
* {@link #getInetAddresses()} to obtain all IP addresses for this node |
2 | 336 |
* |
337 |
* @return an Enumeration of NetworkInterfaces found on this machine |
|
338 |
* @exception SocketException if an I/O error occurs. |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
339 |
* @see #networkInterfaces() |
2 | 340 |
*/ |
341 |
public static Enumeration<NetworkInterface> getNetworkInterfaces() |
|
342 |
throws SocketException { |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
343 |
NetworkInterface[] netifs = getAll(); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
344 |
assert netifs != null && netifs.length > 0; |
2 | 345 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
346 |
return enumerationFromArray(netifs); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
347 |
} |
2 | 348 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
349 |
/** |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
350 |
* Returns a {@code Stream} of all the interfaces on this machine. The |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
351 |
* {@code Stream} contains at least one interface, possibly representing a |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
352 |
* loopback interface that only supports communication between entities on |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
353 |
* this machine. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
354 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
355 |
* @apiNote this method can be used in combination with |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
356 |
* {@link #inetAddresses()}} to obtain a stream of all IP addresses for |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
357 |
* this node, for example: |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
358 |
* <pre> {@code |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
359 |
* Stream<InetAddress> addrs = NetworkInterface.networkInterfaces() |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
360 |
* .flatMap(NetworkInterface::inetAddresses); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
361 |
* }</pre> |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
362 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
363 |
* @return a Stream of NetworkInterfaces found on this machine |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
364 |
* @exception SocketException if an I/O error occurs. |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
32649
diff
changeset
|
365 |
* @since 9 |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
366 |
*/ |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
367 |
public static Stream<NetworkInterface> networkInterfaces() |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
368 |
throws SocketException { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
369 |
NetworkInterface[] netifs = getAll(); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
370 |
assert netifs != null && netifs.length > 0; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
371 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
372 |
return streamFromArray(netifs); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
373 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
374 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
375 |
private static <T> Enumeration<T> enumerationFromArray(T[] a) { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
376 |
return new Enumeration<>() { |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
377 |
int i = 0; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
378 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
379 |
@Override |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
380 |
public T nextElement() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
381 |
if (i < a.length) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
382 |
return a[i++]; |
2 | 383 |
} else { |
384 |
throw new NoSuchElementException(); |
|
385 |
} |
|
386 |
} |
|
387 |
||
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
388 |
@Override |
2 | 389 |
public boolean hasMoreElements() { |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
390 |
return i < a.length; |
2 | 391 |
} |
392 |
}; |
|
393 |
} |
|
394 |
||
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
395 |
private static <T> Stream<T> streamFromArray(T[] a) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
396 |
return StreamSupport.stream( |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
397 |
Spliterators.spliterator( |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
398 |
a, |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
399 |
Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL), |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
400 |
false); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
401 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
29986
diff
changeset
|
402 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
403 |
private static native NetworkInterface[] getAll() |
2 | 404 |
throws SocketException; |
405 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
406 |
private static native NetworkInterface getByName0(String name) |
2 | 407 |
throws SocketException; |
408 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
409 |
private static native NetworkInterface getByIndex0(int index) |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
410 |
throws SocketException; |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
715
diff
changeset
|
411 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
412 |
private static native NetworkInterface getByInetAddress0(InetAddress addr) |
2 | 413 |
throws SocketException; |
414 |
||
415 |
/** |
|
416 |
* Returns whether a network interface is up and running. |
|
417 |
* |
|
19069 | 418 |
* @return {@code true} if the interface is up and running. |
2 | 419 |
* @exception SocketException if an I/O error occurs. |
420 |
* @since 1.6 |
|
421 |
*/ |
|
422 |
||
423 |
public boolean isUp() throws SocketException { |
|
424 |
return isUp0(name, index); |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Returns whether a network interface is a loopback interface. |
|
429 |
* |
|
19069 | 430 |
* @return {@code true} if the interface is a loopback interface. |
2 | 431 |
* @exception SocketException if an I/O error occurs. |
432 |
* @since 1.6 |
|
433 |
*/ |
|
434 |
||
435 |
public boolean isLoopback() throws SocketException { |
|
436 |
return isLoopback0(name, index); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Returns whether a network interface is a point to point interface. |
|
441 |
* A typical point to point interface would be a PPP connection through |
|
442 |
* a modem. |
|
443 |
* |
|
19069 | 444 |
* @return {@code true} if the interface is a point to point |
2 | 445 |
* interface. |
446 |
* @exception SocketException if an I/O error occurs. |
|
447 |
* @since 1.6 |
|
448 |
*/ |
|
449 |
||
450 |
public boolean isPointToPoint() throws SocketException { |
|
451 |
return isP2P0(name, index); |
|
452 |
} |
|
453 |
||
454 |
/** |
|
455 |
* Returns whether a network interface supports multicasting or not. |
|
456 |
* |
|
19069 | 457 |
* @return {@code true} if the interface supports Multicasting. |
2 | 458 |
* @exception SocketException if an I/O error occurs. |
459 |
* @since 1.6 |
|
460 |
*/ |
|
461 |
||
462 |
public boolean supportsMulticast() throws SocketException { |
|
463 |
return supportsMulticast0(name, index); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Returns the hardware address (usually MAC) of the interface if it |
|
468 |
* 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
|
469 |
* 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
|
470 |
* the permission {@link NetPermission}("getNetworkInformation"). |
2 | 471 |
* |
19069 | 472 |
* @return a byte array containing the address, or {@code null} if |
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
473 |
* 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
|
474 |
* 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
|
475 |
* NetPermission("getNetworkInformation") |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
476 |
* |
2 | 477 |
* @exception SocketException if an I/O error occurs. |
478 |
* @since 1.6 |
|
479 |
*/ |
|
480 |
public byte[] getHardwareAddress() throws SocketException { |
|
6872
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
481 |
SecurityManager sec = System.getSecurityManager(); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
482 |
if (sec != null) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
483 |
try { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
484 |
sec.checkPermission(new NetPermission("getNetworkInformation")); |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
485 |
} catch (SecurityException e) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
486 |
if (!getInetAddresses().hasMoreElements()) { |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
487 |
// don't have connect permission to any local address |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
488 |
return null; |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
489 |
} |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
490 |
} |
b90e4fee471f
6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents:
5506
diff
changeset
|
491 |
} |
2 | 492 |
for (InetAddress addr : addrs) { |
493 |
if (addr instanceof Inet4Address) { |
|
494 |
return getMacAddr0(((Inet4Address)addr).getAddress(), name, index); |
|
495 |
} |
|
496 |
} |
|
497 |
return getMacAddr0(null, name, index); |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Returns the Maximum Transmission Unit (MTU) of this interface. |
|
502 |
* |
|
503 |
* @return the value of the MTU for that interface. |
|
504 |
* @exception SocketException if an I/O error occurs. |
|
505 |
* @since 1.6 |
|
506 |
*/ |
|
507 |
public int getMTU() throws SocketException { |
|
508 |
return getMTU0(name, index); |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Returns whether this interface is a virtual interface (also called |
|
513 |
* subinterface). |
|
514 |
* Virtual interfaces are, on some systems, interfaces created as a child |
|
515 |
* of a physical interface and given different settings (like address or |
|
516 |
* MTU). Usually the name of the interface will the name of the parent |
|
517 |
* followed by a colon (:) and a number identifying the child since there |
|
518 |
* can be several virtual interfaces attached to a single physical |
|
519 |
* interface. |
|
520 |
* |
|
19069 | 521 |
* @return {@code true} if this interface is a virtual interface. |
2 | 522 |
* @since 1.6 |
523 |
*/ |
|
524 |
public boolean isVirtual() { |
|
525 |
return virtual; |
|
526 |
} |
|
527 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
528 |
private static native boolean isUp0(String name, int ind) throws SocketException; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
529 |
private static native boolean isLoopback0(String name, int ind) throws SocketException; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
530 |
private static native boolean supportsMulticast0(String name, int ind) throws SocketException; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
531 |
private static native boolean isP2P0(String name, int ind) throws SocketException; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
532 |
private static native byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31469
diff
changeset
|
533 |
private static native int getMTU0(String name, int ind) throws SocketException; |
2 | 534 |
|
535 |
/** |
|
536 |
* Compares this object against the specified object. |
|
19069 | 537 |
* The result is {@code true} if and only if the argument is |
538 |
* not {@code null} and it represents the same NetworkInterface |
|
2 | 539 |
* as this object. |
540 |
* <p> |
|
19069 | 541 |
* Two instances of {@code NetworkInterface} represent the same |
2 | 542 |
* NetworkInterface if both name and addrs are the same for both. |
543 |
* |
|
544 |
* @param obj the object to compare against. |
|
19069 | 545 |
* @return {@code true} if the objects are the same; |
546 |
* {@code false} otherwise. |
|
2 | 547 |
* @see java.net.InetAddress#getAddress() |
548 |
*/ |
|
549 |
public boolean equals(Object obj) { |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
550 |
if (!(obj instanceof NetworkInterface)) { |
2 | 551 |
return false; |
552 |
} |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
553 |
NetworkInterface that = (NetworkInterface)obj; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
554 |
if (this.name != null ) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
555 |
if (!this.name.equals(that.name)) { |
2 | 556 |
return false; |
557 |
} |
|
558 |
} else { |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
559 |
if (that.name != null) { |
2 | 560 |
return false; |
561 |
} |
|
562 |
} |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
563 |
|
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
564 |
if (this.addrs == null) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
565 |
return that.addrs == null; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
566 |
} else if (that.addrs == null) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
567 |
return false; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
568 |
} |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
569 |
|
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
570 |
/* Both addrs not null. Compare number of addresses */ |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
571 |
|
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
572 |
if (this.addrs.length != that.addrs.length) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
573 |
return false; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
574 |
} |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
575 |
|
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
576 |
InetAddress[] thatAddrs = that.addrs; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
577 |
int count = thatAddrs.length; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
578 |
|
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
579 |
for (int i=0; i<count; i++) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
580 |
boolean found = false; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
581 |
for (int j=0; j<count; j++) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
582 |
if (addrs[i].equals(thatAddrs[j])) { |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
583 |
found = true; |
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
584 |
break; |
2 | 585 |
} |
586 |
} |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
587 |
if (!found) { |
2 | 588 |
return false; |
589 |
} |
|
590 |
} |
|
591 |
return true; |
|
592 |
} |
|
593 |
||
594 |
public int hashCode() { |
|
7986
350f0f3c453b
7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents:
7668
diff
changeset
|
595 |
return name == null? 0: name.hashCode(); |
2 | 596 |
} |
597 |
||
598 |
public String toString() { |
|
599 |
String result = "name:"; |
|
600 |
result += name == null? "null": name; |
|
601 |
if (displayName != null) { |
|
602 |
result += " (" + displayName + ")"; |
|
603 |
} |
|
604 |
return result; |
|
605 |
} |
|
9828
9c9dd9a1b11b
7013969: NetworkInterface.toString can reveal bindings
chegar
parents:
7986
diff
changeset
|
606 |
|
2 | 607 |
private static native void init(); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
608 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
609 |
/** |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
610 |
* Returns the default network interface of this system |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
611 |
* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
612 |
* @return the default interface |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
613 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
614 |
static NetworkInterface getDefault() { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
615 |
return defaultInterface; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9842
diff
changeset
|
616 |
} |
2 | 617 |
} |