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