src/java.base/share/classes/sun/nio/ch/Net.java
author michaelm
Thu, 14 Nov 2019 10:16:28 +0000
branchunixdomainchannels
changeset 59076 998df1368cca
parent 59004 84e08e00c29c
child 59123 ddb8977e44da
permissions -rw-r--r--
unixdomainchannels: unix domain automatic bind
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53422
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    28
import java.io.FileDescriptor;
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
    29
import java.io.FilePermission;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    30
import java.io.IOException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    31
import java.net.Inet4Address;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    32
import java.net.Inet6Address;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    33
import java.net.InetAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    34
import java.net.InetSocketAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    35
import java.net.NetworkInterface;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    36
import java.net.ProtocolFamily;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    37
import java.net.SocketAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    38
import java.net.SocketException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    39
import java.net.SocketOption;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    40
import java.net.StandardProtocolFamily;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    41
import java.net.StandardSocketOptions;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    42
import java.net.UnknownHostException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.AlreadyBoundException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.ClosedChannelException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    45
import java.nio.channels.NotYetBoundException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    46
import java.nio.channels.NotYetConnectedException;
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
    47
import java.nio.channels.UnixDomainSocketAddress;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.UnresolvedAddressException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    49
import java.nio.channels.UnsupportedAddressTypeException;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    50
import java.security.AccessController;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    51
import java.security.PrivilegedAction;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    52
import java.util.Enumeration;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
    53
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 37593
diff changeset
    54
import sun.net.ext.ExtendedSocketOptions;
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
    55
import sun.net.util.IPAddressUtil;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36115
diff changeset
    56
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9679
diff changeset
    58
