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