src/java.base/share/classes/sun/nio/ch/Net.java
changeset 47216 71c04702a3d5
parent 47020 2c55106dc37b
child 49001 ce06058197a4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.io.*;
       
    29 import java.net.*;
       
    30 import java.nio.channels.*;
       
    31 import java.util.*;
       
    32 import java.security.AccessController;
       
    33 import java.security.PrivilegedAction;
       
    34 import sun.net.ext.ExtendedSocketOptions;
       
    35 import sun.security.action.GetPropertyAction;
       
    36 
       
    37 public class Net {
       
    38 
       
    39     private Net() { }
       
    40 
       
    41     // unspecified protocol family
       
    42     static final ProtocolFamily UNSPEC = new ProtocolFamily() {
       
    43         public String name() {
       
    44             return "UNSPEC";
       
    45         }
       
    46     };
       
    47 
       
    48     // set to true if exclusive binding is on for Windows
       
    49     private static final boolean exclusiveBind;
       
    50 
       
    51     // set to true if the fast tcp loopback should be enabled on Windows
       
    52     private static final boolean fastLoopback;
       
    53 
       
    54     // -- Miscellaneous utilities --
       
    55 
       
    56     private static volatile boolean checkedIPv6;
       
    57     private static volatile boolean isIPv6Available;
       
    58     private static volatile boolean checkedReusePort;
       
    59     private static volatile boolean isReusePortAvailable;
       
    60 
       
    61     /**
       
    62      * Tells whether dual-IPv4/IPv6 sockets should be used.
       
    63      */
       
    64     static boolean isIPv6Available() {
       
    65         if (!checkedIPv6) {
       
    66             isIPv6Available = isIPv6Available0();
       
    67             checkedIPv6 = true;
       
    68         }
       
    69         return isIPv6Available;
       
    70     }
       
    71 
       
    72     /**
       
    73      * Tells whether SO_REUSEPORT is supported.
       
    74      */
       
    75     static boolean isReusePortAvailable() {
       
    76         if (!checkedReusePort) {
       
    77             isReusePortAvailable = isReusePortAvailable0();
       
    78             checkedReusePort = true;
       
    79         }
       
    80         return isReusePortAvailable;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Returns true if exclusive binding is on
       
    85      */
       
    86     static boolean useExclusiveBind() {
       
    87         return exclusiveBind;
       
    88     }
       
    89 
       
    90     /**
       
    91      * Tells whether IPv6 sockets can join IPv4 multicast groups
       
    92      */
       
    93     static boolean canIPv6SocketJoinIPv4Group() {
       
    94         return canIPv6SocketJoinIPv4Group0();
       
    95     }
       
    96 
       
    97     /**
       
    98      * Tells whether {@link #join6} can be used to join an IPv4
       
    99      * multicast group (IPv4 group as IPv4-mapped IPv6 address)
       
   100      */
       
   101     static boolean canJoin6WithIPv4Group() {
       
   102         return canJoin6WithIPv4Group0();
       
   103     }
       
   104 
       
   105     public static InetSocketAddress checkAddress(SocketAddress sa) {
       
   106         if (sa == null)
       
   107             throw new NullPointerException();
       
   108         if (!(sa instanceof InetSocketAddress))
       
   109             throw new UnsupportedAddressTypeException(); // ## needs arg
       
   110         InetSocketAddress isa = (InetSocketAddress)sa;
       
   111         if (isa.isUnresolved())
       
   112             throw new UnresolvedAddressException(); // ## needs arg
       
   113         InetAddress addr = isa.getAddress();
       
   114         if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))
       
   115             throw new IllegalArgumentException("Invalid address type");
       
   116         return isa;
       
   117     }
       
   118 
       
   119     static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
       
   120         if (!(sa instanceof InetSocketAddress))
       
   121             throw new UnsupportedAddressTypeException();
       
   122         return (InetSocketAddress)sa;
       
   123     }
       
   124 
       
   125     static void translateToSocketException(Exception x)
       
   126         throws SocketException
       
   127     {
       
   128         if (x instanceof SocketException)
       
   129             throw (SocketException)x;
       
   130         Exception nx = x;
       
   131         if (x instanceof ClosedChannelException)
       
   132             nx = new SocketException("Socket is closed");
       
   133         else if (x instanceof NotYetConnectedException)
       
   134             nx = new SocketException("Socket is not connected");
       
   135         else if (x instanceof AlreadyBoundException)
       
   136             nx = new SocketException("Already bound");
       
   137         else if (x instanceof NotYetBoundException)
       
   138             nx = new SocketException("Socket is not bound yet");
       
   139         else if (x instanceof UnsupportedAddressTypeException)
       
   140             nx = new SocketException("Unsupported address type");
       
   141         else if (x instanceof UnresolvedAddressException) {
       
   142             nx = new SocketException("Unresolved address");
       
   143         }
       
   144         if (nx != x)
       
   145             nx.initCause(x);
       
   146 
       
   147         if (nx instanceof SocketException)
       
   148             throw (SocketException)nx;
       
   149         else if (nx instanceof RuntimeException)
       
   150             throw (RuntimeException)nx;
       
   151         else
       
   152             throw new Error("Untranslated exception", nx);
       
   153     }
       
   154 
       
   155     static void translateException(Exception x,
       
   156                                    boolean unknownHostForUnresolved)
       
   157         throws IOException
       
   158     {
       
   159         if (x instanceof IOException)
       
   160             throw (IOException)x;
       
   161         // Throw UnknownHostException from here since it cannot
       
   162         // be thrown as a SocketException
       
   163         if (unknownHostForUnresolved &&
       
   164             (x instanceof UnresolvedAddressException))
       
   165         {
       
   166              throw new UnknownHostException();
       
   167         }
       
   168         translateToSocketException(x);
       
   169     }
       
   170 
       
   171     static void translateException(Exception x)
       
   172         throws IOException
       
   173     {
       
   174         translateException(x, false);
       
   175     }
       
   176 
       
   177     /**
       
   178      * Returns the local address after performing a SecurityManager#checkConnect.
       
   179      */
       
   180     static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
       
   181         SecurityManager sm = System.getSecurityManager();
       
   182         if (addr == null || sm == null)
       
   183             return addr;
       
   184 
       
   185         try{
       
   186             sm.checkConnect(addr.getAddress().getHostAddress(), -1);
       
   187             // Security check passed
       
   188         } catch (SecurityException e) {
       
   189             // Return loopback address only if security check fails
       
   190             addr = getLoopbackAddress(addr.getPort());
       
   191         }
       
   192         return addr;
       
   193     }
       
   194 
       
   195     static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
       
   196         return System.getSecurityManager() == null ? addr.toString() :
       
   197                 getLoopbackAddress(addr.getPort()).toString();
       
   198     }
       
   199 
       
   200     private static InetSocketAddress getLoopbackAddress(int port) {
       
   201         return new InetSocketAddress(InetAddress.getLoopbackAddress(),
       
   202                                      port);
       
   203     }
       
   204 
       
   205     /**
       
   206      * Returns any IPv4 address of the given network interface, or
       
   207      * null if the interface does not have any IPv4 addresses.
       
   208      */
       
   209     static Inet4Address anyInet4Address(final NetworkInterface interf) {
       
   210         return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
       
   211             public Inet4Address run() {
       
   212                 Enumeration<InetAddress> addrs = interf.getInetAddresses();
       
   213                 while (addrs.hasMoreElements()) {
       
   214                     InetAddress addr = addrs.nextElement();
       
   215                     if (addr instanceof Inet4Address) {
       
   216                         return (Inet4Address)addr;
       
   217                     }
       
   218                 }
       
   219                 return null;
       
   220             }
       
   221         });
       
   222     }
       
   223 
       
   224     /**
       
   225      * Returns an IPv4 address as an int.
       
   226      */
       
   227     static int inet4AsInt(InetAddress ia) {
       
   228         if (ia instanceof Inet4Address) {
       
   229             byte[] addr = ia.getAddress();
       
   230             int address  = addr[3] & 0xFF;
       
   231             address |= ((addr[2] << 8) & 0xFF00);
       
   232             address |= ((addr[1] << 16) & 0xFF0000);
       
   233             address |= ((addr[0] << 24) & 0xFF000000);
       
   234             return address;
       
   235         }
       
   236         throw new AssertionError("Should not reach here");
       
   237     }
       
   238 
       
   239     /**
       
   240      * Returns an InetAddress from the given IPv4 address
       
   241      * represented as an int.
       
   242      */
       
   243     static InetAddress inet4FromInt(int address) {
       
   244         byte[] addr = new byte[4];
       
   245         addr[0] = (byte) ((address >>> 24) & 0xFF);
       
   246         addr[1] = (byte) ((address >>> 16) & 0xFF);
       
   247         addr[2] = (byte) ((address >>> 8) & 0xFF);
       
   248         addr[3] = (byte) (address & 0xFF);
       
   249         try {
       
   250             return InetAddress.getByAddress(addr);
       
   251         } catch (UnknownHostException uhe) {
       
   252             throw new AssertionError("Should not reach here");
       
   253         }
       
   254     }
       
   255 
       
   256     /**
       
   257      * Returns an IPv6 address as a byte array
       
   258      */
       
   259     static byte[] inet6AsByteArray(InetAddress ia) {
       
   260         if (ia instanceof Inet6Address) {
       
   261             return ia.getAddress();
       
   262         }
       
   263 
       
   264         // need to construct IPv4-mapped address
       
   265         if (ia instanceof Inet4Address) {
       
   266             byte[] ip4address = ia.getAddress();
       
   267             byte[] address = new byte[16];
       
   268             address[10] = (byte)0xff;
       
   269             address[11] = (byte)0xff;
       
   270             address[12] = ip4address[0];
       
   271             address[13] = ip4address[1];
       
   272             address[14] = ip4address[2];
       
   273             address[15] = ip4address[3];
       
   274             return address;
       
   275         }
       
   276 
       
   277         throw new AssertionError("Should not reach here");
       
   278     }
       
   279 
       
   280     // -- Socket options
       
   281 
       
   282     static final ExtendedSocketOptions extendedOptions =
       
   283             ExtendedSocketOptions.getInstance();
       
   284 
       
   285     static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
       
   286                                 SocketOption<?> name, Object value)
       
   287         throws IOException
       
   288     {
       
   289         if (value == null)
       
   290             throw new IllegalArgumentException("Invalid option value");
       
   291 
       
   292         // only simple values supported by this method
       
   293         Class<?> type = name.type();
       
   294 
       
   295         if (extendedOptions.isOptionSupported(name)) {
       
   296             extendedOptions.setOption(fd, name, value);
       
   297             return;
       
   298         }
       
   299 
       
   300         if (type != Integer.class && type != Boolean.class)
       
   301             throw new AssertionError("Should not reach here");
       
   302 
       
   303         // special handling
       
   304         if (name == StandardSocketOptions.SO_RCVBUF ||
       
   305             name == StandardSocketOptions.SO_SNDBUF)
       
   306         {
       
   307             int i = ((Integer)value).intValue();
       
   308             if (i < 0)
       
   309                 throw new IllegalArgumentException("Invalid send/receive buffer size");
       
   310         }
       
   311         if (name == StandardSocketOptions.SO_LINGER) {
       
   312             int i = ((Integer)value).intValue();
       
   313             if (i < 0)
       
   314                 value = Integer.valueOf(-1);
       
   315             if (i > 65535)
       
   316                 value = Integer.valueOf(65535);
       
   317         }
       
   318         if (name == StandardSocketOptions.IP_TOS) {
       
   319             int i = ((Integer)value).intValue();
       
   320             if (i < 0 || i > 255)
       
   321                 throw new IllegalArgumentException("Invalid IP_TOS value");
       
   322         }
       
   323         if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
       
   324             int i = ((Integer)value).intValue();
       
   325             if (i < 0 || i > 255)
       
   326                 throw new IllegalArgumentException("Invalid TTL/hop value");
       
   327         }
       
   328 
       
   329         // map option name to platform level/name
       
   330         OptionKey key = SocketOptionRegistry.findOption(name, family);
       
   331         if (key == null)
       
   332             throw new AssertionError("Option not found");
       
   333 
       
   334         int arg;
       
   335         if (type == Integer.class) {
       
   336             arg = ((Integer)value).intValue();
       
   337         } else {
       
   338             boolean b = ((Boolean)value).booleanValue();
       
   339             arg = (b) ? 1 : 0;
       
   340         }
       
   341 
       
   342         boolean mayNeedConversion = (family == UNSPEC);
       
   343         boolean isIPv6 = (family == StandardProtocolFamily.INET6);
       
   344         setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
       
   345     }
       
   346 
       
   347     static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
       
   348                                   SocketOption<?> name)
       
   349         throws IOException
       
   350     {
       
   351         Class<?> type = name.type();
       
   352 
       
   353         if (extendedOptions.isOptionSupported(name)) {
       
   354             return extendedOptions.getOption(fd, name);
       
   355         }
       
   356 
       
   357         // only simple values supported by this method
       
   358         if (type != Integer.class && type != Boolean.class)
       
   359             throw new AssertionError("Should not reach here");
       
   360 
       
   361         // map option name to platform level/name
       
   362         OptionKey key = SocketOptionRegistry.findOption(name, family);
       
   363         if (key == null)
       
   364             throw new AssertionError("Option not found");
       
   365 
       
   366         boolean mayNeedConversion = (family == UNSPEC);
       
   367         int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
       
   368 
       
   369         if (type == Integer.class) {
       
   370             return Integer.valueOf(value);
       
   371         } else {
       
   372             return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
       
   373         }
       
   374     }
       
   375 
       
   376     public static boolean isFastTcpLoopbackRequested() {
       
   377         String loopbackProp = GetPropertyAction
       
   378                 .privilegedGetProperty("jdk.net.useFastTcpLoopback");
       
   379         boolean enable;
       
   380         if ("".equals(loopbackProp)) {
       
   381             enable = true;
       
   382         } else {
       
   383             enable = Boolean.parseBoolean(loopbackProp);
       
   384         }
       
   385         return enable;
       
   386     }
       
   387 
       
   388     // -- Socket operations --
       
   389 
       
   390     private static native boolean isIPv6Available0();
       
   391 
       
   392     private static native boolean isReusePortAvailable0();
       
   393 
       
   394     /*
       
   395      * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
       
   396      */
       
   397     private static native int isExclusiveBindAvailable();
       
   398 
       
   399     private static native boolean canIPv6SocketJoinIPv4Group0();
       
   400 
       
   401     private static native boolean canJoin6WithIPv4Group0();
       
   402 
       
   403     static FileDescriptor socket(boolean stream) throws IOException {
       
   404         return socket(UNSPEC, stream);
       
   405     }
       
   406 
       
   407     static FileDescriptor socket(ProtocolFamily family, boolean stream)
       
   408         throws IOException {
       
   409         boolean preferIPv6 = isIPv6Available() &&
       
   410             (family != StandardProtocolFamily.INET);
       
   411         return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
       
   412     }
       
   413 
       
   414     static FileDescriptor serverSocket(boolean stream) {
       
   415         return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
       
   416     }
       
   417 
       
   418     // Due to oddities SO_REUSEADDR on windows reuse is ignored
       
   419     private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,
       
   420                                       boolean fastLoopback);
       
   421 
       
   422     public static void bind(FileDescriptor fd, InetAddress addr, int port)
       
   423         throws IOException
       
   424     {
       
   425         bind(UNSPEC, fd, addr, port);
       
   426     }
       
   427 
       
   428     static void bind(ProtocolFamily family, FileDescriptor fd,
       
   429                      InetAddress addr, int port) throws IOException
       
   430     {
       
   431         boolean preferIPv6 = isIPv6Available() &&
       
   432             (family != StandardProtocolFamily.INET);
       
   433         bind0(fd, preferIPv6, exclusiveBind, addr, port);
       
   434     }
       
   435 
       
   436     private static native void bind0(FileDescriptor fd, boolean preferIPv6,
       
   437                                      boolean useExclBind, InetAddress addr,
       
   438                                      int port)
       
   439         throws IOException;
       
   440 
       
   441     static native void listen(FileDescriptor fd, int backlog) throws IOException;
       
   442 
       
   443     static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
       
   444         throws IOException
       
   445     {
       
   446         return connect(UNSPEC, fd, remote, remotePort);
       
   447     }
       
   448 
       
   449     static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
       
   450         throws IOException
       
   451     {
       
   452         boolean preferIPv6 = isIPv6Available() &&
       
   453             (family != StandardProtocolFamily.INET);
       
   454         return connect0(preferIPv6, fd, remote, remotePort);
       
   455     }
       
   456 
       
   457     private static native int connect0(boolean preferIPv6,
       
   458                                        FileDescriptor fd,
       
   459                                        InetAddress remote,
       
   460                                        int remotePort)
       
   461         throws IOException;
       
   462 
       
   463 
       
   464     public static final int SHUT_RD = 0;
       
   465     public static final int SHUT_WR = 1;
       
   466     public static final int SHUT_RDWR = 2;
       
   467 
       
   468     static native void shutdown(FileDescriptor fd, int how) throws IOException;
       
   469 
       
   470     private static native int localPort(FileDescriptor fd)
       
   471         throws IOException;
       
   472 
       
   473     private static native InetAddress localInetAddress(FileDescriptor fd)
       
   474         throws IOException;
       
   475 
       
   476     public static InetSocketAddress localAddress(FileDescriptor fd)
       
   477         throws IOException
       
   478     {
       
   479         return new InetSocketAddress(localInetAddress(fd), localPort(fd));
       
   480     }
       
   481 
       
   482     private static native int remotePort(FileDescriptor fd)
       
   483         throws IOException;
       
   484 
       
   485     private static native InetAddress remoteInetAddress(FileDescriptor fd)
       
   486         throws IOException;
       
   487 
       
   488     static InetSocketAddress remoteAddress(FileDescriptor fd)
       
   489         throws IOException
       
   490     {
       
   491         return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
       
   492     }
       
   493 
       
   494     private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
       
   495                                             int level, int opt)
       
   496         throws IOException;
       
   497 
       
   498     private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,
       
   499                                              int level, int opt, int arg, boolean isIPv6)
       
   500         throws IOException;
       
   501 
       
   502     static native int poll(FileDescriptor fd, int events, long timeout)
       
   503         throws IOException;
       
   504 
       
   505     // -- Multicast support --
       
   506 
       
   507 
       
   508     /**
       
   509      * Join IPv4 multicast group
       
   510      */
       
   511     static int join4(FileDescriptor fd, int group, int interf, int source)
       
   512         throws IOException
       
   513     {
       
   514         return joinOrDrop4(true, fd, group, interf, source);
       
   515     }
       
   516 
       
   517     /**
       
   518      * Drop membership of IPv4 multicast group
       
   519      */
       
   520     static void drop4(FileDescriptor fd, int group, int interf, int source)
       
   521         throws IOException
       
   522     {
       
   523         joinOrDrop4(false, fd, group, interf, source);
       
   524     }
       
   525 
       
   526     private static native int joinOrDrop4(boolean join, FileDescriptor fd, int group, int interf, int source)
       
   527         throws IOException;
       
   528 
       
   529     /**
       
   530      * Block IPv4 source
       
   531      */
       
   532     static int block4(FileDescriptor fd, int group, int interf, int source)
       
   533         throws IOException
       
   534     {
       
   535         return blockOrUnblock4(true, fd, group, interf, source);
       
   536     }
       
   537 
       
   538     /**
       
   539      * Unblock IPv6 source
       
   540      */
       
   541     static void unblock4(FileDescriptor fd, int group, int interf, int source)
       
   542         throws IOException
       
   543     {
       
   544         blockOrUnblock4(false, fd, group, interf, source);
       
   545     }
       
   546 
       
   547     private static native int blockOrUnblock4(boolean block, FileDescriptor fd, int group,
       
   548                                               int interf, int source)
       
   549         throws IOException;
       
   550 
       
   551     /**
       
   552      * Join IPv6 multicast group
       
   553      */
       
   554     static int join6(FileDescriptor fd, byte[] group, int index, byte[] source)
       
   555         throws IOException
       
   556     {
       
   557         return joinOrDrop6(true, fd, group, index, source);
       
   558     }
       
   559 
       
   560     /**
       
   561      * Drop membership of IPv6 multicast group
       
   562      */
       
   563     static void drop6(FileDescriptor fd, byte[] group, int index, byte[] source)
       
   564         throws IOException
       
   565     {
       
   566         joinOrDrop6(false, fd, group, index, source);
       
   567     }
       
   568 
       
   569     private static native int joinOrDrop6(boolean join, FileDescriptor fd, byte[] group, int index, byte[] source)
       
   570         throws IOException;
       
   571 
       
   572     /**
       
   573      * Block IPv6 source
       
   574      */
       
   575     static int block6(FileDescriptor fd, byte[] group, int index, byte[] source)
       
   576         throws IOException
       
   577     {
       
   578         return blockOrUnblock6(true, fd, group, index, source);
       
   579     }
       
   580 
       
   581     /**
       
   582      * Unblock IPv6 source
       
   583      */
       
   584     static void unblock6(FileDescriptor fd, byte[] group, int index, byte[] source)
       
   585         throws IOException
       
   586     {
       
   587         blockOrUnblock6(false, fd, group, index, source);
       
   588     }
       
   589 
       
   590     static native int blockOrUnblock6(boolean block, FileDescriptor fd, byte[] group, int index, byte[] source)
       
   591         throws IOException;
       
   592 
       
   593     static native void setInterface4(FileDescriptor fd, int interf) throws IOException;
       
   594 
       
   595     static native int getInterface4(FileDescriptor fd) throws IOException;
       
   596 
       
   597     static native void setInterface6(FileDescriptor fd, int index) throws IOException;
       
   598 
       
   599     static native int getInterface6(FileDescriptor fd) throws IOException;
       
   600 
       
   601     private static native void initIDs();
       
   602 
       
   603     /**
       
   604      * Event masks for the various poll system calls.
       
   605      * They will be set platform dependent in the static initializer below.
       
   606      */
       
   607     public static final short POLLIN;
       
   608     public static final short POLLOUT;
       
   609     public static final short POLLERR;
       
   610     public static final short POLLHUP;
       
   611     public static final short POLLNVAL;
       
   612     public static final short POLLCONN;
       
   613 
       
   614     static native short pollinValue();
       
   615     static native short polloutValue();
       
   616     static native short pollerrValue();
       
   617     static native short pollhupValue();
       
   618     static native short pollnvalValue();
       
   619     static native short pollconnValue();
       
   620 
       
   621     static {
       
   622         IOUtil.load();
       
   623         initIDs();
       
   624 
       
   625         POLLIN     = pollinValue();
       
   626         POLLOUT    = polloutValue();
       
   627         POLLERR    = pollerrValue();
       
   628         POLLHUP    = pollhupValue();
       
   629         POLLNVAL   = pollnvalValue();
       
   630         POLLCONN   = pollconnValue();
       
   631     }
       
   632 
       
   633     static {
       
   634         int availLevel = isExclusiveBindAvailable();
       
   635         if (availLevel >= 0) {
       
   636             String exclBindProp = GetPropertyAction
       
   637                     .privilegedGetProperty("sun.net.useExclusiveBind");
       
   638             if (exclBindProp != null) {
       
   639                 exclusiveBind = exclBindProp.isEmpty() ?
       
   640                         true : Boolean.parseBoolean(exclBindProp);
       
   641             } else if (availLevel == 1) {
       
   642                 exclusiveBind = true;
       
   643             } else {
       
   644                 exclusiveBind = false;
       
   645             }
       
   646         } else {
       
   647             exclusiveBind = false;
       
   648         }
       
   649 
       
   650         fastLoopback = isFastTcpLoopbackRequested();
       
   651     }
       
   652 }