src/java.base/windows/native/libnet/TwoStacksPlainSocketImpl.c
author igerasim
Sun, 18 Feb 2018 17:06:20 -0800
changeset 48911 edaa989b4e67
parent 47216 71c04702a3d5
child 49413 0ee57b9b376c
permissions -rw-r--r--
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
jfieldID psi_portID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
jfieldID psi_localportID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
jfieldID psi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
jfieldID psi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
jfieldID psi_serverSocketID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the level of the TCP protocol for setsockopt and getsockopt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * we only want to look this up once, from the static initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static int tcp_level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The initProto function is called whenever TwoStacksPlainSocketImpl is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * loaded, to cache fieldIds for efficiency. This is called everytime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the Java class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Method:    initProto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
Java_java_net_TwoStacksPlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    struct protoent *proto = getprotobyname("TCP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    psi_fdID = (*env)->GetFieldID(env, cls , "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    CHECK_NULL(psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    psi_addressID = (*env)->GetFieldID(env, cls, "address",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    CHECK_NULL(psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    CHECK_NULL(psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    CHECK_NULL(psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    CHECK_NULL(psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                            "Ljava/net/ServerSocket;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    CHECK_NULL(psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Method:    socketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Signature: (Z)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
Java_java_net_TwoStacksPlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                           jboolean stream) {
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   108
    jobject fdObj;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   109
    int fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        "null fd object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    fd = socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        NET_ThrowCurrent(env, "create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /* Set socket attribute so it is not passed to any child process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * inetAddress is the address object passed to the socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Method:    socketConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   139
                                                     jobject iaObj, jint port,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   140
                                                     jint timeout)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    jint localport = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /* family and localport are int fields of iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int family;
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   146
    jint fd = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    jint len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   151
    SOCKETADDRESS sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /* The result of the connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    int connect_res;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   155
    memset((char *)&sa, 0, sizeof(sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        JNU_ThrowNullPointerException(env, "inet address argument is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   166
    if (NET_InetAddressToSockaddr(env, iaObj, port, &sa, &len,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   167
                                  JNI_FALSE) != 0) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   168
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   171
    family = sa.sa.sa_family;
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   172
    if (family != AF_INET) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   173
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   174
                        "Protocol family not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   176
    if (fd == -1) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   177
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   178
                        "Destination unreachable");
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   179
        return;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   180
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (timeout <= 0) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   183
        connect_res = connect(fd, &sa.sa, sizeof(SOCKETADDRESS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (connect_res == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            connect_res = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int optlen = sizeof(optval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /* make socket non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        optval = 1;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   193
        ioctlsocket(fd, FIONBIO, &optval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /* initiate the connect */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   196
        connect_res = connect(fd, &sa.sa, sizeof(SOCKETADDRESS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (connect_res == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                connect_res = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 * Wait for timout, connection established or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                 * connection failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                connect_res = select(fd+1, 0, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                 * Timeout before connection is established/failed so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                 * we throw exception and shutdown input/output to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                 * socket from being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                 * The socket should be closed immediately by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (connect_res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                    "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    shutdown( fd, SD_BOTH );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                     /* make socket blocking again - just in case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    optval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    ioctlsocket( fd, FIONBIO, &optval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 * We must now determine if the connection has been established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 * or if it has failed. The logic here is designed to work around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                 * bug on Windows NT whereby using getsockopt to obtain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                 * last error (SO_ERROR) indicates there is no error. The workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                 * on NT is to allow winsock to be scheduled and this is done by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                 * yielding and retrying. As yielding is problematic in heavy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 * load conditions we attempt up to 3 times to get the error reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (!FD_ISSET(fd, &ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    connect_res = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    int retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    for (retry=0; retry<3; retry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        NET_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                       (char*)&connect_res, &optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        if (connect_res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        Sleep(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    if (connect_res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                        "Unable to establish connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        /* make socket blocking again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        optval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ioctlsocket(fd, FIONBIO, &optval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if (connect_res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (connect_res == WSAEADDRNOTAVAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            JNU_ThrowByName(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                "connect: Address is invalid on local machine, or port is not valid on remote machine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            NET_ThrowNew(env, connect_res, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /* set the remote peer address and port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    (*env)->SetIntField(env, this, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * we need to initialize the local port field if bind was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * previously to the connect (by the client) then localport field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * will already be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        u_short port;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   296
        int len = sizeof(SOCKETADDRESS);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   297
        if (getsockname(fd, &sa.sa, &len) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (WSAGetLastError() == WSAENOTSOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   300
                                "Socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                NET_ThrowCurrent(env, "getsockname failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   306
        port = ntohs((u_short)GET_PORT(&sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        (*env)->SetIntField(env, this, psi_localportID, (int) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * Method:    socketBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   318
                                         jobject iaObj, jint localport,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   319
                                         jboolean exclBind) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /* fdObj is the FileDescriptor field on this */
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   322
    jobject fdObj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /* fd is an int field on fdObj */
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   324
    int fd, len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /* family is an int field of iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    int family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   330
    SOCKETADDRESS sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   334
    family = getInetAddress_family(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   336
    if (family != java_net_InetAddress_IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        "Protocol family not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   342
    if (IS_NULL(fdObj)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        JNU_ThrowNullPointerException(env, "inet address argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   354
    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
   355
                                  JNI_FALSE) != 0) {
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   356
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   358
    rv = NET_WinBind(fd, &sa, len, exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if (rv == -1) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   361
        NET_ThrowCurrent(env, "NET_Bind");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /* set the address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /* intialize the local port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        /* Now that we're a bound socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   373
        int len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        u_short port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   376
        if (getsockname(fd, &sa.sa, &len) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            NET_ThrowCurrent(env, "getsockname in plain socketBind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   380
        port = ntohs((u_short) GET_PORT (&sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   382
        (*env)->SetIntField(env, this, psi_localportID, (int)port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 * Method:    socketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
JNIEXPORT void JNICALL
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   394
Java_java_net_TwoStacksPlainSocketImpl_socketListen
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   395
  (JNIEnv *env, jobject this, jint count)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /* this FileDescriptor fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    jobject address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /* fdObj's int fd field */
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   401
    int fd = INVALID_SOCKET;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   402
    SOCKETADDRESS addr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   403
    int addrlen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   405
    if (IS_NULL(fdObj)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    address = (*env)->GetObjectField(env, this, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        JNU_ThrowNullPointerException(env, "socket address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   419
    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
   420
                                  JNI_FALSE) != 0) {
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   421
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   424
    if (addr.sa.sa_family == AF_INET) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        /* listen on v4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (listen(fd, count) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            NET_ThrowCurrent(env, "listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    } else {
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   430
        NET_SocketClose(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        (*env)->SetObjectField(env, this, psi_fdID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 * Method:    socketAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * Signature: (Ljava/net/SocketImpl;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                           jobject socket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /* fields on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    jint port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /* the FileDescriptor field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    jobject socketFdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   452
    /* cache the Inet4 class */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    static jclass inet4Cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /* the InetAddress field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    jobject socketAddressObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /* the fd int field on fdObj */
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   459
    jint fd=-1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   461
    SOCKETADDRESS sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    jint len;
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   463
    int ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   465
    if (IS_NULL(fdObj)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   470
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   471
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   472
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (IS_NULL(socket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        JNU_ThrowNullPointerException(env, "socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        socketAddressObj = (*env)->GetObjectField(env, socket, psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    if ((IS_NULL(socketAddressObj)) || (IS_NULL(socketFdObj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        JNU_ThrowNullPointerException(env, "socket address or fd obj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   484
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   485
    len = sizeof(struct sockaddr_in);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   486
    if (timeout) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   487
        ret = NET_Timeout(fd, timeout);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   488
        if (ret == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                            "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return;
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   492
        } else if (ret == -1) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   493
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   494
        /* REMIND: SOCKET CLOSED PROBLEM */
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   495
/*        NET_ThrowCurrent(env, "Accept failed"); */
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   496
            return;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   497
        } else if (ret == -2) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   498
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   499
                            "operation interrupted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   503
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   504
    fd = accept(fd, &sa.sa, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /* REMIND: SOCKET CLOSED PROBLEM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (fd == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
28681
fc0698dfbb23 8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows
chegar
parents: 28059
diff changeset
   516
    SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   519
    if (sa.sa.sa_family != AF_INET) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   520
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   521
                        "Protocol family not supported");
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   522
        return;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   523
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   525
    if (inet4Cls == NULL) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   526
        jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   527
        if (c != NULL) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   528
            inet4Cls = (*env)->NewGlobalRef(env, c);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   529
            (*env)->DeleteLocalRef(env, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   531
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   533
    /*
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   534
     * fill up the remote peer port and address in the new socket structure
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   535
     */
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   536
    if (inet4Cls != NULL) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   537
        socketAddressObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    } else {
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   539
        socketAddressObj = NULL;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   540
    }
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   541
    if (socketAddressObj == NULL) {
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   542
        /*
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   543
         * FindClass or NewObject failed so close connection and
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   544
         * exit (there will be a pending exception).
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   545
         */
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   546
        NET_SocketClose(fd);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   547
        return;
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   548
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   550
    setInetAddress_addr(env, socketAddressObj, ntohl(sa.sa4.sin_addr.s_addr));
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   551
    setInetAddress_family(env, socketAddressObj, java_net_InetAddress_IPv4);
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   552
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   553
    port = ntohs ((u_short)GET_PORT(&sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    (*env)->SetIntField(env, socket, psi_portID, (int)port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    port = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    (*env)->SetIntField(env, socket, psi_localportID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
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
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 * Method:    socketAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
Java_java_net_TwoStacksPlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    jint available = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    jint res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    res = ioctlsocket(fd, FIONREAD, &available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /* if result isn't 0, it means an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        NET_ThrowNew(env, res, "socket available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
 * Method:    socketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
Java_java_net_TwoStacksPlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                           jboolean useDeferredClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   597
    jint fd=-1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   599
    if (IS_NULL(fdObj)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
 * Socket options for plainsocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
 * Class:     java_net_TwoStacksPlainSocketImpl
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   618
 * Method:    socketNativeSetOption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 * Signature: (IZLjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
JNIEXPORT void JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   622
Java_java_net_TwoStacksPlainSocketImpl_socketNativeSetOption
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   623
  (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
   624
{
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   625
    int fd;
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
   626
    int level = 0, optname = 0, optlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
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
   632
    memset((char *)&optval, 0, sizeof(optval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Get SOCKET and check that it hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    fd = getFD(env, this);
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   637
    if (fd < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * SO_TIMEOUT is the socket option used to specify the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * for ServerSocket.accept and Socket.getInputStream().read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * It does not typically map to a native level socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * For Windows we special-case this and use the SOL_SOCKET/SO_RCVTIMEO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * socket option to specify a receive timeout on the socket. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * receive timeout is applicable to Socket only and the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * option should not be set on ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         * Don't enable the socket option on ServerSocket as it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * meaningless (we don't receive on a ServerSocket).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        jobject ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (ssObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         * SO_RCVTIMEO is only supported on Microsoft's implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         * of Windows Sockets so if WSAENOPROTOOPT returned then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * reset flag and timeout will be implemented using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         * select() -- see SocketInputStream.socketRead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (isRcvTimeoutSupported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            jclass iCls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            jfieldID i_valueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            jint timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            CHECK_NULL(iCls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            i_valueID = (*env)->GetFieldID(env, iCls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            CHECK_NULL(i_valueID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            timeout = (*env)->GetIntField(env, value, i_valueID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
             * Disable SO_RCVTIMEO if timeout is <= 5 second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (timeout <= 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                sizeof(timeout)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                if (WSAGetLastError() == WSAENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    isRcvTimeoutSupported = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   691
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * 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
   700
     * level and option name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    if (NET_MapSocketOption(cmd, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   703
        JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        case java_net_SocketOptions_TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        case java_net_SocketOptions_SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        case java_net_SocketOptions_SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        case java_net_SocketOptions_SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        case java_net_SocketOptions_SO_LINGER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    optval.ling.l_onoff = 1;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   746
                    optval.ling.l_linger =
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   747
                        (unsigned short)(*env)->GetIntField(env, value, fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    optval.ling.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    optval.ling.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        default: /* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                "Option not supported by TwoStacksPlainSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (NET_SetSockOpt(fd, level, optname, (void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            NET_ThrowCurrent(env, "setsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
JNIEXPORT jint JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   776
Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   777
  (JNIEnv *env, jobject this, jint opt, jobject iaContainerObj)
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   778
{
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   779
    int fd;
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
   780
    int level = 0, optname = 0, optlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    } optval;
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   785
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Get SOCKET and check it hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    fd = getFD(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
   790
    memset((char *)&optval, 0, sizeof(optval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
48911
edaa989b4e67 8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
   792
    if (fd < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * SO_BINDADDR isn't a socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   801
        SOCKETADDRESS sa;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   802
        int len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        jclass iaCntrClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        jfieldID iaFieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   808
        memset((char *)&sa, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   810
        if (getsockname(fd, &sa.sa, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28681
diff changeset
   811
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28681
diff changeset
   812
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   815
        iaObj = NET_SockaddrToInetAddress(env, &sa, &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        CHECK_NULL_RETURN(iaObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        CHECK_NULL_RETURN(iaFieldID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        return 0; /* notice change from before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    if (NET_MapSocketOption(opt, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
   830
        JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Args are int except for SO_LINGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    if (opt == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        optval.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        NET_ThrowCurrent(env, "getsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        case java_net_SocketOptions_SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        case java_net_SocketOptions_TCP_NODELAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        case java_net_SocketOptions_SO_OOBINLINE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        case java_net_SocketOptions_SO_KEEPALIVE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        case java_net_SocketOptions_SO_REUSEADDR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            return (optval.i == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        default: /* shouldn't get here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                "Option not supported by TwoStacksPlainSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
 * Method:    socketShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
Java_java_net_TwoStacksPlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                                             jint howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * -1 already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    shutdown(fd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
 * Class:     java_net_TwoStacksPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
 * Method:    socketSendUrgentData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
Java_java_net_TwoStacksPlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                             jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /* The fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    int n, fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    unsigned char d = data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        /* 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
   917
         * was closed (sysCloseFD), the file descriptor is set to -1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    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
   926
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        NET_ThrowCurrent(env, "send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
}