author | khazra |
Thu, 14 Mar 2013 13:46:15 -0700 | |
changeset 18192 | fa6bd0992104 |
parent 16870 | f35b2bd19761 |
child 20821 | e0d0a585aa49 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12813
diff
changeset
|
2 |
* Copyright (c) 1997, 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 <windows.h> |
|
27 |
#include <winsock2.h> |
|
28 |
#include <ctype.h> |
|
29 |
#include <stdio.h> |
|
30 |
#include <stdlib.h> |
|
31 |
#include <malloc.h> |
|
32 |
#include <sys/types.h> |
|
33 |
||
34 |
#include "java_net_SocketOptions.h" |
|
35 |
#include "java_net_TwoStacksPlainSocketImpl.h" |
|
36 |
#include "java_net_InetAddress.h" |
|
37 |
#include "java_io_FileDescriptor.h" |
|
38 |
#include "java_lang_Integer.h" |
|
39 |
||
40 |
#include "jvm.h" |
|
41 |
#include "net_util.h" |
|
42 |
#include "jni_util.h" |
|
43 |
||
44 |
/************************************************************************ |
|
45 |
* TwoStacksPlainSocketImpl |
|
46 |
*/ |
|
47 |
||
48 |
static jfieldID IO_fd_fdID; |
|
49 |
||
50 |
jfieldID psi_fdID; |
|
51 |
jfieldID psi_fd1ID; |
|
52 |
jfieldID psi_addressID; |
|
53 |
jfieldID psi_portID; |
|
54 |
jfieldID psi_localportID; |
|
55 |
jfieldID psi_timeoutID; |
|
56 |
jfieldID psi_trafficClassID; |
|
57 |
jfieldID psi_serverSocketID; |
|
58 |
jfieldID psi_lastfdID; |
|
59 |
||
60 |
/* |
|
61 |
* the level of the TCP protocol for setsockopt and getsockopt |
|
62 |
* we only want to look this up once, from the static initializer |
|
63 |
* of TwoStacksPlainSocketImpl |
|
64 |
*/ |
|
65 |
static int tcp_level = -1; |
|
66 |
||
67 |
static int getFD(JNIEnv *env, jobject this) { |
|
68 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
69 |
||
70 |
if (fdObj == NULL) { |
|
71 |
return -1; |
|
72 |
} |
|
73 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
74 |
} |
|
75 |
||
76 |
static int getFD1(JNIEnv *env, jobject this) { |
|
77 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
78 |
||
79 |
if (fdObj == NULL) { |
|
80 |
return -1; |
|
81 |
} |
|
82 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
83 |
} |
|
84 |
||
85 |
||
86 |
/* |
|
87 |
* The initProto function is called whenever TwoStacksPlainSocketImpl is |
|
88 |
* loaded, to cache fieldIds for efficiency. This is called everytime |
|
89 |
* the Java class is loaded. |
|
90 |
* |
|
91 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
92 |
* Method: initProto |
|
93 |
||
94 |
* Signature: ()V |
|
95 |
*/ |
|
96 |
JNIEXPORT void JNICALL |
|
97 |
Java_java_net_TwoStacksPlainSocketImpl_initProto(JNIEnv *env, jclass cls) { |
|
98 |
||
99 |
struct protoent *proto = getprotobyname("TCP"); |
|
100 |
tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto); |
|
101 |
||
102 |
psi_fdID = (*env)->GetFieldID(env, cls , "fd", "Ljava/io/FileDescriptor;"); |
|
103 |
CHECK_NULL(psi_fdID); |
|
104 |
psi_fd1ID =(*env)->GetFieldID(env, cls , "fd1", "Ljava/io/FileDescriptor;"); |
|
105 |
CHECK_NULL(psi_fd1ID); |
|
106 |
psi_addressID = (*env)->GetFieldID(env, cls, "address", |
|
107 |
"Ljava/net/InetAddress;"); |
|
108 |
CHECK_NULL(psi_addressID); |
|
109 |
psi_portID = (*env)->GetFieldID(env, cls, "port", "I"); |
|
110 |
CHECK_NULL(psi_portID); |
|
111 |
psi_lastfdID = (*env)->GetFieldID(env, cls, "lastfd", "I"); |
|
112 |
CHECK_NULL(psi_portID); |
|
113 |
psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I"); |
|
114 |
CHECK_NULL(psi_localportID); |
|
115 |
psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I"); |
|
116 |
CHECK_NULL(psi_timeoutID); |
|
117 |
psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I"); |
|
118 |
CHECK_NULL(psi_trafficClassID); |
|
119 |
psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket", |
|
120 |
"Ljava/net/ServerSocket;"); |
|
121 |
CHECK_NULL(psi_serverSocketID); |
|
122 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
123 |
CHECK_NULL(IO_fd_fdID); |
|
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
128 |
* Method: socketCreate |
|
129 |
* Signature: (Z)V |
|
130 |
*/ |
|
131 |
JNIEXPORT void JNICALL |
|
132 |
Java_java_net_TwoStacksPlainSocketImpl_socketCreate(JNIEnv *env, jobject this, |
|
133 |
jboolean stream) { |
|
134 |
jobject fdObj, fd1Obj; |
|
135 |
int fd, fd1; |
|
136 |
||
137 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
138 |
||
139 |
if (IS_NULL(fdObj)) { |
|
140 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
141 |
"null fd object"); |
|
142 |
return; |
|
143 |
} |
|
144 |
fd = socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0); |
|
145 |
if (fd == -1) { |
|
146 |
NET_ThrowCurrent(env, "create"); |
|
147 |
return; |
|
148 |
} else { |
|
149 |
/* Set socket attribute so it is not passed to any child process */ |
|
150 |
SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE); |
|
151 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd); |
|
152 |
} |
|
153 |
if (ipv6_available()) { |
|
154 |
fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
155 |
||
156 |
if (IS_NULL(fd1Obj)) { |
|
157 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
158 |
"null fd1 object"); |
|
159 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
160 |
NET_SocketClose(fd); |
|
161 |
return; |
|
162 |
} |
|
163 |
fd1 = socket(AF_INET6, (stream ? SOCK_STREAM: SOCK_DGRAM), 0); |
|
164 |
if (fd1 == -1) { |
|
165 |
NET_ThrowCurrent(env, "create"); |
|
166 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
167 |
NET_SocketClose(fd); |
|
168 |
return; |
|
169 |
} else { |
|
170 |
/* Set socket attribute so it is not passed to any child process */ |
|
171 |
SetHandleInformation((HANDLE)(UINT_PTR)fd1, HANDLE_FLAG_INHERIT, FALSE); |
|
172 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1); |
|
173 |
} |
|
174 |
} else { |
|
175 |
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
/* |
|
180 |
* inetAddress is the address object passed to the socket connect |
|
181 |
* call. |
|
182 |
* |
|
183 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
184 |
* Method: socketConnect |
|
185 |
* Signature: (Ljava/net/InetAddress;I)V |
|
186 |
*/ |
|
187 |
JNIEXPORT void JNICALL |
|
188 |
Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this, |
|
189 |
jobject iaObj, jint port, |
|
190 |
jint timeout) |
|
191 |
{ |
|
192 |
jint localport = (*env)->GetIntField(env, this, psi_localportID); |
|
193 |
||
194 |
/* family and localport are int fields of iaObj */ |
|
195 |
int family; |
|
196 |
jint fd, fd1=-1; |
|
197 |
jint len; |
|
198 |
int ipv6_supported = ipv6_available(); |
|
199 |
||
200 |
/* fd initially points to the IPv4 socket and fd1 to the IPv6 socket |
|
201 |
* If we want to connect to IPv6 then we swap the two sockets/objects |
|
202 |
* This way, fd is always the connected socket, and fd1 always gets closed. |
|
203 |
*/ |
|
204 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
205 |
jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
206 |
||
207 |
SOCKETADDRESS him; |
|
208 |
||
209 |
/* The result of the connection */ |
|
210 |
int connect_res; |
|
211 |
||
212 |
if (!IS_NULL(fdObj)) { |
|
213 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
214 |
} |
|
215 |
||
216 |
if (ipv6_supported && !IS_NULL(fd1Obj)) { |
|
217 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
218 |
} |
|
219 |
||
220 |
if (IS_NULL(iaObj)) { |
|
221 |
JNU_ThrowNullPointerException(env, "inet address argument is null."); |
|
222 |
return; |
|
223 |
} |
|
224 |
||
225 |
if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_FALSE) != 0) { |
|
226 |
return; |
|
227 |
} |
|
228 |
||
229 |
family = him.him.sa_family; |
|
230 |
if (family == AF_INET6) { |
|
231 |
if (!ipv6_supported) { |
|
232 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
233 |
"Protocol family not supported"); |
|
234 |
return; |
|
235 |
} else { |
|
236 |
if (fd1 == -1) { |
|
237 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
238 |
"Destination unreachable"); |
|
239 |
return; |
|
240 |
} |
|
241 |
/* close the v4 socket, and set fd to be the v6 socket */ |
|
242 |
(*env)->SetObjectField(env, this, psi_fdID, fd1Obj); |
|
243 |
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); |
|
244 |
NET_SocketClose(fd); |
|
245 |
fd = fd1; fdObj = fd1Obj; |
|
246 |
} |
|
247 |
} else { |
|
248 |
if (fd1 != -1) { |
|
249 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1); |
|
250 |
NET_SocketClose(fd1); |
|
251 |
} |
|
252 |
if (fd == -1) { |
|
253 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
254 |
"Destination unreachable"); |
|
255 |
return; |
|
256 |
} |
|
257 |
} |
|
258 |
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); |
|
259 |
||
260 |
if (timeout <= 0) { |
|
261 |
connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him)); |
|
262 |
if (connect_res == SOCKET_ERROR) { |
|
263 |
connect_res = WSAGetLastError(); |
|
264 |
} |
|
265 |
} else { |
|
266 |
int optval; |
|
267 |
int optlen = sizeof(optval); |
|
268 |
||
269 |
/* make socket non-blocking */ |
|
270 |
optval = 1; |
|
271 |
ioctlsocket( fd, FIONBIO, &optval ); |
|
272 |
||
273 |
/* initiate the connect */ |
|
274 |
connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him)); |
|
275 |
if (connect_res == SOCKET_ERROR) { |
|
276 |
if (WSAGetLastError() != WSAEWOULDBLOCK) { |
|
277 |
connect_res = WSAGetLastError(); |
|
278 |
} else { |
|
279 |
fd_set wr, ex; |
|
280 |
struct timeval t; |
|
281 |
||
282 |
FD_ZERO(&wr); |
|
283 |
FD_ZERO(&ex); |
|
284 |
FD_SET(fd, &wr); |
|
285 |
FD_SET(fd, &ex); |
|
286 |
t.tv_sec = timeout / 1000; |
|
287 |
t.tv_usec = (timeout % 1000) * 1000; |
|
288 |
||
289 |
/* |
|
290 |
* Wait for timout, connection established or |
|
291 |
* connection failed. |
|
292 |
*/ |
|
293 |
connect_res = select(fd+1, 0, &wr, &ex, &t); |
|
294 |
||
295 |
/* |
|
296 |
* Timeout before connection is established/failed so |
|
297 |
* we throw exception and shutdown input/output to prevent |
|
298 |
* socket from being used. |
|
299 |
* The socket should be closed immediately by the caller. |
|
300 |
*/ |
|
301 |
if (connect_res == 0) { |
|
302 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
303 |
"connect timed out"); |
|
304 |
shutdown( fd, SD_BOTH ); |
|
305 |
||
306 |
/* make socket blocking again - just in case */ |
|
307 |
optval = 0; |
|
308 |
ioctlsocket( fd, FIONBIO, &optval ); |
|
309 |
return; |
|
310 |
} |
|
311 |
||
312 |
/* |
|
313 |
* We must now determine if the connection has been established |
|
314 |
* or if it has failed. The logic here is designed to work around |
|
315 |
* bug on Windows NT whereby using getsockopt to obtain the |
|
316 |
* last error (SO_ERROR) indicates there is no error. The workaround |
|
317 |
* on NT is to allow winsock to be scheduled and this is done by |
|
318 |
* yielding and retrying. As yielding is problematic in heavy |
|
319 |
* load conditions we attempt up to 3 times to get the error reason. |
|
320 |
*/ |
|
321 |
if (!FD_ISSET(fd, &ex)) { |
|
322 |
connect_res = 0; |
|
323 |
} else { |
|
324 |
int retry; |
|
325 |
for (retry=0; retry<3; retry++) { |
|
326 |
NET_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, |
|
327 |
(char*)&connect_res, &optlen); |
|
328 |
if (connect_res) { |
|
329 |
break; |
|
330 |
} |
|
331 |
Sleep(0); |
|
332 |
} |
|
333 |
||
334 |
if (connect_res == 0) { |
|
335 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
336 |
"Unable to establish connection"); |
|
337 |
return; |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
/* make socket blocking again */ |
|
344 |
optval = 0; |
|
345 |
ioctlsocket(fd, FIONBIO, &optval); |
|
346 |
} |
|
347 |
||
348 |
if (connect_res) { |
|
349 |
if (connect_res == WSAEADDRNOTAVAIL) { |
|
350 |
JNU_ThrowByName(env, JNU_JAVANETPKG "ConnectException", |
|
351 |
"connect: Address is invalid on local machine, or port is not valid on remote machine"); |
|
352 |
} else { |
|
353 |
NET_ThrowNew(env, connect_res, "connect"); |
|
354 |
} |
|
355 |
return; |
|
356 |
} |
|
357 |
||
358 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd); |
|
359 |
||
360 |
/* set the remote peer address and port */ |
|
361 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
362 |
(*env)->SetIntField(env, this, psi_portID, port); |
|
363 |
||
364 |
/* |
|
365 |
* we need to initialize the local port field if bind was called |
|
366 |
* previously to the connect (by the client) then localport field |
|
367 |
* will already be initialized |
|
368 |
*/ |
|
369 |
if (localport == 0) { |
|
370 |
/* Now that we're a connected socket, let's extract the port number |
|
371 |
* that the system chose for us and store it in the Socket object. |
|
372 |
*/ |
|
373 |
u_short port; |
|
374 |
int len = SOCKETADDRESS_LEN(&him); |
|
375 |
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { |
|
376 |
||
377 |
if (WSAGetLastError() == WSAENOTSOCK) { |
|
378 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
379 |
"Socket closed"); |
|
380 |
} else { |
|
381 |
NET_ThrowCurrent(env, "getsockname failed"); |
|
382 |
} |
|
383 |
return; |
|
384 |
} |
|
385 |
port = ntohs ((u_short)GET_PORT(&him)); |
|
386 |
(*env)->SetIntField(env, this, psi_localportID, (int) port); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
/* |
|
391 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
392 |
* Method: socketBind |
|
393 |
* Signature: (Ljava/net/InetAddress;I)V |
|
394 |
*/ |
|
395 |
JNIEXPORT void JNICALL |
|
396 |
Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this, |
|
18192 | 397 |
jobject iaObj, jint localport, |
398 |
jboolean exclBind) { |
|
2 | 399 |
|
400 |
/* fdObj is the FileDescriptor field on this */ |
|
401 |
jobject fdObj, fd1Obj; |
|
402 |
/* fd is an int field on fdObj */ |
|
403 |
int fd, fd1, len; |
|
404 |
int ipv6_supported = ipv6_available(); |
|
405 |
||
406 |
/* family is an int field of iaObj */ |
|
407 |
int family; |
|
408 |
int rv; |
|
409 |
||
410 |
SOCKETADDRESS him; |
|
411 |
||
412 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
413 |
fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
414 |
||
16870 | 415 |
family = getInetAddress_family(env, iaObj); |
2 | 416 |
|
417 |
if (family == IPv6 && !ipv6_supported) { |
|
418 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
419 |
"Protocol family not supported"); |
|
420 |
return; |
|
421 |
} |
|
422 |
||
423 |
if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) { |
|
424 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
425 |
"Socket closed"); |
|
426 |
return; |
|
427 |
} else { |
|
428 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
429 |
if (ipv6_supported) { |
|
430 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
431 |
} |
|
432 |
} |
|
433 |
if (IS_NULL(iaObj)) { |
|
434 |
JNU_ThrowNullPointerException(env, "inet address argument"); |
|
435 |
return; |
|
436 |
} |
|
437 |
||
438 |
if (NET_InetAddressToSockaddr(env, iaObj, localport, |
|
439 |
(struct sockaddr *)&him, &len, JNI_FALSE) != 0) { |
|
440 |
return; |
|
441 |
} |
|
442 |
if (ipv6_supported) { |
|
443 |
struct ipv6bind v6bind; |
|
444 |
v6bind.addr = &him; |
|
445 |
v6bind.ipv4_fd = fd; |
|
446 |
v6bind.ipv6_fd = fd1; |
|
18192 | 447 |
rv = NET_BindV6(&v6bind, exclBind); |
2 | 448 |
if (rv != -1) { |
449 |
/* check if the fds have changed */ |
|
450 |
if (v6bind.ipv4_fd != fd) { |
|
451 |
fd = v6bind.ipv4_fd; |
|
452 |
if (fd == -1) { |
|
453 |
/* socket is closed. */ |
|
454 |
(*env)->SetObjectField(env, this, psi_fdID, NULL); |
|
455 |
} else { |
|
456 |
/* socket was re-created */ |
|
457 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
|
458 |
} |
|
459 |
} |
|
460 |
if (v6bind.ipv6_fd != fd1) { |
|
461 |
fd1 = v6bind.ipv6_fd; |
|
462 |
if (fd1 == -1) { |
|
463 |
/* socket is closed. */ |
|
464 |
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); |
|
465 |
} else { |
|
466 |
/* socket was re-created */ |
|
467 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1); |
|
468 |
} |
|
469 |
} |
|
470 |
} |
|
471 |
} else { |
|
18192 | 472 |
rv = NET_WinBind(fd, (struct sockaddr *)&him, len, exclBind); |
2 | 473 |
} |
474 |
||
475 |
if (rv == -1) { |
|
476 |
NET_ThrowCurrent(env, "JVM_Bind"); |
|
477 |
return; |
|
478 |
} |
|
479 |
||
480 |
/* set the address */ |
|
481 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
482 |
||
483 |
/* intialize the local port */ |
|
484 |
if (localport == 0) { |
|
485 |
/* Now that we're a bound socket, let's extract the port number |
|
486 |
* that the system chose for us and store it in the Socket object. |
|
487 |
*/ |
|
488 |
int len = SOCKETADDRESS_LEN(&him); |
|
489 |
u_short port; |
|
490 |
fd = him.him.sa_family == AF_INET? fd: fd1; |
|
491 |
||
492 |
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { |
|
493 |
NET_ThrowCurrent(env, "getsockname in plain socketBind"); |
|
494 |
return; |
|
495 |
} |
|
496 |
port = ntohs ((u_short) GET_PORT (&him)); |
|
497 |
||
498 |
(*env)->SetIntField(env, this, psi_localportID, (int) port); |
|
499 |
} else { |
|
500 |
(*env)->SetIntField(env, this, psi_localportID, localport); |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
/* |
|
505 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
506 |
* Method: socketListen |
|
507 |
* Signature: (I)V |
|
508 |
*/ |
|
509 |
JNIEXPORT void JNICALL |
|
510 |
Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this, |
|
511 |
jint count) |
|
512 |
{ |
|
513 |
/* this FileDescriptor fd field */ |
|
514 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
515 |
jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
516 |
jobject address; |
|
517 |
/* fdObj's int fd field */ |
|
518 |
int fd, fd1; |
|
519 |
SOCKETADDRESS addr; int addrlen; |
|
520 |
||
521 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
522 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
523 |
"socket closed"); |
|
524 |
return; |
|
525 |
} |
|
526 |
||
527 |
if (!IS_NULL(fdObj)) { |
|
528 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
529 |
} |
|
530 |
/* Listen on V4 if address type is v4 or if v6 and address is ::0. |
|
531 |
* Listen on V6 if address type is v6 or if v4 and address is 0.0.0.0. |
|
532 |
* In cases, where we listen on one space only, we close the other socket. |
|
533 |
*/ |
|
534 |
address = (*env)->GetObjectField(env, this, psi_addressID); |
|
535 |
if (IS_NULL(address)) { |
|
536 |
JNU_ThrowNullPointerException(env, "socket address"); |
|
537 |
return; |
|
538 |
} |
|
539 |
if (NET_InetAddressToSockaddr(env, address, 0, (struct sockaddr *)&addr, |
|
540 |
&addrlen, JNI_FALSE) != 0) { |
|
541 |
return; |
|
542 |
} |
|
543 |
||
544 |
if (addr.him.sa_family == AF_INET || IN6ADDR_ISANY(&addr.him6)) { |
|
545 |
/* listen on v4 */ |
|
546 |
if (listen(fd, count) == -1) { |
|
547 |
NET_ThrowCurrent(env, "listen failed"); |
|
548 |
} |
|
549 |
} else { |
|
550 |
NET_SocketClose (fd); |
|
551 |
(*env)->SetObjectField(env, this, psi_fdID, NULL); |
|
552 |
} |
|
553 |
if (ipv6_available() && !IS_NULL(fd1Obj)) { |
|
554 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
555 |
if (addr.him.sa_family == AF_INET6 || addr.him4.sin_addr.s_addr == INADDR_ANY) { |
|
556 |
/* listen on v6 */ |
|
557 |
if (listen(fd1, count) == -1) { |
|
558 |
NET_ThrowCurrent(env, "listen failed"); |
|
559 |
} |
|
560 |
} else { |
|
561 |
NET_SocketClose (fd1); |
|
562 |
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); |
|
563 |
} |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
/* |
|
568 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
569 |
* Method: socketAccept |
|
570 |
* Signature: (Ljava/net/SocketImpl;)V |
|
571 |
*/ |
|
572 |
JNIEXPORT void JNICALL |
|
573 |
Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this, |
|
574 |
jobject socket) |
|
575 |
{ |
|
576 |
/* fields on this */ |
|
577 |
jint port; |
|
10426
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
578 |
jint scope; |
2 | 579 |
jint timeout = (*env)->GetIntField(env, this, psi_timeoutID); |
580 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
581 |
jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
582 |
||
583 |
/* the FileDescriptor field on socket */ |
|
584 |
jobject socketFdObj; |
|
585 |
||
586 |
/* cache the Inet4/6Address classes */ |
|
587 |
static jclass inet4Cls; |
|
588 |
static jclass inet6Cls; |
|
589 |
||
590 |
/* the InetAddress field on socket */ |
|
591 |
jobject socketAddressObj; |
|
592 |
||
593 |
/* the fd int field on fdObj */ |
|
594 |
jint fd=-1, fd1=-1; |
|
595 |
||
596 |
SOCKETADDRESS him; |
|
597 |
jint len; |
|
598 |
||
599 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
600 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
601 |
"Socket closed"); |
|
602 |
return; |
|
603 |
} |
|
604 |
if (!IS_NULL(fdObj)) { |
|
605 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
606 |
} |
|
607 |
if (!IS_NULL(fd1Obj)) { |
|
608 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
609 |
} |
|
610 |
if (IS_NULL(socket)) { |
|
611 |
JNU_ThrowNullPointerException(env, "socket is null"); |
|
612 |
return; |
|
613 |
} else { |
|
614 |
socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID); |
|
615 |
socketAddressObj = (*env)->GetObjectField(env, socket, psi_addressID); |
|
616 |
} |
|
617 |
if ((IS_NULL(socketAddressObj)) || (IS_NULL(socketFdObj))) { |
|
618 |
JNU_ThrowNullPointerException(env, "socket address or fd obj"); |
|
619 |
return; |
|
620 |
} |
|
621 |
if (fd != -1 && fd1 != -1) { |
|
622 |
fd_set rfds; |
|
623 |
struct timeval t, *tP=&t; |
|
624 |
int lastfd, res, fd2; |
|
625 |
FD_ZERO(&rfds); |
|
626 |
FD_SET(fd,&rfds); |
|
627 |
FD_SET(fd1,&rfds); |
|
628 |
if (timeout) { |
|
629 |
t.tv_sec = timeout/1000; |
|
630 |
t.tv_usec = (timeout%1000)*1000; |
|
631 |
} else { |
|
632 |
tP = NULL; |
|
633 |
} |
|
634 |
res = select (fd, &rfds, NULL, NULL, tP); |
|
635 |
if (res == 0) { |
|
636 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
637 |
"Accept timed out"); |
|
638 |
return; |
|
639 |
} else if (res == 1) { |
|
640 |
fd2 = FD_ISSET(fd, &rfds)? fd: fd1; |
|
641 |
} else if (res == 2) { |
|
642 |
/* avoid starvation */ |
|
643 |
lastfd = (*env)->GetIntField(env, this, psi_lastfdID); |
|
644 |
if (lastfd != -1) { |
|
645 |
fd2 = lastfd==fd? fd1: fd; |
|
646 |
} else { |
|
647 |
fd2 = fd; |
|
648 |
} |
|
649 |
(*env)->SetIntField(env, this, psi_lastfdID, fd2); |
|
650 |
} else { |
|
651 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
652 |
"select failed"); |
|
653 |
return; |
|
654 |
} |
|
655 |
if (fd2 == fd) { /* v4 */ |
|
656 |
len = sizeof (struct sockaddr_in); |
|
657 |
} else { |
|
658 |
len = sizeof (struct SOCKADDR_IN6); |
|
659 |
} |
|
660 |
fd = fd2; |
|
661 |
} else { |
|
662 |
int ret; |
|
663 |
if (fd1 != -1) { |
|
664 |
fd = fd1; |
|
665 |
len = sizeof (struct SOCKADDR_IN6); |
|
666 |
} else { |
|
667 |
len = sizeof (struct sockaddr_in); |
|
668 |
} |
|
669 |
if (timeout) { |
|
670 |
ret = NET_Timeout(fd, timeout); |
|
671 |
if (ret == 0) { |
|
672 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
673 |
"Accept timed out"); |
|
674 |
return; |
|
675 |
} else if (ret == -1) { |
|
676 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
|
677 |
/* REMIND: SOCKET CLOSED PROBLEM */ |
|
678 |
/* NET_ThrowCurrent(env, "Accept failed"); */ |
|
679 |
return; |
|
680 |
} else if (ret == -2) { |
|
681 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
682 |
"operation interrupted"); |
|
683 |
return; |
|
684 |
} |
|
685 |
} |
|
686 |
} |
|
687 |
fd = accept(fd, (struct sockaddr *)&him, &len); |
|
688 |
if (fd < 0) { |
|
689 |
/* REMIND: SOCKET CLOSED PROBLEM */ |
|
690 |
if (fd == -2) { |
|
691 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
692 |
"operation interrupted"); |
|
693 |
} else { |
|
694 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
695 |
"socket closed"); |
|
696 |
} |
|
697 |
return; |
|
698 |
} |
|
699 |
(*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd); |
|
700 |
||
701 |
if (him.him.sa_family == AF_INET) { |
|
702 |
if (inet4Cls == NULL) { |
|
703 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
704 |
if (c != NULL) { |
|
705 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
706 |
(*env)->DeleteLocalRef(env, c); |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
/* |
|
711 |
* fill up the remote peer port and address in the new socket structure |
|
712 |
*/ |
|
713 |
if (inet4Cls != NULL) { |
|
714 |
socketAddressObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
715 |
} else { |
|
716 |
socketAddressObj = NULL; |
|
717 |
} |
|
718 |
if (socketAddressObj == NULL) { |
|
719 |
/* |
|
720 |
* FindClass or NewObject failed so close connection and |
|
721 |
* exist (there will be a pending exception). |
|
722 |
*/ |
|
723 |
NET_SocketClose(fd); |
|
724 |
return; |
|
725 |
} |
|
726 |
||
16870 | 727 |
setInetAddress_addr(env, socketAddressObj, ntohl(him.him4.sin_addr.s_addr)); |
728 |
setInetAddress_family(env, socketAddressObj, IPv4); |
|
2 | 729 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); |
730 |
} else { |
|
731 |
jbyteArray addr; |
|
732 |
/* AF_INET6 -> Inet6Address */ |
|
733 |
if (inet6Cls == 0) { |
|
734 |
jclass c = (*env)->FindClass(env, "java/net/Inet6Address"); |
|
735 |
if (c != NULL) { |
|
736 |
inet6Cls = (*env)->NewGlobalRef(env, c); |
|
737 |
(*env)->DeleteLocalRef(env, c); |
|
738 |
} |
|
739 |
} |
|
740 |
||
741 |
if (inet6Cls != NULL) { |
|
742 |
socketAddressObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID); |
|
743 |
} else { |
|
744 |
socketAddressObj = NULL; |
|
745 |
} |
|
746 |
if (socketAddressObj == NULL) { |
|
747 |
/* |
|
748 |
* FindClass or NewObject failed so close connection and |
|
749 |
* exist (there will be a pending exception). |
|
750 |
*/ |
|
751 |
NET_SocketClose(fd); |
|
752 |
return; |
|
753 |
} |
|
754 |
addr = (*env)->GetObjectField (env, socketAddressObj, ia6_ipaddressID); |
|
755 |
(*env)->SetByteArrayRegion (env, addr, 0, 16, (const char *)&him.him6.sin6_addr); |
|
16870 | 756 |
setInetAddress_family(env, socketAddressObj, IPv6); |
10426
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
757 |
scope = him.him6.sin6_scope_id; |
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
758 |
(*env)->SetIntField(env, socketAddressObj, ia6_scopeidID, scope); |
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
759 |
if(scope>0) { |
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
760 |
(*env)->SetBooleanField(env, socketAddressObj, ia6_scopeidsetID, JNI_TRUE); |
90d1b830cdd6
7084032: test/java/net/Inet6Address/B6558853.java fails on Windows XP/2003 if IPv6
chegar
parents:
9035
diff
changeset
|
761 |
} |
2 | 762 |
} |
763 |
/* fields common to AF_INET and AF_INET6 */ |
|
764 |
||
765 |
port = ntohs ((u_short) GET_PORT (&him)); |
|
766 |
(*env)->SetIntField(env, socket, psi_portID, (int)port); |
|
767 |
port = (*env)->GetIntField(env, this, psi_localportID); |
|
768 |
(*env)->SetIntField(env, socket, psi_localportID, port); |
|
769 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); |
|
770 |
} |
|
771 |
||
772 |
/* |
|
773 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
774 |
* Method: socketAvailable |
|
775 |
* Signature: ()I |
|
776 |
*/ |
|
777 |
JNIEXPORT jint JNICALL |
|
778 |
Java_java_net_TwoStacksPlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) { |
|
779 |
||
780 |
jint available = -1; |
|
781 |
jint res; |
|
782 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
783 |
jint fd; |
|
784 |
||
785 |
if (IS_NULL(fdObj)) { |
|
786 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
787 |
return -1; |
|
788 |
} else { |
|
789 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
790 |
} |
|
791 |
res = ioctlsocket(fd, FIONREAD, &available); |
|
792 |
/* if result isn't 0, it means an error */ |
|
793 |
if (res != 0) { |
|
794 |
NET_ThrowNew(env, res, "socket available"); |
|
795 |
} |
|
796 |
return available; |
|
797 |
} |
|
798 |
||
799 |
/* |
|
800 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
801 |
* Method: socketClose |
|
802 |
* Signature: ()V |
|
803 |
*/ |
|
804 |
JNIEXPORT void JNICALL |
|
805 |
Java_java_net_TwoStacksPlainSocketImpl_socketClose0(JNIEnv *env, jobject this, |
|
806 |
jboolean useDeferredClose) { |
|
807 |
||
808 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
809 |
jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); |
|
810 |
jint fd=-1, fd1=-1; |
|
811 |
||
812 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
813 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
814 |
"socket already closed"); |
|
815 |
return; |
|
816 |
} |
|
817 |
if (!IS_NULL(fdObj)) { |
|
818 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
819 |
} |
|
820 |
if (!IS_NULL(fd1Obj)) { |
|
821 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
822 |
} |
|
823 |
if (fd != -1) { |
|
824 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
825 |
NET_SocketClose(fd); |
|
826 |
} |
|
827 |
if (fd1 != -1) { |
|
828 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1); |
|
829 |
NET_SocketClose(fd1); |
|
830 |
} |
|
831 |
} |
|
832 |
||
833 |
/* |
|
834 |
* Socket options for plainsocketImpl |
|
835 |
* |
|
836 |
* |
|
837 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
18192 | 838 |
* Method: socketNativeSetOption |
2 | 839 |
* Signature: (IZLjava/lang/Object;)V |
840 |
*/ |
|
841 |
JNIEXPORT void JNICALL |
|
18192 | 842 |
Java_java_net_TwoStacksPlainSocketImpl_socketNativeSetOption(JNIEnv *env, |
843 |
jobject this, |
|
2 | 844 |
jint cmd, jboolean on, |
845 |
jobject value) { |
|
846 |
int fd, fd1; |
|
847 |
int level, optname, optlen; |
|
848 |
union { |
|
849 |
int i; |
|
850 |
struct linger ling; |
|
851 |
} optval; |
|
852 |
||
853 |
/* |
|
854 |
* Get SOCKET and check that it hasn't been closed |
|
855 |
*/ |
|
856 |
fd = getFD(env, this); |
|
857 |
fd1 = getFD1(env, this); |
|
858 |
if (fd < 0 && fd1 < 0) { |
|
859 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
860 |
return; |
|
861 |
} |
|
862 |
||
863 |
/* |
|
864 |
* SO_TIMEOUT is the socket option used to specify the timeout |
|
865 |
* for ServerSocket.accept and Socket.getInputStream().read. |
|
866 |
* It does not typically map to a native level socket option. |
|
867 |
* For Windows we special-case this and use the SOL_SOCKET/SO_RCVTIMEO |
|
868 |
* socket option to specify a receive timeout on the socket. This |
|
869 |
* receive timeout is applicable to Socket only and the socket |
|
870 |
* option should not be set on ServerSocket. |
|
871 |
*/ |
|
872 |
if (cmd == java_net_SocketOptions_SO_TIMEOUT) { |
|
873 |
||
874 |
/* |
|
875 |
* Don't enable the socket option on ServerSocket as it's |
|
876 |
* meaningless (we don't receive on a ServerSocket). |
|
877 |
*/ |
|
878 |
jobject ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID); |
|
879 |
if (ssObj != NULL) { |
|
880 |
return; |
|
881 |
} |
|
882 |
||
883 |
/* |
|
884 |
* SO_RCVTIMEO is only supported on Microsoft's implementation |
|
885 |
* of Windows Sockets so if WSAENOPROTOOPT returned then |
|
886 |
* reset flag and timeout will be implemented using |
|
887 |
* select() -- see SocketInputStream.socketRead. |
|
888 |
*/ |
|
889 |
if (isRcvTimeoutSupported) { |
|
890 |
jclass iCls = (*env)->FindClass(env, "java/lang/Integer"); |
|
891 |
jfieldID i_valueID; |
|
892 |
jint timeout; |
|
893 |
||
894 |
CHECK_NULL(iCls); |
|
895 |
i_valueID = (*env)->GetFieldID(env, iCls, "value", "I"); |
|
896 |
CHECK_NULL(i_valueID); |
|
897 |
timeout = (*env)->GetIntField(env, value, i_valueID); |
|
898 |
||
899 |
/* |
|
900 |
* Disable SO_RCVTIMEO if timeout is <= 5 second. |
|
901 |
*/ |
|
902 |
if (timeout <= 5000) { |
|
903 |
timeout = 0; |
|
904 |
} |
|
905 |
||
906 |
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, |
|
907 |
sizeof(timeout)) < 0) { |
|
908 |
if (WSAGetLastError() == WSAENOPROTOOPT) { |
|
909 |
isRcvTimeoutSupported = JNI_FALSE; |
|
910 |
} else { |
|
911 |
NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO"); |
|
912 |
} |
|
913 |
} |
|
914 |
if (fd1 != -1) { |
|
915 |
if (setsockopt(fd1, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, |
|
916 |
sizeof(timeout)) < 0) { |
|
917 |
NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO"); |
|
918 |
} |
|
919 |
} |
|
920 |
} |
|
921 |
return; |
|
922 |
} |
|
923 |
||
924 |
/* |
|
925 |
* Map the Java level socket option to the platform specific |
|
926 |
* level |
|
927 |
*/ |
|
928 |
if (NET_MapSocketOption(cmd, &level, &optname)) { |
|
929 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
930 |
"Invalid option"); |
|
931 |
return; |
|
932 |
} |
|
933 |
||
934 |
switch (cmd) { |
|
935 |
||
936 |
case java_net_SocketOptions_TCP_NODELAY : |
|
937 |
case java_net_SocketOptions_SO_OOBINLINE : |
|
938 |
case java_net_SocketOptions_SO_KEEPALIVE : |
|
939 |
case java_net_SocketOptions_SO_REUSEADDR : |
|
940 |
optval.i = (on ? 1 : 0); |
|
941 |
optlen = sizeof(optval.i); |
|
942 |
break; |
|
943 |
||
944 |
case java_net_SocketOptions_SO_SNDBUF : |
|
945 |
case java_net_SocketOptions_SO_RCVBUF : |
|
946 |
case java_net_SocketOptions_IP_TOS : |
|
947 |
{ |
|
948 |
jclass cls; |
|
949 |
jfieldID fid; |
|
950 |
||
951 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
952 |
CHECK_NULL(cls); |
|
953 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
954 |
CHECK_NULL(fid); |
|
955 |
||
956 |
optval.i = (*env)->GetIntField(env, value, fid); |
|
957 |
optlen = sizeof(optval.i); |
|
958 |
} |
|
959 |
break; |
|
960 |
||
961 |
case java_net_SocketOptions_SO_LINGER : |
|
962 |
{ |
|
963 |
jclass cls; |
|
964 |
jfieldID fid; |
|
965 |
||
966 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
967 |
CHECK_NULL(cls); |
|
968 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
969 |
CHECK_NULL(fid); |
|
970 |
||
971 |
if (on) { |
|
972 |
optval.ling.l_onoff = 1; |
|
910 | 973 |
optval.ling.l_linger = |
974 |
(unsigned short)(*env)->GetIntField(env, value, fid); |
|
2 | 975 |
} else { |
976 |
optval.ling.l_onoff = 0; |
|
977 |
optval.ling.l_linger = 0; |
|
978 |
} |
|
979 |
optlen = sizeof(optval.ling); |
|
980 |
} |
|
981 |
break; |
|
982 |
||
983 |
default: /* shouldn't get here */ |
|
984 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
985 |
"Option not supported by TwoStacksPlainSocketImpl"); |
|
986 |
return; |
|
987 |
} |
|
988 |
||
989 |
if (fd != -1) { |
|
990 |
if (NET_SetSockOpt(fd, level, optname, (void *)&optval, optlen) < 0) { |
|
991 |
NET_ThrowCurrent(env, "setsockopt"); |
|
992 |
} |
|
993 |
} |
|
994 |
||
995 |
if (fd1 != -1) { |
|
996 |
if (NET_SetSockOpt(fd1, level, optname, (void *)&optval, optlen) < 0) { |
|
997 |
NET_ThrowCurrent(env, "setsockopt"); |
|
998 |
} |
|
999 |
} |
|
1000 |
} |
|
1001 |
||
1002 |
||
1003 |
/* |
|
1004 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
1005 |
* Method: socketGetOption |
|
1006 |
* Signature: (I)I |
|
1007 |
*/ |
|
1008 |
JNIEXPORT jint JNICALL |
|
1009 |
Java_java_net_TwoStacksPlainSocketImpl_socketGetOption(JNIEnv *env, jobject this, |
|
1010 |
jint opt, jobject iaContainerObj) { |
|
1011 |
||
1012 |
int fd, fd1; |
|
1013 |
int level, optname, optlen; |
|
1014 |
union { |
|
1015 |
int i; |
|
1016 |
struct linger ling; |
|
1017 |
} optval; |
|
1018 |
||
1019 |
/* |
|
1020 |
* Get SOCKET and check it hasn't been closed |
|
1021 |
*/ |
|
1022 |
fd = getFD(env, this); |
|
1023 |
fd1 = getFD1(env, this); |
|
1024 |
||
1025 |
if (fd < 0 && fd1 < 0) { |
|
1026 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
1027 |
return -1; |
|
1028 |
} |
|
1029 |
if (fd < 0) { |
|
1030 |
fd = fd1; |
|
1031 |
} |
|
1032 |
||
1033 |
/* For IPv6, we assume both sockets have the same setting always */ |
|
1034 |
||
1035 |
/* |
|
1036 |
* SO_BINDADDR isn't a socket option |
|
1037 |
*/ |
|
1038 |
if (opt == java_net_SocketOptions_SO_BINDADDR) { |
|
7969
1a6282ab0d2a
7009760: Possible stack corruption in Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
chegar
parents:
5506
diff
changeset
|
1039 |
SOCKETADDRESS him; |
2 | 1040 |
int len; |
1041 |
int port; |
|
1042 |
jobject iaObj; |
|
1043 |
jclass iaCntrClass; |
|
1044 |
jfieldID iaFieldID; |
|
1045 |
||
7969
1a6282ab0d2a
7009760: Possible stack corruption in Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
chegar
parents:
5506
diff
changeset
|
1046 |
len = sizeof(him); |
2 | 1047 |
|
1048 |
if (fd == -1) { |
|
1049 |
/* must be an IPV6 only socket. Case where both sockets are != -1 |
|
1050 |
* is handled in java |
|
1051 |
*/ |
|
1052 |
fd = getFD1 (env, this); |
|
1053 |
} |
|
1054 |
||
1055 |
if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) { |
|
1056 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1057 |
"Error getting socket name"); |
|
1058 |
return -1; |
|
1059 |
} |
|
1060 |
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); |
|
1061 |
CHECK_NULL_RETURN(iaObj, -1); |
|
1062 |
||
1063 |
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj); |
|
1064 |
iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;"); |
|
1065 |
CHECK_NULL_RETURN(iaFieldID, -1); |
|
1066 |
(*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj); |
|
1067 |
return 0; /* notice change from before */ |
|
1068 |
} |
|
1069 |
||
1070 |
/* |
|
1071 |
* Map the Java level socket option to the platform specific |
|
1072 |
* level and option name. |
|
1073 |
*/ |
|
1074 |
if (NET_MapSocketOption(opt, &level, &optname)) { |
|
1075 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option"); |
|
1076 |
return -1; |
|
1077 |
} |
|
1078 |
||
1079 |
/* |
|
1080 |
* Args are int except for SO_LINGER |
|
1081 |
*/ |
|
1082 |
if (opt == java_net_SocketOptions_SO_LINGER) { |
|
1083 |
optlen = sizeof(optval.ling); |
|
1084 |
} else { |
|
1085 |
optlen = sizeof(optval.i); |
|
1086 |
optval.i = 0; |
|
1087 |
} |
|
1088 |
||
1089 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { |
|
1090 |
NET_ThrowCurrent(env, "getsockopt"); |
|
1091 |
return -1; |
|
1092 |
} |
|
1093 |
||
1094 |
switch (opt) { |
|
1095 |
case java_net_SocketOptions_SO_LINGER: |
|
1096 |
return (optval.ling.l_onoff ? optval.ling.l_linger: -1); |
|
1097 |
||
1098 |
case java_net_SocketOptions_SO_SNDBUF: |
|
1099 |
case java_net_SocketOptions_SO_RCVBUF: |
|
1100 |
case java_net_SocketOptions_IP_TOS: |
|
1101 |
return optval.i; |
|
1102 |
||
1103 |
case java_net_SocketOptions_TCP_NODELAY : |
|
1104 |
case java_net_SocketOptions_SO_OOBINLINE : |
|
1105 |
case java_net_SocketOptions_SO_KEEPALIVE : |
|
1106 |
case java_net_SocketOptions_SO_REUSEADDR : |
|
1107 |
return (optval.i == 0) ? -1 : 1; |
|
1108 |
||
1109 |
default: /* shouldn't get here */ |
|
1110 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1111 |
"Option not supported by TwoStacksPlainSocketImpl"); |
|
1112 |
return -1; |
|
1113 |
} |
|
1114 |
} |
|
1115 |
||
1116 |
/* |
|
1117 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
1118 |
* Method: socketShutdown |
|
1119 |
* Signature: (I)V |
|
1120 |
*/ |
|
1121 |
JNIEXPORT void JNICALL |
|
1122 |
Java_java_net_TwoStacksPlainSocketImpl_socketShutdown(JNIEnv *env, jobject this, |
|
1123 |
jint howto) |
|
1124 |
{ |
|
1125 |
||
1126 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
1127 |
jint fd; |
|
1128 |
||
1129 |
/* |
|
1130 |
* WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being |
|
1131 |
* -1 already? |
|
1132 |
*/ |
|
1133 |
if (IS_NULL(fdObj)) { |
|
1134 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1135 |
"socket already closed"); |
|
1136 |
return; |
|
1137 |
} else { |
|
1138 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1139 |
} |
|
1140 |
shutdown(fd, howto); |
|
1141 |
} |
|
1142 |
||
1143 |
/* |
|
1144 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
1145 |
* Method: socketSendUrgentData |
|
1146 |
* Signature: (B)V |
|
1147 |
*/ |
|
1148 |
JNIEXPORT void JNICALL |
|
1149 |
Java_java_net_TwoStacksPlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this, |
|
1150 |
jint data) { |
|
1151 |
/* The fd field */ |
|
1152 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
1153 |
int n, fd; |
|
1154 |
unsigned char d = data & 0xff; |
|
1155 |
||
1156 |
if (IS_NULL(fdObj)) { |
|
1157 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
1158 |
return; |
|
1159 |
} else { |
|
1160 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1161 |
/* Bug 4086704 - If the Socket associated with this file descriptor |
|
1162 |
* was closed (sysCloseFD), the the file descriptor is set to -1. |
|
1163 |
*/ |
|
1164 |
if (fd == -1) { |
|
1165 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
1166 |
return; |
|
1167 |
} |
|
1168 |
||
1169 |
} |
|
1170 |
n = send(fd, (char *)&data, 1, MSG_OOB); |
|
1171 |
if (n == JVM_IO_ERR) { |
|
1172 |
NET_ThrowCurrent(env, "send"); |
|
1173 |
return; |
|
1174 |
} |
|
1175 |
if (n == JVM_IO_INTR) { |
|
1176 |
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); |
|
1177 |
return; |
|
1178 |
} |
|
1179 |
} |