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