jdk/src/java.base/windows/native/libnet/DualStackPlainSocketImpl.c
author chegar
Thu, 29 Jan 2015 20:45:30 +0000
changeset 28681 fc0698dfbb23
parent 28663 5bedcbb25c5f
child 32477 4a609fc2767a
permissions -rw-r--r--
8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows Reviewed-by: alanb, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2007, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "java_net_DualStackPlainSocketImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#define SET_BLOCKING 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#define SET_NONBLOCKING 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
static jclass isa_class;        /* java.net.InetSocketAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static jmethodID isa_ctorID;    /* InetSocketAddress(InetAddress, int) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Method:    initIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_initIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  (JNIEnv *env, jclass clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jclass cls = (*env)->FindClass(env, "java/net/InetSocketAddress");
18540
1f637992378a 8017271: Crash may occur in java.net.DualStackPlainSocketImpl::initIDs due to unchecked values returned from JNI functions
chegar
parents: 18192
diff changeset
    46
    CHECK_NULL(cls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    isa_class = (*env)->NewGlobalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    isa_ctorID = (*env)->GetMethodID(env, cls, "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                     "(Ljava/net/InetAddress;I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
28663
5bedcbb25c5f 8071424: JCK test api/java_net/Socket/descriptions.html#Bind crashes on Windows
chegar
parents: 25859
diff changeset
    51
    initInetAddressIDs(env);
5bedcbb25c5f 8071424: JCK test api/java_net/Socket/descriptions.html#Bind crashes on Windows
chegar
parents: 25859
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // implement read timeout with select.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    isRcvTimeoutSupported = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Method:    socket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Signature: (ZZ)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_socket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  (JNIEnv *env, jclass clazz, jboolean stream, jboolean v6Only /*unused*/) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int fd, rv, opt=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    fd = NET_Socket(AF_INET6, (stream ? SOCK_STREAM : SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    if (fd == INVALID_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        NET_ThrowNew(env, WSAGetLastError(), "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    rv = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &opt, sizeof(opt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        NET_ThrowNew(env, WSAGetLastError(), "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Method:    bind0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Signature: (ILjava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_bind0
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 5506
diff changeset
    88
  (JNIEnv *env, jclass clazz, jint fd, jobject iaObj, jint port,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 5506
diff changeset
    89
   jboolean exclBind)
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 5506
diff changeset
    90
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    SOCKETADDRESS sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    int sa_len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&sa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                 &sa_len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 5506
diff changeset
   100
    rv = NET_WinBind(fd, (struct sockaddr *)&sa, sa_len, exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if (rv == SOCKET_ERROR)
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   103
        NET_ThrowNew(env, WSAGetLastError(), "NET_Bind");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Method:    connect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Signature: (ILjava/net/InetAddress;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_connect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  (JNIEnv *env, jclass clazz, jint fd, jobject iaObj, jint port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    SOCKETADDRESS sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int sa_len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&sa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                 &sa_len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    rv = connect(fd, (struct sockaddr *)&sa, sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int err = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (err == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return java_net_DualStackPlainSocketImpl_WOULDBLOCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } else if (err == WSAEADDRNOTAVAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            JNU_ThrowByName(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                "connect: Address is invalid on local machine, or port is not valid on remote machine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            NET_ThrowNew(env, err, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return -1;  // return value not important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Method:    waitForConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * Signature: (II)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_waitForConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  (JNIEnv *env, jclass clazz, jint fd, jint timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int rv, retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    int optlen = sizeof(rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Wait for timeout, connection established or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * connection failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    rv = select(fd+1, 0, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Timeout before connection is established/failed so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * we throw exception and shutdown input/output to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * socket from being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The socket should be closed immediately by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        shutdown( fd, SD_BOTH );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /*
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18540
diff changeset
   177
     * Socket is writable or error occurred. On some Windows editions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the socket will appear writable when the connect fails so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * check for error rather than writable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (!FD_ISSET(fd, &ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return;         /* connection established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Connection failed. The logic here is designed to work around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * bug on Windows NT whereby using getsockopt to obtain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * last error (SO_ERROR) indicates there is no error. The workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * on NT is to allow winsock to be scheduled and this is done by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * yielding and retrying. As yielding is problematic in heavy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * load conditions we attempt up to 3 times to get the error reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    for (retry=0; retry<3; retry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        NET_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                       (char*)&rv, &optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (rv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Sleep(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        "Unable to establish connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        NET_ThrowNew(env, rv, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * Method:    localPort0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_localPort0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
  (JNIEnv *env, jclass clazz, jint fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    SOCKETADDRESS sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    int len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    if (getsockname(fd, (struct sockaddr *)&sa, &len) == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (WSAGetLastError() == WSAENOTSOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            NET_ThrowNew(env, WSAGetLastError(), "getsockname failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    return (int) ntohs((u_short)GET_PORT(&sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * Method:    localAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * Signature: (ILjava/net/InetAddressContainer;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_localAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
  (JNIEnv *env, jclass clazz, jint fd, jobject iaContainerObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    SOCKETADDRESS sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    int len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    jclass iaContainerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    jfieldID iaFieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    if (getsockname(fd, (struct sockaddr *)&sa, &len) == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        NET_ThrowNew(env, WSAGetLastError(), "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    CHECK_NULL(iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    iaContainerClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    iaFieldID = (*env)->GetFieldID(env, iaContainerClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    CHECK_NULL(iaFieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * Method:    listen0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * Signature: (II)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_listen0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  (JNIEnv *env, jclass clazz, jint fd, jint backlog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (listen(fd, backlog) == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        NET_ThrowNew(env, WSAGetLastError(), "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * Method:    accept0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * Signature: (I[Ljava/net/InetSocketAddress;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_accept0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
  (JNIEnv *env, jclass clazz, jint fd, jobjectArray isaa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    int newfd, port=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    jobject isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    jobject ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    SOCKETADDRESS sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    int len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    memset((char *)&sa, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    newfd = accept(fd, (struct sockaddr *)&sa, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (newfd == INVALID_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (WSAGetLastError() == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
28681
fc0698dfbb23 8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows
chegar
parents: 28663
diff changeset
   299
    SetHandleInformation((HANDLE)(UINT_PTR)newfd, HANDLE_FLAG_INHERIT, 0);
fc0698dfbb23 8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows
chegar
parents: 28663
diff changeset
   300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    (*env)->SetObjectArrayElement(env, isaa, 0, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    return newfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * Method:    waitForNewConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * Signature: (II)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_waitForNewConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
  (JNIEnv *env, jclass clazz, jint fd, jint timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    rv = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    } else if (rv == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    } else if (rv == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * Method:    available0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_available0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  (JNIEnv *env, jclass clazz, jint fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    jint available = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    if ((ioctlsocket(fd, FIONREAD, &available)) == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        NET_ThrowNew(env, WSAGetLastError(), "socket available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 * Method:    close0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_close0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
  (JNIEnv *env, jclass clazz, jint fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 * Method:    shutdown0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 * Signature: (II)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_shutdown0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
  (JNIEnv *env, jclass clazz, jint fd, jint howto) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    shutdown(fd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * Method:    setIntOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * Signature: (III)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_setIntOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
  (JNIEnv *env, jclass clazz, jint fd, jint cmd, jint value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   374
    int level = 0, opt = 0;
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   375
    struct linger linger = {0, 0};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    char *parg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    int arglen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        JNU_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                     JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                     "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    if (opt == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        parg = (char *)&linger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        arglen = sizeof(linger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            linger.l_onoff = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            linger.l_linger = (unsigned short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            linger.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            linger.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        parg = (char *)&value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        arglen = sizeof(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    if (NET_SetSockOpt(fd, level, opt, parg, arglen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 * Method:    getIntOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
 * Signature: (II)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_getIntOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
  (JNIEnv *env, jclass clazz, jint fd, jint cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   414
    int level = 0, opt = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    int result=0;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   416
    struct linger linger = {0, 0};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    char *arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    int arglen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        JNU_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                     JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                     "Unsupported socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    if (opt == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        arg = (char *)&linger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        arglen = sizeof(linger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        arg = (char *)&result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        arglen = sizeof(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (NET_GetSockOpt(fd, level, opt, arg, &arglen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (opt == java_net_SocketOptions_SO_LINGER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return linger.l_onoff ? linger.l_linger : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 * Method:    sendOOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 * Signature: (II)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_sendOOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
  (JNIEnv *env, jclass clazz, jint fd, jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    unsigned char d = (unsigned char) data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    n = send(fd, (char *)&data, 1, MSG_OOB);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   458
    if (n == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        NET_ThrowNew(env, WSAGetLastError(), "send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 * Class:     java_net_DualStackPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 * Method:    configureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 * Signature: (IZ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
JNIEXPORT void JNICALL Java_java_net_DualStackPlainSocketImpl_configureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
  (JNIEnv *env, jclass clazz, jint fd, jboolean blocking) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    u_long arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (blocking == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        arg = SET_BLOCKING;    // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        arg = SET_NONBLOCKING;   // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    result = ioctlsocket(fd, FIONBIO, &arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        NET_ThrowNew(env, WSAGetLastError(), "configureBlocking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
}