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