-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
NetworkConfiguration.java
468 lines (421 loc) · 16.5 KB
/
NetworkConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.test.lib;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import static java.net.NetworkInterface.getNetworkInterfaces;
import static java.util.Collections.list;
/**
* Helper class for retrieving network interfaces and local addresses
* suitable for testing.
*/
public class NetworkConfiguration {
private Map<NetworkInterface,List<Inet4Address>> ip4Interfaces;
private Map<NetworkInterface,List<Inet6Address>> ip6Interfaces;
private final boolean isIPv6Available;
private boolean has_testableipv6address = false;
private boolean has_sitelocaladdress = false;
private boolean has_linklocaladdress = false;
private boolean has_globaladdress = false;
private NetworkConfiguration(
Map<NetworkInterface,List<Inet4Address>> ip4Interfaces,
Map<NetworkInterface,List<Inet6Address>> ip6Interfaces) {
this.ip4Interfaces = ip4Interfaces;
this.ip6Interfaces = ip6Interfaces;
// initialize properties that can be queried
isIPv6Available = !ip6Interfaces().collect(Collectors.toList()).isEmpty();
ip6Interfaces().forEach(nif -> {
ip6Addresses(nif)
// On AIX, a configuration with only local or loopback
// addresses does not fully enable IPv6 operations.
// E.g. IPv6 multicasting does not work.
// So, don't set has_testableipv6address if we only find these.
.filter(addr -> Platform.isAix() ?
!(addr.isAnyLocalAddress() || addr.isLoopbackAddress()) : true)
.forEach(ia -> {
has_testableipv6address = true;
if (ia.isLinkLocalAddress()) has_linklocaladdress = true;
if (ia.isSiteLocalAddress()) has_sitelocaladdress = true;
if (!ia.isLinkLocalAddress() &&
!ia.isSiteLocalAddress() &&
!ia.isLoopbackAddress()) {
has_globaladdress = true;
}
});
});
}
private static boolean isIPv6LinkLocal(InetAddress a) {
return Inet6Address.class.isInstance(a) && a.isLinkLocalAddress();
}
/**
* Returns true if the two interfaces point to the same network interface.
*
* @implNote
* This method first looks at whether the two interfaces are
* {@linkplain NetworkInterface#equals(Object) equals}, and if so it returns
* true. Otherwise, it looks at whether the two interfaces have the same
* {@linkplain NetworkInterface#getName() name} and
* {@linkplain NetworkInterface#getIndex() index}, and if so returns true.
* Otherwise, it returns false.
*
* @apiNote
* This method ignores differences in the addresses to which the network
* interfaces are bound, to cater for possible reconfiguration that might
* have happened between the time at which each interface configuration
* was looked up.
*
* @param ni1 A network interface, may be {@code null}
* @param ni2 An other network interface, may be {@code null}
* @return {@code true} if the two network interfaces have the same name
* and index, {@code false} otherwise.
*/
public static boolean isSameInterface(NetworkInterface ni1, NetworkInterface ni2) {
if (Objects.equals(ni1, ni2)) return true;
// Objects equals has taken care of the case where
// ni1 == ni2 so either they are both non-null or only
// one of them is null - in which case they can't be equal.
if (ni1 == null || ni2 == null) return false;
if (ni1.getIndex() != ni2.getIndex()) return false;
return Objects.equals(ni1.getName(), ni2.getName());
}
public static boolean isTestable(NetworkInterface nif) {
if (Platform.isOSX()) {
if (nif.getName().contains("awdl")) {
return false; // exclude awdl
}
// filter out interfaces that only have link-local IPv6 addresses
// on macOS interfaces like 'en6' fall in this category and
// need to be skipped
return nif.inetAddresses()
.filter(Predicate.not(NetworkConfiguration::isIPv6LinkLocal))
.findAny()
.isPresent();
}
if (Platform.isWindows()) {
String dName = nif.getDisplayName();
if (dName != null && dName.contains("Teredo")) {
return false;
}
}
return true;
}
private static boolean isNotLoopback(NetworkInterface nif) {
try {
return !nif.isLoopback();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private boolean hasIp4Addresses(NetworkInterface nif) {
return ip4Interfaces.get(nif).stream().anyMatch(a -> !a.isAnyLocalAddress());
}
private boolean hasIp6Addresses(NetworkInterface nif) {
return ip6Interfaces.get(nif).stream().anyMatch(a -> !a.isAnyLocalAddress());
}
public static boolean hasNonLinkLocalAddress(NetworkInterface nif) {
return nif.inetAddresses()
.filter(Predicate.not(InetAddress::isLinkLocalAddress))
.findAny().isPresent();
}
private boolean supportsIp4Multicast(NetworkInterface nif) {
try {
if (!nif.supportsMulticast()) {
return false;
}
// On AIX there is a bug:
// When IPv6 is enabled on the system, the JDK opens sockets as AF_INET6.
// If there's an interface configured with IPv4 addresses only, it should
// be able to become the network interface for a multicast socket (that
// could be in both, IPv4 or IPv6 space). But both possible setsockopt
// calls for either IPV6_MULTICAST_IF or IP_MULTICAST_IF return
// EADDRNOTAVAIL. So we must skip such interfaces here.
if (Platform.isAix() && isIPv6Available() && !hasIp6Addresses(nif)) {
return false;
}
if (Platform.isOSX()) {
// multicasting may not work on interfaces that only
// have link local addresses
if (!hasNonLinkLocalAddress(nif)) {
return false;
}
}
return hasIp4Addresses(nif);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private boolean supportsIp6Multicast(NetworkInterface nif) {
try {
if (!nif.supportsMulticast()) {
return false;
}
if (Platform.isOSX()) {
// multicasting may not work on interfaces that only
// have link local addresses
if (!hasNonLinkLocalAddress(nif)) {
return false;
}
}
return hasIp6Addresses(nif);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Returns whether IPv6 is available at all.
* This should resemble the result of native ipv6_available() in net_util.c
*/
public boolean isIPv6Available() {
return isIPv6Available;
}
/**
* Does any (usable) IPv6 address exist in the network configuration?
*/
public boolean hasTestableIPv6Address() {
return has_testableipv6address;
}
/**
* Does any site local address exist?
*/
public boolean hasSiteLocalAddress() {
return has_sitelocaladdress;
}
/**
* Does any link local address exist?
*/
public boolean hasLinkLocalAddress() {
return has_linklocaladdress;
}
/**
* Does any global IPv6 address exist?
*/
public boolean has_globaladdress() {
return has_globaladdress;
}
/**
* Returns a stream of interfaces suitable for functional tests.
*/
public Stream<NetworkInterface> interfaces() {
return Stream.concat(ip4Interfaces(), ip6Interfaces())
.distinct();
}
/**
* Returns a stream of interfaces suitable for IPv4 functional tests.
*/
public Stream<NetworkInterface> ip4Interfaces() {
return ip4Interfaces.keySet()
.stream()
.filter(NetworkConfiguration::isTestable)
.filter(this::hasIp4Addresses);
}
/**
* Returns a stream of interfaces suitable for IPv6 functional tests.
*/
public Stream<NetworkInterface> ip6Interfaces() {
return ip6Interfaces.keySet()
.stream()
.filter(NetworkConfiguration::isTestable)
.filter(this::hasIp6Addresses);
}
/**
* Returns a stream of interfaces suitable for functional tests.
*/
public Stream<NetworkInterface> multicastInterfaces(boolean includeLoopback) {
return Stream
.concat(ip4MulticastInterfaces(includeLoopback),
ip6MulticastInterfaces(includeLoopback))
.distinct();
}
/**
* Returns a stream of interfaces suitable for IPv4 multicast tests.
*
* The loopback interface will not be included.
*/
public Stream<NetworkInterface> ip4MulticastInterfaces() {
return ip4MulticastInterfaces(false);
}
/**
* Returns a stream of interfaces suitable for IPv4 multicast tests.
*/
public Stream<NetworkInterface> ip4MulticastInterfaces(boolean includeLoopback) {
return (includeLoopback) ?
ip4Interfaces().filter(this::supportsIp4Multicast) :
ip4Interfaces().filter(this::supportsIp4Multicast)
.filter(NetworkConfiguration::isNotLoopback);
}
/**
* Returns a stream of interfaces suitable for IPv6 multicast tests.
*
* The loopback interface will not be included.
*/
public Stream<NetworkInterface> ip6MulticastInterfaces() {
return ip6MulticastInterfaces(false);
}
/**
* Returns a stream of interfaces suitable for IPv6 multicast tests.
*/
public Stream<NetworkInterface> ip6MulticastInterfaces(boolean includeLoopback) {
return (includeLoopback) ?
ip6Interfaces().filter(this::supportsIp6Multicast) :
ip6Interfaces().filter(this::supportsIp6Multicast)
.filter(NetworkConfiguration::isNotLoopback);
}
/**
* Returns all addresses on all "functional" interfaces.
*/
public Stream<InetAddress> addresses(NetworkInterface nif) {
return Stream.concat(ip4Interfaces.get(nif).stream(),
ip6Interfaces.get(nif).stream());
}
/**
* Returns all IPv4 addresses on all "functional" interfaces.
*/
public Stream<Inet4Address> ip4Addresses() {
return ip4Interfaces().flatMap(this::ip4Addresses);
}
/**
* Returns all IPv6 addresses on all "functional" interfaces.
*/
public Stream<Inet6Address> ip6Addresses() {
return ip6Interfaces().flatMap(this::ip6Addresses);
}
/**
* Returns all IPv4 addresses the given interface.
*/
public Stream<Inet4Address> ip4Addresses(NetworkInterface nif) {
return ip4Interfaces.get(nif).stream();
}
/**
* Returns all IPv6 addresses for the given interface.
*/
public Stream<Inet6Address> ip6Addresses(NetworkInterface nif) {
return ip6Interfaces.get(nif).stream();
}
@Override
public String toString() {
return interfaces().map(NetworkConfiguration::interfaceInformation)
.collect(Collectors.joining());
}
/**
* Return a NetworkConfiguration instance.
*/
public static NetworkConfiguration probe() throws IOException {
Map<NetworkInterface, List<Inet4Address>> ip4Interfaces = new LinkedHashMap<>();
Map<NetworkInterface, List<Inet6Address>> ip6Interfaces = new LinkedHashMap<>();
List<NetworkInterface> nifs = list(getNetworkInterfaces());
for (NetworkInterface nif : nifs) {
// ignore interfaces that are down
if (!nif.isUp() || nif.isPointToPoint()) {
continue;
}
List<Inet4Address> ip4Addresses = new LinkedList<>();
List<Inet6Address> ip6Addresses = new LinkedList<>();
ip4Interfaces.put(nif, ip4Addresses);
ip6Interfaces.put(nif, ip6Addresses);
for (InetAddress addr : list(nif.getInetAddresses())) {
if (addr instanceof Inet4Address) {
ip4Addresses.add((Inet4Address) addr);
} else if (addr instanceof Inet6Address) {
ip6Addresses.add((Inet6Address) addr);
}
}
}
return new NetworkConfiguration(ip4Interfaces, ip6Interfaces);
}
/** Returns detailed information for the given interface. */
public static String interfaceInformation(NetworkInterface nif) {
StringBuilder sb = new StringBuilder();
try {
sb.append("Display name: ")
.append(nif.getDisplayName())
.append("\n");
sb.append("Name: ")
.append(nif.getName())
.append("\n");
for (InetAddress inetAddress : list(nif.getInetAddresses())) {
sb.append("InetAddress: ")
.append(inetAddress)
.append("\n");
}
sb.append("Up? ")
.append(nif.isUp())
.append("\n");
sb.append("Loopback? ")
.append(nif.isLoopback())
.append("\n");
sb.append("PointToPoint? ")
.append(nif.isPointToPoint())
.append("\n");
sb.append("Supports multicast? ")
.append(nif.supportsMulticast())
.append("\n");
sb.append("Virtual? ")
.append(nif.isVirtual())
.append("\n");
sb.append("Hardware address: ")
.append(Arrays.toString(nif.getHardwareAddress()))
.append("\n");
sb.append("MTU: ")
.append(nif.getMTU())
.append("\n");
sb.append("Index: ")
.append(nif.getIndex())
.append("\n");
sb.append("\n");
return sb.toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/** Prints all the system interface information to the give stream. */
public static void printSystemConfiguration(PrintStream out) {
PrivilegedAction<Void> pa = () -> {
try {
out.println("*** all system network interface configuration ***");
for (NetworkInterface nif : list(getNetworkInterfaces())) {
out.print(interfaceInformation(nif));
}
out.println("*** end ***");
return null;
} catch (IOException e) {
throw new UncheckedIOException(e);
}};
AccessController.doPrivileged(pa);
}
}