src/java.base/unix/native/libnio/ch/DatagramChannelImpl.c
author igerasim
Fri, 25 May 2018 12:44:34 -0700
changeset 50275 69204b98dc3d
parent 47216 71c04702a3d5
child 59329 289000934908
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
/*
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 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: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    38
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "net_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "sun_nio_ch_DatagramChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static jfieldID dci_senderID;   /* sender in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static jclass isa_class;        /* java.net.InetSocketAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jmethodID isa_ctorID;    /*   .InetSocketAddress(InetAddress, int) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    59
    CHECK_NULL(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    isa_class = (*env)->NewGlobalRef(env, clazz);
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    61
    if (isa_class == NULL) {
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    62
        JNU_ThrowOutOfMemoryError(env, NULL);
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    63
        return;
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    64
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                     "(Ljava/net/InetAddress;I)V");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    67
    CHECK_NULL(isa_ctorID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    70
    CHECK_NULL(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                      "Ljava/net/SocketAddress;");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    73
    CHECK_NULL(dci_senderID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    dci_senderAddrID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                          "cachedSenderInetAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                          "Ljava/net/InetAddress;");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    77
    CHECK_NULL(dci_senderAddrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    dci_senderPortID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                          "cachedSenderPort", "I");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
    80
    CHECK_NULL(dci_senderPortID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12047
diff changeset
    85
                                                jobject fdo, jboolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
    90
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    rv = connect(fd, 0, 0);
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
    92
#else
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
    93
    SOCKETADDRESS sa;
43889
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
    94
    socklen_t len = isIPv6 ? sizeof(struct sockaddr_in6) :
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
    95
                             sizeof(struct sockaddr_in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
    97
    memset(&sa, 0, sizeof(sa));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    98
#if defined(_ALLBSD_SOURCE)
43889
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
    99
    sa.sa.sa_family = isIPv6 ? AF_INET6 : AF_INET;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   100
#else
43889
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
   101
    sa.sa.sa_family = AF_UNSPEC;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   102
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   104
    rv = connect(fd, &sa.sa, len);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   105
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   106
#if defined(_ALLBSD_SOURCE)
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   107
    if (rv < 0 && errno == EADDRNOTAVAIL)
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   108
        rv = errno = 0;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   109
#elif defined(_AIX)
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   110
    /* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254:
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   111
     * 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   112
     * but that is acceptable.
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   113
     */
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   114
    if (rv < 0 && errno == EAFNOSUPPORT)
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   115
        rv = errno = 0;
43889
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
   116
#endif // defined(_ALLBSD_SOURCE) || defined(_AIX)
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
   117
212e765112d4 8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents: 43100
diff changeset
   118
#endif // defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    if (rv < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                             jobject fdo, jlong address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                             jint len, jboolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    void *buf = (void *)jlong_to_ptr(address);
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   131
    SOCKETADDRESS sa;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   132
    socklen_t sa_len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    jboolean retry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    jint n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    jobject senderAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    if (len > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        len = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        retry = JNI_FALSE;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   143
        n = recvfrom(fd, buf, len, 0, &sa.sa, &sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (n < 0) {
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 47216
diff changeset
   145
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (connected == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    retry = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    JNU_ThrowByName(env, JNU_JAVANETPKG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                    "PortUnreachableException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    } while (retry == JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * If the source address and port match the cached address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * and port in DatagramChannelImpl then we don't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * create InetAddress and InetSocketAddress objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (senderAddr != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   172
        if (!NET_SockaddrEqualsInetAddress(env, &sa, senderAddr)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            senderAddr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            jint port = (*env)->GetIntField(env, this, dci_senderPortID);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   176
            if (port != NET_GetPortFromSockaddr(&sa)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                senderAddr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (senderAddr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        jobject isa = NULL;
29118
8782a8e91d4c 8046901: Check jdk/src/solaris/native/sun/nio for Parfait flagged uninitialized memory
msheppar
parents: 25859
diff changeset
   183
        int port = 0;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   184
        jobject ia = NET_SockaddrToInetAddress(env, &sa, &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (ia != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22605
diff changeset
   188
        CHECK_NULL_RETURN(isa, IOS_THROWN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        (*env)->SetIntField(env, this, dci_senderPortID,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   192
                            NET_GetPortFromSockaddr(&sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        (*env)->SetObjectField(env, this, dci_senderID, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   200
                                          jboolean preferIPv6, jobject fdo, jlong address,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12547
diff changeset
   201
                                          jint len, jobject destAddress, jint destPort)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    void *buf = (void *)jlong_to_ptr(address);
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   205
    SOCKETADDRESS sa;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   206
    int sa_len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    jint n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (len > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        len = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41380
diff changeset
   213
    if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   214
                                  &sa_len, preferIPv6) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
      return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 29118
diff changeset
   218
    n = sendto(fd, buf, len, 0, &sa.sa, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    if (n < 0) {
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 47216
diff changeset
   220
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}