jdk/src/java.base/unix/native/libnet/PlainSocketImpl.c
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 30963 88469d06e03f
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13999
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1335
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1335
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1335
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1335
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1335
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <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>
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
    30
#if defined(__linux__)
2
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__
8549
b924a92a7c84 6835668: Use of /usr/include/linux/ files creates a dependence on kernel-headers
michaelm
parents: 7668
diff changeset
    45
#include <unistd.h>
b924a92a7c84 6835668: Use of /usr/include/linux/ files creates a dependence on kernel-headers
michaelm
parents: 7668
diff changeset
    46
#include <sys/sysctl.h>
2
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
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    73
extern void setDefaultScopeID(JNIEnv *env, struct sockaddr *him);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * file descriptor used for dup2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static int marker_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#define SET_NONBLOCKING(fd) {           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        flags |= O_NONBLOCK;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#define SET_BLOCKING(fd) {              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        flags &= ~O_NONBLOCK;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Create the marker file descriptor by establishing a loopback connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * which we shutdown but do not close the fd. The result is an fd that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * can be used for read/write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
static int getMarkerFD()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   100
    int sv[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   102
#ifdef AF_UNIX
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   103
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   104
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   106
#else
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   107
    return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   111
     * Finally shutdown sv[0] (any reads to this fd will get
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * EOF; any writes will get an error).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   114
    shutdown(sv[0], 2);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   115
    close(sv[1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   117
    return sv[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Return the file descriptor given a PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    CHECK_NULL_RETURN(fdObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * The initroto function is called whenever PlainSocketImpl is
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 12047
diff changeset
   131
 * loaded, to cache field IDs for efficiency. This is called every time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * the Java class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Method:    initProto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
Java_java_net_PlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    psi_fdID = (*env)->GetFieldID(env, cls , "fd",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                  "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    CHECK_NULL(psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    psi_addressID = (*env)->GetFieldID(env, cls, "address",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    CHECK_NULL(psi_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    CHECK_NULL(psi_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    CHECK_NULL(psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    CHECK_NULL(psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    CHECK_NULL(psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        "Ljava/net/ServerSocket;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    CHECK_NULL(psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    psi_fdLockID = (*env)->GetFieldID(env, cls, "fdLock",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                      "Ljava/lang/Object;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    CHECK_NULL(psi_fdLockID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    CHECK_NULL(psi_closePendingID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22597
diff changeset
   165
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22597
diff changeset
   166
    JNU_CHECK_EXCEPTION(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22597
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /* Create the marker fd used for dup2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    marker_fd = getMarkerFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
/* a global reference to the java.net.SocketException class. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * socketCreate, we ensure that this is initialized. This is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * prevent the problem where socketCreate runs out of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * descriptors, and is then unable to load the exception class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
static jclass socketExceptionCls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * Method:    socketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * Signature: (Z)V */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                           jboolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    jobject fdObj, ssObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    int fd;
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   188
    int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   189
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   190
    int domain = ipv6_available() ? AF_INET6 : AF_INET;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   191
#else
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   192
    int domain = AF_INET;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   193
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (socketExceptionCls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        jclass c = (*env)->FindClass(env, "java/net/SocketException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        CHECK_NULL(socketExceptionCls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        (*env)->ThrowNew(env, socketExceptionCls, "null fd object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   207
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   208
    if ((fd = socket(domain, type, 0)) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        NET_ThrowNew(env, errno, "can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   217
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   218
    /* Disable IPV6_V6ONLY to ensure dual-socket support */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   219
    if (domain == AF_INET6) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   220
        int arg = 0;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   221
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   222
                       sizeof(int)) < 0) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   223
            NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   224
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   225
            return;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   226
        }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   227
    }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   228
#endif /* AF_INET6 */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * If this is a server socket then enable SO_REUSEADDR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * automatically and set to non blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    if (ssObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int arg = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        SET_NONBLOCKING(fd);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   238
        if (NET_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   239
                       sizeof(arg)) < 0) {
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   240
            NET_ThrowNew(env, errno, "cannot set SO_REUSEADDR");
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   241
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   242
            return;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   243
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   245
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   246
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * inetAddress is the address object passed to the socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * Method:    socketConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                            jobject iaObj, jint port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                            jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    jint localport = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   267
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   268
    jclass clazz = (*env)->GetObjectClass(env, this);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    jobject fdLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    jint trafficClass = (*env)->GetIntField(env, this, psi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /* fd is an int field on iaObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /* The result of the connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        JNU_ThrowNullPointerException(env, "inet address argument null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /* connect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   296
    setDefaultScopeID(env, (struct sockaddr *)&him);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    if (trafficClass != 0 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        NET_SetTrafficClass((struct sockaddr *)&him, trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
#ifdef __solaris__
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   306
        if (connect_rv == -1 && errno == EINPROGRESS ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            /* This can happen if a blocking connect is interrupted by a signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             * See 6343810.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            while (1) {
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   312
                struct pollfd pfd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   313
                pfd.fd = fd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   314
                pfd.events = POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   316
                connect_rv = NET_Poll(&pfd, 1, -1);
624
e2bffc6b2d97 6670408: testcase panics 1.5.0_12&_14 JVM when java.net.PlainSocketImpl trying to throw an exception
chegar
parents: 2
diff changeset
   317
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   318
                if (connect_rv == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (connect_rv > 0) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   326
                    socklen_t optlen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    optlen = sizeof(connect_rv);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   329
                    if (getsockopt(fd, SOL_SOCKET, SO_ERROR,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   330
                                   (void*)&connect_rv, &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    if (connect_rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        /* restore errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        errno = connect_rv;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   337
                        connect_rv = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * A timeout was specified. We put the socket into non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * mode, connect, and then wait for the connection to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * established, fail, or timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        connect_rv = connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /* connection not established immediately */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (connect_rv != 0) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   357
            socklen_t optlen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            jlong prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                             "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                SET_BLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
             * Wait for the connection to be established or a
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   369
             * timeout occurs. poll needs to handle EINTR in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             * case lwp sig handler redirects any process signals to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
             * this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                jlong newTime;
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   375
                struct pollfd pfd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   376
                pfd.fd = fd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   377
                pfd.events = POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   379
                errno = 0;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   380
                connect_rv = NET_Poll(&pfd, 1, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (connect_rv >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (errno != EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                 * The poll was interrupted so adjust timeout and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                 * restart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    connect_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            } /* while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (connect_rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                 * Timeout out but connection may still be established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                 * At the high level it should be closed immediately but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 * just in case we make the socket blocking again and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                 * shutdown input & output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                SET_BLOCKING(fd);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   414
                shutdown(fd, 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            optlen = sizeof(connect_rv);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   420
            if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   421
                           &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /* make socket blocking again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        SET_BLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        /* restore errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (connect_rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            errno = connect_rv;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   432
            connect_rv = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /* report the appropriate exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    if (connect_rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Linux/GNU distribution setup /etc/hosts so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * InetAddress.getLocalHost gets back the loopback address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * rather than the host address. Thus a socket can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * bound to the loopback address and the connect will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * fail with EADDRNOTAVAIL. In addition the Linux kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * returns the wrong error in this case - it returns EINVAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * instead of EADDRNOTAVAIL. We handle this here so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * a more descriptive exception text is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   450
        if (connect_rv == -1 && errno == EINVAL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                "Invalid argument or cannot assign requested address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
#endif
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   456
#if defined(EPROTO)
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   457
        if (errno == EPROTO) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ProtocolException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                           "Protocol error");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   460
            return;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   461
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   462
#endif
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   463
        if (errno == ECONNREFUSED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                           "Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } else if (errno == ETIMEDOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                           "Connection timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else if (errno == EHOSTUNREACH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                           "Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } else if (errno == EADDRNOTAVAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                             "Address not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else if ((errno == EISCONN) || (errno == EBADF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /* set the remote peer address and port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    (*env)->SetIntField(env, this, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * we need to initialize the local port field if bind was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * previously to the connect (by the client) then localport field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * will already be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   499
        socklen_t slen = SOCKADDR_LEN;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   500
        if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * Method:    socketBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                         jobject iaObj, jint localport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /* fd is an int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        JNU_ThrowNullPointerException(env, "iaObj is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /* bind */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   542
    setDefaultScopeID(env, (struct sockaddr *)&him);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            errno == EPERM || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                           "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                           "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /* set the address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    (*env)->SetObjectField(env, this, psi_addressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 12047
diff changeset
   559
    /* initialize the local port */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    if (localport == 0) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   561
        socklen_t slen = sizeof(him);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   565
        if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        (*env)->SetIntField(env, this, psi_localportID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 * Method:    socketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
Java_java_net_PlainSocketImpl_socketListen (JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                            jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /* this FileDescriptor fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /* fdObj's int fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Workaround for bugid 4101691 in Solaris 2.6. See 4106600.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * If listen backlog is Integer.MAX_VALUE then subtract 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    if (count == 0x7fffffff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        count -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   606
    if (listen(fd, count) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                       "Listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
 * Method:    socketAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
 * Signature: (Ljava/net/SocketImpl;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                           jobject socket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /* fields on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    jlong prevTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /* the FileDescriptor field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jobject socketFdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /* the InetAddress field on socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    jobject socketAddressObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /* the ServerSocket fd int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /* accepted fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    jint newfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    SOCKADDR him;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   639
    socklen_t slen = SOCKADDR_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    if (IS_NULL(socket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        JNU_ThrowNullPointerException(env, "socket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * accept connection but ignore ECONNABORTED indicating that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * connection was eagerly accepted by the OS but was reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * before accept() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * If accept timeout in place and timeout is adjusted with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * each ECONNABORTED or EWOULDBLOCK to ensure that semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * of timeout are preserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        /* first usage pick up current time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (prevTime == 0 && timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /* passing a timeout of 0 to poll will return immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
           but in the case of ServerSocket 0 means infinite. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (timeout <= 0) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   673
            ret = NET_Timeout(fd, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        } else {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   675
            ret = NET_Timeout(fd, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                            "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   681
        } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
               JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   684
            } else if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   685
               JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
               NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   692
        newfd = NET_Accept(fd, (struct sockaddr *)&him, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        /* connection accepted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (newfd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            SET_BLOCKING(newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        /* non (ECONNABORTED or EWOULDBLOCK) error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            jlong currTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            timeout -= (currTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                "Accept timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            prevTime = currTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    if (newfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (newfd == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (errno == EINVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * fill up the remote peer port and address in the new socket structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    if (socketAddressObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        /* should be pending exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        close(newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Populate SocketImpl.fd.fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    (*env)->SetIntField(env, socket, psi_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /* also fill up the local port information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     port = (*env)->GetIntField(env, this, psi_localportID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    (*env)->SetIntField(env, socket, psi_localportID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
 * Method:    socketAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   779
    /* NET_SocketAvailable returns 0 for failure, 1 for success */
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   780
    if (NET_SocketAvailable(fd, &ret) == 0){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (errno == ECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            JNU_ThrowByName(env, "sun/net/ConnectionResetException", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                         "ioctl FIONREAD failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
 * Method:    socketClose0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
 * Signature: (Z)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
Java_java_net_PlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                          jboolean useDeferredClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (useDeferredClose && marker_fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            NET_Dup2(marker_fd, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
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
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
 * Method:    socketShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
Java_java_net_PlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                             jint howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * -1 already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        "socket already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   844
    shutdown(fd, howto);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
}
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
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
 * Method:    socketSetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
 * Signature: (IZLjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
Java_java_net_PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                              jint cmd, jboolean on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                              jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 12047
diff changeset
   875
     * SO_TIMEOUT is a NOOP on Solaris/Linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    if (NET_MapSocketOption(cmd, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        case java_net_SocketOptions_SO_LINGER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                fid = (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                if (cmd == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                        optval.ling.l_onoff = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                        optval.ling.l_linger = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        optval.ling.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        optval.ling.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        /* Boolean -> int */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 14342
diff changeset
   929
#if defined(__solaris__) || defined(_AIX)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (errno == EINVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // On Solaris setsockopt will set errno to EINVAL if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            // is closed. The default error message is then confusing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            char fullMsg[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
#endif /* __solaris__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                      "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
Java_java_net_PlainSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                                              jint cmd, jobject iaContainerObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        struct linger ling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * SO_BINDADDR isn't a socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    if (cmd == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        SOCKADDR him;
710
57367409c4a5 6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents: 624
diff changeset
   975
        socklen_t len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        jclass iaCntrClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        jfieldID iaFieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                             "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        CHECK_NULL_RETURN(iaObj, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        CHECK_NULL_RETURN(iaFieldID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        (*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return 0; /* notice change from before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    if (NET_MapSocketOption(cmd, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * Args are int except for SO_LINGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    if (cmd == java_net_SocketOptions_SO_LINGER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        optlen = sizeof(optval.ling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                      "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        case java_net_SocketOptions_SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            return optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return (optval.i == 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
 * Class:     java_net_PlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
 * Method:    socketSendUrgentData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
Java_java_net_PlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                             jint data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /* The fd field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    int n, fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    unsigned char d = data & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        /* Bug 4086704 - If the Socket associated with this file descriptor
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  1056
         * was closed (sysCloseFD), the file descriptor is set to -1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
  1064
    n = NET_Send(fd, (char *)&d, 1, MSG_OOB);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
  1065
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        NET_ThrowByNameWithLastError(env, "java/io/IOException", "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
}