2
|
1 |
/*
|
|
2 |
* Copyright 1997-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 <errno.h>
|
|
27 |
#include <string.h>
|
|
28 |
#include <sys/types.h>
|
|
29 |
#include <sys/socket.h>
|
|
30 |
#if defined(__linux__) && !defined(USE_SELECT)
|
|
31 |
#include <sys/poll.h>
|
|
32 |
#endif
|
|
33 |
#include <netinet/tcp.h> /* Defines TCP_NODELAY, needed for 2.6 */
|
|
34 |
#include <netinet/in.h>
|
|
35 |
#ifdef __linux__
|
|
36 |
#include <netinet/ip.h>
|
|
37 |
#endif
|
|
38 |
#include <netdb.h>
|
|
39 |
#include <stdlib.h>
|
|
40 |
|
|
41 |
#ifdef __solaris__
|
|
42 |
#include <fcntl.h>
|
|
43 |
#endif
|
|
44 |
#ifdef __linux__
|
|
45 |
#include <linux/unistd.h>
|
|
46 |
#include <linux/sysctl.h>
|
|
47 |
#endif
|
|
48 |
|
|
49 |
#include "jvm.h"
|
|
50 |
#include "jni_util.h"
|
|
51 |
#include "net_util.h"
|
|
52 |
|
|
53 |
#include "java_net_SocketOptions.h"
|
|
54 |
#include "java_net_PlainSocketImpl.h"
|
|
55 |
|
|
56 |
/************************************************************************
|
|
57 |
* PlainSocketImpl
|
|
58 |
*/
|
|
59 |
|
|
60 |
static jfieldID IO_fd_fdID;
|
|
61 |
|
|
62 |
jfieldID psi_fdID;
|
|
63 |
jfieldID psi_addressID;
|
|
64 |
jfieldID psi_ipaddressID;
|
|
65 |
jfieldID psi_portID;
|
|
66 |
jfieldID psi_localportID;
|
|
67 |
jfieldID psi_timeoutID;
|
|
68 |
jfieldID psi_trafficClassID;
|
|
69 |
jfieldID psi_serverSocketID;
|
|
70 |
jfieldID psi_fdLockID;
|
|
71 |
jfieldID psi_closePendingID;
|
|
72 |
|
|
73 |
/*
|
|
74 |
* file descriptor used for dup2
|
|
75 |
*/
|
|
76 |
static int marker_fd = -1;
|
|
77 |
|
|
78 |
|
|
79 |
#define SET_NONBLOCKING(fd) { \
|
|
80 |
int flags = fcntl(fd, F_GETFL); \
|
|
81 |
flags |= O_NONBLOCK; \
|
|
82 |
fcntl(fd, F_SETFL, flags); \
|
|
83 |
}
|
|
84 |
|
|
85 |
#define SET_BLOCKING(fd) { \
|
|
86 |
int flags = fcntl(fd, F_GETFL); \
|
|
87 |
flags &= ~O_NONBLOCK; \
|
|
88 |
fcntl(fd, F_SETFL, flags); \
|
|
89 |
}
|
|
90 |
|
|
91 |
/*
|
|
92 |
* Create the marker file descriptor by establishing a loopback connection
|
|
93 |
* which we shutdown but do not close the fd. The result is an fd that
|
|
94 |
* can be used for read/write.
|
|
95 |
*/
|
|
96 |
static int getMarkerFD()
|
|
97 |
{
|
|
98 |
int server_fd, child_fd, connect_fd;
|
|
99 |
SOCKADDR him;
|
|
100 |
int type, len, port;
|
|
101 |
|
|
102 |
type = AF_INET;
|
|
103 |
#ifdef AF_INET6
|
|
104 |
if (ipv6_available()) {
|
|
105 |
type = AF_INET6;
|
|
106 |
}
|
|
107 |
#endif
|
|
108 |
|
|
109 |
/*
|
|
110 |
* Create listener on any port
|
|
111 |
*/
|
|
112 |
server_fd = JVM_Socket(type, SOCK_STREAM, 0);
|
|
113 |
if (server_fd < 0) {
|
|
114 |
return -1;
|
|
115 |
}
|
|
116 |
if (JVM_Listen(server_fd, 1) == -1) {
|
|
117 |
JVM_SocketClose(server_fd);
|
|
118 |
return -1;
|
|
119 |
}
|
|
120 |
len = SOCKADDR_LEN;
|
|
121 |
if (JVM_GetSockName(server_fd, (struct sockaddr *)&him, &len) == -1) {
|
|
122 |
JVM_SocketClose(server_fd);
|
|
123 |
return -1;
|
|
124 |
}
|
|
125 |
port = NET_GetPortFromSockaddr((struct sockaddr *)&him);
|
|
126 |
|
|
127 |
/*
|
|
128 |
* Establish connection from client socket.
|
|
129 |
* Server is bound to 0.0.0.0/X or ::/X
|
|
130 |
* We connect to 127.0.0.1/X or ::1/X
|
|
131 |
*/
|
|
132 |
#ifdef AF_INET6
|
|
133 |
if (ipv6_available()) {
|
|
134 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&him;
|
|
135 |
jbyte caddr[16];
|
|
136 |
memset((char *) caddr, 0, 16);
|
|
137 |
caddr[15] = 1;
|
|
138 |
memset((char *)him6, 0, sizeof(struct sockaddr_in6));
|
|
139 |
memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
|
|
140 |
him6->sin6_port = htons((short) port);
|
|
141 |
him6->sin6_family = AF_INET6;
|
|
142 |
len = sizeof(struct sockaddr_in6) ;
|
|
143 |
} else
|
|
144 |
#endif /* AF_INET6 */
|
|
145 |
{
|
|
146 |
struct sockaddr_in *him4 = (struct sockaddr_in*)&him;
|
|
147 |
memset((char *) him4, 0, sizeof(struct sockaddr_in));
|
|
148 |
him4->sin_port = htons((short) port);
|
|
149 |
him4->sin_addr.s_addr = (uint32_t) htonl(0x7f000001);
|
|
150 |
him4->sin_family = AF_INET;
|
|
151 |
len = sizeof(struct sockaddr_in);
|
|
152 |
}
|
|
153 |
connect_fd = JVM_Socket(type, SOCK_STREAM, 0);
|
|
154 |
if (connect_fd < 0) {
|
|
155 |
JVM_SocketClose(server_fd);
|
|
156 |
return -1;
|
|
157 |
}
|
|
158 |
if (JVM_Connect(connect_fd, (struct sockaddr *) &him, len) == -1) {
|
|
159 |
JVM_SocketClose(server_fd);
|
|
160 |
JVM_SocketClose(connect_fd);
|
|
161 |
return -1;
|
|
162 |
}
|
|
163 |
|
|
164 |
/*
|
|
165 |
* Server accepts connection - do in in non-blocking mode to avoid
|
|
166 |
* hanging if there's an error (should never happen!!!)
|
|
167 |
*/
|
|
168 |
SET_NONBLOCKING(server_fd);
|
|
169 |
len = SOCKADDR_LEN;
|
|
170 |
child_fd = JVM_Accept(server_fd, (struct sockaddr *)&him, (jint *)&len);
|
|
171 |
if (child_fd == -1) {
|
|
172 |
JVM_SocketClose(server_fd);
|
|
173 |
JVM_SocketClose(connect_fd);
|
|
174 |
return -1;
|
|
175 |
}
|
|
176 |
|
|
177 |
/*
|
|
178 |
* Finally shutdown connect_fd (any reads to this fd will get
|
|
179 |
* EOF; any writes will get an error).
|
|
180 |
*/
|
|
181 |
JVM_SocketShutdown(connect_fd, 2);
|
|
182 |
JVM_SocketClose(child_fd);
|
|
183 |
JVM_SocketClose(server_fd);
|
|
184 |
|
|
185 |
return connect_fd;
|
|
186 |
}
|
|
187 |
|
|
188 |
|
|
189 |
/*
|
|
190 |
* Return the file descriptor given a PlainSocketImpl
|
|
191 |
*/
|
|
192 |
static int getFD(JNIEnv *env, jobject this) {
|
|
193 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
194 |
CHECK_NULL_RETURN(fdObj, -1);
|
|
195 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
196 |
}
|
|
197 |
|
|
198 |
/*
|
|
199 |
* The initroto function is called whenever PlainSocketImpl is
|
|
200 |
* loaded, to cache fieldIds for efficiency. This is called everytime
|
|
201 |
* the Java class is loaded.
|
|
202 |
*
|
|
203 |
* Class: java_net_PlainSocketImpl
|
|
204 |
* Method: initProto
|
|
205 |
* Signature: ()V
|
|
206 |
*/
|
|
207 |
JNIEXPORT void JNICALL
|
|
208 |
Java_java_net_PlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
|
|
209 |
char *s;
|
|
210 |
|
|
211 |
psi_fdID = (*env)->GetFieldID(env, cls , "fd",
|
|
212 |
"Ljava/io/FileDescriptor;");
|
|
213 |
CHECK_NULL(psi_fdID);
|
|
214 |
psi_addressID = (*env)->GetFieldID(env, cls, "address",
|
|
215 |
"Ljava/net/InetAddress;");
|
|
216 |
CHECK_NULL(psi_addressID);
|
|
217 |
psi_portID = (*env)->GetFieldID(env, cls, "port", "I");
|
|
218 |
CHECK_NULL(psi_portID);
|
|
219 |
psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I");
|
|
220 |
CHECK_NULL(psi_localportID);
|
|
221 |
psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
|
|
222 |
CHECK_NULL(psi_timeoutID);
|
|
223 |
psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
|
|
224 |
CHECK_NULL(psi_trafficClassID);
|
|
225 |
psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket",
|
|
226 |
"Ljava/net/ServerSocket;");
|
|
227 |
CHECK_NULL(psi_serverSocketID);
|
|
228 |
psi_fdLockID = (*env)->GetFieldID(env, cls, "fdLock",
|
|
229 |
"Ljava/lang/Object;");
|
|
230 |
CHECK_NULL(psi_fdLockID);
|
|
231 |
psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z");
|
|
232 |
CHECK_NULL(psi_closePendingID);
|
|
233 |
IO_fd_fdID = NET_GetFileDescriptorID(env);
|
|
234 |
CHECK_NULL(IO_fd_fdID);
|
|
235 |
|
|
236 |
/* Create the marker fd used for dup2 */
|
|
237 |
marker_fd = getMarkerFD();
|
|
238 |
}
|
|
239 |
|
|
240 |
/* a global reference to the java.net.SocketException class. In
|
|
241 |
* socketCreate, we ensure that this is initialized. This is to
|
|
242 |
* prevent the problem where socketCreate runs out of file
|
|
243 |
* descriptors, and is then unable to load the exception class.
|
|
244 |
*/
|
|
245 |
static jclass socketExceptionCls;
|
|
246 |
|
|
247 |
/*
|
|
248 |
* Class: java_net_PlainSocketImpl
|
|
249 |
* Method: socketCreate
|
|
250 |
* Signature: (Z)V */
|
|
251 |
JNIEXPORT void JNICALL
|
|
252 |
Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
|
|
253 |
jboolean stream) {
|
|
254 |
jobject fdObj, ssObj;
|
|
255 |
int fd;
|
|
256 |
int arg = -1;
|
|
257 |
|
|
258 |
if (socketExceptionCls == NULL) {
|
|
259 |
jclass c = (*env)->FindClass(env, "java/net/SocketException");
|
|
260 |
CHECK_NULL(c);
|
|
261 |
socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
|
|
262 |
CHECK_NULL(socketExceptionCls);
|
|
263 |
}
|
|
264 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
265 |
|
|
266 |
if (fdObj == NULL) {
|
|
267 |
(*env)->ThrowNew(env, socketExceptionCls, "null fd object");
|
|
268 |
return;
|
|
269 |
}
|
|
270 |
#ifdef AF_INET6
|
|
271 |
if (ipv6_available()) {
|
|
272 |
fd = JVM_Socket(AF_INET6, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
|
|
273 |
} else
|
|
274 |
#endif /* AF_INET6 */
|
|
275 |
{
|
|
276 |
fd = JVM_Socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0);
|
|
277 |
}
|
|
278 |
if (fd == JVM_IO_ERR) {
|
|
279 |
/* note: if you run out of fds, you may not be able to load
|
|
280 |
* the exception class, and get a NoClassDefFoundError
|
|
281 |
* instead.
|
|
282 |
*/
|
|
283 |
NET_ThrowNew(env, errno, "can't create socket");
|
|
284 |
return;
|
|
285 |
} else {
|
|
286 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
|
|
287 |
}
|
|
288 |
|
|
289 |
/*
|
|
290 |
* If this is a server socket then enable SO_REUSEADDR
|
|
291 |
* automatically and set to non blocking.
|
|
292 |
*/
|
|
293 |
ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
|
|
294 |
if (ssObj != NULL) {
|
|
295 |
int arg = 1;
|
|
296 |
SET_NONBLOCKING(fd);
|
|
297 |
JVM_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
|
|
298 |
sizeof(arg));
|
|
299 |
}
|
|
300 |
}
|
|
301 |
|
|
302 |
/*
|
|
303 |
* inetAddress is the address object passed to the socket connect
|
|
304 |
* call.
|
|
305 |
*
|
|
306 |
* Class: java_net_PlainSocketImpl
|
|
307 |
* Method: socketConnect
|
|
308 |
* Signature: (Ljava/net/InetAddress;I)V
|
|
309 |
*/
|
|
310 |
JNIEXPORT void JNICALL
|
|
311 |
Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
|
|
312 |
jobject iaObj, jint port,
|
|
313 |
jint timeout)
|
|
314 |
{
|
|
315 |
jint localport = (*env)->GetIntField(env, this, psi_localportID);
|
|
316 |
int len = 0;
|
|
317 |
|
|
318 |
/* fdObj is the FileDescriptor field on this */
|
|
319 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
320 |
jobject fdLock;
|
|
321 |
|
|
322 |
jint trafficClass = (*env)->GetIntField(env, this, psi_trafficClassID);
|
|
323 |
|
|
324 |
/* fd is an int field on iaObj */
|
|
325 |
jint fd;
|
|
326 |
|
|
327 |
SOCKADDR him;
|
|
328 |
/* The result of the connection */
|
|
329 |
int connect_rv = -1;
|
|
330 |
|
|
331 |
if (IS_NULL(fdObj)) {
|
|
332 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
|
|
333 |
return;
|
|
334 |
} else {
|
|
335 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
336 |
}
|
|
337 |
if (IS_NULL(iaObj)) {
|
|
338 |
JNU_ThrowNullPointerException(env, "inet address argument null.");
|
|
339 |
return;
|
|
340 |
}
|
|
341 |
|
|
342 |
/* connect */
|
|
343 |
if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
|
|
344 |
return;
|
|
345 |
}
|
|
346 |
|
|
347 |
#ifdef AF_INET6
|
|
348 |
if (trafficClass != 0 && ipv6_available()) {
|
|
349 |
NET_SetTrafficClass((struct sockaddr *)&him, trafficClass);
|
|
350 |
}
|
|
351 |
#endif /* AF_INET6 */
|
|
352 |
if (timeout <= 0) {
|
|
353 |
connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
|
|
354 |
#ifdef __solaris__
|
|
355 |
if (connect_rv == JVM_IO_ERR && errno == EINPROGRESS ) {
|
|
356 |
|
|
357 |
/* This can happen if a blocking connect is interrupted by a signal.
|
|
358 |
* See 6343810.
|
|
359 |
*/
|
|
360 |
while (1) {
|
|
361 |
fd_set wr, ex;
|
|
362 |
|
|
363 |
FD_ZERO(&wr);
|
|
364 |
FD_SET(fd, &wr);
|
|
365 |
FD_ZERO(&ex);
|
|
366 |
FD_SET(fd, &ex);
|
|
367 |
|
|
368 |
errno = 0;
|
|
369 |
connect_rv = NET_Select(fd+1, 0, &wr, &ex, 0);
|
|
370 |
if (connect_rv == JVM_IO_ERR) {
|
|
371 |
if (errno == EINTR) {
|
|
372 |
continue;
|
|
373 |
} else {
|
|
374 |
break;
|
|
375 |
}
|
|
376 |
}
|
|
377 |
if (connect_rv > 0) {
|
|
378 |
int optlen;
|
|
379 |
/* has connection been established */
|
|
380 |
optlen = sizeof(connect_rv);
|
|
381 |
if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
|
|
382 |
(void*)&connect_rv, &optlen) <0) {
|
|
383 |
connect_rv = errno;
|
|
384 |
}
|
|
385 |
|
|
386 |
if (connect_rv != 0) {
|
|
387 |
/* restore errno */
|
|
388 |
errno = connect_rv;
|
|
389 |
connect_rv = JVM_IO_ERR;
|
|
390 |
}
|
|
391 |
break;
|
|
392 |
}
|
|
393 |
}
|
|
394 |
}
|
|
395 |
#endif
|
|
396 |
} else {
|
|
397 |
/*
|
|
398 |
* A timeout was specified. We put the socket into non-blocking
|
|
399 |
* mode, connect, and then wait for the connection to be
|
|
400 |
* established, fail, or timeout.
|
|
401 |
*/
|
|
402 |
SET_NONBLOCKING(fd);
|
|
403 |
|
|
404 |
/* no need to use NET_Connect as non-blocking */
|
|
405 |
connect_rv = connect(fd, (struct sockaddr *)&him, len);
|
|
406 |
|
|
407 |
/* connection not established immediately */
|
|
408 |
if (connect_rv != 0) {
|
|
409 |
int optlen;
|
|
410 |
jlong prevTime = JVM_CurrentTimeMillis(env, 0);
|
|
411 |
|
|
412 |
if (errno != EINPROGRESS) {
|
|
413 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
|
|
414 |
"connect failed");
|
|
415 |
SET_BLOCKING(fd);
|
|
416 |
return;
|
|
417 |
}
|
|
418 |
|
|
419 |
/*
|
|
420 |
* Wait for the connection to be established or a
|
|
421 |
* timeout occurs. poll/select needs to handle EINTR in
|
|
422 |
* case lwp sig handler redirects any process signals to
|
|
423 |
* this thread.
|
|
424 |
*/
|
|
425 |
while (1) {
|
|
426 |
jlong newTime;
|
|
427 |
#ifndef USE_SELECT
|
|
428 |
{
|
|
429 |
struct pollfd pfd;
|
|
430 |
pfd.fd = fd;
|
|
431 |
pfd.events = POLLOUT;
|
|
432 |
|
|
433 |
errno = 0;
|
|
434 |
connect_rv = NET_Poll(&pfd, 1, timeout);
|
|
435 |
}
|
|
436 |
#else
|
|
437 |
{
|
|
438 |
fd_set wr, ex;
|
|
439 |
struct timeval t;
|
|
440 |
|
|
441 |
t.tv_sec = timeout / 1000;
|
|
442 |
t.tv_usec = (timeout % 1000) * 1000;
|
|
443 |
|
|
444 |
FD_ZERO(&wr);
|
|
445 |
FD_SET(fd, &wr);
|
|
446 |
FD_ZERO(&ex);
|
|
447 |
FD_SET(fd, &ex);
|
|
448 |
|
|
449 |
errno = 0;
|
|
450 |
connect_rv = NET_Select(fd+1, 0, &wr, &ex, &t);
|
|
451 |
}
|
|
452 |
#endif
|
|
453 |
|
|
454 |
if (connect_rv >= 0) {
|
|
455 |
break;
|
|
456 |
}
|
|
457 |
if (errno != EINTR) {
|
|
458 |
break;
|
|
459 |
}
|
|
460 |
|
|
461 |
/*
|
|
462 |
* The poll was interrupted so adjust timeout and
|
|
463 |
* restart
|
|
464 |
*/
|
|
465 |
newTime = JVM_CurrentTimeMillis(env, 0);
|
|
466 |
timeout -= (newTime - prevTime);
|
|
467 |
if (timeout <= 0) {
|
|
468 |
connect_rv = 0;
|
|
469 |
break;
|
|
470 |
}
|
|
471 |
prevTime = newTime;
|
|
472 |
|
|
473 |
} /* while */
|
|
474 |
|
|
475 |
if (connect_rv == 0) {
|
|
476 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
|
|
477 |
"connect timed out");
|
|
478 |
|
|
479 |
/*
|
|
480 |
* Timeout out but connection may still be established.
|
|
481 |
* At the high level it should be closed immediately but
|
|
482 |
* just in case we make the socket blocking again and
|
|
483 |
* shutdown input & output.
|
|
484 |
*/
|
|
485 |
SET_BLOCKING(fd);
|
|
486 |
JVM_SocketShutdown(fd, 2);
|
|
487 |
return;
|
|
488 |
}
|
|
489 |
|
|
490 |
/* has connection been established */
|
|
491 |
optlen = sizeof(connect_rv);
|
|
492 |
if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
|
|
493 |
&optlen) <0) {
|
|
494 |
connect_rv = errno;
|
|
495 |
}
|
|
496 |
}
|
|
497 |
|
|
498 |
/* make socket blocking again */
|
|
499 |
SET_BLOCKING(fd);
|
|
500 |
|
|
501 |
/* restore errno */
|
|
502 |
if (connect_rv != 0) {
|
|
503 |
errno = connect_rv;
|
|
504 |
connect_rv = JVM_IO_ERR;
|
|
505 |
}
|
|
506 |
}
|
|
507 |
|
|
508 |
/* report the appropriate exception */
|
|
509 |
if (connect_rv < 0) {
|
|
510 |
|
|
511 |
#ifdef __linux__
|
|
512 |
/*
|
|
513 |
* Linux/GNU distribution setup /etc/hosts so that
|
|
514 |
* InetAddress.getLocalHost gets back the loopback address
|
|
515 |
* rather than the host address. Thus a socket can be
|
|
516 |
* bound to the loopback address and the connect will
|
|
517 |
* fail with EADDRNOTAVAIL. In addition the Linux kernel
|
|
518 |
* returns the wrong error in this case - it returns EINVAL
|
|
519 |
* instead of EADDRNOTAVAIL. We handle this here so that
|
|
520 |
* a more descriptive exception text is used.
|
|
521 |
*/
|
|
522 |
if (connect_rv == JVM_IO_ERR && errno == EINVAL) {
|
|
523 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
524 |
"Invalid argument or cannot assign requested address");
|
|
525 |
return;
|
|
526 |
}
|
|
527 |
#endif
|
|
528 |
if (connect_rv == JVM_IO_INTR) {
|
|
529 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
|
|
530 |
"operation interrupted");
|
|
531 |
} else if (errno == EPROTO) {
|
|
532 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ProtocolException",
|
|
533 |
"Protocol error");
|
|
534 |
} else if (errno == ECONNREFUSED) {
|
|
535 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
|
|
536 |
"Connection refused");
|
|
537 |
} else if (errno == ETIMEDOUT) {
|
|
538 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
|
|
539 |
"Connection timed out");
|
|
540 |
} else if (errno == EHOSTUNREACH) {
|
|
541 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
|
|
542 |
"Host unreachable");
|
|
543 |
} else if (errno == EADDRNOTAVAIL) {
|
|
544 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException",
|
|
545 |
"Address not available");
|
|
546 |
} else if ((errno == EISCONN) || (errno == EBADF)) {
|
|
547 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
548 |
"Socket closed");
|
|
549 |
} else {
|
|
550 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "connect failed");
|
|
551 |
}
|
|
552 |
return;
|
|
553 |
}
|
|
554 |
|
|
555 |
/*
|
|
556 |
* The socket may have been closed (dup'ed) while we were
|
|
557 |
* poll/select. In that case SO_ERROR will return 0 making
|
|
558 |
* it appear that the connection has been established.
|
|
559 |
* To avoid any race conditions we therefore grab the
|
|
560 |
* fd lock, check if the socket has been closed, and
|
|
561 |
* set the various fields whilst holding the lock
|
|
562 |
*/
|
|
563 |
fdLock = (*env)->GetObjectField(env, this, psi_fdLockID);
|
|
564 |
(*env)->MonitorEnter(env, fdLock);
|
|
565 |
|
|
566 |
if ((*env)->GetBooleanField(env, this, psi_closePendingID)) {
|
|
567 |
|
|
568 |
/* release fdLock */
|
|
569 |
(*env)->MonitorExit(env, fdLock);
|
|
570 |
|
|
571 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
572 |
"Socket closed");
|
|
573 |
return;
|
|
574 |
}
|
|
575 |
|
|
576 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
|
|
577 |
|
|
578 |
/* set the remote peer address and port */
|
|
579 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj);
|
|
580 |
(*env)->SetIntField(env, this, psi_portID, port);
|
|
581 |
|
|
582 |
/*
|
|
583 |
* we need to initialize the local port field if bind was called
|
|
584 |
* previously to the connect (by the client) then localport field
|
|
585 |
* will already be initialized
|
|
586 |
*/
|
|
587 |
if (localport == 0) {
|
|
588 |
/* Now that we're a connected socket, let's extract the port number
|
|
589 |
* that the system chose for us and store it in the Socket object.
|
|
590 |
*/
|
|
591 |
len = SOCKADDR_LEN;
|
|
592 |
if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
|
|
593 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
594 |
"Error getting socket name");
|
|
595 |
} else {
|
|
596 |
localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
|
|
597 |
(*env)->SetIntField(env, this, psi_localportID, localport);
|
|
598 |
}
|
|
599 |
}
|
|
600 |
|
|
601 |
/*
|
|
602 |
* Finally release fdLock
|
|
603 |
*/
|
|
604 |
(*env)->MonitorExit(env, fdLock);
|
|
605 |
}
|
|
606 |
|
|
607 |
/*
|
|
608 |
* Class: java_net_PlainSocketImpl
|
|
609 |
* Method: socketBind
|
|
610 |
* Signature: (Ljava/net/InetAddress;I)V
|
|
611 |
*/
|
|
612 |
JNIEXPORT void JNICALL
|
|
613 |
Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
|
|
614 |
jobject iaObj, jint localport) {
|
|
615 |
|
|
616 |
/* fdObj is the FileDescriptor field on this */
|
|
617 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
618 |
/* fd is an int field on fdObj */
|
|
619 |
int fd;
|
|
620 |
int len;
|
|
621 |
SOCKADDR him;
|
|
622 |
|
|
623 |
if (IS_NULL(fdObj)) {
|
|
624 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
625 |
"Socket closed");
|
|
626 |
return;
|
|
627 |
} else {
|
|
628 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
629 |
}
|
|
630 |
if (IS_NULL(iaObj)) {
|
|
631 |
JNU_ThrowNullPointerException(env, "iaObj is null.");
|
|
632 |
return;
|
|
633 |
}
|
|
634 |
|
|
635 |
/* bind */
|
|
636 |
if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
|
|
637 |
return;
|
|
638 |
}
|
|
639 |
|
|
640 |
if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) {
|
|
641 |
if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
|
|
642 |
errno == EPERM || errno == EACCES) {
|
|
643 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
|
|
644 |
"Bind failed");
|
|
645 |
} else {
|
|
646 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
647 |
"Bind failed");
|
|
648 |
}
|
|
649 |
return;
|
|
650 |
}
|
|
651 |
|
|
652 |
/* set the address */
|
|
653 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj);
|
|
654 |
|
|
655 |
/* intialize the local port */
|
|
656 |
if (localport == 0) {
|
|
657 |
/* Now that we're a connected socket, let's extract the port number
|
|
658 |
* that the system chose for us and store it in the Socket object.
|
|
659 |
*/
|
|
660 |
if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
|
|
661 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
662 |
"Error getting socket name");
|
|
663 |
return;
|
|
664 |
}
|
|
665 |
localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
|
|
666 |
(*env)->SetIntField(env, this, psi_localportID, localport);
|
|
667 |
} else {
|
|
668 |
(*env)->SetIntField(env, this, psi_localportID, localport);
|
|
669 |
}
|
|
670 |
}
|
|
671 |
|
|
672 |
/*
|
|
673 |
* Class: java_net_PlainSocketImpl
|
|
674 |
* Method: socketListen
|
|
675 |
* Signature: (I)V
|
|
676 |
*/
|
|
677 |
JNIEXPORT void JNICALL
|
|
678 |
Java_java_net_PlainSocketImpl_socketListen (JNIEnv *env, jobject this,
|
|
679 |
jint count)
|
|
680 |
{
|
|
681 |
/* this FileDescriptor fd field */
|
|
682 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
683 |
/* fdObj's int fd field */
|
|
684 |
int fd;
|
|
685 |
|
|
686 |
if (IS_NULL(fdObj)) {
|
|
687 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
688 |
"Socket closed");
|
|
689 |
return;
|
|
690 |
} else {
|
|
691 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
692 |
}
|
|
693 |
|
|
694 |
/*
|
|
695 |
* Workaround for bugid 4101691 in Solaris 2.6. See 4106600.
|
|
696 |
* If listen backlog is Integer.MAX_VALUE then subtract 1.
|
|
697 |
*/
|
|
698 |
if (count == 0x7fffffff)
|
|
699 |
count -= 1;
|
|
700 |
|
|
701 |
if (JVM_Listen(fd, count) == JVM_IO_ERR) {
|
|
702 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
703 |
"Listen failed");
|
|
704 |
}
|
|
705 |
}
|
|
706 |
|
|
707 |
/*
|
|
708 |
* Class: java_net_PlainSocketImpl
|
|
709 |
* Method: socketAccept
|
|
710 |
* Signature: (Ljava/net/SocketImpl;)V
|
|
711 |
*/
|
|
712 |
JNIEXPORT void JNICALL
|
|
713 |
Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
|
|
714 |
jobject socket)
|
|
715 |
{
|
|
716 |
/* fields on this */
|
|
717 |
int port;
|
|
718 |
jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
|
|
719 |
jlong prevTime = 0;
|
|
720 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
721 |
|
|
722 |
/* the FileDescriptor field on socket */
|
|
723 |
jobject socketFdObj;
|
|
724 |
/* the InetAddress field on socket */
|
|
725 |
jobject socketAddressObj;
|
|
726 |
|
|
727 |
/* the ServerSocket fd int field on fdObj */
|
|
728 |
jint fd;
|
|
729 |
|
|
730 |
/* accepted fd */
|
|
731 |
jint newfd;
|
|
732 |
|
|
733 |
jthrowable error;
|
|
734 |
|
|
735 |
SOCKADDR him;
|
|
736 |
int len;
|
|
737 |
|
|
738 |
len = SOCKADDR_LEN;
|
|
739 |
|
|
740 |
if (IS_NULL(fdObj)) {
|
|
741 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
742 |
"Socket closed");
|
|
743 |
return;
|
|
744 |
} else {
|
|
745 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
746 |
}
|
|
747 |
if (IS_NULL(socket)) {
|
|
748 |
JNU_ThrowNullPointerException(env, "socket is null");
|
|
749 |
return;
|
|
750 |
}
|
|
751 |
|
|
752 |
/*
|
|
753 |
* accept connection but ignore ECONNABORTED indicating that
|
|
754 |
* connection was eagerly accepted by the OS but was reset
|
|
755 |
* before accept() was called.
|
|
756 |
*
|
|
757 |
* If accept timeout in place and timeout is adjusted with
|
|
758 |
* each ECONNABORTED or EWOULDBLOCK to ensure that semantics
|
|
759 |
* of timeout are preserved.
|
|
760 |
*/
|
|
761 |
for (;;) {
|
|
762 |
int ret;
|
|
763 |
|
|
764 |
/* first usage pick up current time */
|
|
765 |
if (prevTime == 0 && timeout > 0) {
|
|
766 |
prevTime = JVM_CurrentTimeMillis(env, 0);
|
|
767 |
}
|
|
768 |
|
|
769 |
/* passing a timeout of 0 to poll will return immediately,
|
|
770 |
but in the case of ServerSocket 0 means infinite. */
|
|
771 |
if (timeout <= 0) {
|
|
772 |
ret = NET_Timeout(fd, -1);
|
|
773 |
} else {
|
|
774 |
ret = NET_Timeout(fd, timeout);
|
|
775 |
}
|
|
776 |
|
|
777 |
if (ret == 0) {
|
|
778 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
|
|
779 |
"Accept timed out");
|
|
780 |
return;
|
|
781 |
} else if (ret == JVM_IO_ERR) {
|
|
782 |
if (errno == EBADF) {
|
|
783 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
|
|
784 |
} else {
|
|
785 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
|
|
786 |
}
|
|
787 |
return;
|
|
788 |
} else if (ret == JVM_IO_INTR) {
|
|
789 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
|
|
790 |
"operation interrupted");
|
|
791 |
return;
|
|
792 |
}
|
|
793 |
|
|
794 |
newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
|
|
795 |
|
|
796 |
/* connection accepted */
|
|
797 |
if (newfd >= 0) {
|
|
798 |
SET_BLOCKING(newfd);
|
|
799 |
break;
|
|
800 |
}
|
|
801 |
|
|
802 |
/* non (ECONNABORTED or EWOULDBLOCK) error */
|
|
803 |
if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
|
|
804 |
break;
|
|
805 |
}
|
|
806 |
|
|
807 |
/* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */
|
|
808 |
if (timeout) {
|
|
809 |
jlong currTime = JVM_CurrentTimeMillis(env, 0);
|
|
810 |
timeout -= (currTime - prevTime);
|
|
811 |
|
|
812 |
if (timeout <= 0) {
|
|
813 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
|
|
814 |
"Accept timed out");
|
|
815 |
return;
|
|
816 |
}
|
|
817 |
prevTime = currTime;
|
|
818 |
}
|
|
819 |
}
|
|
820 |
|
|
821 |
if (newfd < 0) {
|
|
822 |
if (newfd == -2) {
|
|
823 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
|
|
824 |
"operation interrupted");
|
|
825 |
} else {
|
|
826 |
if (errno == EINVAL) {
|
|
827 |
errno = EBADF;
|
|
828 |
}
|
|
829 |
if (errno == EBADF) {
|
|
830 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
|
|
831 |
} else {
|
|
832 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
|
|
833 |
}
|
|
834 |
}
|
|
835 |
return;
|
|
836 |
}
|
|
837 |
|
|
838 |
/*
|
|
839 |
* fill up the remote peer port and address in the new socket structure.
|
|
840 |
*/
|
|
841 |
socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
|
|
842 |
if (socketAddressObj == NULL) {
|
|
843 |
/* should be pending exception */
|
|
844 |
close(newfd);
|
|
845 |
return;
|
|
846 |
}
|
|
847 |
|
|
848 |
/*
|
|
849 |
* Populate SocketImpl.fd.fd
|
|
850 |
*/
|
|
851 |
socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
|
|
852 |
(*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd);
|
|
853 |
|
|
854 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
|
|
855 |
(*env)->SetIntField(env, socket, psi_portID, port);
|
|
856 |
/* also fill up the local port information */
|
|
857 |
port = (*env)->GetIntField(env, this, psi_localportID);
|
|
858 |
(*env)->SetIntField(env, socket, psi_localportID, port);
|
|
859 |
}
|
|
860 |
|
|
861 |
|
|
862 |
/*
|
|
863 |
* Class: java_net_PlainSocketImpl
|
|
864 |
* Method: socketAvailable
|
|
865 |
* Signature: ()I
|
|
866 |
*/
|
|
867 |
JNIEXPORT jint JNICALL
|
|
868 |
Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
|
|
869 |
|
|
870 |
jint ret = -1;
|
|
871 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
872 |
jint fd;
|
|
873 |
|
|
874 |
if (IS_NULL(fdObj)) {
|
|
875 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
876 |
"Socket closed");
|
|
877 |
return -1;
|
|
878 |
} else {
|
|
879 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
880 |
}
|
|
881 |
/* JVM_SocketAvailable returns 0 for failure, 1 for success */
|
|
882 |
if (!JVM_SocketAvailable(fd, &ret)){
|
|
883 |
if (errno == ECONNRESET) {
|
|
884 |
JNU_ThrowByName(env, "sun/net/ConnectionResetException", "");
|
|
885 |
} else {
|
|
886 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
887 |
"ioctl FIONREAD failed");
|
|
888 |
}
|
|
889 |
}
|
|
890 |
return ret;
|
|
891 |
}
|
|
892 |
|
|
893 |
/*
|
|
894 |
* Class: java_net_PlainSocketImpl
|
|
895 |
* Method: socketClose0
|
|
896 |
* Signature: (Z)V
|
|
897 |
*/
|
|
898 |
JNIEXPORT void JNICALL
|
|
899 |
Java_java_net_PlainSocketImpl_socketClose0(JNIEnv *env, jobject this,
|
|
900 |
jboolean useDeferredClose) {
|
|
901 |
|
|
902 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
903 |
jint fd;
|
|
904 |
|
|
905 |
if (IS_NULL(fdObj)) {
|
|
906 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
907 |
"socket already closed");
|
|
908 |
return;
|
|
909 |
} else {
|
|
910 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
911 |
}
|
|
912 |
if (fd != -1) {
|
|
913 |
if (useDeferredClose && marker_fd >= 0) {
|
|
914 |
NET_Dup2(marker_fd, fd);
|
|
915 |
} else {
|
|
916 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
|
|
917 |
NET_SocketClose(fd);
|
|
918 |
}
|
|
919 |
}
|
|
920 |
}
|
|
921 |
|
|
922 |
/*
|
|
923 |
* Class: java_net_PlainSocketImpl
|
|
924 |
* Method: socketShutdown
|
|
925 |
* Signature: (I)V
|
|
926 |
*/
|
|
927 |
JNIEXPORT void JNICALL
|
|
928 |
Java_java_net_PlainSocketImpl_socketShutdown(JNIEnv *env, jobject this,
|
|
929 |
jint howto)
|
|
930 |
{
|
|
931 |
|
|
932 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
933 |
jint fd;
|
|
934 |
|
|
935 |
/*
|
|
936 |
* WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being
|
|
937 |
* -1 already?
|
|
938 |
*/
|
|
939 |
if (IS_NULL(fdObj)) {
|
|
940 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
941 |
"socket already closed");
|
|
942 |
return;
|
|
943 |
} else {
|
|
944 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
945 |
}
|
|
946 |
JVM_SocketShutdown(fd, howto);
|
|
947 |
}
|
|
948 |
|
|
949 |
|
|
950 |
/*
|
|
951 |
* Class: java_net_PlainSocketImpl
|
|
952 |
* Method: socketSetOption
|
|
953 |
* Signature: (IZLjava/lang/Object;)V
|
|
954 |
*/
|
|
955 |
JNIEXPORT void JNICALL
|
|
956 |
Java_java_net_PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
|
|
957 |
jint cmd, jboolean on,
|
|
958 |
jobject value) {
|
|
959 |
int fd;
|
|
960 |
int level, optname, optlen;
|
|
961 |
union {
|
|
962 |
int i;
|
|
963 |
struct linger ling;
|
|
964 |
} optval;
|
|
965 |
|
|
966 |
/*
|
|
967 |
* Check that socket hasn't been closed
|
|
968 |
*/
|
|
969 |
fd = getFD(env, this);
|
|
970 |
if (fd < 0) {
|
|
971 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
972 |
"Socket closed");
|
|
973 |
return;
|
|
974 |
}
|
|
975 |
|
|
976 |
/*
|
|
977 |
* SO_TIMEOUT is a no-op on Solaris/Linux
|
|
978 |
*/
|
|
979 |
if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
|
|
980 |
return;
|
|
981 |
}
|
|
982 |
|
|
983 |
/*
|
|
984 |
* Map the Java level socket option to the platform specific
|
|
985 |
* level and option name.
|
|
986 |
*/
|
|
987 |
if (NET_MapSocketOption(cmd, &level, &optname)) {
|
|
988 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
|
|
989 |
return;
|
|
990 |
}
|
|
991 |
|
|
992 |
switch (cmd) {
|
|
993 |
case java_net_SocketOptions_SO_SNDBUF :
|
|
994 |
case java_net_SocketOptions_SO_RCVBUF :
|
|
995 |
case java_net_SocketOptions_SO_LINGER :
|
|
996 |
case java_net_SocketOptions_IP_TOS :
|
|
997 |
{
|
|
998 |
jclass cls;
|
|
999 |
jfieldID fid;
|
|
1000 |
|
|
1001 |
cls = (*env)->FindClass(env, "java/lang/Integer");
|
|
1002 |
CHECK_NULL(cls);
|
|
1003 |
fid = (*env)->GetFieldID(env, cls, "value", "I");
|
|
1004 |
CHECK_NULL(fid);
|
|
1005 |
|
|
1006 |
if (cmd == java_net_SocketOptions_SO_LINGER) {
|
|
1007 |
if (on) {
|
|
1008 |
optval.ling.l_onoff = 1;
|
|
1009 |
optval.ling.l_linger = (*env)->GetIntField(env, value, fid);
|
|
1010 |
} else {
|
|
1011 |
optval.ling.l_onoff = 0;
|
|
1012 |
optval.ling.l_linger = 0;
|
|
1013 |
}
|
|
1014 |
optlen = sizeof(optval.ling);
|
|
1015 |
} else {
|
|
1016 |
optval.i = (*env)->GetIntField(env, value, fid);
|
|
1017 |
optlen = sizeof(optval.i);
|
|
1018 |
}
|
|
1019 |
|
|
1020 |
break;
|
|
1021 |
}
|
|
1022 |
|
|
1023 |
/* Boolean -> int */
|
|
1024 |
default :
|
|
1025 |
optval.i = (on ? 1 : 0);
|
|
1026 |
optlen = sizeof(optval.i);
|
|
1027 |
|
|
1028 |
}
|
|
1029 |
|
|
1030 |
if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
|
|
1031 |
#ifdef __solaris__
|
|
1032 |
if (errno == EINVAL) {
|
|
1033 |
// On Solaris setsockopt will set errno to EINVAL if the socket
|
|
1034 |
// is closed. The default error message is then confusing
|
|
1035 |
char fullMsg[128];
|
|
1036 |
jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer");
|
|
1037 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
|
|
1038 |
return;
|
|
1039 |
}
|
|
1040 |
#endif /* __solaris__ */
|
|
1041 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
1042 |
"Error setting socket option");
|
|
1043 |
}
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
/*
|
|
1047 |
* Class: java_net_PlainSocketImpl
|
|
1048 |
* Method: socketGetOption
|
|
1049 |
* Signature: (I)I
|
|
1050 |
*/
|
|
1051 |
JNIEXPORT jint JNICALL
|
|
1052 |
Java_java_net_PlainSocketImpl_socketGetOption(JNIEnv *env, jobject this,
|
|
1053 |
jint cmd, jobject iaContainerObj) {
|
|
1054 |
|
|
1055 |
int fd;
|
|
1056 |
int level, optname, optlen;
|
|
1057 |
union {
|
|
1058 |
int i;
|
|
1059 |
struct linger ling;
|
|
1060 |
} optval;
|
|
1061 |
|
|
1062 |
/*
|
|
1063 |
* Check that socket hasn't been closed
|
|
1064 |
*/
|
|
1065 |
fd = getFD(env, this);
|
|
1066 |
if (fd < 0) {
|
|
1067 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
1068 |
"Socket closed");
|
|
1069 |
return -1;
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
/*
|
|
1073 |
* SO_BINDADDR isn't a socket option
|
|
1074 |
*/
|
|
1075 |
if (cmd == java_net_SocketOptions_SO_BINDADDR) {
|
|
1076 |
SOCKADDR him;
|
|
1077 |
int len = 0;
|
|
1078 |
int port;
|
|
1079 |
jobject iaObj;
|
|
1080 |
jclass iaCntrClass;
|
|
1081 |
jfieldID iaFieldID;
|
|
1082 |
|
|
1083 |
len = SOCKADDR_LEN;
|
|
1084 |
|
|
1085 |
if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
|
|
1086 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
1087 |
"Error getting socket name");
|
|
1088 |
return -1;
|
|
1089 |
}
|
|
1090 |
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
|
|
1091 |
CHECK_NULL_RETURN(iaObj, -1);
|
|
1092 |
|
|
1093 |
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);
|
|
1094 |
iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;");
|
|
1095 |
CHECK_NULL_RETURN(iaFieldID, -1);
|
|
1096 |
(*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj);
|
|
1097 |
return 0; /* notice change from before */
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
/*
|
|
1101 |
* Map the Java level socket option to the platform specific
|
|
1102 |
* level and option name.
|
|
1103 |
*/
|
|
1104 |
if (NET_MapSocketOption(cmd, &level, &optname)) {
|
|
1105 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
|
|
1106 |
return -1;
|
|
1107 |
}
|
|
1108 |
|
|
1109 |
/*
|
|
1110 |
* Args are int except for SO_LINGER
|
|
1111 |
*/
|
|
1112 |
if (cmd == java_net_SocketOptions_SO_LINGER) {
|
|
1113 |
optlen = sizeof(optval.ling);
|
|
1114 |
} else {
|
|
1115 |
optlen = sizeof(optval.i);
|
|
1116 |
}
|
|
1117 |
|
|
1118 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
|
|
1119 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
|
|
1120 |
"Error getting socket option");
|
|
1121 |
return -1;
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
switch (cmd) {
|
|
1125 |
case java_net_SocketOptions_SO_LINGER:
|
|
1126 |
return (optval.ling.l_onoff ? optval.ling.l_linger: -1);
|
|
1127 |
|
|
1128 |
case java_net_SocketOptions_SO_SNDBUF:
|
|
1129 |
case java_net_SocketOptions_SO_RCVBUF:
|
|
1130 |
case java_net_SocketOptions_IP_TOS:
|
|
1131 |
return optval.i;
|
|
1132 |
|
|
1133 |
default :
|
|
1134 |
return (optval.i == 0) ? -1 : 1;
|
|
1135 |
}
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
|
|
1139 |
/*
|
|
1140 |
* Class: java_net_PlainSocketImpl
|
|
1141 |
* Method: socketSendUrgentData
|
|
1142 |
* Signature: (B)V
|
|
1143 |
*/
|
|
1144 |
JNIEXPORT void JNICALL
|
|
1145 |
Java_java_net_PlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this,
|
|
1146 |
jint data) {
|
|
1147 |
char *buf;
|
|
1148 |
/* The fd field */
|
|
1149 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
|
|
1150 |
int n, fd;
|
|
1151 |
unsigned char d = data & 0xFF;
|
|
1152 |
|
|
1153 |
if (IS_NULL(fdObj)) {
|
|
1154 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
|
|
1155 |
return;
|
|
1156 |
} else {
|
|
1157 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
1158 |
/* Bug 4086704 - If the Socket associated with this file descriptor
|
|
1159 |
* was closed (sysCloseFD), the the file descriptor is set to -1.
|
|
1160 |
*/
|
|
1161 |
if (fd == -1) {
|
|
1162 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
|
|
1163 |
return;
|
|
1164 |
}
|
|
1165 |
|
|
1166 |
}
|
|
1167 |
n = JVM_Send(fd, (char *)&d, 1, MSG_OOB);
|
|
1168 |
if (n == JVM_IO_ERR) {
|
|
1169 |
NET_ThrowByNameWithLastError(env, "java/io/IOException", "Write failed");
|
|
1170 |
return;
|
|
1171 |
}
|
|
1172 |
if (n == JVM_IO_INTR) {
|
|
1173 |
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
|
|
1174 |
return;
|
|
1175 |
}
|
|
1176 |
}
|