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