2
|
1 |
/*
|
|
2 |
* Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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 jfieldID ia_addrID;
|
|
43 |
static jfieldID ia_famID;
|
|
44 |
static jclass isa_class; /* java.net.InetSocketAddress */
|
|
45 |
static jclass ia_class;
|
|
46 |
static jmethodID isa_ctorID; /* .InetSocketAddress(InetAddress, int) */
|
|
47 |
static jmethodID ia_ctorID;
|
|
48 |
|
|
49 |
/*
|
|
50 |
* Returns JNI_TRUE if DatagramChannelImpl has already cached an
|
|
51 |
* InetAddress/port corresponding to the socket address.
|
|
52 |
*/
|
|
53 |
static jboolean isSenderCached(JNIEnv *env, jobject this, struct sockaddr_in *sa) {
|
|
54 |
jobject senderAddr;
|
|
55 |
|
|
56 |
/* shouldn't happen until we have dual IPv4/IPv6 stack (post-XP ?) */
|
|
57 |
if (sa->sin_family != AF_INET) {
|
|
58 |
return JNI_FALSE;
|
|
59 |
}
|
|
60 |
|
|
61 |
/*
|
|
62 |
* Compare source address to cached InetAddress
|
|
63 |
*/
|
|
64 |
senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
|
|
65 |
if (senderAddr == NULL) {
|
|
66 |
return JNI_FALSE;
|
|
67 |
}
|
|
68 |
if ((jint)ntohl(sa->sin_addr.s_addr) !=
|
|
69 |
(*env)->GetIntField(env, senderAddr, ia_addrID)) {
|
|
70 |
return JNI_FALSE;
|
|
71 |
}
|
|
72 |
|
|
73 |
/*
|
|
74 |
* Compare source port to cached port
|
|
75 |
*/
|
|
76 |
if ((jint)ntohs(sa->sin_port) !=
|
|
77 |
(*env)->GetIntField(env, this, dci_senderPortID)) {
|
|
78 |
return JNI_FALSE;
|
|
79 |
}
|
|
80 |
return JNI_TRUE;
|
|
81 |
}
|
|
82 |
|
|
83 |
JNIEXPORT void JNICALL
|
|
84 |
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
|
|
85 |
{
|
|
86 |
clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
|
|
87 |
isa_class = (*env)->NewGlobalRef(env, clazz);
|
|
88 |
isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
|
|
89 |
"(Ljava/net/InetAddress;I)V");
|
|
90 |
isa_addrID = (*env)->GetFieldID(env, clazz, "addr",
|
|
91 |
"Ljava/net/InetAddress;");
|
|
92 |
isa_portID = (*env)->GetFieldID(env, clazz, "port", "I");
|
|
93 |
|
|
94 |
clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl");
|
|
95 |
dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
|
|
96 |
"Ljava/net/SocketAddress;");
|
|
97 |
dci_senderAddrID = (*env)->GetFieldID(env, clazz,
|
|
98 |
"cachedSenderInetAddress",
|
|
99 |
"Ljava/net/InetAddress;");
|
|
100 |
dci_senderPortID = (*env)->GetFieldID(env, clazz,
|
|
101 |
"cachedSenderPort", "I");
|
|
102 |
clazz = (*env)->FindClass(env, "java/net/Inet4Address");
|
|
103 |
ia_class = (*env)->NewGlobalRef(env, clazz);
|
|
104 |
ia_addrID = (*env)->GetFieldID(env, clazz, "address", "I");
|
|
105 |
ia_famID = (*env)->GetFieldID(env, clazz, "family", "I");
|
|
106 |
ia_ctorID = (*env)->GetMethodID(env, clazz, "<init>", "()V");
|
|
107 |
}
|
|
108 |
|
|
109 |
/*
|
|
110 |
* Return JNI_TRUE if this Windows edition supports ICMP Port Unreachable
|
|
111 |
*/
|
|
112 |
__inline static jboolean supportPortUnreachable() {
|
|
113 |
static jboolean initDone;
|
|
114 |
static jboolean portUnreachableSupported;
|
|
115 |
|
|
116 |
if (!initDone) {
|
|
117 |
OSVERSIONINFO ver;
|
|
118 |
ver.dwOSVersionInfoSize = sizeof(ver);
|
|
119 |
GetVersionEx(&ver);
|
|
120 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT && ver.dwMajorVersion >= 5) {
|
|
121 |
portUnreachableSupported = JNI_TRUE;
|
|
122 |
} else {
|
|
123 |
portUnreachableSupported = JNI_FALSE;
|
|
124 |
}
|
|
125 |
initDone = JNI_TRUE;
|
|
126 |
}
|
|
127 |
return portUnreachableSupported;
|
|
128 |
}
|
|
129 |
|
|
130 |
/*
|
|
131 |
* This function "purges" all outstanding ICMP port unreachable packets
|
|
132 |
* outstanding on a socket and returns JNI_TRUE if any ICMP messages
|
|
133 |
* have been purged. The rational for purging is to emulate normal BSD
|
|
134 |
* behaviour whereby receiving a "connection reset" status resets the
|
|
135 |
* socket.
|
|
136 |
*/
|
|
137 |
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
|
|
138 |
{
|
|
139 |
jboolean got_icmp = JNI_FALSE;
|
|
140 |
char buf[1];
|
|
141 |
fd_set tbl;
|
|
142 |
struct timeval t = { 0, 0 };
|
|
143 |
struct sockaddr_in rmtaddr;
|
|
144 |
int addrlen = sizeof(rmtaddr);
|
|
145 |
|
|
146 |
/*
|
|
147 |
* A no-op if this OS doesn't support it.
|
|
148 |
*/
|
|
149 |
if (!supportPortUnreachable()) {
|
|
150 |
return JNI_FALSE;
|
|
151 |
}
|
|
152 |
|
|
153 |
/*
|
|
154 |
* Peek at the queue to see if there is an ICMP port unreachable. If there
|
|
155 |
* is then receive it.
|
|
156 |
*/
|
|
157 |
FD_ZERO(&tbl);
|
|
158 |
FD_SET((u_int)fd, &tbl);
|
|
159 |
while(1) {
|
|
160 |
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
|
|
161 |
break;
|
|
162 |
}
|
|
163 |
if (recvfrom(fd, buf, 1, MSG_PEEK,
|
|
164 |
(struct sockaddr *)&rmtaddr, &addrlen) != SOCKET_ERROR) {
|
|
165 |
break;
|
|
166 |
}
|
|
167 |
if (WSAGetLastError() != WSAECONNRESET) {
|
|
168 |
/* some other error - we don't care here */
|
|
169 |
break;
|
|
170 |
}
|
|
171 |
|
|
172 |
recvfrom(fd, buf, 1, 0, (struct sockaddr *)&rmtaddr, &addrlen);
|
|
173 |
got_icmp = JNI_TRUE;
|
|
174 |
}
|
|
175 |
|
|
176 |
return got_icmp;
|
|
177 |
}
|
|
178 |
|
|
179 |
JNIEXPORT void JNICALL
|
|
180 |
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
|
|
181 |
jobject fdo)
|
|
182 |
{
|
|
183 |
jint fd = fdval(env, fdo);
|
|
184 |
int rv = 0;
|
|
185 |
struct sockaddr_in psa;
|
|
186 |
int sa_len = sizeof(psa);
|
|
187 |
|
|
188 |
memset(&psa, 0, sa_len);
|
|
189 |
|
|
190 |
rv = connect((SOCKET)fd, (struct sockaddr *)&psa, sa_len);
|
|
191 |
if (rv == SOCKET_ERROR) {
|
|
192 |
handleSocketError(env, WSAGetLastError());
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
JNIEXPORT jint JNICALL
|
|
197 |
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
|
|
198 |
jobject fdo, jlong address,
|
|
199 |
jint len, jboolean connected)
|
|
200 |
{
|
|
201 |
jint fd = fdval(env, fdo);
|
|
202 |
void *buf = (void *)jlong_to_ptr(address);
|
|
203 |
struct sockaddr_in psa;
|
|
204 |
int sa_len = sizeof(psa);
|
|
205 |
BOOL retry = FALSE;
|
|
206 |
jint n;
|
|
207 |
|
|
208 |
do {
|
|
209 |
retry = FALSE;
|
|
210 |
n = recvfrom((SOCKET)fd,
|
|
211 |
(char *)buf,
|
|
212 |
len,
|
|
213 |
0,
|
|
214 |
(struct sockaddr *)&psa,
|
|
215 |
&sa_len);
|
|
216 |
|
|
217 |
if (n == SOCKET_ERROR) {
|
|
218 |
int theErr = (jint)WSAGetLastError();
|
|
219 |
if (theErr == WSAEMSGSIZE) {
|
|
220 |
/* Spec says the rest of the data will be discarded... */
|
|
221 |
n = len;
|
|
222 |
} else if (theErr == WSAECONNRESET) {
|
|
223 |
purgeOutstandingICMP(env, this, fd);
|
|
224 |
if (connected == JNI_FALSE) {
|
|
225 |
retry = TRUE;
|
|
226 |
} else {
|
|
227 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
|
|
228 |
return IOS_THROWN;
|
|
229 |
}
|
|
230 |
} else if (theErr == WSAEWOULDBLOCK) {
|
|
231 |
return IOS_UNAVAILABLE;
|
|
232 |
} else return handleSocketError(env, theErr);
|
|
233 |
}
|
|
234 |
} while (retry);
|
|
235 |
|
|
236 |
if (!isSenderCached(env, this, &psa)) {
|
|
237 |
int port = ntohs(psa.sin_port);
|
|
238 |
jobject ia = (*env)->NewObject(env, ia_class, ia_ctorID);
|
|
239 |
jobject isa = NULL;
|
|
240 |
|
|
241 |
if (psa.sin_family != AF_INET) {
|
|
242 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
243 |
"Protocol family unavailable");
|
|
244 |
}
|
|
245 |
|
|
246 |
if (ia != NULL) {
|
|
247 |
// populate InetAddress (assumes AF_INET)
|
|
248 |
(*env)->SetIntField(env, ia, ia_addrID, ntohl(psa.sin_addr.s_addr));
|
|
249 |
|
|
250 |
// create InetSocketAddress
|
|
251 |
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
|
|
252 |
}
|
|
253 |
|
|
254 |
if (isa == NULL) {
|
|
255 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
|
|
256 |
return IOS_THROWN;
|
|
257 |
}
|
|
258 |
|
|
259 |
// update cachedSenderInetAddress/cachedSenderPort
|
|
260 |
(*env)->SetObjectField(env, this, dci_senderAddrID, ia);
|
|
261 |
(*env)->SetIntField(env, this, dci_senderPortID, port);
|
|
262 |
|
|
263 |
// update sender
|
|
264 |
(*env)->SetObjectField(env, this, dci_senderID, isa);
|
|
265 |
}
|
|
266 |
return n;
|
|
267 |
}
|
|
268 |
|
|
269 |
JNIEXPORT jint JNICALL
|
|
270 |
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
|
|
271 |
jobject fdo, jlong address,
|
|
272 |
jint len, jobject dest)
|
|
273 |
{
|
|
274 |
jint fd = fdval(env, fdo);
|
|
275 |
void *buf = (void *)jlong_to_ptr(address);
|
|
276 |
SOCKETADDRESS psa;
|
|
277 |
int sa_len = sizeof(psa);
|
|
278 |
jint rv = 0;
|
|
279 |
jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID);
|
|
280 |
jint destPort = (*env)->GetIntField(env, dest, isa_portID);
|
|
281 |
|
|
282 |
|
|
283 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort,
|
|
284 |
(struct sockaddr *)&psa,
|
|
285 |
&sa_len, JNI_FALSE) != 0) {
|
|
286 |
return IOS_THROWN;
|
|
287 |
}
|
|
288 |
|
|
289 |
rv = sendto((SOCKET)fd,
|
|
290 |
buf,
|
|
291 |
len,
|
|
292 |
0,
|
|
293 |
(struct sockaddr *)&psa,
|
|
294 |
sa_len);
|
|
295 |
if (rv == SOCKET_ERROR) {
|
|
296 |
int theErr = (jint)WSAGetLastError();
|
|
297 |
if (theErr == WSAEWOULDBLOCK) {
|
|
298 |
return IOS_UNAVAILABLE;
|
|
299 |
}
|
|
300 |
return handleSocketError(env, (jint)WSAGetLastError());
|
|
301 |
}
|
|
302 |
return rv;
|
|
303 |
}
|