src/java.base/windows/native/libnio/ch/SocketChannelImpl.c
author alanb
Tue, 06 Feb 2018 16:04:46 +0000
changeset 48750 ffbb784a8873
parent 47216 71c04702a3d5
child 49141 ac95c7a76132
permissions -rw-r--r--
8196787: (ch) Moving network channels to use j.u.c locks Reviewed-by: prappo, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sun_nio_ch_SocketChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static jfieldID ia_addrID;      /* java.net.InetAddress.address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
Java_sun_nio_ch_SocketChannelImpl_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 21278
diff changeset
    45
    CHECK_NULL(cls = (*env)->FindClass(env, "java/net/InetAddress"));
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 21278
diff changeset
    46
    CHECK_NULL(ia_addrID = (*env)->GetFieldID(env, cls, "address", "I"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
handleSocketError(JNIEnv *env, int errorValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    NET_ThrowNew(env, errorValue, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
Java_sun_nio_ch_SocketChannelImpl_checkConnect(JNIEnv *env, jobject this,
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    58
                                               jobject fdo, jboolean block)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int optError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int lastError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int retry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int n = sizeof(int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    struct timeval t = { 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    FD_SET((u_int)fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    FD_SET((u_int)fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    result = select(fd+1, 0, &wr, &ex, block ? NULL : &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* save last winsock error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        lastError = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (block) { /* must configure socket back to blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        u_long argp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int r = ioctlsocket(fd, FIONBIO, &argp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (r == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            handleSocketError(env, WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (result == 0) {  /* timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return block ? 0 : IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (result == SOCKET_ERROR)     { /* select failed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            handleSocketError(env, lastError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /*
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 7668
diff changeset
    99
     * Socket is writable or error occurred. On some Windows editions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * the socket will appear writable when the connect fails so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * check for error rather than writable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (!FD_ISSET(fd, &ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return 1;               /* connection established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * A getsockopt( SO_ERROR ) may indicate success on NT4 even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * though the connection has failed. The workaround is to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * winsock to be scheduled and this is done via by yielding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * As the yield approach is problematic in heavy load situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * we attempt up to 3 times to get the failure reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    for (retry=0; retry<3; retry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        result = getsockopt((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            SOL_SOCKET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            (char *)&optError,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            &n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int lastError = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (lastError == WSAEINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            NET_ThrowNew(env, lastError, "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (optError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Sleep(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if (optError != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        handleSocketError(env, optError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   141
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   142
JNIEXPORT jint JNICALL
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   143
Java_sun_nio_ch_SocketChannelImpl_sendOutOfBandData(JNIEnv* env, jclass this,
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   144
                                                    jobject fdo, jbyte b)
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   145
{
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   146
    int n = send(fdval(env, fdo), (const char*)&b, 1, MSG_OOB);
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   147
    if (n == SOCKET_ERROR) {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   148
        handleSocketError(env, WSAGetLastError());
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   149
        return IOS_THROWN;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   150
    } else {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   151
        return n;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   152
    }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   153
}