author | alanb |
Wed, 09 Jun 2010 18:51:32 +0100 | |
changeset 5777 | 1c86018749bd |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, 2008, 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 isa_addrID; /* address in java.net.InetSocketAddress */ |
|
38 |
static jfieldID isa_portID; /* port in java.net.InetSocketAddress */ |
|
39 |
static jfieldID dci_senderID; /* sender in sun.nio.ch.DatagramChannelImpl */ |
|
40 |
static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */ |
|
41 |
static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */ |
|
42 |
static jclass isa_class; /* java.net.InetSocketAddress */ |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
43 |
static jmethodID isa_ctorID; /* java.net.InetSocketAddress(InetAddress, int) */ |
2 | 44 |
|
45 |
||
46 |
JNIEXPORT void JNICALL |
|
47 |
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz) |
|
48 |
{ |
|
49 |
clazz = (*env)->FindClass(env, "java/net/InetSocketAddress"); |
|
50 |
isa_class = (*env)->NewGlobalRef(env, clazz); |
|
51 |
isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>", |
|
52 |
"(Ljava/net/InetAddress;I)V"); |
|
53 |
isa_addrID = (*env)->GetFieldID(env, clazz, "addr", |
|
54 |
"Ljava/net/InetAddress;"); |
|
55 |
isa_portID = (*env)->GetFieldID(env, clazz, "port", "I"); |
|
56 |
||
57 |
clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl"); |
|
58 |
dci_senderID = (*env)->GetFieldID(env, clazz, "sender", |
|
59 |
"Ljava/net/SocketAddress;"); |
|
60 |
dci_senderAddrID = (*env)->GetFieldID(env, clazz, |
|
61 |
"cachedSenderInetAddress", |
|
62 |
"Ljava/net/InetAddress;"); |
|
63 |
dci_senderPortID = (*env)->GetFieldID(env, clazz, |
|
64 |
"cachedSenderPort", "I"); |
|
65 |
} |
|
66 |
||
67 |
/* |
|
68 |
* This function "purges" all outstanding ICMP port unreachable packets |
|
69 |
* outstanding on a socket and returns JNI_TRUE if any ICMP messages |
|
70 |
* have been purged. The rational for purging is to emulate normal BSD |
|
71 |
* behaviour whereby receiving a "connection reset" status resets the |
|
72 |
* socket. |
|
73 |
*/ |
|
74 |
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd) |
|
75 |
{ |
|
76 |
jboolean got_icmp = JNI_FALSE; |
|
77 |
char buf[1]; |
|
78 |
fd_set tbl; |
|
79 |
struct timeval t = { 0, 0 }; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
80 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
81 |
int addrlen = sizeof(sa); |
2 | 82 |
|
83 |
/* |
|
84 |
* Peek at the queue to see if there is an ICMP port unreachable. If there |
|
85 |
* is then receive it. |
|
86 |
*/ |
|
87 |
FD_ZERO(&tbl); |
|
88 |
FD_SET((u_int)fd, &tbl); |
|
89 |
while(1) { |
|
90 |
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) { |
|
91 |
break; |
|
92 |
} |
|
93 |
if (recvfrom(fd, buf, 1, MSG_PEEK, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
94 |
(struct sockaddr *)&sa, &addrlen) != SOCKET_ERROR) { |
2 | 95 |
break; |
96 |
} |
|
97 |
if (WSAGetLastError() != WSAECONNRESET) { |
|
98 |
/* some other error - we don't care here */ |
|
99 |
break; |
|
100 |
} |
|
101 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
102 |
recvfrom(fd, buf, 1, 0, (struct sockaddr *)&sa, &addrlen); |
2 | 103 |
got_icmp = JNI_TRUE; |
104 |
} |
|
105 |
||
106 |
return got_icmp; |
|
107 |
} |
|
108 |
||
109 |
JNIEXPORT void JNICALL |
|
110 |
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this, |
|
111 |
jobject fdo) |
|
112 |
{ |
|
113 |
jint fd = fdval(env, fdo); |
|
114 |
int rv = 0; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
115 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
int sa_len = sizeof(sa); |
2 | 117 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
118 |
memset(&sa, 0, sa_len); |
2 | 119 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
120 |
rv = connect((SOCKET)fd, (struct sockaddr *)&sa, sa_len); |
2 | 121 |
if (rv == SOCKET_ERROR) { |
122 |
handleSocketError(env, WSAGetLastError()); |
|
5777
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
123 |
} else { |
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
124 |
/* Disable WSAECONNRESET errors as socket is no longer connected */ |
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
125 |
BOOL enable = FALSE; |
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
126 |
DWORD bytesReturned = 0; |
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
127 |
WSAIoctl((SOCKET)fd, SIO_UDP_CONNRESET, &enable, sizeof(enable), |
1c86018749bd
6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
5506
diff
changeset
|
128 |
NULL, 0, &bytesReturned, NULL, NULL); |
2 | 129 |
} |
130 |
} |
|
131 |
||
132 |
JNIEXPORT jint JNICALL |
|
133 |
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, |
|
134 |
jobject fdo, jlong address, |
|
135 |
jint len, jboolean connected) |
|
136 |
{ |
|
137 |
jint fd = fdval(env, fdo); |
|
138 |
void *buf = (void *)jlong_to_ptr(address); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
139 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
140 |
int sa_len = sizeof(sa); |
2 | 141 |
BOOL retry = FALSE; |
142 |
jint n; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
143 |
jobject senderAddr; |
2 | 144 |
|
145 |
do { |
|
146 |
retry = FALSE; |
|
147 |
n = recvfrom((SOCKET)fd, |
|
148 |
(char *)buf, |
|
149 |
len, |
|
150 |
0, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
151 |
(struct sockaddr *)&sa, |
2 | 152 |
&sa_len); |
153 |
||
154 |
if (n == SOCKET_ERROR) { |
|
155 |
int theErr = (jint)WSAGetLastError(); |
|
156 |
if (theErr == WSAEMSGSIZE) { |
|
157 |
/* Spec says the rest of the data will be discarded... */ |
|
158 |
n = len; |
|
159 |
} else if (theErr == WSAECONNRESET) { |
|
160 |
purgeOutstandingICMP(env, this, fd); |
|
161 |
if (connected == JNI_FALSE) { |
|
162 |
retry = TRUE; |
|
163 |
} else { |
|
164 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
|
165 |
return IOS_THROWN; |
|
166 |
} |
|
167 |
} else if (theErr == WSAEWOULDBLOCK) { |
|
168 |
return IOS_UNAVAILABLE; |
|
169 |
} else return handleSocketError(env, theErr); |
|
170 |
} |
|
171 |
} while (retry); |
|
172 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
/* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
* If the source address and port match the cached address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
* and port in DatagramChannelImpl then we don't need to |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
* create InetAddress and InetSocketAddress objects. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
178 |
senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
if (senderAddr != NULL) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
senderAddr)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
senderAddr = NULL; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
183 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
184 |
jint port = (*env)->GetIntField(env, this, dci_senderPortID); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
185 |
if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) { |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
if (senderAddr == NULL) { |
2 | 191 |
jobject isa = NULL; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
int port; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
&port); |
2 | 195 |
|
196 |
if (ia != NULL) { |
|
197 |
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); |
|
198 |
} |
|
199 |
||
200 |
if (isa == NULL) { |
|
201 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); |
|
202 |
return IOS_THROWN; |
|
203 |
} |
|
204 |
||
205 |
// update cachedSenderInetAddress/cachedSenderPort |
|
206 |
(*env)->SetObjectField(env, this, dci_senderAddrID, ia); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
207 |
(*env)->SetIntField(env, this, dci_senderPortID, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
208 |
NET_GetPortFromSockaddr((struct sockaddr *)&sa)); |
2 | 209 |
(*env)->SetObjectField(env, this, dci_senderID, isa); |
210 |
} |
|
211 |
return n; |
|
212 |
} |
|
213 |
||
214 |
JNIEXPORT jint JNICALL |
|
215 |
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
|
216 |
jboolean preferIPv6, jobject fdo, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
217 |
jlong address, jint len, jobject dest) |
2 | 218 |
{ |
219 |
jint fd = fdval(env, fdo); |
|
220 |
void *buf = (void *)jlong_to_ptr(address); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
int sa_len; |
2 | 223 |
jint rv = 0; |
224 |
jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID); |
|
225 |
jint destPort = (*env)->GetIntField(env, dest, isa_portID); |
|
226 |
||
227 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
(struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
&sa_len, preferIPv6) != 0) { |
2 | 230 |
return IOS_THROWN; |
231 |
} |
|
232 |
||
233 |
rv = sendto((SOCKET)fd, |
|
234 |
buf, |
|
235 |
len, |
|
236 |
0, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
(struct sockaddr *)&sa, |
2 | 238 |
sa_len); |
239 |
if (rv == SOCKET_ERROR) { |
|
240 |
int theErr = (jint)WSAGetLastError(); |
|
241 |
if (theErr == WSAEWOULDBLOCK) { |
|
242 |
return IOS_UNAVAILABLE; |
|
243 |
} |
|
244 |
return handleSocketError(env, (jint)WSAGetLastError()); |
|
245 |
} |
|
246 |
return rv; |
|
247 |
} |