jdk/src/windows/native/java/net/TwoStacksPlainSocketImpl.c
author khazra
Thu, 14 Mar 2013 13:46:15 -0700
changeset 18192 fa6bd0992104
parent 16870 f35b2bd19761
child 20821 e0d0a585aa49
permissions -rw-r--r--
7170730: Improve Windows network stack support. Summary: Enable exclusive binding of ports on Windows Reviewed-by: alanb, chegar, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12813
diff changeset
     2
 * Copyright (c) 1997, 2012, 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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_net_SocketOptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "java_net_TwoStacksPlainSocketImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "java_io_FileDescriptor.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "java_lang_Integer.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
jfieldID psi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jfieldID psi_fd1ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
jfieldID psi_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
jfieldID psi_portID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
jfieldID psi_localportID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jfieldID psi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
jfieldID psi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
jfieldID psi_serverSocketID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
jfieldID psi_lastfdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * the level of the TCP protocol for setsockopt and getsockopt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * we only want to look this up once, from the static initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * of TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static int tcp_level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static int getFD1(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * The initProto function is called whenever TwoStacksPlainSocketImpl is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * loaded, to cache fieldIds for efficiency. This is called everytime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the Java class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Method:    initProto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
Java_java_net_TwoStacksPlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    struct protoent *proto = getprotobyname("TCP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    psi_fdID = (*env)->GetFieldID(env, cls , "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    CHECK_NULL(psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    psi_fd1ID =(*env)->GetFieldID(env, cls , "fd1", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    CHECK_NULL(psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    psi_addressID = (*env)->GetFieldID(env, cls, "address",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    CHECK_NULL(psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    psi_lastfdID = (*env)->GetFieldID(env, cls, "lastfd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    CHECK_NULL(psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    CHECK_NULL(psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    CHECK_NULL(psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                            "Ljava/net/ServerSocket;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    CHECK_NULL(psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Method:    socketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Signature: (Z)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
Java_java_net_TwoStacksPlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                           jboolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    jobject fdObj, fd1Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        "null fd object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    fd = socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        NET_ThrowCurrent(env, "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /* Set socket attribute so it is not passed to any child process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            "null fd1 object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        fd1 = socket(AF_INET6, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            NET_ThrowCurrent(env, "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /* Set socket attribute so it is not passed to any child process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            SetHandleInformation((HANDLE)(UINT_PTR)fd1, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * inetAddress is the address object passed to the socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * Method:    socketConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                            jobject iaObj, jint port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                            jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    jint localport = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /* family and localport are int fields of iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    int family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    jint fd, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    jint len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    int  ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /* fd initially points to the IPv4 socket and fd1 to the IPv6 socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If we want to connect to IPv6 then we swap the two sockets/objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This way, fd is always the connected socket, and fd1 always gets closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    SOCKETADDRESS him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /* The result of the connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int connect_res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    if (ipv6_supported && !IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        JNU_ThrowNullPointerException(env, "inet address argument is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_FALSE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    family = him.him.sa_family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    if (family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (!ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            "Protocol family not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                "Destination unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            /* close the v4 socket, and set fd to be the v6 socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            (*env)->SetObjectField(env, this, psi_fdID, fd1Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            fd = fd1; fdObj = fd1Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            NET_SocketClose(fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            "Destination unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (connect_res == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            connect_res = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int optlen = sizeof(optval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /* make socket non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        optval = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        ioctlsocket( fd, FIONBIO, &optval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        /* initiate the connect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (connect_res == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                connect_res = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                 * Wait for timout, connection established or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                 * connection failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                connect_res = select(fd+1, 0, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                 * Timeout before connection is established/failed so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                 * we throw exception and shutdown input/output to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                 * socket from being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                 * The socket should be closed immediately by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                if (connect_res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                    "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    shutdown( fd, SD_BOTH );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     /* make socket blocking again - just in case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    optval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    ioctlsocket( fd, FIONBIO, &optval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                 * We must now determine if the connection has been established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                 * or if it has failed. The logic here is designed to work around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                 * bug on Windows NT whereby using getsockopt to obtain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                 * last error (SO_ERROR) indicates there is no error. The workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                 * on NT is to allow winsock to be scheduled and this is done by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                 * yielding and retrying. As yielding is problematic in heavy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                 * load conditions we attempt up to 3 times to get the error reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (!FD_ISSET(fd, &ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    connect_res = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    int retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    for (retry=0; retry<3; retry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        NET_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                       (char*)&connect_res, &optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        if (connect_res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        Sleep(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    if (connect_res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                        "Unable to establish connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /* make socket blocking again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        optval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ioctlsocket(fd, FIONBIO, &optval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (connect_res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (connect_res == WSAEADDRNOTAVAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            JNU_ThrowByName(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                "connect: Address is invalid on local machine, or port is not valid on remote machine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            NET_ThrowNew(env, connect_res, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /* set the remote peer address and port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    (*env)->SetIntField(env, this, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * we need to initialize the local port field if bind was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * previously to the connect (by the client) then localport field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * will already be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        u_short port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int len = SOCKETADDRESS_LEN(&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (WSAGetLastError() == WSAENOTSOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                NET_ThrowCurrent(env, "getsockname failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
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
        port = ntohs ((u_short)GET_PORT(&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        (*env)->SetIntField(env, this, psi_localportID, (int) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * Method:    socketBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   397
                                         jobject iaObj, jint localport,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   398
                                         jboolean exclBind) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    jobject fdObj, fd1Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /* fd is an int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    int fd, fd1, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /* family is an int field of iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    int family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    SOCKETADDRESS him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   415
    family = getInetAddress_family(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    if (family == IPv6 && !ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        "Protocol family not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
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 (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        JNU_ThrowNullPointerException(env, "inet address argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    if (NET_InetAddressToSockaddr(env, iaObj, localport,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                          (struct sockaddr *)&him, &len, JNI_FALSE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        struct ipv6bind v6bind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        v6bind.addr = &him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        v6bind.ipv4_fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        v6bind.ipv6_fd = fd1;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   447
        rv = NET_BindV6(&v6bind, exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (rv != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            /* check if the fds have changed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (v6bind.ipv4_fd != fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                fd = v6bind.ipv4_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    /* socket is closed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    (*env)->SetObjectField(env, this, psi_fdID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    /* socket was re-created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (v6bind.ipv6_fd != fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                fd1 = v6bind.ipv6_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    /* socket is closed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    /* socket was re-created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    } else {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   472
        rv = NET_WinBind(fd, (struct sockaddr *)&him, len, exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    if (rv == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        NET_ThrowCurrent(env, "JVM_Bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /* set the address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /* intialize the local port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        /* Now that we're a bound socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        int len = SOCKETADDRESS_LEN(&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        u_short port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        fd = him.him.sa_family == AF_INET? fd: fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            NET_ThrowCurrent(env, "getsockname in plain socketBind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        port = ntohs ((u_short) GET_PORT (&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        (*env)->SetIntField(env, this, psi_localportID, (int) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 * Method:    socketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                            jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /* this FileDescriptor fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    jobject address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /* fdObj's int fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    SOCKETADDRESS addr; int addrlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /* Listen on V4 if address type is v4 or if v6 and address is ::0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Listen on V6 if address type is v6 or if v4 and address is 0.0.0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * In cases, where we listen on one space only, we close the other socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    address = (*env)->GetObjectField(env, this, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        JNU_ThrowNullPointerException(env, "socket address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    if (NET_InetAddressToSockaddr(env, address, 0, (struct sockaddr *)&addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                  &addrlen, JNI_FALSE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    if (addr.him.sa_family == AF_INET || IN6ADDR_ISANY(&addr.him6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        /* listen on v4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (listen(fd, count) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            NET_ThrowCurrent(env, "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        NET_SocketClose (fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        (*env)->SetObjectField(env, this, psi_fdID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    if (ipv6_available() && !IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (addr.him.sa_family == AF_INET6 || addr.him4.sin_addr.s_addr == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            /* listen on v6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (listen(fd1, count) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                NET_ThrowCurrent(env, "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            NET_SocketClose (fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * Method:    socketAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 * Signature: (Ljava/net/SocketImpl;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                           jobject socket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /* fields on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    jint port;
10426
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   578
    jint scope;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /* the FileDescriptor field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    jobject socketFdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /* cache the Inet4/6Address classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    static jclass inet4Cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    static jclass inet6Cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /* the InetAddress field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    jobject socketAddressObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /* the fd int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    jint fd=-1, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    SOCKETADDRESS him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    jint len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    if (IS_NULL(socket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        JNU_ThrowNullPointerException(env, "socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        socketAddressObj = (*env)->GetObjectField(env, socket, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    if ((IS_NULL(socketAddressObj)) || (IS_NULL(socketFdObj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        JNU_ThrowNullPointerException(env, "socket address or fd obj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    if (fd != -1 && fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        fd_set rfds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        struct timeval t, *tP=&t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        int lastfd, res, fd2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        FD_ZERO(&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        FD_SET(fd,&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        FD_SET(fd1,&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            t.tv_sec = timeout/1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            t.tv_usec = (timeout%1000)*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            tP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        res = select (fd, &rfds, NULL, NULL, tP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                            "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else if (res == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            fd2 = FD_ISSET(fd, &rfds)? fd: fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } else if (res == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            /* avoid starvation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            lastfd = (*env)->GetIntField(env, this, psi_lastfdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (lastfd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                fd2 = lastfd==fd? fd1: fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                fd2 = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            (*env)->SetIntField(env, this, psi_lastfdID, fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            "select failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (fd2 == fd) { /* v4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            len = sizeof (struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            len = sizeof (struct SOCKADDR_IN6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        fd = fd2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            fd = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            len = sizeof (struct SOCKADDR_IN6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            len = sizeof (struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } else if (ret == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            /* REMIND: SOCKET CLOSED PROBLEM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /*        NET_ThrowCurrent(env, "Accept failed"); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            } else if (ret == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    fd = accept(fd, (struct sockaddr *)&him, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        /* REMIND: SOCKET CLOSED PROBLEM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (fd == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    if (him.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (inet4Cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            if (c != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                inet4Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * fill up the remote peer port and address in the new socket structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (inet4Cls != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            socketAddressObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            socketAddressObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
             * FindClass or NewObject failed so close connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
             * exist (there will be a pending exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   727
        setInetAddress_addr(env, socketAddressObj, ntohl(him.him4.sin_addr.s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   728
        setInetAddress_family(env, socketAddressObj, IPv4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        jbyteArray addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        /* AF_INET6 -> Inet6Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if (inet6Cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            jclass c = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if (c != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                inet6Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (inet6Cls != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            socketAddressObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            socketAddressObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
             * FindClass or NewObject failed so close connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
             * exist (there will be a pending exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        addr = (*env)->GetObjectField (env, socketAddressObj, ia6_ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        (*env)->SetByteArrayRegion (env, addr, 0, 16, (const char *)&him.him6.sin6_addr);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   756
        setInetAddress_family(env, socketAddressObj, IPv6);
10426
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   757
        scope = him.him6.sin6_scope_id;
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   758
        (*env)->SetIntField(env, socketAddressObj, ia6_scopeidID, scope);
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   759
        if(scope>0) {
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   760
            (*env)->SetBooleanField(env, socketAddressObj, ia6_scopeidsetID, JNI_TRUE);
90d1b830cdd6 7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents: 9035
diff changeset
   761
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /* fields common to AF_INET and AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    port = ntohs ((u_short) GET_PORT (&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    (*env)->SetIntField(env, socket, psi_portID, (int)port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    port = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    (*env)->SetIntField(env, socket, psi_localportID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 * Method:    socketAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
Java_java_net_TwoStacksPlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    jint available = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    jint res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    res = ioctlsocket(fd, FIONREAD, &available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /* if result isn't 0, it means an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        NET_ThrowNew(env, res, "socket available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
 * Method:    socketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
Java_java_net_TwoStacksPlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                           jboolean useDeferredClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    jint fd=-1, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        NET_SocketClose(fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
 * Socket options for plainsocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
 * Class:     java_net_TwoStacksPlainSocketImpl
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   838
 * Method:    socketNativeSetOption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
 * Signature: (IZLjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
JNIEXPORT void JNICALL
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   842
Java_java_net_TwoStacksPlainSocketImpl_socketNativeSetOption(JNIEnv *env,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   843
                                              jobject this,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                              jint cmd, jboolean on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                              jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Get SOCKET and check that it hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    fd1 = getFD1(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    if (fd < 0 && fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * SO_TIMEOUT is the socket option used to specify the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * for ServerSocket.accept and Socket.getInputStream().read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * It does not typically map to a native level socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * For Windows we special-case this and use the SOL_SOCKET/SO_RCVTIMEO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * socket option to specify a receive timeout on the socket. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * receive timeout is applicable to Socket only and the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * option should not be set on ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * Don't enable the socket option on ServerSocket as it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * meaningless (we don't receive on a ServerSocket).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        jobject ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if (ssObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * SO_RCVTIMEO is only supported on Microsoft's implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * of Windows Sockets so if WSAENOPROTOOPT returned then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         * reset flag and timeout will be implemented using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         * select() -- see SocketInputStream.socketRead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        if (isRcvTimeoutSupported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            jclass iCls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            jfieldID i_valueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            jint timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            CHECK_NULL(iCls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            i_valueID = (*env)->GetFieldID(env, iCls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            CHECK_NULL(i_valueID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            timeout = (*env)->GetIntField(env, value, i_valueID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
             * Disable SO_RCVTIMEO if timeout is <= 5 second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            if (timeout <= 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                sizeof(timeout)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                if (WSAGetLastError() == WSAENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    isRcvTimeoutSupported = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                if (setsockopt(fd1, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                                        sizeof(timeout)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    if (NET_MapSocketOption(cmd, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                        "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        case java_net_SocketOptions_TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        case java_net_SocketOptions_SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        case java_net_SocketOptions_SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        case java_net_SocketOptions_SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        case java_net_SocketOptions_SO_LINGER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    optval.ling.l_onoff = 1;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   973
                    optval.ling.l_linger =
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   974
                        (unsigned short)(*env)->GetIntField(env, value, fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    optval.ling.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    optval.ling.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        default: /* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                "Option not supported by TwoStacksPlainSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        if (NET_SetSockOpt(fd, level, optname, (void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            NET_ThrowCurrent(env, "setsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        if (NET_SetSockOpt(fd1, level, optname, (void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            NET_ThrowCurrent(env, "setsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
Java_java_net_TwoStacksPlainSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                              jint opt, jobject iaContainerObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * Get SOCKET and check it hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    fd1 = getFD1(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    if (fd < 0 && fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        fd = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /* For IPv6, we assume both sockets have the same setting always */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * SO_BINDADDR isn't a socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
7969
1a6282ab0d2a 7009760: Possible stack corruption in Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
chegar
parents: 5506
diff changeset
  1039
        SOCKETADDRESS him;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        jclass iaCntrClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        jfieldID iaFieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
7969
1a6282ab0d2a 7009760: Possible stack corruption in Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
chegar
parents: 5506
diff changeset
  1046
        len = sizeof(him);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            /* must be an IPV6 only socket. Case where both sockets are != -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
             * is handled in java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            fd = getFD1 (env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                             "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        CHECK_NULL_RETURN(iaObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        CHECK_NULL_RETURN(iaFieldID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return 0; /* notice change from before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Args are int except for SO_LINGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    if (opt == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        optval.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        NET_ThrowCurrent(env, "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        case java_net_SocketOptions_SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        case java_net_SocketOptions_TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        case java_net_SocketOptions_SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        case java_net_SocketOptions_SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        case java_net_SocketOptions_SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return (optval.i == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        default: /* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                "Option not supported by TwoStacksPlainSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
 * Method:    socketShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
Java_java_net_TwoStacksPlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                             jint howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * -1 already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    shutdown(fd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
 * Method:    socketSendUrgentData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
Java_java_net_TwoStacksPlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                                             jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    /* The fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    int n, fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    unsigned char d = data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        /* Bug 4086704 - If the Socket associated with this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
         * was closed (sysCloseFD), the the file descriptor is set to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    n = send(fd, (char *)&data, 1, MSG_OOB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        NET_ThrowCurrent(env, "send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
}