public class Net {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Net() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
    62
    private static final boolean unixDomainSupported =
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
    63
        unixDomainSocketSupported();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    65
    // unspecified protocol family
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    66
    static final ProtocolFamily UNSPEC = new ProtocolFamily() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    67
        public String name() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    68
            return "UNSPEC";
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    69
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    70
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
    72
    // set to true if exclusive binding is on for Windows
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
    73
    private static final boolean exclusiveBind;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
    74
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
    75
    // 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
    76
    private static final boolean fastLoopback;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // -- Miscellaneous utilities --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 32649
diff changeset
    80
    private static volatile boolean checkedIPv6;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    81
    private static volatile boolean isIPv6Available;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
    82
    private static volatile boolean checkedReusePort;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
    83
    private static volatile boolean isReusePortAvailable;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    84
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    85
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    86
     * Tells whether dual-IPv4/IPv6 sockets should be used.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    87
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    88
    static boolean isIPv6Available() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    89
        if (!checkedIPv6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    90
            isIPv6Available = isIPv6Available0();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    91
            checkedIPv6 = true;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    92
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    93
        return isIPv6Available;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    94
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    95
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    96
    /**
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
    97
     * Tells whether SO_REUSEPORT is supported.
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
    98
     */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
    99
    static boolean isReusePortAvailable() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   100
        if (!checkedReusePort) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   101
            isReusePortAvailable = isReusePortAvailable0();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   102
            checkedReusePort = true;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   103
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   104
        return isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   105
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   106
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   107
    /**
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   108
     * Returns true if exclusive binding is on
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   109
     */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   110
    static boolean useExclusiveBind() {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   111
        return exclusiveBind;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   112
    }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   113
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   114
    /**
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   115
     * 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
   116
     */
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   117
    static boolean canIPv6SocketJoinIPv4Group() {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   118
        return canIPv6SocketJoinIPv4Group0();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   119
    }
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   120
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   121
    /**
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   122
     * 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
   123
     * 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
   124
     */
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   125
    static boolean canJoin6WithIPv4Group() {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   126
        return canJoin6WithIPv4Group0();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   127
    }
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   128
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   129
    /**
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   130
     * Tells whether IPV6_XXX socket options should be used on an IPv6 socket
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   131
     * that is bound to an IPv4 address.
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   132
     */
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   133
    static boolean canUseIPv6OptionsWithIPv4LocalAddress() {
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   134
        return canUseIPv6OptionsWithIPv4LocalAddress0();
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   135
    }
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   136
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9679
diff changeset
   137
    public static InetSocketAddress checkAddress(SocketAddress sa) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (sa == null)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   139
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (!(sa instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new UnsupportedAddressTypeException(); // ## needs arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        InetSocketAddress isa = (InetSocketAddress)sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (isa.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new UnresolvedAddressException(); // ## needs arg
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2057
diff changeset
   145
        InetAddress addr = isa.getAddress();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2057
diff changeset
   146
        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2057
diff changeset
   147
            throw new IllegalArgumentException("Invalid address type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   151
    static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   152
        InetSocketAddress isa = checkAddress(sa);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   153
        if (family == StandardProtocolFamily.INET) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   154
            InetAddress addr = isa.getAddress();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   155
            if (!(addr instanceof Inet4Address))
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   156
                throw new UnsupportedAddressTypeException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   157
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   158
        return isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   159
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 47216
diff changeset
   160
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!(sa instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new UnsupportedAddressTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return (InetSocketAddress)sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static void translateToSocketException(Exception x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        throws SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (x instanceof SocketException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw (SocketException)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Exception nx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (x instanceof ClosedChannelException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            nx = new SocketException("Socket is closed");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
        else if (x instanceof NotYetConnectedException)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   176
            nx = new SocketException("Socket is not connected");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        else if (x instanceof AlreadyBoundException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            nx = new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        else if (x instanceof NotYetBoundException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            nx = new SocketException("Socket is not bound yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        else if (x instanceof UnsupportedAddressTypeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            nx = new SocketException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        else if (x instanceof UnresolvedAddressException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            nx = new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (nx != x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            nx.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (nx instanceof SocketException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw (SocketException)nx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        else if (nx instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw (RuntimeException)nx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new Error("Untranslated exception", nx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static void translateException(Exception x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                   boolean unknownHostForUnresolved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (x instanceof IOException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw (IOException)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Throw UnknownHostException from here since it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // be thrown as a SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (unknownHostForUnresolved &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            (x instanceof UnresolvedAddressException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
             throw new UnknownHostException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static void translateException(Exception x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        translateException(x, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   219
    /**
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   220
     * Returns the local address after performing a SecurityManager#checkConnect.
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   221
     */
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   222
    static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   223
        SecurityManager sm = System.getSecurityManager();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   224
        if (addr == null || sm == null)
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   225
            return addr;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   226
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   227
        try{
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   228
            sm.checkConnect(addr.getAddress().getHostAddress(), -1);
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   229
            // Security check passed
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   230
        } catch (SecurityException e) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   231
            // Return loopback address only if security check fails
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   232
            addr = getLoopbackAddress(addr.getPort());
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   233
        }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   234
        return addr;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   235
    }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   236
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   237
    static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   238
        return System.getSecurityManager() == null ? addr.toString() :
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   239
                getLoopbackAddress(addr.getPort()).toString();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   240
    }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   241
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   242
    private static InetSocketAddress getLoopbackAddress(int port) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   243
        return new InetSocketAddress(InetAddress.getLoopbackAddress(),
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   244
                                     port);
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   245
    }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   246
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   247
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
     * Returns any IPv4 address of the given network interface, or
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
     * null if the interface does not have any IPv4 addresses.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   250
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   251
    static Inet4Address anyInet4Address(final NetworkInterface interf) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   252
        return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   253
            public Inet4Address run() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
                Enumeration<InetAddress> addrs = interf.getInetAddresses();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   255
                while (addrs.hasMoreElements()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
                    InetAddress addr = addrs.nextElement();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
                    if (addr instanceof Inet4Address) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   258
                        return (Inet4Address)addr;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   259
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   260
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   261
                return null;
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
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   266
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   267
     * Returns an IPv4 address as an int.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   268
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
    static int inet4AsInt(InetAddress ia) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
        if (ia instanceof Inet4Address) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   271
            byte[] addr = ia.getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
            int address  = addr[3] & 0xFF;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   273
            address |= ((addr[2] << 8) & 0xFF00);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   274
            address |= ((addr[1] << 16) & 0xFF0000);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   275
            address |= ((addr[0] << 24) & 0xFF000000);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   276
            return address;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   277
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   278
        throw new AssertionError("Should not reach here");
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   281
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   282
     * Returns an InetAddress from the given IPv4 address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   283
     * represented as an int.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   284
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
    static InetAddress inet4FromInt(int address) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   286
        byte[] addr = new byte[4];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   287
        addr[0] = (byte) ((address >>> 24) & 0xFF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   288
        addr[1] = (byte) ((address >>> 16) & 0xFF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   289
        addr[2] = (byte) ((address >>> 8) & 0xFF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   290
        addr[3] = (byte) (address & 0xFF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   292
            return InetAddress.getByAddress(addr);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   293
        } catch (UnknownHostException uhe) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   294
            throw new AssertionError("Should not reach here");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   295
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   297
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   298
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   299
     * Returns an IPv6 address as a byte array
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   300
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   301
    static byte[] inet6AsByteArray(InetAddress ia) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   302
        if (ia instanceof Inet6Address) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   303
            return ia.getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   304
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   305
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   306
        // need to construct IPv4-mapped address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   307
        if (ia instanceof Inet4Address) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   308
            byte[] ip4address = ia.getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   309
            byte[] address = new byte[16];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   310
            address[10] = (byte)0xff;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   311
            address[11] = (byte)0xff;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   312
            address[12] = ip4address[0];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   313
            address[13] = ip4address[1];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   314
            address[14] = ip4address[2];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   315
            address[15] = ip4address[3];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   316
            return address;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   317
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   318
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   319
        throw new AssertionError("Should not reach here");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   320
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   321
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   322
    // -- Socket options
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   323
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 37593
diff changeset
   324
    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
   325
            ExtendedSocketOptions.getInstance();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 37593
diff changeset
   326
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   327
    static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   328
        throws IOException
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   329
    {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   330
        setSocketOption(fd, Net.UNSPEC, name, value);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   331
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   332
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   333
    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
   334
                                SocketOption<?> name, Object value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   335
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   336
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   337
        if (value == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   338
            throw new IllegalArgumentException("Invalid option value");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   339
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   340
        // only simple values supported by this method
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   341
        Class<?> type = name.type();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   342
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 37593
diff changeset
   343
        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
   344
            extendedOptions.setOption(fd, name, value);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   345
            return;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   346
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   347
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   348
        if (type != Integer.class && type != Boolean.class)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   349
            throw new AssertionError("Should not reach here");
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
        // special handling
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   352
        if (name == StandardSocketOptions.SO_RCVBUF ||
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   353
            name == StandardSocketOptions.SO_SNDBUF)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   354
        {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   355
            int i = ((Integer)value).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   356
            if (i < 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   357
                throw new IllegalArgumentException("Invalid send/receive buffer size");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   358
        }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   359
        if (name == StandardSocketOptions.SO_LINGER) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   360
            int i = ((Integer)value).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   361
            if (i < 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   362
                value = Integer.valueOf(-1);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   363
            if (i > 65535)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   364
                value = Integer.valueOf(65535);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   365
        }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   366
        if (name == StandardSocketOptions.IP_TOS) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   367
            int i = ((Integer)value).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   368
            if (i < 0 || i > 255)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   369
                throw new IllegalArgumentException("Invalid IP_TOS value");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   370
        }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   371
        if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   372
            int i = ((Integer)value).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   373
            if (i < 0 || i > 255)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   374
                throw new IllegalArgumentException("Invalid TTL/hop value");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   375
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   376
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   377
        // map option name to platform level/name
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   378
        OptionKey key = SocketOptionRegistry.findOption(name, family);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   379
        if (key == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   380
            throw new AssertionError("Option not found");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   381
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   382
        int arg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   383
        if (type == Integer.class) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   384
            arg = ((Integer)value).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   385
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   386
            boolean b = ((Boolean)value).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   387
            arg = (b) ? 1 : 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   388
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   389
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   390
        boolean mayNeedConversion = (family == UNSPEC);
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   391
        boolean isIPv6 = (family == StandardProtocolFamily.INET6);
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   392
        setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   393
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   394
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   395
    static Object getSocketOption(FileDescriptor fd, SocketOption<?> name)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   396
        throws IOException
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   397
    {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   398
        return getSocketOption(fd, Net.UNSPEC, name);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   399
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   400
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   401
    static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   402
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   403
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   404
        Class<?> type = name.type();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   405
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 37593
diff changeset
   406
        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
   407
            return extendedOptions.getOption(fd, name);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   408
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23010
diff changeset
   409
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   410
        // only simple values supported by this method
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   411
        if (type != Integer.class && type != Boolean.class)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   412
            throw new AssertionError("Should not reach here");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   413
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   414
        // map option name to platform level/name
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   415
        OptionKey key = SocketOptionRegistry.findOption(name, family);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   416
        if (key == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   417
            throw new AssertionError("Option not found");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   418
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   419
        boolean mayNeedConversion = (family == UNSPEC);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   420
        int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   421
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   422
        if (type == Integer.class) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   423
            return Integer.valueOf(value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   424
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   425
            return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   426
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   427
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   429
    public static boolean isFastTcpLoopbackRequested() {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37676
diff changeset
   430
        String loopbackProp = GetPropertyAction
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 49001
diff changeset
   431
                .privilegedGetProperty("jdk.net.useFastTcpLoopback", "false");
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 49001
diff changeset
   432
        return loopbackProp.isEmpty() ? true : Boolean.parseBoolean(loopbackProp);
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   433
    }
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   434
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // -- Socket operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   437
    private static native boolean isIPv6Available0();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   438
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   439
    private static native boolean isReusePortAvailable0();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   440
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   441
    /*
32232
8d58fc5a0349 8074821: Resolve disabled warnings for libnio
bpb
parents: 27178
diff changeset
   442
     * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   443
     */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   444
    private static native int isExclusiveBindAvailable();
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   445
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   446
    private static native boolean canIPv6SocketJoinIPv4Group0();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   447
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   448
    private static native boolean canJoin6WithIPv4Group0();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   449
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   450
    private static native boolean canUseIPv6OptionsWithIPv4LocalAddress0();
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 55375
diff changeset
   451
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   452
    static FileDescriptor socket(boolean stream) throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   453
        return socket(UNSPEC, stream);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   454
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   455
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   456
    static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   457
        boolean preferIPv6 = isIPv6Available() &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   458
            (family != StandardProtocolFamily.INET);
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   459
        return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    static FileDescriptor serverSocket(boolean stream) {
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   463
        return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    // 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
   467
    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
   468
                                      boolean fastLoopback);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   469
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9679
diff changeset
   470
    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
   471
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   472
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   473
        bind(UNSPEC, fd, addr, port);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   474
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   476
    static void bind(ProtocolFamily family, FileDescriptor fd,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   477
                     InetAddress addr, int port) 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
        boolean preferIPv6 = isIPv6Available() &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   480
            (family != StandardProtocolFamily.INET);
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   481
        if (addr.isLinkLocalAddress()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   482
            addr = IPAddressUtil.toScopedAddress(addr);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   483
        }
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   484
        bind0(fd, preferIPv6, exclusiveBind, addr, port);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   485
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   486
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   487
    private static native void bind0(FileDescriptor fd, boolean preferIPv6,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   488
                                     boolean useExclBind, InetAddress addr,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   489
                                     int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   492
    static native void listen(FileDescriptor fd, int backlog) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   493
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   494
    static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   495
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   496
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   497
        return connect(UNSPEC, fd, remote, remotePort);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   498
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   499
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   500
    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
   501
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   502
    {
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   503
        if (remote.isLinkLocalAddress()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   504
            remote = IPAddressUtil.toScopedAddress(remote);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54246
diff changeset
   505
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   506
        boolean preferIPv6 = isIPv6Available() &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   507
            (family != StandardProtocolFamily.INET);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   508
        return connect0(preferIPv6, fd, remote, remotePort);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   509
    }
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
    private static native int connect0(boolean preferIPv6,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   512
                                       FileDescriptor fd,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   513
                                       InetAddress remote,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   514
                                       int remotePort)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53445
diff changeset
   517
    public static native int accept(FileDescriptor fd,
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53445
diff changeset
   518
                                    FileDescriptor newfd,
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53445
diff changeset
   519
                                    InetSocketAddress[] isaa)
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53445
diff changeset
   520
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32232
diff changeset
   522
    public static final int SHUT_RD = 0;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32232
diff changeset
   523
    public static final int SHUT_WR = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32232
diff changeset
   524
    public static final int SHUT_RDWR = 2;
1152
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
    static native void shutdown(FileDescriptor fd, int how) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   527
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    private static native int localPort(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    private static native InetAddress localInetAddress(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9679
diff changeset
   534
    public static InetSocketAddress localAddress(FileDescriptor fd)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   535
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   536
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   537
        return new InetSocketAddress(localInetAddress(fd), localPort(fd));
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   540
    private static native int remotePort(FileDescriptor fd)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   541
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   542
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   543
    private static native InetAddress remoteInetAddress(FileDescriptor fd)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   544
        throws IOException;
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
    static InetSocketAddress remoteAddress(FileDescriptor fd)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   547
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   548
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   549
        return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   552
    private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   553
                                            int level, int opt)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   554
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   555
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   556
    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
   557
                                             int level, int opt, int arg, boolean isIPv6)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   558
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   559
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   560
    /**
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   561
     * Polls a file descriptor for events.
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   562
     * @param timeout the timeout to wait; 0 to not wait, -1 to wait indefinitely
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   563
     * @return the polled events or 0 if no events are polled
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   564
     */
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11823
diff changeset
   565
    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
   566
        throws IOException;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11823
diff changeset
   567
53422
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
   568
    /**
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   569
     * Performs a non-blocking poll of a file descriptor.
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   570
     * @return the polled events or 0 if no events are polled
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   571
     */
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   572
    static int pollNow(FileDescriptor fd, int events) throws IOException {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   573
        return poll(fd, events, 0);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   574
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   575
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   576
    /**
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   577
     * Polls a connecting socket to test if the connection has been established.
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   578
     *
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   579
     * @apiNote This method is public to allow it be used by code in jdk.sctp.
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   580
     *
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   581
     * @param timeout the timeout to wait; 0 to not wait, -1 to wait indefinitely
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   582
     * @return true if connected
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   583
     */
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   584
    public static native boolean pollConnect(FileDescriptor fd, long timeout)
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   585
        throws IOException;
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   586
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   587
    /**
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   588
     * Performs a non-blocking poll of a connecting socket to test if the
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   589
     * connection has been established.
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   590
     *
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   591
     * @return true if connected
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   592
     */
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   593
    static boolean pollConnectNow(FileDescriptor fd) throws IOException {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   594
        return pollConnect(fd, 0);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   595
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   596
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 54154
diff changeset
   597
    /**
53422
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
   598
     * Return the number of bytes in the socket input buffer.
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
   599
     */
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
   600
    static native int available(FileDescriptor fd) throws IOException;
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53018
diff changeset
   601
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   602
    /**
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   603
     * Send one byte of urgent data (MSG_OOB) on the socket.
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   604
     */
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   605
    static native int sendOOB(FileDescriptor fd, byte data) throws IOException;
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   606
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53422
diff changeset
   607
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   608
    // -- Multicast support --
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   609
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   610
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   611
     * Join IPv4 multicast group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   612
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   613
    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
   614
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   615
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   616
        return joinOrDrop4(true, fd, group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   617
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   618
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   619
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   620
     * Drop membership of IPv4 multicast group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   621
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   622
    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
   623
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   624
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   625
        joinOrDrop4(false, fd, group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   626
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   627
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   628
    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
   629
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   630
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   631
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   632
     * Block IPv4 source
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   633
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   634
    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
   635
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   636
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   637
        return blockOrUnblock4(true, fd, group, interf, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   640
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   641
     * Unblock IPv6 source
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   642
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   643
    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
   644
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   645
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   646
        blockOrUnblock4(false, fd, group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   647
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   648
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   649
    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
   650
                                              int interf, int source)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   653
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   654
     * Join IPv6 multicast group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   655
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   656
    static int join6(FileDescriptor fd, byte[] group, int index, byte[] source)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   659
        return joinOrDrop6(true, fd, group, index, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   660
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   661
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   662
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   663
     * Drop membership of IPv6 multicast group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   664
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   665
    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
   666
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   667
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   668
        joinOrDrop6(false, fd, group, index, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   671
    private static native int joinOrDrop6(boolean join, FileDescriptor fd, byte[] group, int index, byte[] source)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   674
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   675
     * Block IPv6 source
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   676
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   677
    static int block6(FileDescriptor fd, byte[] group, int index, byte[] source)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   680
        return blockOrUnblock6(true, fd, group, index, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   681
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   682
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   683
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   684
     * Unblock IPv6 source
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   685
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   686
    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
   687
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   688
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   689
        blockOrUnblock6(false, fd, group, index, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   692
    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
   693
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   694
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   695
    static native void setInterface4(FileDescriptor fd, int interf) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   696
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   697
    static native int getInterface4(FileDescriptor fd) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   698
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   699
    static native void setInterface6(FileDescriptor fd, int index) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   700
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   701
    static native int getInterface6(FileDescriptor fd) throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   705
    /**
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   706
     * Event masks for the various poll system calls.
47020
2c55106dc37b 8186851: fix misspellings of "dependent" and "independent" in the JDK repo
smarks
parents: 37781
diff changeset
   707
     * They will be set platform dependent in the static initializer below.
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   708
     */
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   709
    public static final short POLLIN;
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   710
    public static final short POLLOUT;
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   711
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   712
    public static UnixDomainSocketAddress checkUnixAddress(SocketAddress sa) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   713
        if (sa == null)
59076
998df1368cca unixdomainchannels: unix domain automatic bind
michaelm
parents: 59004
diff changeset
   714
            return null;
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   715
        if (!(sa instanceof UnixDomainSocketAddress))
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   716
            throw new UnsupportedAddressTypeException();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   717
        UnixDomainSocketAddress usa = (UnixDomainSocketAddress)sa;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   718
        return usa;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   719
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   720
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   721
    public static boolean isUnixDomainSupported() {
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   722
        return unixDomainSupported;
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   723
    }
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   724
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   725
    private static UnixDomainSocketAddress UNNAMED = new UnixDomainSocketAddress("");
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   726
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   727
    static UnixDomainSocketAddress getRevealedLocalAddress(UnixDomainSocketAddress addr) {
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   728
        SecurityManager sm = System.getSecurityManager();
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   729
        if (addr == null || sm == null)
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   730
            return addr;
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   731
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   732
        try{
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   733
            FilePermission p = new FilePermission(addr.getPath(), "read");
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   734
            sm.checkPermission(p);
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   735
            // Security check passed
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   736
        } catch (SecurityException e) {
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   737
            // Return unnamed address only if security check fails
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   738
            addr = UNNAMED;
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   739
        }
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   740
        return addr;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   741
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   742
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   743
    static String getRevealedLocalAddressAsString(UnixDomainSocketAddress addr) {
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   744
        return System.getSecurityManager() == null ? addr.toString() :
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   745
                UNNAMED.toString();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   746
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   747
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   748
    // -- Socket operations --
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   749
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   750
    public static FileDescriptor unixDomainSocket() throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   751
        return IOUtil.newFD(unixDomainSocket0());
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   752
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   753
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   754
    private static native int unixDomainSocket0();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   755
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   756
    private static native boolean unixDomainSocketSupported();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   757
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   758
    static native void unixDomainBind(FileDescriptor fd, UnixDomainSocketAddress addr)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   759
        throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   760
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   761
    static native int unixDomainConnect(FileDescriptor fd, UnixDomainSocketAddress remote)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   762
        throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   763
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   764
    static native int unixDomainAccept(FileDescriptor fd,
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   765
                                     FileDescriptor newfd,
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   766
                                     SocketAddress[] isaa)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   767
        throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   768
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   769
    public static native UnixDomainSocketAddress localUnixAddress(FileDescriptor fd)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55375
diff changeset
   770
        throws IOException;
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   771
    public static final short POLLERR;
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   772
    public static final short POLLHUP;
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   773
    public static final short POLLNVAL;
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   774
    public static final short POLLCONN;
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   775
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   776
    static native short pollinValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   777
    static native short polloutValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   778
    static native short pollerrValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   779
    static native short pollhupValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   780
    static native short pollnvalValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   781
    static native short pollconnValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   782
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
   784
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        initIDs();
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   786
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   787
        POLLIN     = pollinValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   788
        POLLOUT    = polloutValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   789
        POLLERR    = pollerrValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   790
        POLLHUP    = pollhupValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   791
        POLLNVAL   = pollnvalValue();
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   792
        POLLCONN   = pollconnValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   795
    static {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   796
        int availLevel = isExclusiveBindAvailable();
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   797
        if (availLevel >= 0) {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37676
diff changeset
   798
            String exclBindProp = GetPropertyAction
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37676
diff changeset
   799
                    .privilegedGetProperty("sun.net.useExclusiveBind");
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   800
            if (exclBindProp != null) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36115
diff changeset
   801
                exclusiveBind = exclBindProp.isEmpty() ?
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   802
                        true : Boolean.parseBoolean(exclBindProp);
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   803
            } else if (availLevel == 1) {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   804
                exclusiveBind = true;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   805
            } else {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   806
                exclusiveBind = false;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   807
            }
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   808
        } else {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   809
            exclusiveBind = false;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   810
        }
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   811
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   812
        fastLoopback = isFastTcpLoopbackRequested();
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   813
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
}