src/java.base/windows/native/libnio/ch/DatagramChannelImpl.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43100 jdk/src/java.base/windows/native/libnio/ch/DatagramChannelImpl.c@a7e3457672c7
child 59329 289000934908
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12547
diff changeset
     2
 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "sun_nio_ch_DatagramChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
static jfieldID dci_senderID;   /* sender in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static jclass isa_class;        /* java.net.InetSocketAddress */
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    41
static jmethodID isa_ctorID;    /* java.net.InetSocketAddress(InetAddress, int) */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    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: 16100
diff changeset
    48
    CHECK_NULL(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    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: 16100
diff changeset
    50
    if (isa_class == NULL) {
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    51
        JNU_ThrowOutOfMemoryError(env, NULL);
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    52
        return;
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    53
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                     "(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: 16100
diff changeset
    56
    CHECK_NULL(isa_ctorID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    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: 16100
diff changeset
    59
    CHECK_NULL(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                      "Ljava/net/SocketAddress;");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    62
    CHECK_NULL(dci_senderID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    dci_senderAddrID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                          "cachedSenderInetAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                          "Ljava/net/InetAddress;");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    66
    CHECK_NULL(dci_senderAddrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    dci_senderPortID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                          "cachedSenderPort", "I");
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
    69
    CHECK_NULL(dci_senderPortID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * This function "purges" all outstanding ICMP port unreachable packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * outstanding on a socket and returns JNI_TRUE if any ICMP messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * have been purged. The rational for purging is to emulate normal BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * behaviour whereby receiving a "connection reset" status resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    jboolean got_icmp = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    struct timeval t = { 0, 0 };
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    85
    SOCKETADDRESS sa;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    86
    int addrlen = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Peek at the queue to see if there is an ICMP port unreachable. If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * is then receive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    FD_SET((u_int)fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    while(1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (recvfrom(fd, buf, 1, MSG_PEEK,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
    99
                     &sa.sa, &addrlen) != SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (WSAGetLastError() != WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            /* some other error - we don't care here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   107
        recvfrom(fd, buf, 1, 0, &sa.sa, &addrlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        got_icmp = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return got_icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
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: 7668
diff changeset
   116
                                                jobject fdo, jboolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    int rv = 0;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   120
    SOCKETADDRESS sa;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   121
    int sa_len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   123
    memset(&sa, 0, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   125
    rv = connect((SOCKET)fd, &sa.sa, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        handleSocketError(env, WSAGetLastError());
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   128
    } else {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   129
        /* Disable WSAECONNRESET errors as socket is no longer connected */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   130
        BOOL enable = FALSE;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   131
        DWORD bytesReturned = 0;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   132
        WSAIoctl((SOCKET)fd, SIO_UDP_CONNRESET, &enable, sizeof(enable),
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
   133
                 NULL, 0, &bytesReturned, NULL, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                            jobject fdo, jlong address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                            jint len, jboolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void *buf = (void *)jlong_to_ptr(address);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   144
    SOCKETADDRESS sa;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   145
    int sa_len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    BOOL retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    jint n;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   148
    jobject senderAddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        n = recvfrom((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                     (char *)buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                     len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     0,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   156
                     &sa.sa,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                     &sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (n == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (theErr == WSAEMSGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                /* Spec says the rest of the data will be discarded... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else if (theErr == WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                purgeOutstandingICMP(env, this, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (connected == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else return handleSocketError(env, theErr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   178
    /*
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   179
     * If the source address and port match the cached address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
     * and port in DatagramChannelImpl then we don't need to
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   181
     * create InetAddress and InetSocketAddress objects.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   183
    senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   184
    if (senderAddr != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   185
        if (!NET_SockaddrEqualsInetAddress(env, &sa, senderAddr)) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   186
            senderAddr = NULL;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
            jint port = (*env)->GetIntField(env, this, dci_senderPortID);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   189
            if (port != NET_GetPortFromSockaddr(&sa)) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   190
                senderAddr = NULL;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   191
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   192
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   193
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   194
    if (senderAddr == NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        jobject isa = NULL;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   196
        int port;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   197
        jobject ia = NET_SockaddrToInetAddress(env, &sa, &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (ia != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16100
diff changeset
   201
        CHECK_NULL_RETURN(isa, IOS_THROWN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // update cachedSenderInetAddress/cachedSenderPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
        (*env)->SetIntField(env, this, dci_senderPortID,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   206
                            NET_GetPortFromSockaddr(&sa));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        (*env)->SetObjectField(env, this, dci_senderID, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
                                          jboolean preferIPv6, jobject fdo,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12547
diff changeset
   215
                                          jlong address, jint len,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12547
diff changeset
   216
                                          jobject destAddress, jint destPort)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void *buf = (void *)jlong_to_ptr(address);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   220
    SOCKETADDRESS sa;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   221
    int sa_len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   224
    if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   225
                                  &sa_len, preferIPv6) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   229
    rv = sendto((SOCKET)fd, buf, len, 0, &sa.sa, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return handleSocketError(env, (jint)WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}