author | xdono |
Thu, 02 Oct 2008 19:58:32 -0700 | |
changeset 1247 | b4c26443dee5 |
parent 1152 | 29d6145d1097 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2001-2008 Sun Microsystems, Inc. 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 |
|
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 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()); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
JNIEXPORT jint JNICALL |
|
127 |
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, |
|
128 |
jobject fdo, jlong address, |
|
129 |
jint len, jboolean connected) |
|
130 |
{ |
|
131 |
jint fd = fdval(env, fdo); |
|
132 |
void *buf = (void *)jlong_to_ptr(address); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
int sa_len = sizeof(sa); |
2 | 135 |
BOOL retry = FALSE; |
136 |
jint n; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
137 |
jobject senderAddr; |
2 | 138 |
|
139 |
do { |
|
140 |
retry = FALSE; |
|
141 |
n = recvfrom((SOCKET)fd, |
|
142 |
(char *)buf, |
|
143 |
len, |
|
144 |
0, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
145 |
(struct sockaddr *)&sa, |
2 | 146 |
&sa_len); |
147 |
||
148 |
if (n == SOCKET_ERROR) { |
|
149 |
int theErr = (jint)WSAGetLastError(); |
|
150 |
if (theErr == WSAEMSGSIZE) { |
|
151 |
/* Spec says the rest of the data will be discarded... */ |
|
152 |
n = len; |
|
153 |
} else if (theErr == WSAECONNRESET) { |
|
154 |
purgeOutstandingICMP(env, this, fd); |
|
155 |
if (connected == JNI_FALSE) { |
|
156 |
retry = TRUE; |
|
157 |
} else { |
|
158 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
|
159 |
return IOS_THROWN; |
|
160 |
} |
|
161 |
} else if (theErr == WSAEWOULDBLOCK) { |
|
162 |
return IOS_UNAVAILABLE; |
|
163 |
} else return handleSocketError(env, theErr); |
|
164 |
} |
|
165 |
} while (retry); |
|
166 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
/* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
* If the source address and port match the cached address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
* and port in DatagramChannelImpl then we don't need to |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
* create InetAddress and InetSocketAddress objects. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
171 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
if (senderAddr != NULL) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
senderAddr)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
senderAddr = NULL; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
178 |
jint port = (*env)->GetIntField(env, this, dci_senderPortID); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
senderAddr = NULL; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
} |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
184 |
if (senderAddr == NULL) { |
2 | 185 |
jobject isa = NULL; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
int port; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
187 |
jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
&port); |
2 | 189 |
|
190 |
if (ia != NULL) { |
|
191 |
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); |
|
192 |
} |
|
193 |
||
194 |
if (isa == NULL) { |
|
195 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); |
|
196 |
return IOS_THROWN; |
|
197 |
} |
|
198 |
||
199 |
// update cachedSenderInetAddress/cachedSenderPort |
|
200 |
(*env)->SetObjectField(env, this, dci_senderAddrID, ia); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
201 |
(*env)->SetIntField(env, this, dci_senderPortID, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
NET_GetPortFromSockaddr((struct sockaddr *)&sa)); |
2 | 203 |
(*env)->SetObjectField(env, this, dci_senderID, isa); |
204 |
} |
|
205 |
return n; |
|
206 |
} |
|
207 |
||
208 |
JNIEXPORT jint JNICALL |
|
209 |
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
|
210 |
jboolean preferIPv6, jobject fdo, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
jlong address, jint len, jobject dest) |
2 | 212 |
{ |
213 |
jint fd = fdval(env, fdo); |
|
214 |
void *buf = (void *)jlong_to_ptr(address); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
SOCKETADDRESS sa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
216 |
int sa_len; |
2 | 217 |
jint rv = 0; |
218 |
jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID); |
|
219 |
jint destPort = (*env)->GetIntField(env, dest, isa_portID); |
|
220 |
||
221 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
(struct sockaddr *)&sa, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
&sa_len, preferIPv6) != 0) { |
2 | 224 |
return IOS_THROWN; |
225 |
} |
|
226 |
||
227 |
rv = sendto((SOCKET)fd, |
|
228 |
buf, |
|
229 |
len, |
|
230 |
0, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
231 |
(struct sockaddr *)&sa, |
2 | 232 |
sa_len); |
233 |
if (rv == SOCKET_ERROR) { |
|
234 |
int theErr = (jint)WSAGetLastError(); |
|
235 |
if (theErr == WSAEWOULDBLOCK) { |
|
236 |
return IOS_UNAVAILABLE; |
|
237 |
} |
|
238 |
return handleSocketError(env, (jint)WSAGetLastError()); |
|
239 |
} |
|
240 |
return rv; |
|
241 |
} |