author | alanb |
Sat, 30 Nov 2019 16:21:19 +0000 | |
changeset 59329 | 289000934908 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <winsock2.h> |
|
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 | 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 | 36 |
|
37 |
/* |
|
38 |
* This function "purges" all outstanding ICMP port unreachable packets |
|
39 |
* outstanding on a socket and returns JNI_TRUE if any ICMP messages |
|
40 |
* have been purged. The rational for purging is to emulate normal BSD |
|
41 |
* behaviour whereby receiving a "connection reset" status resets the |
|
42 |
* socket. |
|
43 |
*/ |
|
44 |
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd) |
|
45 |
{ |
|
46 |
jboolean got_icmp = JNI_FALSE; |
|
47 |
char buf[1]; |
|
48 |
fd_set tbl; |
|
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 | 52 |
|
53 |
/* |
|
54 |
* Peek at the queue to see if there is an ICMP port unreachable. If there |
|
55 |
* is then receive it. |
|
56 |
*/ |
|
57 |
FD_ZERO(&tbl); |
|
58 |
FD_SET((u_int)fd, &tbl); |
|
59 |
while(1) { |
|
60 |
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) { |
|
61 |
break; |
|
62 |
} |
|
63 |
if (recvfrom(fd, buf, 1, MSG_PEEK, |
|
43100 | 64 |
&sa.sa, &addrlen) != SOCKET_ERROR) { |
2 | 65 |
break; |
66 |
} |
|
67 |
if (WSAGetLastError() != WSAECONNRESET) { |
|
68 |
/* some other error - we don't care here */ |
|
69 |
break; |
|
70 |
} |
|
71 |
||
43100 | 72 |
recvfrom(fd, buf, 1, 0, &sa.sa, &addrlen); |
2 | 73 |
got_icmp = JNI_TRUE; |
74 |
} |
|
75 |
||
76 |
return got_icmp; |
|
77 |
} |
|
78 |
||
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 | 82 |
{ |
83 |
jint fd = fdval(env, fdo); |
|
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 | 87 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
88 |
memset(&sa, 0, sa_len); |
2 | 89 |
|
43100 | 90 |
rv = connect((SOCKET)fd, &sa.sa, sa_len); |
2 | 91 |
if (rv == SOCKET_ERROR) { |
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 | 99 |
} |
100 |
} |
|
101 |
||
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 | 107 |
{ |
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 | 112 |
BOOL retry = FALSE; |
113 |
jint n; |
|
114 |
||
115 |
do { |
|
116 |
retry = FALSE; |
|
117 |
n = recvfrom((SOCKET)fd, |
|
118 |
(char *)buf, |
|
119 |
len, |
|
120 |
0, |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
47216
diff
changeset
|
121 |
(struct sockaddr *)sa, |
2 | 122 |
&sa_len); |
123 |
||
124 |
if (n == SOCKET_ERROR) { |
|
125 |
int theErr = (jint)WSAGetLastError(); |
|
126 |
if (theErr == WSAEMSGSIZE) { |
|
127 |
/* Spec says the rest of the data will be discarded... */ |
|
128 |
n = len; |
|
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 | 131 |
if (connected == JNI_FALSE) { |
132 |
retry = TRUE; |
|
133 |
} else { |
|
134 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
|
135 |
return IOS_THROWN; |
|
136 |
} |
|
137 |
} else if (theErr == WSAEWOULDBLOCK) { |
|
138 |
return IOS_UNAVAILABLE; |
|
139 |
} else return handleSocketError(env, theErr); |
|
140 |
} |
|
141 |
} while (retry); |
|
142 |
||
143 |
return n; |
|
144 |
} |
|
145 |
||
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 | 149 |
jlong address, jint len, |
150 |
jobject destAddress, jint destPort) |
|
2 | 151 |
{ |
152 |
jint fd = fdval(env, fdo); |
|
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 | 155 |
int sa_len = 0; |
2 | 156 |
jint rv = 0; |
157 |
||
43100 | 158 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa, |
159 |
&sa_len, preferIPv6) != 0) { |
|
2 | 160 |
return IOS_THROWN; |
161 |
} |
|
162 |
||
43100 | 163 |
rv = sendto((SOCKET)fd, buf, len, 0, &sa.sa, sa_len); |
2 | 164 |
if (rv == SOCKET_ERROR) { |
165 |
int theErr = (jint)WSAGetLastError(); |
|
166 |
if (theErr == WSAEWOULDBLOCK) { |
|
167 |
return IOS_UNAVAILABLE; |
|
168 |
} |
|
169 |
return handleSocketError(env, (jint)WSAGetLastError()); |
|
170 |
} |
|
171 |
return rv; |
|
172 |
} |