< prev index next >

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

Print this page




  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


 133             throw new IllegalArgumentException("Invalid address type");
 134         return isa;
 135     }
 136 
 137     static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
 138         InetSocketAddress isa = checkAddress(sa);
 139         if (family == StandardProtocolFamily.INET) {
 140             InetAddress addr = isa.getAddress();
 141             if (!(addr instanceof Inet4Address))
 142                 throw new UnsupportedAddressTypeException();
 143         }
 144         return isa;
 145     }
 146 
 147     static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
 148         if (!(sa instanceof InetSocketAddress))
 149             throw new UnsupportedAddressTypeException();
 150         return (InetSocketAddress)sa;
 151     }
 152 
 153     static void translateToSocketException(Exception x)
 154         throws SocketException
 155     {
 156         if (x instanceof SocketException)
 157             throw (SocketException)x;
 158         Exception nx = x;
 159         if (x instanceof ClosedChannelException)
 160             nx = new SocketException("Socket is closed");
 161         else if (x instanceof NotYetConnectedException)
 162             nx = new SocketException("Socket is not connected");
 163         else if (x instanceof AlreadyBoundException)
 164             nx = new SocketException("Already bound");
 165         else if (x instanceof NotYetBoundException)
 166             nx = new SocketException("Socket is not bound yet");
 167         else if (x instanceof UnsupportedAddressTypeException)
 168             nx = new SocketException("Unsupported address type");
 169         else if (x instanceof UnresolvedAddressException) {
 170             nx = new SocketException("Unresolved address");
 171         }
 172         if (nx != x)
 173             nx.initCause(x);
 174 
 175         if (nx instanceof SocketException)
 176             throw (SocketException)nx;
 177         else if (nx instanceof RuntimeException)
 178             throw (RuntimeException)nx;
 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) {




  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     public 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


 133             throw new IllegalArgumentException("Invalid address type");
 134         return isa;
 135     }
 136 
 137     static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
 138         InetSocketAddress isa = checkAddress(sa);
 139         if (family == StandardProtocolFamily.INET) {
 140             InetAddress addr = isa.getAddress();
 141             if (!(addr instanceof Inet4Address))
 142                 throw new UnsupportedAddressTypeException();
 143         }
 144         return isa;
 145     }
 146 
 147     static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
 148         if (!(sa instanceof InetSocketAddress))
 149             throw new UnsupportedAddressTypeException();
 150         return (InetSocketAddress)sa;
 151     }
 152 
 153     public static void translateToSocketException(Exception x)
 154         throws SocketException
 155     {
 156         if (x instanceof SocketException)
 157             throw (SocketException)x;
 158         Exception nx = x;
 159         if (x instanceof ClosedChannelException)
 160             nx = new SocketException("Socket is closed");
 161         else if (x instanceof NotYetConnectedException)
 162             nx = new SocketException("Socket is not connected");
 163         else if (x instanceof AlreadyBoundException)
 164             nx = new SocketException("Already bound");
 165         else if (x instanceof NotYetBoundException)
 166             nx = new SocketException("Socket is not bound yet");
 167         else if (x instanceof UnsupportedAddressTypeException)
 168             nx = new SocketException("Unsupported address type");
 169         else if (x instanceof UnresolvedAddressException) {
 170             nx = new SocketException("Unresolved address");
 171         }
 172         if (nx != x)
 173             nx.initCause(x);
 174 
 175         if (nx instanceof SocketException)
 176             throw (SocketException)nx;
 177         else if (nx instanceof RuntimeException)
 178             throw (RuntimeException)nx;
 179         else
 180             throw new Error("Untranslated exception", nx);
 181     }
 182 
 183     public 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 >