jdk/src/solaris/native/java/net/PlainSocketImpl.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 624 e2bffc6b2d97
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#if defined(__linux__) && !defined(USE_SELECT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <sys/poll.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <netinet/tcp.h>        /* Defines TCP_NODELAY, needed for 2.6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <netinet/ip.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <linux/unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <linux/sysctl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include "java_net_SocketOptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include "java_net_PlainSocketImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
jfieldID psi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
jfieldID psi_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
jfieldID psi_ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
jfieldID psi_portID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
jfieldID psi_localportID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jfieldID psi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jfieldID psi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
jfieldID psi_serverSocketID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
jfieldID psi_fdLockID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
jfieldID psi_closePendingID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * file descriptor used for dup2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static int marker_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#define SET_NONBLOCKING(fd) {           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        flags |= O_NONBLOCK;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#define SET_BLOCKING(fd) {              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        flags &= ~O_NONBLOCK;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Create the marker file descriptor by establishing a loopback connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * which we shutdown but do not close the fd. The result is an fd that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * can be used for read/write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
static int getMarkerFD()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int server_fd, child_fd, connect_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int type, len, port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    type = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        type = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Create listener on any port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    server_fd = JVM_Socket(type, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (server_fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (JVM_Listen(server_fd, 1) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (JVM_GetSockName(server_fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    port = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Establish connection from client socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Server is bound to 0.0.0.0/X or ::/X
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * We connect to 127.0.0.1/X or ::1/X
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        caddr[15] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        memset((char *)him6, 0, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        him6->sin6_port = htons((short) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        him6->sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        len = sizeof(struct sockaddr_in6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        struct sockaddr_in *him4 = (struct sockaddr_in*)&him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        memset((char *) him4, 0, sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        him4->sin_port = htons((short) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        him4->sin_addr.s_addr = (uint32_t) htonl(0x7f000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        him4->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    connect_fd = JVM_Socket(type, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (connect_fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (JVM_Connect(connect_fd, (struct sockaddr *) &him, len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        JVM_SocketClose(connect_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Server accepts connection - do in in non-blocking mode to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * hanging if there's an error (should never happen!!!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    SET_NONBLOCKING(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    child_fd = JVM_Accept(server_fd, (struct sockaddr *)&him, (jint *)&len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (child_fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        JVM_SocketClose(connect_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Finally shutdown connect_fd (any reads to this fd will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * EOF; any writes will get an error).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    JVM_SocketShutdown(connect_fd, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    JVM_SocketClose(child_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    JVM_SocketClose(server_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    return connect_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * Return the file descriptor given a PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    CHECK_NULL_RETURN(fdObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * The initroto function is called whenever PlainSocketImpl is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * loaded, to cache fieldIds for efficiency. This is called everytime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * the Java class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * Method:    initProto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
Java_java_net_PlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    char *s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    psi_fdID = (*env)->GetFieldID(env, cls , "fd",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                  "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    CHECK_NULL(psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    psi_addressID = (*env)->GetFieldID(env, cls, "address",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    CHECK_NULL(psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    CHECK_NULL(psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    CHECK_NULL(psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    CHECK_NULL(psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        "Ljava/net/ServerSocket;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    CHECK_NULL(psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    psi_fdLockID = (*env)->GetFieldID(env, cls, "fdLock",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                      "Ljava/lang/Object;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    CHECK_NULL(psi_fdLockID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    CHECK_NULL(psi_closePendingID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /* Create the marker fd used for dup2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    marker_fd = getMarkerFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/* a global reference to the java.net.SocketException class. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * socketCreate, we ensure that this is initialized. This is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * prevent the problem where socketCreate runs out of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * descriptors, and is then unable to load the exception class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
static jclass socketExceptionCls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * Method:    socketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Signature: (Z)V */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                           jboolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    jobject fdObj, ssObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    int arg = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    if (socketExceptionCls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        jclass c = (*env)->FindClass(env, "java/net/SocketException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        CHECK_NULL(socketExceptionCls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        (*env)->ThrowNew(env, socketExceptionCls, "null fd object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        fd = JVM_Socket(AF_INET6, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            fd = JVM_Socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        NET_ThrowNew(env, errno, "can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * If this is a server socket then enable SO_REUSEADDR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * automatically and set to non blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (ssObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int arg = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        JVM_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            sizeof(arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * inetAddress is the address object passed to the socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * Method:    socketConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                            jobject iaObj, jint port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                            jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    jint localport = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    jobject fdLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    jint trafficClass = (*env)->GetIntField(env, this, psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /* fd is an int field on iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /* The result of the connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        JNU_ThrowNullPointerException(env, "inet address argument null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /* connect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (trafficClass != 0 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        NET_SetTrafficClass((struct sockaddr *)&him, trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (connect_rv == JVM_IO_ERR && errno == EINPROGRESS ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            /* This can happen if a blocking connect is interrupted by a signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
             * See 6343810.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                connect_rv = NET_Select(fd+1, 0, &wr, &ex, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (connect_rv == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                if (connect_rv > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    optlen = sizeof(connect_rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                        (void*)&connect_rv, &optlen) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    if (connect_rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        /* restore errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        errno = connect_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        connect_rv = JVM_IO_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * A timeout was specified. We put the socket into non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * mode, connect, and then wait for the connection to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * established, fail, or timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        connect_rv = connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        /* connection not established immediately */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (connect_rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            jlong prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                             "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                SET_BLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
             * Wait for the connection to be established or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
             * timeout occurs. poll/select needs to handle EINTR in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             * case lwp sig handler redirects any process signals to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
             * this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                jlong newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
#ifndef USE_SELECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    struct pollfd pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    pfd.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    pfd.events = POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    connect_rv = NET_Poll(&pfd, 1, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    fd_set wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    connect_rv = NET_Select(fd+1, 0, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (connect_rv >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (errno != EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                 * The poll was interrupted so adjust timeout and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                 * restart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    connect_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            } /* while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (connect_rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                 * Timeout out but connection may still be established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                 * At the high level it should be closed immediately but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                 * just in case we make the socket blocking again and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                 * shutdown input & output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                SET_BLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                JVM_SocketShutdown(fd, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            optlen = sizeof(connect_rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                               &optlen) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        /* make socket blocking again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        SET_BLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /* restore errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (connect_rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            errno = connect_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            connect_rv = JVM_IO_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /* report the appropriate exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    if (connect_rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * Linux/GNU distribution setup /etc/hosts so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * InetAddress.getLocalHost gets back the loopback address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * rather than the host address. Thus a socket can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * bound to the loopback address and the connect will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * fail with EADDRNOTAVAIL. In addition the Linux kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * returns the wrong error in this case - it returns EINVAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * instead of EADDRNOTAVAIL. We handle this here so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         * a more descriptive exception text is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (connect_rv == JVM_IO_ERR && errno == EINVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                "Invalid argument or cannot assign requested address");
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
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (connect_rv == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        } else if (errno == EPROTO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ProtocolException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                           "Protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        } else if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                           "Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else if (errno == ETIMEDOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                           "Connection timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } else if (errno == EHOSTUNREACH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                           "Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        } else if (errno == EADDRNOTAVAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                             "Address not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        } else if ((errno == EISCONN) || (errno == EBADF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                            "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * The socket may have been closed (dup'ed) while we were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * poll/select. In that case SO_ERROR will return 0 making
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * it appear that the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * To avoid any race conditions we therefore grab the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * fd lock, check if the socket has been closed, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * set the various fields whilst holding the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    fdLock = (*env)->GetObjectField(env, this, psi_fdLockID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    (*env)->MonitorEnter(env, fdLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    if ((*env)->GetBooleanField(env, this, psi_closePendingID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        /* release fdLock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        (*env)->MonitorExit(env, fdLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /* set the remote peer address and port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    (*env)->SetIntField(env, this, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * we need to initialize the local port field if bind was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * previously to the connect (by the client) then localport field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * will already be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Finally release fdLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    (*env)->MonitorExit(env, fdLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
 * Method:    socketBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                         jobject iaObj, jint localport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /* fd is an int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        JNU_ThrowNullPointerException(env, "iaObj is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /* bind */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            errno == EPERM || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                           "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                           "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /* set the address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /* intialize the local port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 * Method:    socketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
Java_java_net_PlainSocketImpl_socketListen (JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                            jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /* this FileDescriptor fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /* fdObj's int fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Workaround for bugid 4101691 in Solaris 2.6. See 4106600.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * If listen backlog is Integer.MAX_VALUE then subtract 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    if (count == 0x7fffffff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        count -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    if (JVM_Listen(fd, count) == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                       "Listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
 * Method:    socketAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
 * Signature: (Ljava/net/SocketImpl;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                           jobject socket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /* fields on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    jlong prevTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /* the FileDescriptor field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    jobject socketFdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /* the InetAddress field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    jobject socketAddressObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /* the ServerSocket fd int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /* accepted fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    jint newfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    jthrowable error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    if (IS_NULL(socket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        JNU_ThrowNullPointerException(env, "socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * accept connection but ignore ECONNABORTED indicating that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * connection was eagerly accepted by the OS but was reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * before accept() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * If accept timeout in place and timeout is adjusted with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * each ECONNABORTED or EWOULDBLOCK to ensure that semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * of timeout are preserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        /* first usage pick up current time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if (prevTime == 0 && timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        /* passing a timeout of 0 to poll will return immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
           but in the case of ServerSocket 0 means infinite. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            ret = NET_Timeout(fd, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
               JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
               NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        /* connection accepted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (newfd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            SET_BLOCKING(newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        /* non (ECONNABORTED or EWOULDBLOCK) error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        /* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            jlong currTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            timeout -= (currTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                                "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            prevTime = currTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    if (newfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (newfd == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (errno == EINVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * fill up the remote peer port and address in the new socket structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        /* should be pending exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        close(newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Populate SocketImpl.fd.fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    (*env)->SetIntField(env, socket, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /* also fill up the local port information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     port = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    (*env)->SetIntField(env, socket, psi_localportID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
 * Method:    socketAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /* JVM_SocketAvailable returns 0 for failure, 1 for success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    if (!JVM_SocketAvailable(fd, &ret)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (errno == ECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            JNU_ThrowByName(env, "sun/net/ConnectionResetException", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                         "ioctl FIONREAD failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * Method:    socketClose0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 * Signature: (Z)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
Java_java_net_PlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                          jboolean useDeferredClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if (useDeferredClose && marker_fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            NET_Dup2(marker_fd, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            NET_SocketClose(fd);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
 * Method:    socketShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
Java_java_net_PlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                                             jint howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * -1 already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    JVM_SocketShutdown(fd, howto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
 * Method:    socketSetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
 * Signature: (IZLjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
Java_java_net_PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                                              jint cmd, jboolean on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                              jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * SO_TIMEOUT is a no-op on Solaris/Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    if (NET_MapSocketOption(cmd, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        case java_net_SocketOptions_SO_LINGER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                if (cmd == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                        optval.ling.l_onoff = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                        optval.ling.l_linger = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        optval.ling.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        optval.ling.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        /* Boolean -> int */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (errno == EINVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            // On Solaris setsockopt will set errno to EINVAL if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            // is closed. The default error message is then confusing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            char fullMsg[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
#endif /* __solaris__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                      "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
Java_java_net_PlainSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                                              jint cmd, jobject iaContainerObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * SO_BINDADDR isn't a socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    if (cmd == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        jclass iaCntrClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        jfieldID iaFieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                             "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        CHECK_NULL_RETURN(iaObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        CHECK_NULL_RETURN(iaFieldID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        return 0; /* notice change from before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    if (NET_MapSocketOption(cmd, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * Args are int except for SO_LINGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    if (cmd == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                                      "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        case java_net_SocketOptions_SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            return optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            return (optval.i == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
}
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_PlainSocketImpl
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_PlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                                             jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /* The fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    int n, fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    unsigned char d = data & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        /* Bug 4086704 - If the Socket associated with this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
         * was closed (sysCloseFD), the the file descriptor is set to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    n = JVM_Send(fd, (char *)&d, 1, MSG_OOB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        NET_ThrowByNameWithLastError(env, "java/io/IOException", "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
}