8225239: Refactor NetworkInterface lookups

Reviewed-by: michaelm, dfuchs, chegar
This commit is contained in:
Claes Redestad 2019-07-05 13:40:29 +02:00
parent eb2818421a
commit 7f1f9a50ae
8 changed files with 417 additions and 144 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2019, 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
@ -25,6 +25,8 @@
package java.net;
import java.io.IOException;
import static java.net.InetAddress.IPv6;
import static java.net.InetAddress.PREFER_IPV6_VALUE;
import static java.net.InetAddress.PREFER_SYSTEM_VALUE;
@ -70,7 +72,7 @@ class Inet6AddressImpl implements InetAddressImpl {
* stack system).
*/
java.util.Enumeration<InetAddress> it = netif.getInetAddresses();
InetAddress inetaddr = null;
InetAddress inetaddr;
while (it.hasMoreElements()) {
inetaddr = it.nextElement();
if (inetaddr.getClass().isInstance(addr)) {
@ -110,20 +112,23 @@ class Inet6AddressImpl implements InetAddressImpl {
boolean preferIPv6Address =
InetAddress.preferIPv6Address == PREFER_IPV6_VALUE ||
InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE;
InetAddress loopback4 = (new Inet4AddressImpl()).loopbackAddress();
InetAddress loopback6 = new Inet6Address("localhost",
new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
// Order the candidate addresses by preference.
InetAddress[] addresses = preferIPv6Address
? new InetAddress[] {loopback6, loopback4}
: new InetAddress[] {loopback4, loopback6};
// In case of failure, default to the preferred address.
loopbackAddress = addresses[0];
// Pick the first candidate address that actually exists.
for (InetAddress address : addresses) {
for (int i = 0; i < 2; i++) {
InetAddress address;
// Order the candidate addresses by preference.
if (i == (preferIPv6Address ? 0 : 1)) {
address = new Inet6Address("localhost",
new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
} else {
address = new Inet4Address("localhost", new byte[]{ 0x7f,0x00,0x00,0x01 });
}
if (i == 0) {
// In case of failure, default to the preferred address.
loopbackAddress = address;
}
try {
if (NetworkInterface.getByInetAddress(address) == null) {
if (!NetworkInterface.isBoundInetAddress(address)) {
continue;
}
} catch (SocketException e) {

View file

@ -290,7 +290,7 @@ class InetAddress implements java.io.Serializable {
}
/* Used to store the name service provider */
private static transient NameService nameService = null;
private static transient NameService nameService;
/**
* Used to store the best available hostname.
@ -305,8 +305,7 @@ class InetAddress implements java.io.Serializable {
* Load net library into runtime, and perform initializations.
*/
static {
String str = java.security.AccessController.doPrivileged(
new GetPropertyAction("java.net.preferIPv6Addresses"));
String str = GetPropertyAction.privilegedGetProperty("java.net.preferIPv6Addresses");
if (str == null) {
preferIPv6Address = PREFER_IPV4_VALUE;
} else if (str.equalsIgnoreCase("true")) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2019, 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
@ -321,17 +321,16 @@ public final class NetworkInterface {
if (addr == null) {
throw new NullPointerException();
}
if (addr instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) addr;
if (inet4Address.holder.family != InetAddress.IPv4) {
if (addr.holder.family == InetAddress.IPv4) {
if (!(addr instanceof Inet4Address)) {
throw new IllegalArgumentException("invalid family type: "
+ inet4Address.holder.family);
+ addr.holder.family);
}
} else if (addr instanceof Inet6Address) {
Inet6Address inet6Address = (Inet6Address) addr;
if (inet6Address.holder.family != InetAddress.IPv6) {
} else if (addr.holder.family == InetAddress.IPv6) {
if (!(addr instanceof Inet6Address)) {
throw new IllegalArgumentException("invalid family type: "
+ inet6Address.holder.family);
+ addr.holder.family);
}
} else {
throw new IllegalArgumentException("invalid address type: " + addr);
@ -394,6 +393,23 @@ public final class NetworkInterface {
}
}
/**
* Checks if the given address is bound to any of the interfaces on this
* machine.
*
* @param addr
* The {@code InetAddress} to search with.
* @return true iff the addr parameter is currently bound to one of
* the interfaces on this machine.
*
* @throws SocketException
* If an I/O error occurs.
*/
/* package-private */ static boolean isBoundInetAddress(InetAddress addr)
throws SocketException {
return boundInetAddress0(addr);
}
private static <T> Enumeration<T> enumerationFromArray(T[] a) {
return new Enumeration<>() {
int i = 0;
@ -431,6 +447,9 @@ public final class NetworkInterface {
private static native NetworkInterface getByIndex0(int index)
throws SocketException;
private static native boolean boundInetAddress0(InetAddress addr)
throws SocketException;
private static native NetworkInterface getByInetAddress0(InetAddress addr)
throws SocketException;