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