diff --git a/jdk/src/share/classes/java/net/ServerSocket.java b/jdk/src/share/classes/java/net/ServerSocket.java index 887a1e7de5f..2cb24a1f524 100644 --- a/jdk/src/share/classes/java/net/ServerSocket.java +++ b/jdk/src/share/classes/java/net/ServerSocket.java @@ -390,15 +390,29 @@ class ServerSocket implements java.io.Closeable { * If the socket was bound prior to being {@link #close closed}, * then this method will continue to return the local address * after the socket is closed. + *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
*
* @return the address to which this socket is bound,
- * or null
if the socket is unbound.
+ * or the loopback address if denied by the security manager,
+ * or {@code null} if the socket is unbound.
+ *
+ * @see SecurityManager#checkConnect
*/
public InetAddress getInetAddress() {
if (!isBound())
return null;
try {
- return getImpl().getInetAddress();
+ InetAddress in = getImpl().getInetAddress();
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null)
+ sm.checkConnect(in.getHostAddress(), -1);
+ return in;
+ } catch (SecurityException e) {
+ return InetAddress.getLoopbackAddress();
} catch (SocketException e) {
// nothing
// If we're bound, the impl has been created
@@ -431,18 +445,28 @@ class ServerSocket implements java.io.Closeable {
}
/**
- * Returns the address of the endpoint this socket is bound to, or
- * null
if it is not bound yet.
+ * Returns the address of the endpoint this socket is bound to.
*
* If the socket was bound prior to being {@link #close closed}, * then this method will continue to return the address of the endpoint * after the socket is closed. + *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link InetAddress#getLoopbackAddress loopback} address and the local
+ * port to which the socket is bound is returned.
+ *
+ * @return a {@code SocketAddress} representing the local endpoint of
+ * this socket, or a {@code SocketAddress} representing the
+ * loopback address if denied by the security manager,
+ * or {@code null} if the socket is not bound yet.
*
- * @return a SocketAddress
representing the local endpoint of this
- * socket, or null
if it is not bound yet.
* @see #getInetAddress()
* @see #getLocalPort()
* @see #bind(SocketAddress)
+ * @see SecurityManager#checkConnect
* @since 1.4
*/
@@ -708,13 +732,25 @@ class ServerSocket implements java.io.Closeable {
/**
* Returns the implementation address and implementation port of
* this socket as a String
.
+ *
+ * If there is a security manager set, its {@code checkConnect} method is + * called with the local address and {@code -1} as its arguments to see + * if the operation is allowed. If the operation is not allowed, + * an {@code InetAddress} representing the + * {@link InetAddress#getLoopbackAddress loopback} address is returned as + * the implementation address. * * @return a string representation of this socket. */ public String toString() { if (!isBound()) return "ServerSocket[unbound]"; - return "ServerSocket[addr=" + impl.getInetAddress() + + InetAddress in; + if (System.getSecurityManager() != null) + in = InetAddress.getLoopbackAddress(); + else + in = impl.getInetAddress(); + return "ServerSocket[addr=" + in + ",localport=" + impl.getLocalPort() + "]"; } diff --git a/jdk/src/share/classes/java/net/Socket.java b/jdk/src/share/classes/java/net/Socket.java index d4f28e9ab6d..e28e86aade1 100644 --- a/jdk/src/share/classes/java/net/Socket.java +++ b/jdk/src/share/classes/java/net/Socket.java @@ -682,11 +682,18 @@ class Socket implements java.io.Closeable { /** * Gets the local address to which the socket is bound. + *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
*
- * @return the local address to which the socket is bound, or
- * the {@link InetAddress#isAnyLocalAddress wildcard} address
- * if the socket is closed or not bound yet.
+ * @return the local address to which the socket is bound,
+ * the loopback address if denied by the security manager, or
+ * the wildcard address if the socket is closed or not bound yet.
* @since JDK1.1
+ *
+ * @see SecurityManager#checkConnect
*/
public InetAddress getLocalAddress() {
// This is for backward compatibility
@@ -695,9 +702,14 @@ class Socket implements java.io.Closeable {
InetAddress in = null;
try {
in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null)
+ sm.checkConnect(in.getHostAddress(), -1);
if (in.isAnyLocalAddress()) {
in = InetAddress.anyLocalAddress();
}
+ } catch (SecurityException e) {
+ in = InetAddress.getLoopbackAddress();
} catch (Exception e) {
in = InetAddress.anyLocalAddress(); // "0.0.0.0"
}
@@ -770,8 +782,7 @@ class Socket implements java.io.Closeable {
}
/**
- * Returns the address of the endpoint this socket is bound to, or
- * null
if it is not bound yet.
+ * Returns the address of the endpoint this socket is bound to.
*
* If a socket bound to an endpoint represented by an
* InetSocketAddress
is {@link #close closed},
@@ -780,12 +791,23 @@ class Socket implements java.io.Closeable {
* InetSocketAddress
's address is the
* {@link InetAddress#isAnyLocalAddress wildcard} address
* and its port is the local port that it was bound to.
+ *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link InetAddress#getLoopbackAddress loopback} address and the local
+ * port to which this socket is bound is returned.
+ *
+ * @return a {@code SocketAddress} representing the local endpoint of
+ * this socket, or a {@code SocketAddress} representing the
+ * loopback address if denied by the security manager, or
+ * {@code null} if the socket is not bound yet.
*
- * @return a
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
+ * local port of the channel's socket is returned.
+ *
+ * @return The {@code SocketAddress} that the socket is bound to, or the
+ * {@code SocketAddress} representing the loopback address if
+ * denied by the security manager, or {@code null} if the
+ * channel's socket is not bound
+ *
+ * @throws ClosedChannelException {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ @Override
+ public abstract SocketAddress getLocalAddress() throws IOException;
}
diff --git a/jdk/src/share/classes/java/nio/channels/AsynchronousSocketChannel.java b/jdk/src/share/classes/java/nio/channels/AsynchronousSocketChannel.java
index 8cde54dd7ac..74c93c872af 100644
--- a/jdk/src/share/classes/java/nio/channels/AsynchronousSocketChannel.java
+++ b/jdk/src/share/classes/java/nio/channels/AsynchronousSocketChannel.java
@@ -645,4 +645,24 @@ public abstract class AsynchronousSocketChannel
TimeUnit unit,
A attachment,
CompletionHandler
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
+ * local port of the channel's socket is returned.
+ *
+ * @return The {@code SocketAddress} that the socket is bound to, or the
+ * {@code SocketAddress} representing the loopback address if
+ * denied by the security manager, or {@code null} if the
+ * channel's socket is not bound
+ *
+ * @throws ClosedChannelException {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ public abstract SocketAddress getLocalAddress() throws IOException;
}
diff --git a/jdk/src/share/classes/java/nio/channels/DatagramChannel.java b/jdk/src/share/classes/java/nio/channels/DatagramChannel.java
index 74505d8ce63..fd8b920eebd 100644
--- a/jdk/src/share/classes/java/nio/channels/DatagramChannel.java
+++ b/jdk/src/share/classes/java/nio/channels/DatagramChannel.java
@@ -565,4 +565,25 @@ public abstract class DatagramChannel
return write(srcs, 0, srcs.length);
}
+ /**
+ * {@inheritDoc}
+ *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
+ * local port of the channel's socket is returned.
+ *
+ * @return The {@code SocketAddress} that the socket is bound to, or the
+ * {@code SocketAddress} representing the loopback address if
+ * denied by the security manager, or {@code null} if the
+ * channel's socket is not bound
+ *
+ * @throws ClosedChannelException {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ @Override
+ public abstract SocketAddress getLocalAddress() throws IOException;
+
}
diff --git a/jdk/src/share/classes/java/nio/channels/NetworkChannel.java b/jdk/src/share/classes/java/nio/channels/NetworkChannel.java
index b2ed0f24957..3900f9d284b 100644
--- a/jdk/src/share/classes/java/nio/channels/NetworkChannel.java
+++ b/jdk/src/share/classes/java/nio/channels/NetworkChannel.java
@@ -87,8 +87,7 @@ public interface NetworkChannel
NetworkChannel bind(SocketAddress local) throws IOException;
/**
- * Returns the socket address that this channel's socket is bound to, or
- * {@code null} if the socket is not bound.
+ * Returns the socket address that this channel's socket is bound to.
*
* Where the channel is {@link #bind bound} to an Internet Protocol
* socket address then the return value from this method is of type {@link
diff --git a/jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java b/jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java
index 598746d65b5..90e39b529a4 100644
--- a/jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java
+++ b/jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java
@@ -265,4 +265,25 @@ public abstract class ServerSocketChannel
*/
public abstract SocketChannel accept() throws IOException;
+ /**
+ * {@inheritDoc}
+ *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
+ * local port of the channel's socket is returned.
+ *
+ * @return The {@code SocketAddress} that the socket is bound to, or the
+ * {@code SocketAddress} representing the loopback address if
+ * denied by the security manager, or {@code null} if the
+ * channel's socket is not bound
+ *
+ * @throws ClosedChannelException {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ @Override
+ public abstract SocketAddress getLocalAddress() throws IOException;
+
}
diff --git a/jdk/src/share/classes/java/nio/channels/SocketChannel.java b/jdk/src/share/classes/java/nio/channels/SocketChannel.java
index d5f43ba7750..185862cadb6 100644
--- a/jdk/src/share/classes/java/nio/channels/SocketChannel.java
+++ b/jdk/src/share/classes/java/nio/channels/SocketChannel.java
@@ -493,4 +493,25 @@ public abstract class SocketChannel
return write(srcs, 0, srcs.length);
}
+ /**
+ * {@inheritDoc}
+ *
+ * If there is a security manager set, its {@code checkConnect} method is
+ * called with the local address and {@code -1} as its arguments to see
+ * if the operation is allowed. If the operation is not allowed,
+ * a {@code SocketAddress} representing the
+ * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
+ * local port of the channel's socket is returned.
+ *
+ * @return The {@code SocketAddress} that the socket is bound to, or the
+ * {@code SocketAddress} representing the loopback address if
+ * denied by the security manager, or {@code null} if the
+ * channel's socket is not bound
+ *
+ * @throws ClosedChannelException {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ @Override
+ public abstract SocketAddress getLocalAddress() throws IOException;
+
}
diff --git a/jdk/src/share/classes/sun/net/NetworkClient.java b/jdk/src/share/classes/sun/net/NetworkClient.java
index dc4d12b6d4d..87f18c4f74a 100644
--- a/jdk/src/share/classes/sun/net/NetworkClient.java
+++ b/jdk/src/share/classes/sun/net/NetworkClient.java
@@ -200,7 +200,13 @@ public class NetworkClient {
protected InetAddress getLocalAddress() throws IOException {
if (serverSocket == null)
throw new IOException("not connected");
- return serverSocket.getLocalAddress();
+ return AccessController.doPrivileged(
+ new PrivilegedActionSocketAddress
representing the local endpoint of this
- * socket, or null
if it is not bound yet.
* @see #getLocalAddress()
* @see #getLocalPort()
* @see #bind(SocketAddress)
+ * @see SecurityManager#checkConnect
* @since 1.4
*/
diff --git a/jdk/src/share/classes/java/net/SocksSocketImpl.java b/jdk/src/share/classes/java/net/SocksSocketImpl.java
index dec3eb38777..16155d2f115 100644
--- a/jdk/src/share/classes/java/net/SocksSocketImpl.java
+++ b/jdk/src/share/classes/java/net/SocksSocketImpl.java
@@ -28,6 +28,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import sun.net.SocksProxy;
import sun.net.www.ParseUtil;
@@ -590,7 +591,13 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
/* Test for AnyLocal */
InetAddress naddr = baddr;
if (naddr.isAnyLocalAddress()) {
- naddr = cmdsock.getLocalAddress();
+ naddr = AccessController.doPrivileged(
+ new PrivilegedAction