src/java.base/windows/classes/java/net/DualStackPlainSocketImpl.java
author alanb
Thu, 15 Mar 2018 11:02:22 +0000
changeset 49249 92cca24c8807
parent 47216 71c04702a3d5
child 49431 5812849b5027
permissions -rw-r--r--
8199329: Remove code that attempts to read bytes after connection reset reported Reviewed-by: redestad, clanger, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 25859
diff changeset
    29
import jdk.internal.misc.SharedSecrets;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 25859
diff changeset
    30
import jdk.internal.misc.JavaIOFileDescriptorAccess;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class defines the plain SocketImpl that is used on Windows platforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * greater or equal to Windows Vista. These platforms have a dual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * layer TCP/IP stack and can handle both IPv4 and IPV6 through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * single file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class DualStackPlainSocketImpl extends AbstractPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static JavaIOFileDescriptorAccess fdAccess = SharedSecrets.getJavaIOFileDescriptorAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    45
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    46
    // true if this socket is exclusively bound
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    47
    private final boolean exclusiveBind;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    48
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    49
    // emulates SO_REUSEADDR when exclusiveBind is true
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    50
    private boolean isReuseAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    52
    public DualStackPlainSocketImpl(boolean exclBind) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    53
        exclusiveBind = exclBind;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    54
    }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    55
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    56
    public DualStackPlainSocketImpl(FileDescriptor fd, boolean exclBind) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.fd = fd;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
    58
        exclusiveBind = exclBind;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void socketCreate(boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (fd == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int newfd = socket0(stream, false /*v6 Only*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        fdAccess.set(fd, newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    void socketConnect(InetAddress address, int port, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (address == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new NullPointerException("inet address argument is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int connectResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            connectResult = connect0(nativefd, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            configureBlocking(nativefd, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                connectResult = connect0(nativefd, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (connectResult == WOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    waitForConnect(nativefd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                configureBlocking(nativefd, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * We need to set the local port field. If bind was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * previous to the connect (by the client) then localport field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * will already be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (localport == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            localport = localPort0(nativefd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void socketBind(InetAddress address, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (address == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new NullPointerException("inet address argument is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   106
        bind0(nativefd, address, port, exclusiveBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (port == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            localport = localPort0(nativefd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            localport = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    void socketListen(int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        listen0(nativefd, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void socketAccept(SocketImpl s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NullPointerException("socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int newfd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        InetSocketAddress[] isaa = new InetSocketAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            newfd = accept0(nativefd, isaa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            configureBlocking(nativefd, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                waitForNewConnection(nativefd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                newfd = accept0(nativefd, isaa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (newfd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    configureBlocking(newfd, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                configureBlocking(nativefd, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* Update (SocketImpl)s' fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        fdAccess.set(s.fd, newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /* Update socketImpls remote port, address and localport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        InetSocketAddress isa = isaa[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        s.port = isa.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        s.address = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        s.localport = localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    int socketAvailable() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return available0(nativefd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    void socketClose0(boolean useDeferredClose/*unused*/) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (fd == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!fd.valid())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   165
        final int nativefd = fdAccess.get(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        fdAccess.set(fd, -1);
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   167
        close0(nativefd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void socketShutdown(int howto) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        shutdown0(nativefd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   175
    // Intentional fallthrough after SO_REUSEADDR
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   176
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    void socketSetOption(int opt, boolean on, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (opt == SO_TIMEOUT) {  // timeout implemented through select.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   184
        // SO_REUSEPORT is not supported on Windows.
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   185
        if (opt == SO_REUSEPORT) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   186
            throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   187
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int optionValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        switch(opt) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   192
            case SO_REUSEADDR :
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   193
                if (exclusiveBind) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   194
                    // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   195
                    isReuseAddress = on;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   196
                    return;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   197
                }
18201
f0c28a4c1ab2 8010213: Some api/javax_net/SocketFactory tests fail in 7u25 nightly build
khazra
parents: 18192
diff changeset
   198
                // intentional fallthrough
f0c28a4c1ab2 8010213: Some api/javax_net/SocketFactory tests fail in 7u25 nightly build
khazra
parents: 18192
diff changeset
   199
            case TCP_NODELAY :
f0c28a4c1ab2 8010213: Some api/javax_net/SocketFactory tests fail in 7u25 nightly build
khazra
parents: 18192
diff changeset
   200
            case SO_OOBINLINE :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            case SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                optionValue = on ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            case SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            case SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            case IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                optionValue = ((Integer)value).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            case SO_LINGER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    optionValue =  ((Integer)value).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    optionValue = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            default :/* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new SocketException("Option not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        setIntOption(nativefd, opt, optionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // SO_BINDADDR is not a socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (opt == SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            localAddress(nativefd, (InetAddressContainer)iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return 0;  // return value doesn't matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   231
        // SO_REUSEPORT is not supported on Windows.
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   232
        if (opt == SO_REUSEPORT) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   233
            throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32834
diff changeset
   234
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   236
        // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   237
        if (opt == SO_REUSEADDR && exclusiveBind)
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   238
            return isReuseAddress? 1 : -1;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   239
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int value = getIntOption(nativefd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            case TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            case SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            case SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            case SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return (value == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    void socketSendUrgentData(int data) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int nativefd = checkAndReturnNativeFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        sendOOB(nativefd, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private int checkAndReturnNativeFD() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (fd == null || !fd.valid())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return fdAccess.get(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static final int WOULDBLOCK = -2;       // Nothing available (non-blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /* Native methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static native int socket0(boolean stream, boolean v6Only) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   276
    static native void bind0(int fd, InetAddress localAddress, int localport,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16866
diff changeset
   277
                             boolean exclBind)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static native int connect0(int fd, InetAddress remote, int remotePort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    static native void waitForConnect(int fd, int timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static native int localPort0(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    static native void localAddress(int fd, InetAddressContainer in) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    static native void listen0(int fd, int backlog) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static native int accept0(int fd, InetSocketAddress[] isaa) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    static native void waitForNewConnection(int fd, int timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static native int available0(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    static native void close0(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    static native void shutdown0(int fd, int howto) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    static native void setIntOption(int fd, int cmd, int optionValue) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    static native int getIntOption(int fd, int cmd) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    static native void sendOOB(int fd, int data) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    static native void configureBlocking(int fd, boolean blocking) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}