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