/* * 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.
*/
// 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;
privatestaticboolean 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.
*/ publicstaticboolean isSameInterface(NetworkInterface ni1, NetworkInterface ni2) { if (Objects.equals(ni1, ni2)) returntrue; // 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) returnfalse; if (ni1.getIndex() != ni2.getIndex()) returnfalse; return Objects.equals(ni1.getName(), ni2.getName());
}
publicstaticboolean isTestable(NetworkInterface nif) { if (Platform.isOSX()) { if (nif.getName().contains("awdl")) { returnfalse; // 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")) { returnfalse;
}
} returntrue;
}
// 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)) { returnfalse;
}
if (Platform.isOSX()) { // multicasting may not work on interfaces that only // have link local addresses if (!hasNonLinkLocalAddress(nif)) { returnfalse;
}
}
if (Platform.isOSX()) { // multicasting may not work on interfaces that only // have link local addresses if (!hasNonLinkLocalAddress(nif)) { returnfalse;
}
}
/** * Returns whether IPv6 is available at all. * This should resemble the result of native ipv6_available() in net_util.c
*/ publicboolean isIPv6Available() { return isIPv6Available;
}
/** * Does any (usable) IPv6 address exist in the network configuration?
*/ publicboolean hasTestableIPv6Address() { return has_testableipv6address;
}
/** * Does any site local address exist?
*/ publicboolean hasSiteLocalAddress() { return has_sitelocaladdress;
}
/** * Does any link local address exist?
*/ publicboolean hasLinkLocalAddress() { return has_linklocaladdress;
}
/** * Does any global IPv6 address exist?
*/ publicboolean 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.
*/ publicstatic 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;
}
/** Prints all the system interface information to the give stream. */
@SuppressWarnings("removal") publicstaticvoid 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 ***"); returnnull;
} catch (IOException e) { thrownew UncheckedIOException(e);
}};
AccessController.doPrivileged(pa);
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.