jdk/src/windows/native/sun/nio/ch/SocketChannelImpl.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1247 b4c26443dee5
child 6117 471ae95609d5
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     2
 * Copyright (c) 2000, 2008, 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
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    cls = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    ia_addrID = (*env)->GetFieldID(env, cls, "address", "I");
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                               jobject fdo, jboolean block,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                               jboolean ready)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int optError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int lastError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int retry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int n = sizeof(int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    struct timeval t = { 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    FD_SET((u_int)fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    FD_SET((u_int)fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    result = select(fd+1, 0, &wr, &ex, block ? NULL : &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* save last winsock error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        lastError = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (block) { /* must configure socket back to blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        u_long argp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int r = ioctlsocket(fd, FIONBIO, &argp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (r == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            handleSocketError(env, WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (result == 0) {  /* timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return block ? 0 : IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (result == SOCKET_ERROR)     { /* select failed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            handleSocketError(env, lastError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Socket is writable or error occured. On some Windows editions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the socket will appear writable when the connect fails so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * check for error rather than writable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (!FD_ISSET(fd, &ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return 1;               /* connection established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * A getsockopt( SO_ERROR ) may indicate success on NT4 even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * though the connection has failed. The workaround is to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * winsock to be scheduled and this is done via by yielding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * As the yield approach is problematic in heavy load situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * we attempt up to 3 times to get the failure reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    for (retry=0; retry<3; retry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        result = getsockopt((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            SOL_SOCKET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            (char *)&optError,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            &n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            int lastError = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (lastError == WSAEINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            NET_ThrowNew(env, lastError, "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (optError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Sleep(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (optError != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        handleSocketError(env, optError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}