Skip to content

Commit

Permalink
8344218: Remove calls to SecurityManager and and doPrivileged in java…
Browse files Browse the repository at this point in the history
….net.NetworkInterface after JEP 486 integration

Reviewed-by: dfuchs
  • Loading branch information
Eirik Bjørsnøs committed Nov 19, 2024
1 parent d85dd77 commit b12c5b4
Showing 1 changed file with 21 additions and 73 deletions.
94 changes: 21 additions & 73 deletions src/java.base/share/classes/java/net/NetworkInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Spliterator;
Expand Down Expand Up @@ -79,9 +80,9 @@ public final class NetworkInterface {
private String name;
private String displayName;
private int index;
private InetAddress addrs[];
private InterfaceAddress bindings[];
private NetworkInterface childs[];
private InetAddress[] addrs;
private InterfaceAddress[] bindings;
private NetworkInterface[] childs;
private NetworkInterface parent = null;
private boolean virtual = false;
private static final NetworkInterface defaultInterface;
Expand Down Expand Up @@ -118,87 +119,46 @@ public String getName() {
}

/**
* Get an Enumeration with all, or a subset, of the InetAddresses bound to
* this network interface.
* Get an Enumeration of the InetAddresses bound to this network interface.
*
* @implNote
* The returned enumeration contains all, or a subset, of the InetAddresses that were
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
* The returned enumeration contains the InetAddresses that were bound to
* the interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read}
*
* @return an Enumeration object with all, or a subset, of the InetAddresses
* bound to this network interface
* @return an Enumeration object with the InetAddresses bound to this
* network interface
* @see #inetAddresses()
*/
public Enumeration<InetAddress> getInetAddresses() {
return enumerationFromArray(getCheckedInetAddresses());
return enumerationFromArray(addrs);
}

/**
* Get a Stream of all, or a subset, of the InetAddresses bound to this
* network interface.
* Get a Stream of the InetAddresses bound to this network interface.
*
* @implNote
* The stream contains all, or a subset, of the InetAddresses that were
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
* The stream contains the InetAddresses that were bound to the
* interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read}
*
* @return a Stream object with all, or a subset, of the InetAddresses
* bound to this network interface
* @return a Stream object with the InetAddresses bound to this network interface
* @since 9
*/
public Stream<InetAddress> inetAddresses() {
return streamFromArray(getCheckedInetAddresses());
}

private InetAddress[] getCheckedInetAddresses() {
InetAddress[] local_addrs = new InetAddress[addrs.length];
boolean trusted = true;

@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
trusted = false;
}
}
int i = 0;
for (int j = 0; j < addrs.length; j++) {
try {
if (!trusted) {
sec.checkConnect(addrs[j].getHostAddress(), -1);
}
local_addrs[i++] = addrs[j];
} catch (SecurityException e) { }
}
return Arrays.copyOf(local_addrs, i);
return streamFromArray(addrs);
}

/**
* Get a List of all, or a subset, of the {@code InterfaceAddresses}
* of this network interface.
* Get a List of the {@code InterfaceAddresses} of this network interface.
*
* @return a {@code List} object with the InterfaceAddress of this
* network interface
*
* @return a {@code List} object with all, or a subset, of the
* InterfaceAddress of this network interface
* @since 1.6
*/
public java.util.List<InterfaceAddress> getInterfaceAddresses() {
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1);
if (bindings != null) {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
for (int j=0; j<bindings.length; j++) {
try {
if (sec != null) {
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
}
lst.add(bindings[j]);
} catch (SecurityException e) { }
}
}
return lst;
public List<InterfaceAddress> getInterfaceAddresses() {
return bindings == null ? List.of() : List.of(bindings);
}

/**
Expand Down Expand Up @@ -556,18 +516,6 @@ public boolean supportsMulticast() throws SocketException {
* @since 1.6
*/
public byte[] getHardwareAddress() throws SocketException {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
if (!getInetAddresses().hasMoreElements()) {
// don't have connect permission to any local address
return null;
}
}
}
if (isLoopback0(name, index)) {
return null;
}
Expand Down

0 comments on commit b12c5b4

Please sign in to comment.