jdk/src/java.base/windows/native/libnet/TwoStacksPlainSocketImpl.c
author clanger
Thu, 30 Jun 2016 09:30:09 +0200
changeset 39318 2006d1d41c8b
parent 28681 fc0698dfbb23
child 41214 855ac576eb77
permissions -rw-r--r--
8158023: SocketExceptions contain too little information sometimes Reviewed-by: rriggs, msheppar, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28681
diff changeset
     2
 * Copyright (c) 1997, 2016, 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 "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
jfieldID psi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
jfieldID psi_fd1ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jfieldID psi_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
jfieldID psi_portID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
jfieldID psi_localportID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
jfieldID psi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jfieldID psi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
jfieldID psi_serverSocketID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
jfieldID psi_lastfdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the level of the TCP protocol for setsockopt and getsockopt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * we only want to look this up once, from the static initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * of TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
static int tcp_level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static int getFD1(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
}
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
 * The initProto function is called whenever TwoStacksPlainSocketImpl is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * loaded, to cache fieldIds for efficiency. This is called everytime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * the Java class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Method:    initProto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
Java_java_net_TwoStacksPlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    struct protoent *proto = getprotobyname("TCP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    psi_fdID = (*env)->GetFieldID(env, cls , "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    CHECK_NULL(psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    psi_fd1ID =(*env)->GetFieldID(env, cls , "fd1", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    CHECK_NULL(psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    psi_addressID = (*env)->GetFieldID(env, cls, "address",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    CHECK_NULL(psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    psi_lastfdID = (*env)->GetFieldID(env, cls, "lastfd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    CHECK_NULL(psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    CHECK_NULL(psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    CHECK_NULL(psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                            "Ljava/net/ServerSocket;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    CHECK_NULL(psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Method:    socketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Signature: (Z)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
Java_java_net_TwoStacksPlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                           jboolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    jobject fdObj, fd1Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        "null fd object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    fd = socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        NET_ThrowCurrent(env, "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /* Set socket attribute so it is not passed to any child process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            "null fd1 object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        fd1 = socket(AF_INET6, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            NET_ThrowCurrent(env, "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            /* Set socket attribute so it is not passed to any child process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            SetHandleInformation((HANDLE)(UINT_PTR)fd1, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
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
 * inetAddress is the address object passed to the socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * Method:    socketConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                            jobject iaObj, jint port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                            jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    jint localport = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /* family and localport are int fields of iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    int family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    jint fd, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    jint len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    int  ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* fd initially points to the IPv4 socket and fd1 to the IPv6 socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * If we want to connect to IPv6 then we swap the two sockets/objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * This way, fd is always the connected socket, and fd1 always gets closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    SOCKETADDRESS him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /* The result of the connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    int connect_res;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   210
    memset((char *)&him, 0, sizeof(him));
2
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 */
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   403
    int fd, fd1, len = 0;
2
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
            }
26458
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   470
        } else {
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   471
            /* NET_BindV6() closes both sockets upon a failure */
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   472
            (*env)->SetObjectField(env, this, psi_fdID, NULL);
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   473
            (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    } else {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   476
        rv = NET_WinBind(fd, (struct sockaddr *)&him, len, exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    if (rv == -1) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   480
        NET_ThrowCurrent(env, "NET_Bind");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /* set the address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /* intialize the local port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /* Now that we're a bound socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int len = SOCKETADDRESS_LEN(&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        u_short port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        fd = him.him.sa_family == AF_INET? fd: fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            NET_ThrowCurrent(env, "getsockname in plain socketBind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        port = ntohs ((u_short) GET_PORT (&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        (*env)->SetIntField(env, this, psi_localportID, (int) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * Method:    socketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                            jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /* this FileDescriptor fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    jobject address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /* fdObj's int fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    SOCKETADDRESS addr; int addrlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /* Listen on V4 if address type is v4 or if v6 and address is ::0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * 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
   536
     * In cases, where we listen on one space only, we close the other socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    address = (*env)->GetObjectField(env, this, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        JNU_ThrowNullPointerException(env, "socket address");
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
    if (NET_InetAddressToSockaddr(env, address, 0, (struct sockaddr *)&addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                  &addrlen, JNI_FALSE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    if (addr.him.sa_family == AF_INET || IN6ADDR_ISANY(&addr.him6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        /* listen on v4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (listen(fd, count) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            NET_ThrowCurrent(env, "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        NET_SocketClose (fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        (*env)->SetObjectField(env, this, psi_fdID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    if (ipv6_available() && !IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (addr.him.sa_family == AF_INET6 || addr.him4.sin_addr.s_addr == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            /* listen on v6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (listen(fd1, count) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                NET_ThrowCurrent(env, "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            NET_SocketClose (fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 * Method:    socketAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
 * Signature: (Ljava/net/SocketImpl;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                           jobject socket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /* fields on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    jint port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /* the FileDescriptor field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    jobject socketFdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /* cache the Inet4/6Address classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    static jclass inet4Cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    static jclass inet6Cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /* the InetAddress field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    jobject socketAddressObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /* the fd int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    jint fd=-1, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    SOCKETADDRESS him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    jint len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    if (IS_NULL(socket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        JNU_ThrowNullPointerException(env, "socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        socketAddressObj = (*env)->GetObjectField(env, socket, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    if ((IS_NULL(socketAddressObj)) || (IS_NULL(socketFdObj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        JNU_ThrowNullPointerException(env, "socket address or fd obj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    if (fd != -1 && fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        fd_set rfds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        struct timeval t, *tP=&t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int lastfd, res, fd2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        FD_ZERO(&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        FD_SET(fd,&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        FD_SET(fd1,&rfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            t.tv_sec = timeout/1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            t.tv_usec = (timeout%1000)*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            tP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        res = select (fd, &rfds, NULL, NULL, tP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                            "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } else if (res == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            fd2 = FD_ISSET(fd, &rfds)? fd: fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        } else if (res == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            /* avoid starvation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            lastfd = (*env)->GetIntField(env, this, psi_lastfdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (lastfd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                fd2 = lastfd==fd? fd1: fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                fd2 = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            (*env)->SetIntField(env, this, psi_lastfdID, fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                            "select failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (fd2 == fd) { /* v4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            len = sizeof (struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            len = sizeof (struct SOCKADDR_IN6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        fd = fd2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            fd = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            len = sizeof (struct SOCKADDR_IN6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            len = sizeof (struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            } else if (ret == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            /* REMIND: SOCKET CLOSED PROBLEM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /*        NET_ThrowCurrent(env, "Accept failed"); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            } else if (ret == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    fd = accept(fd, (struct sockaddr *)&him, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        /* REMIND: SOCKET CLOSED PROBLEM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (fd == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
28681
fc0698dfbb23 8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows
chegar
parents: 28059
diff changeset
   702
    SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    if (him.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (inet4Cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (c != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                inet4Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * fill up the remote peer port and address in the new socket structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (inet4Cls != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            socketAddressObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            socketAddressObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
             * FindClass or NewObject failed so close connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
             * exist (there will be a pending exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   731
        setInetAddress_addr(env, socketAddressObj, ntohl(him.him4.sin_addr.s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   732
        setInetAddress_family(env, socketAddressObj, IPv4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        /* AF_INET6 -> Inet6Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (inet6Cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            jclass c = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (c != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                inet6Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (inet6Cls != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            socketAddressObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            socketAddressObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
             * FindClass or NewObject failed so close connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
             * exist (there will be a pending exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 20821
diff changeset
   757
        setInet6Address_ipaddress(env, socketAddressObj, (char *)&him.him6.sin6_addr);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   758
        setInetAddress_family(env, socketAddressObj, IPv6);
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 18192
diff changeset
   759
        setInet6Address_scopeid(env, socketAddressObj, him.him6.sin6_scope_id);
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 18192
diff changeset
   760
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /* fields common to AF_INET and AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    port = ntohs ((u_short) GET_PORT (&him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    (*env)->SetIntField(env, socket, psi_portID, (int)port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    port = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    (*env)->SetIntField(env, socket, psi_localportID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
 * Method:    socketAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
Java_java_net_TwoStacksPlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    jint available = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    jint res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    res = ioctlsocket(fd, FIONREAD, &available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /* if result isn't 0, it means an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        NET_ThrowNew(env, res, "socket available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
 * Method:    socketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
Java_java_net_TwoStacksPlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                           jboolean useDeferredClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    jint fd=-1, fd1=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        NET_SocketClose(fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
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
 * Socket options for plainsocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
 * Class:     java_net_TwoStacksPlainSocketImpl
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   837
 * Method:    socketNativeSetOption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
 * Signature: (IZLjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
JNIEXPORT void JNICALL
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   841
Java_java_net_TwoStacksPlainSocketImpl_socketNativeSetOption(JNIEnv *env,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   842
                                              jobject this,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                              jint cmd, jboolean on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                              jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    int fd, fd1;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   846
    int level = 0, optname = 0, optlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   852
    memset((char *)&optval, 0, sizeof(optval));
2
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;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1013
    int level = 0, optname = 0, optlen = 0;
2
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
     * Get SOCKET and check it hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    fd1 = getFD1(env, this);
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1023
    memset((char *)&optval, 0, sizeof(optval));
2
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);
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1047
        memset((char *)&him, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            /* must be an IPV6 only socket. Case where both sockets are != -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
             * is handled in java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            fd = getFD1 (env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28681
diff changeset
  1057
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28681
diff changeset
  1058
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        CHECK_NULL_RETURN(iaObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        CHECK_NULL_RETURN(iaFieldID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        return 0; /* notice change from before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * Args are int except for SO_LINGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    if (opt == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        optval.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        NET_ThrowCurrent(env, "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        case java_net_SocketOptions_SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        case java_net_SocketOptions_TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        case java_net_SocketOptions_SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        case java_net_SocketOptions_SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        case java_net_SocketOptions_SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            return (optval.i == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        default: /* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                "Option not supported by TwoStacksPlainSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            return -1;
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
 * Method:    socketShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
Java_java_net_TwoStacksPlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                                             jint howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * -1 already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    shutdown(fd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
 * Method:    socketSendUrgentData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
Java_java_net_TwoStacksPlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                                             jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /* The fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    int n, fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    unsigned char d = data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        /* Bug 4086704 - If the Socket associated with this file descriptor
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26458
diff changeset
  1163
         * was closed (sysCloseFD), the file descriptor is set to -1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    n = send(fd, (char *)&data, 1, MSG_OOB);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1172
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        NET_ThrowCurrent(env, "send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
}