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-- |
2 | 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 | 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 "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jvm.h" |
|
29 |
#include "jlong.h" |
|
30 |
#include <io.h> |
|
31 |
#include "sun_nio_ch_DatagramChannelImpl.h" |
|
32 |
#include "nio.h" |
|
33 |
#include "nio_util.h" |
|
34 |
#include "net_util.h" |
|
35 |
#include <winsock2.h> |
|
36 |
||
37 |
static jfieldID dci_senderID; /* sender in sun.nio.ch.DatagramChannelImpl */ |
|
38 |
static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */ |
|
39 |
static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */ |
|
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 | 42 |
|
43 |
||
44 |
JNIEXPORT void JNICALL |
|
45 |
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz) |
|
46 |
{ |
|
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 | 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 | 54 |
isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>", |
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 | 57 |
|
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 | 60 |
dci_senderID = (*env)->GetFieldID(env, clazz, "sender", |
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 | 63 |
dci_senderAddrID = (*env)->GetFieldID(env, clazz, |
64 |
"cachedSenderInetAddress", |
|
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 | 67 |
dci_senderPortID = (*env)->GetFieldID(env, clazz, |
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 | 70 |
} |
71 |
||
72 |
/* |
|
73 |
* This function "purges" all outstanding ICMP port unreachable packets |
|
74 |
* outstanding on a socket and returns JNI_TRUE if any ICMP messages |
|
75 |
* have been purged. The rational for purging is to emulate normal BSD |
|
76 |
* behaviour whereby receiving a "connection reset" status resets the |
|
77 |
* socket. |
|
78 |
*/ |
|
79 |
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd) |
|
80 |
{ |
|
81 |
jboolean got_icmp = JNI_FALSE; |
|
82 |
char buf[1]; |
|
83 |
fd_set tbl; |
|
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 | 87 |
|
88 |
/* |
|
89 |
* Peek at the queue to see if there is an ICMP port unreachable. If there |
|
90 |
* is then receive it. |
|
91 |
*/ |
|
92 |
FD_ZERO(&tbl); |
|
93 |
FD_SET((u_int)fd, &tbl); |
|
94 |
while(1) { |
|
95 |
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) { |
|
96 |
break; |
|
97 |
} |
|
98 |
if (recvfrom(fd, buf, 1, MSG_PEEK, |
|
43100 | 99 |
&sa.sa, &addrlen) != SOCKET_ERROR) { |
2 | 100 |
break; |
101 |
} |
|
102 |
if (WSAGetLastError() != WSAECONNRESET) { |
|
103 |
/* some other error - we don't care here */ |
|
104 |
break; |
|
105 |
} |
|
106 |
||
43100 | 107 |
recvfrom(fd, buf, 1, 0, &sa.sa, &addrlen); |
2 | 108 |
got_icmp = JNI_TRUE; |
109 |
} |
|
110 |
||
111 |
return got_icmp; |
|
112 |
} |
|
113 |
||
114 |
JNIEXPORT void JNICALL |
|
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 | 117 |
{ |
118 |
jint fd = fdval(env, fdo); |
|
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 | 122 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
123 |
memset(&sa, 0, sa_len); |
2 | 124 |
|
43100 | 125 |
rv = connect((SOCKET)fd, &sa.sa, sa_len); |
2 | 126 |
if (rv == SOCKET_ERROR) { |
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 | 134 |
} |
135 |
} |
|
136 |
||
137 |
JNIEXPORT jint JNICALL |
|
138 |
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, |
|
139 |
jobject fdo, jlong address, |
|
140 |
jint len, jboolean connected) |
|
141 |
{ |
|
142 |
jint fd = fdval(env, fdo); |
|
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 | 146 |
BOOL retry = FALSE; |
147 |
jint n; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
148 |
jobject senderAddr; |
2 | 149 |
|
150 |
do { |
|
151 |
retry = FALSE; |
|
152 |
n = recvfrom((SOCKET)fd, |
|
153 |
(char *)buf, |
|
154 |
len, |
|
155 |
0, |
|
43100 | 156 |
&sa.sa, |
2 | 157 |
&sa_len); |
158 |
||
159 |
if (n == SOCKET_ERROR) { |
|
160 |
int theErr = (jint)WSAGetLastError(); |
|
161 |
if (theErr == WSAEMSGSIZE) { |
|
162 |
/* Spec says the rest of the data will be discarded... */ |
|
163 |
n = len; |
|
164 |
} else if (theErr == WSAECONNRESET) { |
|
165 |
purgeOutstandingICMP(env, this, fd); |
|
166 |
if (connected == JNI_FALSE) { |
|
167 |
retry = TRUE; |
|
168 |
} else { |
|
169 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
|
170 |
return IOS_THROWN; |
|
171 |
} |
|
172 |
} else if (theErr == WSAEWOULDBLOCK) { |
|
173 |
return IOS_UNAVAILABLE; |
|
174 |
} else return handleSocketError(env, theErr); |
|
175 |
} |
|
176 |
} while (retry); |
|
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 | 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 | 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 | 195 |
jobject isa = NULL; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
196 |
int port; |
43100 | 197 |
jobject ia = NET_SockaddrToInetAddress(env, &sa, &port); |
2 | 198 |
if (ia != NULL) { |
199 |
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); |
|
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 | 202 |
|
203 |
// update cachedSenderInetAddress/cachedSenderPort |
|
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 | 206 |
NET_GetPortFromSockaddr(&sa)); |
2 | 207 |
(*env)->SetObjectField(env, this, dci_senderID, isa); |
208 |
} |
|
209 |
return n; |
|
210 |
} |
|
211 |
||
212 |
JNIEXPORT jint JNICALL |
|
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 | 215 |
jlong address, jint len, |
216 |
jobject destAddress, jint destPort) |
|
2 | 217 |
{ |
218 |
jint fd = fdval(env, fdo); |
|
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 | 221 |
int sa_len = 0; |
2 | 222 |
jint rv = 0; |
223 |
||
43100 | 224 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa, |
225 |
&sa_len, preferIPv6) != 0) { |
|
2 | 226 |
return IOS_THROWN; |
227 |
} |
|
228 |
||
43100 | 229 |
rv = sendto((SOCKET)fd, buf, len, 0, &sa.sa, sa_len); |
2 | 230 |
if (rv == SOCKET_ERROR) { |
231 |
int theErr = (jint)WSAGetLastError(); |
|
232 |
if (theErr == WSAEWOULDBLOCK) { |
|
233 |
return IOS_UNAVAILABLE; |
|
234 |
} |
|
235 |
return handleSocketError(env, (jint)WSAGetLastError()); |
|
236 |
} |
|
237 |
return rv; |
|
238 |
} |