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