< prev index next >

src/java.base/share/classes/sun/nio/ch/Net.java

Print this page




  37 import java.net.SocketException;
  38 import java.net.SocketOption;
  39 import java.net.StandardProtocolFamily;
  40 import java.net.StandardSocketOptions;
  41 import java.net.UnknownHostException;
  42 import java.nio.channels.AlreadyBoundException;
  43 import java.nio.channels.ClosedChannelException;
  44 import java.nio.channels.NotYetBoundException;
  45 import java.nio.channels.NotYetConnectedException;
  46 import java.nio.channels.UnresolvedAddressException;
  47 import java.nio.channels.UnsupportedAddressTypeException;
  48 import java.security.AccessController;
  49 import java.security.PrivilegedAction;
  50 import java.util.Enumeration;
  51 
  52 import sun.net.ext.ExtendedSocketOptions;
  53 import sun.security.action.GetPropertyAction;
  54 
  55 public class Net {
  56 
  57     private Net() { }
  58 
  59     // unspecified protocol family
  60     static final ProtocolFamily UNSPEC = new ProtocolFamily() {
  61         public String name() {
  62             return "UNSPEC";
  63         }
  64     };
  65 
  66     // set to true if exclusive binding is on for Windows
  67     private static final boolean exclusiveBind;
  68 
  69     // set to true if the fast tcp loopback should be enabled on Windows
  70     private static final boolean fastLoopback;
  71 
  72     // -- Miscellaneous utilities --
  73 
  74     private static volatile boolean checkedIPv6;
  75     private static volatile boolean isIPv6Available;
  76     private static volatile boolean checkedReusePort;
  77     private static volatile boolean isReusePortAvailable;
  78 
  79     /**
  80      * Tells whether dual-IPv4/IPv6 sockets should be used.
  81      */
  82     static boolean isIPv6Available() {
  83         if (!checkedIPv6) {
  84             isIPv6Available = isIPv6Available0();
  85             checkedIPv6 = true;
  86         }
  87         return isIPv6Available;
  88     }
  89 
  90     /**
  91      * Tells whether SO_REUSEPORT is supported.
  92      */
  93     static boolean isReusePortAvailable() {
  94         if (!checkedReusePort) {
  95             isReusePortAvailable = isReusePortAvailable0();
  96             checkedReusePort = true;
  97         }
  98         return isReusePortAvailable;
  99     }
 100 
 101     /**
 102      * Returns true if exclusive binding is on


 179         else
 180             throw new Error("Untranslated exception", nx);
 181     }
 182 
 183     static void translateException(Exception x,
 184                                    boolean unknownHostForUnresolved)
 185         throws IOException
 186     {
 187         if (x instanceof IOException)
 188             throw (IOException)x;
 189         // Throw UnknownHostException from here since it cannot
 190         // be thrown as a SocketException
 191         if (unknownHostForUnresolved &&
 192             (x instanceof UnresolvedAddressException))
 193         {
 194              throw new UnknownHostException();
 195         }
 196         translateToSocketException(x);
 197     }
 198 
 199     static void translateException(Exception x)
 200         throws IOException
 201     {
 202         translateException(x, false);
 203     }
 204 
 205     /**
 206      * Returns the local address after performing a SecurityManager#checkConnect.
 207      */
 208     static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
 209         SecurityManager sm = System.getSecurityManager();
 210         if (addr == null || sm == null)
 211             return addr;
 212 
 213         try{
 214             sm.checkConnect(addr.getAddress().getHostAddress(), -1);
 215             // Security check passed
 216         } catch (SecurityException e) {
 217             // Return loopback address only if security check fails
 218             addr = getLoopbackAddress(addr.getPort());
 219         }
 220         return addr;
 221     }
 222 
 223     static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
 224         return System.getSecurityManager() == null ? addr.toString() :
 225                 getLoopbackAddress(addr.getPort()).toString();
 226     }
 227 
 228     private static InetSocketAddress getLoopbackAddress(int port) {
 229         return new InetSocketAddress(InetAddress.getLoopbackAddress(),
 230                                      port);
 231     }
 232 
 233     /**
 234      * Returns any IPv4 address of the given network interface, or
 235      * null if the interface does not have any IPv4 addresses.
 236      */
 237     static Inet4Address anyInet4Address(final NetworkInterface interf) {
 238         return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
 239             public Inet4Address run() {
 240                 Enumeration<InetAddress> addrs = interf.getInetAddresses();
 241                 while (addrs.hasMoreElements()) {
 242                     InetAddress addr = addrs.nextElement();
 243                     if (addr instanceof Inet4Address) {




  37 import java.net.SocketException;
  38 import java.net.SocketOption;
  39 import java.net.StandardProtocolFamily;
  40 import java.net.StandardSocketOptions;
  41 import java.net.UnknownHostException;
  42 import java.nio.channels.AlreadyBoundException;
  43 import java.nio.channels.ClosedChannelException;
  44 import java.nio.channels.NotYetBoundException;
  45 import java.nio.channels.NotYetConnectedException;
  46 import java.nio.channels.UnresolvedAddressException;
  47 import java.nio.channels.UnsupportedAddressTypeException;
  48 import java.security.AccessController;
  49 import java.security.PrivilegedAction;
  50 import java.util.Enumeration;
  51 
  52 import sun.net.ext.ExtendedSocketOptions;
  53 import sun.security.action.GetPropertyAction;
  54 
  55 public class Net {
  56 
  57     protected Net() { }
  58 
  59     // unspecified protocol family
  60     static final ProtocolFamily UNSPEC = new ProtocolFamily() {
  61         public String name() {
  62             return "UNSPEC";
  63         }
  64     };
  65 
  66     // set to true if exclusive binding is on for Windows
  67     private static final boolean exclusiveBind;
  68 
  69     // set to true if the fast tcp loopback should be enabled on Windows
  70     private static final boolean fastLoopback;
  71 
  72     // -- Miscellaneous utilities --
  73 
  74     private static volatile boolean checkedIPv6;
  75     private static volatile boolean isIPv6Available;
  76     private static volatile boolean checkedReusePort;
  77     private static volatile boolean isReusePortAvailable;
  78 
  79     /**
  80      * Tells whether dual-IPv4/IPv6 sockets should be used.
  81      */
  82     protected static boolean isIPv6Available() {
  83         if (!checkedIPv6) {
  84             isIPv6Available = isIPv6Available0();
  85             checkedIPv6 = true;
  86         }
  87         return isIPv6Available;
  88     }
  89 
  90     /**
  91      * Tells whether SO_REUSEPORT is supported.
  92      */
  93     static boolean isReusePortAvailable() {
  94         if (!checkedReusePort) {
  95             isReusePortAvailable = isReusePortAvailable0();
  96             checkedReusePort = true;
  97         }
  98         return isReusePortAvailable;
  99     }
 100 
 101     /**
 102      * Returns true if exclusive binding is on


 179         else
 180             throw new Error("Untranslated exception", nx);
 181     }
 182 
 183     static void translateException(Exception x,
 184                                    boolean unknownHostForUnresolved)
 185         throws IOException
 186     {
 187         if (x instanceof IOException)
 188             throw (IOException)x;
 189         // Throw UnknownHostException from here since it cannot
 190         // be thrown as a SocketException
 191         if (unknownHostForUnresolved &&
 192             (x instanceof UnresolvedAddressException))
 193         {
 194              throw new UnknownHostException();
 195         }
 196         translateToSocketException(x);
 197     }
 198 
 199     public static void translateException(Exception x)
 200         throws IOException
 201     {
 202         translateException(x, false);
 203     }
 204 
 205     /**
 206      * Returns the local address after performing a SecurityManager#checkConnect.
 207      */
 208     public static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
 209         SecurityManager sm = System.getSecurityManager();
 210         if (addr == null || sm == null)
 211             return addr;
 212 
 213         try{
 214             sm.checkConnect(addr.getAddress().getHostAddress(), -1);
 215             // Security check passed
 216         } catch (SecurityException e) {
 217             // Return loopback address only if security check fails
 218             addr = getLoopbackAddress(addr.getPort());
 219         }
 220         return addr;
 221     }
 222 
 223     public static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
 224         return System.getSecurityManager() == null ? addr.toString() :
 225                 getLoopbackAddress(addr.getPort()).toString();
 226     }
 227 
 228     private static InetSocketAddress getLoopbackAddress(int port) {
 229         return new InetSocketAddress(InetAddress.getLoopbackAddress(),
 230                                      port);
 231     }
 232 
 233     /**
 234      * Returns any IPv4 address of the given network interface, or
 235      * null if the interface does not have any IPv4 addresses.
 236      */
 237     static Inet4Address anyInet4Address(final NetworkInterface interf) {
 238         return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
 239             public Inet4Address run() {
 240                 Enumeration<InetAddress> addrs = interf.getInetAddresses();
 241                 while (addrs.hasMoreElements()) {
 242                     InetAddress addr = addrs.nextElement();
 243                     if (addr instanceof Inet4Address) {


< prev index next >