author | vtewari |
Tue, 02 May 2017 16:39:29 +0530 | |
changeset 44916 | 54d70322b32c |
parent 43100 | a7e3457672c7 |
child 44921 | 0672237e13c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
2 |
* Copyright (c) 1997, 2017, 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 |
*/ |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
25 |
#include <errno.h> |
2 | 26 |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
27 |
#include "jvm.h" |
2 | 28 |
#include "net_util.h" |
29 |
||
30 |
#include "java_net_SocketOptions.h" |
|
31 |
#include "java_net_PlainSocketImpl.h" |
|
32 |
||
33 |
/************************************************************************ |
|
34 |
* PlainSocketImpl |
|
35 |
*/ |
|
36 |
||
37 |
static jfieldID IO_fd_fdID; |
|
38 |
||
39 |
jfieldID psi_fdID; |
|
40 |
jfieldID psi_addressID; |
|
41 |
jfieldID psi_ipaddressID; |
|
42 |
jfieldID psi_portID; |
|
43 |
jfieldID psi_localportID; |
|
44 |
jfieldID psi_timeoutID; |
|
45 |
jfieldID psi_trafficClassID; |
|
46 |
jfieldID psi_serverSocketID; |
|
47 |
jfieldID psi_fdLockID; |
|
48 |
jfieldID psi_closePendingID; |
|
49 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
50 |
extern void setDefaultScopeID(JNIEnv *env, struct sockaddr *him); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
51 |
|
2 | 52 |
/* |
53 |
* file descriptor used for dup2 |
|
54 |
*/ |
|
55 |
static int marker_fd = -1; |
|
56 |
||
57 |
||
58 |
#define SET_NONBLOCKING(fd) { \ |
|
59 |
int flags = fcntl(fd, F_GETFL); \ |
|
60 |
flags |= O_NONBLOCK; \ |
|
61 |
fcntl(fd, F_SETFL, flags); \ |
|
62 |
} |
|
63 |
||
64 |
#define SET_BLOCKING(fd) { \ |
|
65 |
int flags = fcntl(fd, F_GETFL); \ |
|
66 |
flags &= ~O_NONBLOCK; \ |
|
67 |
fcntl(fd, F_SETFL, flags); \ |
|
68 |
} |
|
69 |
||
70 |
/* |
|
71 |
* Create the marker file descriptor by establishing a loopback connection |
|
72 |
* which we shutdown but do not close the fd. The result is an fd that |
|
73 |
* can be used for read/write. |
|
74 |
*/ |
|
75 |
static int getMarkerFD() |
|
76 |
{ |
|
710
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
77 |
int sv[2]; |
2 | 78 |
|
710
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
79 |
#ifdef AF_UNIX |
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
80 |
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
|
81 |
return -1; |
2 | 82 |
} |
710
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
83 |
#else |
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
84 |
return -1; |
2 | 85 |
#endif |
86 |
||
87 |
/* |
|
710
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
88 |
* Finally shutdown sv[0] (any reads to this fd will get |
2 | 89 |
* EOF; any writes will get an error). |
90 |
*/ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
91 |
shutdown(sv[0], 2); |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
92 |
close(sv[1]); |
2 | 93 |
|
710
57367409c4a5
6483406: new ServerSocket() sometimes takes more than 3 minutes on Suse Linux
jccollet
parents:
624
diff
changeset
|
94 |
return sv[0]; |
2 | 95 |
} |
96 |
||
97 |
/* |
|
98 |
* Return the file descriptor given a PlainSocketImpl |
|
99 |
*/ |
|
100 |
static int getFD(JNIEnv *env, jobject this) { |
|
101 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
102 |
CHECK_NULL_RETURN(fdObj, -1); |
|
103 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
104 |
} |
|
105 |
||
106 |
/* |
|
107 |
* The initroto function is called whenever PlainSocketImpl is |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
12047
diff
changeset
|
108 |
* loaded, to cache field IDs for efficiency. This is called every time |
2 | 109 |
* the Java class is loaded. |
110 |
* |
|
111 |
* Class: java_net_PlainSocketImpl |
|
112 |
* Method: initProto |
|
113 |
* Signature: ()V |
|
114 |
*/ |
|
115 |
JNIEXPORT void JNICALL |
|
116 |
Java_java_net_PlainSocketImpl_initProto(JNIEnv *env, jclass cls) { |
|
117 |
psi_fdID = (*env)->GetFieldID(env, cls , "fd", |
|
118 |
"Ljava/io/FileDescriptor;"); |
|
119 |
CHECK_NULL(psi_fdID); |
|
120 |
psi_addressID = (*env)->GetFieldID(env, cls, "address", |
|
121 |
"Ljava/net/InetAddress;"); |
|
122 |
CHECK_NULL(psi_addressID); |
|
123 |
psi_portID = (*env)->GetFieldID(env, cls, "port", "I"); |
|
124 |
CHECK_NULL(psi_portID); |
|
125 |
psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I"); |
|
126 |
CHECK_NULL(psi_localportID); |
|
127 |
psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I"); |
|
128 |
CHECK_NULL(psi_timeoutID); |
|
129 |
psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I"); |
|
130 |
CHECK_NULL(psi_trafficClassID); |
|
131 |
psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket", |
|
132 |
"Ljava/net/ServerSocket;"); |
|
133 |
CHECK_NULL(psi_serverSocketID); |
|
134 |
psi_fdLockID = (*env)->GetFieldID(env, cls, "fdLock", |
|
135 |
"Ljava/lang/Object;"); |
|
136 |
CHECK_NULL(psi_fdLockID); |
|
137 |
psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z"); |
|
138 |
CHECK_NULL(psi_closePendingID); |
|
139 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
140 |
CHECK_NULL(IO_fd_fdID); |
|
141 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
142 |
initInetAddressIDs(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
143 |
JNU_CHECK_EXCEPTION(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
144 |
|
2 | 145 |
/* Create the marker fd used for dup2 */ |
146 |
marker_fd = getMarkerFD(); |
|
147 |
} |
|
148 |
||
149 |
/* a global reference to the java.net.SocketException class. In |
|
150 |
* socketCreate, we ensure that this is initialized. This is to |
|
151 |
* prevent the problem where socketCreate runs out of file |
|
152 |
* descriptors, and is then unable to load the exception class. |
|
153 |
*/ |
|
154 |
static jclass socketExceptionCls; |
|
155 |
||
156 |
/* |
|
157 |
* Class: java_net_PlainSocketImpl |
|
158 |
* Method: socketCreate |
|
159 |
* Signature: (Z)V */ |
|
160 |
JNIEXPORT void JNICALL |
|
161 |
Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this, |
|
162 |
jboolean stream) { |
|
163 |
jobject fdObj, ssObj; |
|
164 |
int fd; |
|
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
165 |
int type = (stream ? SOCK_STREAM : SOCK_DGRAM); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
166 |
int domain = ipv6_available() ? AF_INET6 : AF_INET; |
2 | 167 |
|
168 |
if (socketExceptionCls == NULL) { |
|
169 |
jclass c = (*env)->FindClass(env, "java/net/SocketException"); |
|
170 |
CHECK_NULL(c); |
|
171 |
socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c); |
|
172 |
CHECK_NULL(socketExceptionCls); |
|
173 |
} |
|
174 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
175 |
||
176 |
if (fdObj == NULL) { |
|
177 |
(*env)->ThrowNew(env, socketExceptionCls, "null fd object"); |
|
178 |
return; |
|
179 |
} |
|
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
180 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
181 |
if ((fd = socket(domain, type, 0)) == -1) { |
2 | 182 |
/* note: if you run out of fds, you may not be able to load |
183 |
* the exception class, and get a NoClassDefFoundError |
|
184 |
* instead. |
|
185 |
*/ |
|
186 |
NET_ThrowNew(env, errno, "can't create socket"); |
|
187 |
return; |
|
188 |
} |
|
189 |
||
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
190 |
/* Disable IPV6_V6ONLY to ensure dual-socket support */ |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
191 |
if (domain == AF_INET6) { |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
192 |
int arg = 0; |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
193 |
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg, |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
194 |
sizeof(int)) < 0) { |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
195 |
NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6"); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
196 |
close(fd); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
197 |
return; |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
198 |
} |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
199 |
} |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
200 |
|
2 | 201 |
/* |
202 |
* If this is a server socket then enable SO_REUSEADDR |
|
203 |
* automatically and set to non blocking. |
|
204 |
*/ |
|
205 |
ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID); |
|
206 |
if (ssObj != NULL) { |
|
207 |
int arg = 1; |
|
208 |
SET_NONBLOCKING(fd); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
209 |
if (NET_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
210 |
sizeof(arg)) < 0) { |
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
211 |
NET_ThrowNew(env, errno, "cannot set SO_REUSEADDR"); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
212 |
close(fd); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
213 |
return; |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
214 |
} |
2 | 215 |
} |
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
216 |
|
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
217 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
2 | 218 |
} |
219 |
||
220 |
/* |
|
221 |
* inetAddress is the address object passed to the socket connect |
|
222 |
* call. |
|
223 |
* |
|
224 |
* Class: java_net_PlainSocketImpl |
|
225 |
* Method: socketConnect |
|
226 |
* Signature: (Ljava/net/InetAddress;I)V |
|
227 |
*/ |
|
228 |
JNIEXPORT void JNICALL |
|
229 |
Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this, |
|
230 |
jobject iaObj, jint port, |
|
231 |
jint timeout) |
|
232 |
{ |
|
233 |
jint localport = (*env)->GetIntField(env, this, psi_localportID); |
|
234 |
int len = 0; |
|
235 |
/* fdObj is the FileDescriptor field on this */ |
|
236 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
237 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
238 |
jclass clazz = (*env)->GetObjectClass(env, this); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
239 |
|
2 | 240 |
jobject fdLock; |
241 |
||
242 |
jint trafficClass = (*env)->GetIntField(env, this, psi_trafficClassID); |
|
243 |
||
244 |
/* fd is an int field on iaObj */ |
|
245 |
jint fd; |
|
246 |
||
43100 | 247 |
SOCKETADDRESS sa; |
2 | 248 |
/* The result of the connection */ |
249 |
int connect_rv = -1; |
|
250 |
||
251 |
if (IS_NULL(fdObj)) { |
|
252 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
253 |
return; |
|
254 |
} else { |
|
255 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
256 |
} |
|
257 |
if (IS_NULL(iaObj)) { |
|
258 |
JNU_ThrowNullPointerException(env, "inet address argument null."); |
|
259 |
return; |
|
260 |
} |
|
261 |
||
262 |
/* connect */ |
|
43100 | 263 |
if (NET_InetAddressToSockaddr(env, iaObj, port, &sa, &len, |
264 |
JNI_TRUE) != 0) { |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
265 |
return; |
2 | 266 |
} |
43100 | 267 |
setDefaultScopeID(env, &sa.sa); |
2 | 268 |
|
269 |
if (trafficClass != 0 && ipv6_available()) { |
|
43100 | 270 |
NET_SetTrafficClass(&sa, trafficClass); |
2 | 271 |
} |
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
272 |
|
2 | 273 |
if (timeout <= 0) { |
43100 | 274 |
connect_rv = NET_Connect(fd, &sa.sa, len); |
2 | 275 |
#ifdef __solaris__ |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
276 |
if (connect_rv == -1 && errno == EINPROGRESS ) { |
2 | 277 |
|
278 |
/* This can happen if a blocking connect is interrupted by a signal. |
|
279 |
* See 6343810. |
|
280 |
*/ |
|
281 |
while (1) { |
|
23033
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
282 |
struct pollfd pfd; |
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
283 |
pfd.fd = fd; |
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
284 |
pfd.events = POLLOUT; |
2 | 285 |
|
23033
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
286 |
connect_rv = NET_Poll(&pfd, 1, -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
|
287 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
288 |
if (connect_rv == -1) { |
2 | 289 |
if (errno == EINTR) { |
290 |
continue; |
|
291 |
} else { |
|
292 |
break; |
|
293 |
} |
|
294 |
} |
|
295 |
if (connect_rv > 0) { |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
296 |
socklen_t optlen; |
2 | 297 |
/* has connection been established */ |
298 |
optlen = sizeof(connect_rv); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
299 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
300 |
(void*)&connect_rv, &optlen) <0) { |
2 | 301 |
connect_rv = errno; |
302 |
} |
|
303 |
||
304 |
if (connect_rv != 0) { |
|
305 |
/* restore errno */ |
|
306 |
errno = connect_rv; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
307 |
connect_rv = -1; |
2 | 308 |
} |
309 |
break; |
|
310 |
} |
|
311 |
} |
|
312 |
} |
|
313 |
#endif |
|
314 |
} else { |
|
315 |
/* |
|
316 |
* A timeout was specified. We put the socket into non-blocking |
|
317 |
* mode, connect, and then wait for the connection to be |
|
318 |
* established, fail, or timeout. |
|
319 |
*/ |
|
320 |
SET_NONBLOCKING(fd); |
|
321 |
||
322 |
/* no need to use NET_Connect as non-blocking */ |
|
43100 | 323 |
connect_rv = connect(fd, &sa.sa, len); |
2 | 324 |
|
325 |
/* connection not established immediately */ |
|
326 |
if (connect_rv != 0) { |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
327 |
socklen_t optlen; |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
328 |
jlong nanoTimeout = timeout * NET_NSEC_PER_MSEC; |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
329 |
jlong prevNanoTime = JVM_NanoTime(env, 0); |
2 | 330 |
if (errno != EINPROGRESS) { |
331 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
|
332 |
"connect failed"); |
|
333 |
SET_BLOCKING(fd); |
|
334 |
return; |
|
335 |
} |
|
336 |
||
337 |
/* |
|
338 |
* Wait for the connection to be established or a |
|
23033
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
339 |
* timeout occurs. poll needs to handle EINTR in |
2 | 340 |
* case lwp sig handler redirects any process signals to |
341 |
* this thread. |
|
342 |
*/ |
|
343 |
while (1) { |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
344 |
jlong newNanoTime; |
23033
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
345 |
struct pollfd pfd; |
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
346 |
pfd.fd = fd; |
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
347 |
pfd.events = POLLOUT; |
2 | 348 |
|
23033
0cc7c83fde3c
8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents:
23015
diff
changeset
|
349 |
errno = 0; |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
350 |
connect_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC); |
2 | 351 |
|
352 |
if (connect_rv >= 0) { |
|
353 |
break; |
|
354 |
} |
|
355 |
if (errno != EINTR) { |
|
356 |
break; |
|
357 |
} |
|
358 |
||
359 |
/* |
|
360 |
* The poll was interrupted so adjust timeout and |
|
361 |
* restart |
|
362 |
*/ |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
363 |
newNanoTime = JVM_NanoTime(env, 0); |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
364 |
nanoTimeout -= (newNanoTime - prevNanoTime); |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
365 |
if (nanoTimeout < NET_NSEC_PER_MSEC) { |
2 | 366 |
connect_rv = 0; |
367 |
break; |
|
368 |
} |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
369 |
prevNanoTime = newNanoTime; |
2 | 370 |
|
371 |
} /* while */ |
|
372 |
||
373 |
if (connect_rv == 0) { |
|
374 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
375 |
"connect timed out"); |
|
376 |
||
377 |
/* |
|
378 |
* Timeout out but connection may still be established. |
|
379 |
* At the high level it should be closed immediately but |
|
380 |
* just in case we make the socket blocking again and |
|
381 |
* shutdown input & output. |
|
382 |
*/ |
|
383 |
SET_BLOCKING(fd); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
384 |
shutdown(fd, 2); |
2 | 385 |
return; |
386 |
} |
|
387 |
||
388 |
/* has connection been established */ |
|
389 |
optlen = sizeof(connect_rv); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
390 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
391 |
&optlen) <0) { |
2 | 392 |
connect_rv = errno; |
393 |
} |
|
394 |
} |
|
395 |
||
396 |
/* make socket blocking again */ |
|
397 |
SET_BLOCKING(fd); |
|
398 |
||
399 |
/* restore errno */ |
|
400 |
if (connect_rv != 0) { |
|
401 |
errno = connect_rv; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
402 |
connect_rv = -1; |
2 | 403 |
} |
404 |
} |
|
405 |
||
406 |
/* report the appropriate exception */ |
|
407 |
if (connect_rv < 0) { |
|
408 |
||
409 |
#ifdef __linux__ |
|
410 |
/* |
|
411 |
* Linux/GNU distribution setup /etc/hosts so that |
|
412 |
* InetAddress.getLocalHost gets back the loopback address |
|
413 |
* rather than the host address. Thus a socket can be |
|
414 |
* bound to the loopback address and the connect will |
|
415 |
* fail with EADDRNOTAVAIL. In addition the Linux kernel |
|
416 |
* returns the wrong error in this case - it returns EINVAL |
|
417 |
* instead of EADDRNOTAVAIL. We handle this here so that |
|
418 |
* a more descriptive exception text is used. |
|
419 |
*/ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
420 |
if (connect_rv == -1 && errno == EINVAL) { |
2 | 421 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
422 |
"Invalid argument or cannot assign requested address"); |
|
423 |
return; |
|
424 |
} |
|
425 |
#endif |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
426 |
#if defined(EPROTO) |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
427 |
if (errno == EPROTO) { |
2 | 428 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ProtocolException", |
429 |
"Protocol error"); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
430 |
return; |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
431 |
} |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
432 |
#endif |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
433 |
if (errno == ECONNREFUSED) { |
2 | 434 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
435 |
"Connection refused"); |
|
436 |
} else if (errno == ETIMEDOUT) { |
|
437 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
|
438 |
"Connection timed out"); |
|
439 |
} else if (errno == EHOSTUNREACH) { |
|
440 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException", |
|
441 |
"Host unreachable"); |
|
442 |
} else if (errno == EADDRNOTAVAIL) { |
|
443 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "NoRouteToHostException", |
|
444 |
"Address not available"); |
|
445 |
} else if ((errno == EISCONN) || (errno == EBADF)) { |
|
446 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
447 |
"Socket closed"); |
|
448 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
449 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
450 |
(env, JNU_JAVANETPKG "SocketException", "connect failed"); |
2 | 451 |
} |
452 |
return; |
|
453 |
} |
|
454 |
||
455 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
|
456 |
||
457 |
/* set the remote peer address and port */ |
|
458 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
459 |
(*env)->SetIntField(env, this, psi_portID, port); |
|
460 |
||
461 |
/* |
|
462 |
* we need to initialize the local port field if bind was called |
|
463 |
* previously to the connect (by the client) then localport field |
|
464 |
* will already be initialized |
|
465 |
*/ |
|
466 |
if (localport == 0) { |
|
467 |
/* Now that we're a connected socket, let's extract the port number |
|
468 |
* that the system chose for us and store it in the Socket object. |
|
469 |
*/ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
470 |
socklen_t slen = sizeof(SOCKETADDRESS); |
43100 | 471 |
if (getsockname(fd, &sa.sa, &slen) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
472 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
473 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 474 |
} else { |
43100 | 475 |
localport = NET_GetPortFromSockaddr(&sa); |
2 | 476 |
(*env)->SetIntField(env, this, psi_localportID, localport); |
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
/* |
|
482 |
* Class: java_net_PlainSocketImpl |
|
483 |
* Method: socketBind |
|
484 |
* Signature: (Ljava/net/InetAddress;I)V |
|
485 |
*/ |
|
486 |
JNIEXPORT void JNICALL |
|
487 |
Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this, |
|
488 |
jobject iaObj, jint localport) { |
|
489 |
||
490 |
/* fdObj is the FileDescriptor field on this */ |
|
491 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
492 |
/* fd is an int field on fdObj */ |
|
493 |
int fd; |
|
43100 | 494 |
int len = 0; |
495 |
SOCKETADDRESS sa; |
|
2 | 496 |
|
497 |
if (IS_NULL(fdObj)) { |
|
498 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
499 |
"Socket closed"); |
|
500 |
return; |
|
501 |
} else { |
|
502 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
503 |
} |
|
504 |
if (IS_NULL(iaObj)) { |
|
505 |
JNU_ThrowNullPointerException(env, "iaObj is null."); |
|
506 |
return; |
|
507 |
} |
|
508 |
||
509 |
/* bind */ |
|
43100 | 510 |
if (NET_InetAddressToSockaddr(env, iaObj, localport, &sa, |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
511 |
&len, JNI_TRUE) != 0) { |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
512 |
return; |
2 | 513 |
} |
43100 | 514 |
setDefaultScopeID(env, &sa.sa); |
2 | 515 |
|
43100 | 516 |
if (NET_Bind(fd, &sa, len) < 0) { |
2 | 517 |
if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || |
518 |
errno == EPERM || errno == EACCES) { |
|
519 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", |
|
520 |
"Bind failed"); |
|
521 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
522 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
523 |
(env, JNU_JAVANETPKG "SocketException", "Bind failed"); |
2 | 524 |
} |
525 |
return; |
|
526 |
} |
|
527 |
||
528 |
/* set the address */ |
|
529 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
530 |
||
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
12047
diff
changeset
|
531 |
/* initialize the local port */ |
2 | 532 |
if (localport == 0) { |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
533 |
socklen_t slen = sizeof(SOCKETADDRESS); |
2 | 534 |
/* Now that we're a connected socket, let's extract the port number |
535 |
* that the system chose for us and store it in the Socket object. |
|
536 |
*/ |
|
43100 | 537 |
if (getsockname(fd, &sa.sa, &slen) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
538 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
539 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 540 |
return; |
541 |
} |
|
43100 | 542 |
localport = NET_GetPortFromSockaddr(&sa); |
2 | 543 |
(*env)->SetIntField(env, this, psi_localportID, localport); |
544 |
} else { |
|
545 |
(*env)->SetIntField(env, this, psi_localportID, localport); |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
/* |
|
550 |
* Class: java_net_PlainSocketImpl |
|
551 |
* Method: socketListen |
|
552 |
* Signature: (I)V |
|
553 |
*/ |
|
554 |
JNIEXPORT void JNICALL |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
555 |
Java_java_net_PlainSocketImpl_socketListen(JNIEnv *env, jobject this, |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
556 |
jint count) |
2 | 557 |
{ |
558 |
/* this FileDescriptor fd field */ |
|
559 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
560 |
/* fdObj's int fd field */ |
|
561 |
int fd; |
|
562 |
||
563 |
if (IS_NULL(fdObj)) { |
|
564 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
565 |
"Socket closed"); |
|
566 |
return; |
|
567 |
} else { |
|
568 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
569 |
} |
|
570 |
||
571 |
/* |
|
572 |
* Workaround for bugid 4101691 in Solaris 2.6. See 4106600. |
|
573 |
* If listen backlog is Integer.MAX_VALUE then subtract 1. |
|
574 |
*/ |
|
575 |
if (count == 0x7fffffff) |
|
576 |
count -= 1; |
|
577 |
||
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
578 |
if (listen(fd, count) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
579 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
580 |
(env, JNU_JAVANETPKG "SocketException", "Listen failed"); |
2 | 581 |
} |
582 |
} |
|
583 |
||
584 |
/* |
|
585 |
* Class: java_net_PlainSocketImpl |
|
586 |
* Method: socketAccept |
|
587 |
* Signature: (Ljava/net/SocketImpl;)V |
|
588 |
*/ |
|
589 |
JNIEXPORT void JNICALL |
|
590 |
Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this, |
|
591 |
jobject socket) |
|
592 |
{ |
|
593 |
/* fields on this */ |
|
594 |
int port; |
|
595 |
jint timeout = (*env)->GetIntField(env, this, psi_timeoutID); |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
596 |
jlong prevNanoTime = 0, nanoTimeout = timeout * NET_NSEC_PER_MSEC; |
2 | 597 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
598 |
||
599 |
/* the FileDescriptor field on socket */ |
|
600 |
jobject socketFdObj; |
|
601 |
/* the InetAddress field on socket */ |
|
602 |
jobject socketAddressObj; |
|
603 |
||
604 |
/* the ServerSocket fd int field on fdObj */ |
|
605 |
jint fd; |
|
606 |
||
607 |
/* accepted fd */ |
|
608 |
jint newfd; |
|
609 |
||
43100 | 610 |
SOCKETADDRESS sa; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
611 |
socklen_t slen = sizeof(SOCKETADDRESS); |
2 | 612 |
|
613 |
if (IS_NULL(fdObj)) { |
|
614 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
615 |
"Socket closed"); |
|
616 |
return; |
|
617 |
} else { |
|
618 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
619 |
} |
|
620 |
if (IS_NULL(socket)) { |
|
621 |
JNU_ThrowNullPointerException(env, "socket is null"); |
|
622 |
return; |
|
623 |
} |
|
624 |
||
625 |
/* |
|
626 |
* accept connection but ignore ECONNABORTED indicating that |
|
627 |
* connection was eagerly accepted by the OS but was reset |
|
628 |
* before accept() was called. |
|
629 |
* |
|
630 |
* If accept timeout in place and timeout is adjusted with |
|
631 |
* each ECONNABORTED or EWOULDBLOCK to ensure that semantics |
|
632 |
* of timeout are preserved. |
|
633 |
*/ |
|
634 |
for (;;) { |
|
635 |
int ret; |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
636 |
jlong currNanoTime; |
2 | 637 |
/* first usage pick up current time */ |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
638 |
if (prevNanoTime == 0 && nanoTimeout > 0) { |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
639 |
prevNanoTime = JVM_NanoTime(env, 0); |
2 | 640 |
} |
641 |
||
642 |
/* passing a timeout of 0 to poll will return immediately, |
|
643 |
but in the case of ServerSocket 0 means infinite. */ |
|
644 |
if (timeout <= 0) { |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
645 |
ret = NET_Timeout(env, fd, -1, 0); |
2 | 646 |
} else { |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
647 |
ret = NET_Timeout(env, fd, nanoTimeout / NET_NSEC_PER_MSEC, prevNanoTime); |
2 | 648 |
} |
649 |
if (ret == 0) { |
|
650 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
651 |
"Accept timed out"); |
|
652 |
return; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
653 |
} else if (ret == -1) { |
2 | 654 |
if (errno == EBADF) { |
655 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
23048 | 656 |
} else if (errno == ENOMEM) { |
657 |
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); |
|
2 | 658 |
} else { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
659 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
660 |
(env, JNU_JAVANETPKG "SocketException", "Accept failed"); |
2 | 661 |
} |
662 |
return; |
|
663 |
} |
|
664 |
||
43100 | 665 |
newfd = NET_Accept(fd, &sa.sa, &slen); |
2 | 666 |
|
667 |
/* connection accepted */ |
|
668 |
if (newfd >= 0) { |
|
669 |
SET_BLOCKING(newfd); |
|
670 |
break; |
|
671 |
} |
|
672 |
||
673 |
/* non (ECONNABORTED or EWOULDBLOCK) error */ |
|
674 |
if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) { |
|
675 |
break; |
|
676 |
} |
|
677 |
||
678 |
/* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */ |
|
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
679 |
currNanoTime = JVM_NanoTime(env, 0); |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
680 |
nanoTimeout -= (currNanoTime - prevNanoTime); |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
681 |
if (nanoTimeout < NET_NSEC_PER_MSEC) { |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
682 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
683 |
"Accept timed out"); |
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
684 |
return; |
2 | 685 |
} |
44916
54d70322b32c
8165437: Evaluate the use of gettimeofday in Networking code
vtewari
parents:
43100
diff
changeset
|
686 |
prevNanoTime = currNanoTime; |
2 | 687 |
} |
688 |
||
689 |
if (newfd < 0) { |
|
690 |
if (newfd == -2) { |
|
691 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
692 |
"operation interrupted"); |
|
693 |
} else { |
|
694 |
if (errno == EINVAL) { |
|
695 |
errno = EBADF; |
|
696 |
} |
|
697 |
if (errno == EBADF) { |
|
698 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
699 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
700 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
701 |
(env, JNU_JAVANETPKG "SocketException", "Accept failed"); |
2 | 702 |
} |
703 |
} |
|
704 |
return; |
|
705 |
} |
|
706 |
||
707 |
/* |
|
708 |
* fill up the remote peer port and address in the new socket structure. |
|
709 |
*/ |
|
43100 | 710 |
socketAddressObj = NET_SockaddrToInetAddress(env, &sa, &port); |
2 | 711 |
if (socketAddressObj == NULL) { |
712 |
/* should be pending exception */ |
|
713 |
close(newfd); |
|
714 |
return; |
|
715 |
} |
|
716 |
||
717 |
/* |
|
718 |
* Populate SocketImpl.fd.fd |
|
719 |
*/ |
|
720 |
socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID); |
|
721 |
(*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd); |
|
722 |
||
723 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); |
|
724 |
(*env)->SetIntField(env, socket, psi_portID, port); |
|
725 |
/* also fill up the local port information */ |
|
726 |
port = (*env)->GetIntField(env, this, psi_localportID); |
|
727 |
(*env)->SetIntField(env, socket, psi_localportID, port); |
|
728 |
} |
|
729 |
||
730 |
||
731 |
/* |
|
732 |
* Class: java_net_PlainSocketImpl |
|
733 |
* Method: socketAvailable |
|
734 |
* Signature: ()I |
|
735 |
*/ |
|
736 |
JNIEXPORT jint JNICALL |
|
737 |
Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) { |
|
738 |
||
739 |
jint ret = -1; |
|
740 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
741 |
jint fd; |
|
742 |
||
743 |
if (IS_NULL(fdObj)) { |
|
744 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
745 |
"Socket closed"); |
|
746 |
return -1; |
|
747 |
} else { |
|
748 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
749 |
} |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
750 |
/* NET_SocketAvailable returns 0 for failure, 1 for success */ |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
751 |
if (NET_SocketAvailable(fd, &ret) == 0){ |
2 | 752 |
if (errno == ECONNRESET) { |
753 |
JNU_ThrowByName(env, "sun/net/ConnectionResetException", ""); |
|
754 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
755 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
756 |
(env, JNU_JAVANETPKG "SocketException", "ioctl FIONREAD failed"); |
2 | 757 |
} |
758 |
} |
|
759 |
return ret; |
|
760 |
} |
|
761 |
||
762 |
/* |
|
763 |
* Class: java_net_PlainSocketImpl |
|
764 |
* Method: socketClose0 |
|
765 |
* Signature: (Z)V |
|
766 |
*/ |
|
767 |
JNIEXPORT void JNICALL |
|
768 |
Java_java_net_PlainSocketImpl_socketClose0(JNIEnv *env, jobject this, |
|
769 |
jboolean useDeferredClose) { |
|
770 |
||
771 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
772 |
jint fd; |
|
773 |
||
774 |
if (IS_NULL(fdObj)) { |
|
775 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
776 |
"socket already closed"); |
|
777 |
return; |
|
778 |
} else { |
|
779 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
780 |
} |
|
781 |
if (fd != -1) { |
|
782 |
if (useDeferredClose && marker_fd >= 0) { |
|
783 |
NET_Dup2(marker_fd, fd); |
|
784 |
} else { |
|
785 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
786 |
NET_SocketClose(fd); |
|
787 |
} |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
/* |
|
792 |
* Class: java_net_PlainSocketImpl |
|
793 |
* Method: socketShutdown |
|
794 |
* Signature: (I)V |
|
795 |
*/ |
|
796 |
JNIEXPORT void JNICALL |
|
797 |
Java_java_net_PlainSocketImpl_socketShutdown(JNIEnv *env, jobject this, |
|
798 |
jint howto) |
|
799 |
{ |
|
800 |
||
801 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
802 |
jint fd; |
|
803 |
||
804 |
/* |
|
805 |
* WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being |
|
806 |
* -1 already? |
|
807 |
*/ |
|
808 |
if (IS_NULL(fdObj)) { |
|
809 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
810 |
"socket already closed"); |
|
811 |
return; |
|
812 |
} else { |
|
813 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
814 |
} |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
815 |
shutdown(fd, howto); |
2 | 816 |
} |
817 |
||
818 |
||
819 |
/* |
|
820 |
* Class: java_net_PlainSocketImpl |
|
30963
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
28059
diff
changeset
|
821 |
* Method: socketSetOption0 |
2 | 822 |
* Signature: (IZLjava/lang/Object;)V |
823 |
*/ |
|
824 |
JNIEXPORT void JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
825 |
Java_java_net_PlainSocketImpl_socketSetOption0 |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
826 |
(JNIEnv *env, jobject this, jint cmd, jboolean on, jobject value) |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
827 |
{ |
2 | 828 |
int fd; |
829 |
int level, optname, optlen; |
|
830 |
union { |
|
831 |
int i; |
|
832 |
struct linger ling; |
|
833 |
} optval; |
|
834 |
||
835 |
/* |
|
836 |
* Check that socket hasn't been closed |
|
837 |
*/ |
|
838 |
fd = getFD(env, this); |
|
839 |
if (fd < 0) { |
|
840 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
841 |
"Socket closed"); |
|
842 |
return; |
|
843 |
} |
|
844 |
||
845 |
/* |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
12047
diff
changeset
|
846 |
* SO_TIMEOUT is a NOOP on Solaris/Linux |
2 | 847 |
*/ |
848 |
if (cmd == java_net_SocketOptions_SO_TIMEOUT) { |
|
849 |
return; |
|
850 |
} |
|
851 |
||
852 |
/* |
|
853 |
* Map the Java level socket option to the platform specific |
|
854 |
* level and option name. |
|
855 |
*/ |
|
856 |
if (NET_MapSocketOption(cmd, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
857 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 858 |
return; |
859 |
} |
|
860 |
||
861 |
switch (cmd) { |
|
862 |
case java_net_SocketOptions_SO_SNDBUF : |
|
863 |
case java_net_SocketOptions_SO_RCVBUF : |
|
864 |
case java_net_SocketOptions_SO_LINGER : |
|
865 |
case java_net_SocketOptions_IP_TOS : |
|
866 |
{ |
|
867 |
jclass cls; |
|
868 |
jfieldID fid; |
|
869 |
||
870 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
871 |
CHECK_NULL(cls); |
|
872 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
873 |
CHECK_NULL(fid); |
|
874 |
||
875 |
if (cmd == java_net_SocketOptions_SO_LINGER) { |
|
876 |
if (on) { |
|
877 |
optval.ling.l_onoff = 1; |
|
878 |
optval.ling.l_linger = (*env)->GetIntField(env, value, fid); |
|
879 |
} else { |
|
880 |
optval.ling.l_onoff = 0; |
|
881 |
optval.ling.l_linger = 0; |
|
882 |
} |
|
883 |
optlen = sizeof(optval.ling); |
|
884 |
} else { |
|
885 |
optval.i = (*env)->GetIntField(env, value, fid); |
|
886 |
optlen = sizeof(optval.i); |
|
887 |
} |
|
888 |
||
889 |
break; |
|
890 |
} |
|
891 |
||
892 |
/* Boolean -> int */ |
|
893 |
default : |
|
894 |
optval.i = (on ? 1 : 0); |
|
895 |
optlen = sizeof(optval.i); |
|
896 |
||
897 |
} |
|
898 |
||
899 |
if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) { |
|
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
14342
diff
changeset
|
900 |
#if defined(__solaris__) || defined(_AIX) |
2 | 901 |
if (errno == EINVAL) { |
902 |
// On Solaris setsockopt will set errno to EINVAL if the socket |
|
903 |
// is closed. The default error message is then confusing |
|
904 |
char fullMsg[128]; |
|
905 |
jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer"); |
|
906 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg); |
|
907 |
return; |
|
908 |
} |
|
909 |
#endif /* __solaris__ */ |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
910 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
911 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 912 |
} |
913 |
} |
|
914 |
||
915 |
/* |
|
916 |
* Class: java_net_PlainSocketImpl |
|
917 |
* Method: socketGetOption |
|
918 |
* Signature: (I)I |
|
919 |
*/ |
|
920 |
JNIEXPORT jint JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
921 |
Java_java_net_PlainSocketImpl_socketGetOption |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
922 |
(JNIEnv *env, jobject this, jint cmd, jobject iaContainerObj) |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
923 |
{ |
2 | 924 |
int fd; |
925 |
int level, optname, optlen; |
|
926 |
union { |
|
927 |
int i; |
|
928 |
struct linger ling; |
|
929 |
} optval; |
|
930 |
||
931 |
/* |
|
932 |
* Check that socket hasn't been closed |
|
933 |
*/ |
|
934 |
fd = getFD(env, this); |
|
935 |
if (fd < 0) { |
|
936 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
937 |
"Socket closed"); |
|
938 |
return -1; |
|
939 |
} |
|
940 |
||
941 |
/* |
|
942 |
* SO_BINDADDR isn't a socket option |
|
943 |
*/ |
|
944 |
if (cmd == java_net_SocketOptions_SO_BINDADDR) { |
|
43100 | 945 |
SOCKETADDRESS sa; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
946 |
socklen_t len = sizeof(SOCKETADDRESS); |
2 | 947 |
int port; |
948 |
jobject iaObj; |
|
949 |
jclass iaCntrClass; |
|
950 |
jfieldID iaFieldID; |
|
951 |
||
43100 | 952 |
if (getsockname(fd, &sa.sa, &len) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
953 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
954 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 955 |
return -1; |
956 |
} |
|
43100 | 957 |
iaObj = NET_SockaddrToInetAddress(env, &sa, &port); |
2 | 958 |
CHECK_NULL_RETURN(iaObj, -1); |
959 |
||
960 |
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj); |
|
961 |
iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;"); |
|
962 |
CHECK_NULL_RETURN(iaFieldID, -1); |
|
963 |
(*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj); |
|
964 |
return 0; /* notice change from before */ |
|
965 |
} |
|
966 |
||
967 |
/* |
|
968 |
* Map the Java level socket option to the platform specific |
|
969 |
* level and option name. |
|
970 |
*/ |
|
971 |
if (NET_MapSocketOption(cmd, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
972 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 973 |
return -1; |
974 |
} |
|
975 |
||
976 |
/* |
|
977 |
* Args are int except for SO_LINGER |
|
978 |
*/ |
|
979 |
if (cmd == java_net_SocketOptions_SO_LINGER) { |
|
980 |
optlen = sizeof(optval.ling); |
|
981 |
} else { |
|
982 |
optlen = sizeof(optval.i); |
|
983 |
} |
|
984 |
||
985 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
986 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
987 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
2 | 988 |
return -1; |
989 |
} |
|
990 |
||
991 |
switch (cmd) { |
|
992 |
case java_net_SocketOptions_SO_LINGER: |
|
993 |
return (optval.ling.l_onoff ? optval.ling.l_linger: -1); |
|
994 |
||
995 |
case java_net_SocketOptions_SO_SNDBUF: |
|
996 |
case java_net_SocketOptions_SO_RCVBUF: |
|
997 |
case java_net_SocketOptions_IP_TOS: |
|
998 |
return optval.i; |
|
999 |
||
1000 |
default : |
|
1001 |
return (optval.i == 0) ? -1 : 1; |
|
1002 |
} |
|
1003 |
} |
|
1004 |
||
1005 |
||
1006 |
/* |
|
1007 |
* Class: java_net_PlainSocketImpl |
|
1008 |
* Method: socketSendUrgentData |
|
1009 |
* Signature: (B)V |
|
1010 |
*/ |
|
1011 |
JNIEXPORT void JNICALL |
|
1012 |
Java_java_net_PlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this, |
|
1013 |
jint data) { |
|
1014 |
/* The fd field */ |
|
1015 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
1016 |
int n, fd; |
|
1017 |
unsigned char d = data & 0xFF; |
|
1018 |
||
1019 |
if (IS_NULL(fdObj)) { |
|
1020 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
1021 |
return; |
|
1022 |
} else { |
|
1023 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1024 |
/* Bug 4086704 - If the Socket associated with this file descriptor |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
1025 |
* was closed (sysCloseFD), the file descriptor is set to -1. |
2 | 1026 |
*/ |
1027 |
if (fd == -1) { |
|
1028 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
1029 |
return; |
|
1030 |
} |
|
1031 |
||
1032 |
} |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
1033 |
n = NET_Send(fd, (char *)&d, 1, MSG_OOB); |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22646
diff
changeset
|
1034 |
if (n == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
30963
diff
changeset
|
1035 |
JNU_ThrowIOExceptionWithLastError(env, "Write failed"); |
2 | 1036 |
} |
1037 |
} |