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