src/java.base/windows/native/libnio/ch/DatagramChannelImpl.c
author alanb
Sat, 30 Nov 2019 16:21:19 +0000
changeset 59329 289000934908
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation Reviewed-by: dfuchs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, 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 <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    28
#include "jni.h"
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    29
#include "jni_util.h"
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    30
#include "jlong.h"
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    31
#include "net_util.h"
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    32
#include "nio.h"
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    33
#include "nio_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    35
#include "sun_nio_ch_DatagramChannelImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This function "purges" all outstanding ICMP port unreachable packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * outstanding on a socket and returns JNI_TRUE if any ICMP messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * have been purged. The rational for purging is to emulate normal BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * behaviour whereby receiving a "connection reset" status resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    jboolean got_icmp = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    struct timeval t = { 0, 0 };
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    50
    SOCKETADDRESS sa;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    51
    int addrlen = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Peek at the queue to see if there is an ICMP port unreachable. If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * is then receive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    FD_SET((u_int)fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    while(1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (recvfrom(fd, buf, 1, MSG_PEEK,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
    64
                     &sa.sa, &addrlen) != SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (WSAGetLastError() != WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            /* some other error - we don't care here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
    72
        recvfrom(fd, buf, 1, 0, &sa.sa, &addrlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        got_icmp = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    return got_icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
JNIEXPORT void JNICALL
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
    80
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jclass clazz,
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 7668
diff changeset
    81
                                                jobject fdo, jboolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int rv = 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 sa_len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    88
    memset(&sa, 0, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
    90
    rv = connect((SOCKET)fd, &sa.sa, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        handleSocketError(env, WSAGetLastError());
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    93
    } else {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    94
        /* Disable WSAECONNRESET errors as socket is no longer connected */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    95
        BOOL enable = FALSE;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    96
        DWORD bytesReturned = 0;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    97
        WSAIoctl((SOCKET)fd, SIO_UDP_CONNRESET, &enable, sizeof(enable),
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents: 5506
diff changeset
    98
                 NULL, 0, &bytesReturned, NULL, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
JNIEXPORT jint JNICALL
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   103
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jclass clazz,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   104
                                             jobject fdo, jlong bufAddress,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   105
                                             jint len, jlong senderAddress,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   106
                                             jboolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jint fd = fdval(env, fdo);
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   109
    void *buf = (void *)jlong_to_ptr(bufAddress);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   110
    SOCKETADDRESS *sa = (SOCKETADDRESS *)jlong_to_ptr(senderAddress);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   111
    int sa_len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    BOOL retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        n = recvfrom((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                     (char *)buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                     len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                     0,
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   121
                     (struct sockaddr *)sa,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                     &sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (n == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (theErr == WSAEMSGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                /* Spec says the rest of the data will be discarded... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if (theErr == WSAECONNRESET) {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   130
                purgeOutstandingICMP(env, clazz, fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (connected == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else return handleSocketError(env, theErr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNIEXPORT jint JNICALL
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 47216
diff changeset
   147
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jclass clazz,
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   148
                                          jboolean preferIPv6, jobject fdo,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12547
diff changeset
   149
                                          jlong address, jint len,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12547
diff changeset
   150
                                          jobject destAddress, jint destPort)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    void *buf = (void *)jlong_to_ptr(address);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   154
    SOCKETADDRESS sa;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   155
    int sa_len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   158
    if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   159
                                  &sa_len, preferIPv6) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 25859
diff changeset
   163
    rv = sendto((SOCKET)fd, buf, len, 0, &sa.sa, sa_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return handleSocketError(env, (jint)WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}