jdk/src/share/classes/sun/nio/ch/Net.java
changeset 18212 22f8c33b0690
parent 18192 fa6bd0992104
child 19607 bee007586d06
equal deleted inserted replaced
18211:74aeb4741e3d 18212:22f8c33b0690
    29 import java.net.*;
    29 import java.net.*;
    30 import java.nio.channels.*;
    30 import java.nio.channels.*;
    31 import java.util.*;
    31 import java.util.*;
    32 import java.security.AccessController;
    32 import java.security.AccessController;
    33 import java.security.PrivilegedAction;
    33 import java.security.PrivilegedAction;
       
    34 import java.security.PrivilegedExceptionAction;
    34 
    35 
    35 
    36 
    36 public class Net {
    37 public class Net {
    37 
    38 
    38     private Net() { }
    39     private Net() { }
   181     {
   182     {
   182         translateException(x, false);
   183         translateException(x, false);
   183     }
   184     }
   184 
   185 
   185     /**
   186     /**
       
   187      * Returns the local address after performing a SecurityManager#checkConnect.
       
   188      */
       
   189     static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
       
   190         SecurityManager sm = System.getSecurityManager();
       
   191         if (addr == null || sm == null)
       
   192             return addr;
       
   193 
       
   194         try{
       
   195             sm.checkConnect(addr.getAddress().getHostAddress(), -1);
       
   196             // Security check passed
       
   197         } catch (SecurityException e) {
       
   198             // Return loopback address only if security check fails
       
   199             addr = getLoopbackAddress(addr.getPort());
       
   200         }
       
   201         return addr;
       
   202     }
       
   203 
       
   204     static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
       
   205         return System.getSecurityManager() == null ? addr.toString() :
       
   206                 getLoopbackAddress(addr.getPort()).toString();
       
   207     }
       
   208 
       
   209     private static InetSocketAddress getLoopbackAddress(int port) {
       
   210         return new InetSocketAddress(InetAddress.getLoopbackAddress(),
       
   211                                      port);
       
   212     }
       
   213 
       
   214     /**
   186      * Returns any IPv4 address of the given network interface, or
   215      * Returns any IPv4 address of the given network interface, or
   187      * null if the interface does not have any IPv4 addresses.
   216      * null if the interface does not have any IPv4 addresses.
   188      */
   217      */
   189     static Inet4Address anyInet4Address(final NetworkInterface interf) {
   218     static Inet4Address anyInet4Address(final NetworkInterface interf) {
   190         return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
   219         return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {