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