author | igerasim |
Sun, 18 Feb 2018 17:06:20 -0800 | |
changeset 48911 | edaa989b4e67 |
parent 47216 | 71c04702a3d5 |
child 49413 | 0ee57b9b376c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, 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_addressID; |
|
41 |
jfieldID psi_portID; |
|
42 |
jfieldID psi_localportID; |
|
43 |
jfieldID psi_timeoutID; |
|
44 |
jfieldID psi_trafficClassID; |
|
45 |
jfieldID psi_serverSocketID; |
|
46 |
||
47 |
/* |
|
48 |
* the level of the TCP protocol for setsockopt and getsockopt |
|
49 |
* we only want to look this up once, from the static initializer |
|
50 |
* of TwoStacksPlainSocketImpl |
|
51 |
*/ |
|
52 |
static int tcp_level = -1; |
|
53 |
||
54 |
static int getFD(JNIEnv *env, jobject this) { |
|
55 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
56 |
||
57 |
if (fdObj == NULL) { |
|
58 |
return -1; |
|
59 |
} |
|
60 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
61 |
} |
|
62 |
||
63 |
||
64 |
/* |
|
65 |
* The initProto function is called whenever TwoStacksPlainSocketImpl is |
|
66 |
* loaded, to cache fieldIds for efficiency. This is called everytime |
|
67 |
* the Java class is loaded. |
|
68 |
* |
|
69 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
70 |
* Method: initProto |
|
71 |
||
72 |
* Signature: ()V |
|
73 |
*/ |
|
74 |
JNIEXPORT void JNICALL |
|
75 |
Java_java_net_TwoStacksPlainSocketImpl_initProto(JNIEnv *env, jclass cls) { |
|
76 |
||
77 |
struct protoent *proto = getprotobyname("TCP"); |
|
78 |
tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto); |
|
79 |
||
80 |
psi_fdID = (*env)->GetFieldID(env, cls , "fd", "Ljava/io/FileDescriptor;"); |
|
81 |
CHECK_NULL(psi_fdID); |
|
82 |
psi_addressID = (*env)->GetFieldID(env, cls, "address", |
|
83 |
"Ljava/net/InetAddress;"); |
|
84 |
CHECK_NULL(psi_addressID); |
|
85 |
psi_portID = (*env)->GetFieldID(env, cls, "port", "I"); |
|
86 |
CHECK_NULL(psi_portID); |
|
87 |
psi_localportID = (*env)->GetFieldID(env, cls, "localport", "I"); |
|
88 |
CHECK_NULL(psi_localportID); |
|
89 |
psi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I"); |
|
90 |
CHECK_NULL(psi_timeoutID); |
|
91 |
psi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I"); |
|
92 |
CHECK_NULL(psi_trafficClassID); |
|
93 |
psi_serverSocketID = (*env)->GetFieldID(env, cls, "serverSocket", |
|
94 |
"Ljava/net/ServerSocket;"); |
|
95 |
CHECK_NULL(psi_serverSocketID); |
|
96 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
97 |
CHECK_NULL(IO_fd_fdID); |
|
98 |
} |
|
99 |
||
100 |
/* |
|
101 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
102 |
* Method: socketCreate |
|
103 |
* Signature: (Z)V |
|
104 |
*/ |
|
105 |
JNIEXPORT void JNICALL |
|
106 |
Java_java_net_TwoStacksPlainSocketImpl_socketCreate(JNIEnv *env, jobject this, |
|
107 |
jboolean stream) { |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
108 |
jobject fdObj; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
109 |
int fd; |
2 | 110 |
|
111 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
112 |
||
113 |
if (IS_NULL(fdObj)) { |
|
114 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
115 |
"null fd object"); |
|
116 |
return; |
|
117 |
} |
|
118 |
fd = socket(AF_INET, (stream ? SOCK_STREAM: SOCK_DGRAM), 0); |
|
119 |
if (fd == -1) { |
|
120 |
NET_ThrowCurrent(env, "create"); |
|
121 |
return; |
|
122 |
} else { |
|
123 |
/* Set socket attribute so it is not passed to any child process */ |
|
124 |
SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE); |
|
125 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/* |
|
130 |
* inetAddress is the address object passed to the socket connect |
|
131 |
* call. |
|
132 |
* |
|
133 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
134 |
* Method: socketConnect |
|
135 |
* Signature: (Ljava/net/InetAddress;I)V |
|
136 |
*/ |
|
137 |
JNIEXPORT void JNICALL |
|
138 |
Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this, |
|
43100 | 139 |
jobject iaObj, jint port, |
140 |
jint timeout) |
|
2 | 141 |
{ |
142 |
jint localport = (*env)->GetIntField(env, this, psi_localportID); |
|
143 |
||
144 |
/* family and localport are int fields of iaObj */ |
|
145 |
int family; |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
146 |
jint fd = -1; |
2 | 147 |
jint len; |
148 |
||
149 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
150 |
||
43100 | 151 |
SOCKETADDRESS sa; |
2 | 152 |
|
153 |
/* The result of the connection */ |
|
154 |
int connect_res; |
|
43100 | 155 |
memset((char *)&sa, 0, sizeof(sa)); |
2 | 156 |
|
157 |
if (!IS_NULL(fdObj)) { |
|
158 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
159 |
} |
|
160 |
||
161 |
if (IS_NULL(iaObj)) { |
|
162 |
JNU_ThrowNullPointerException(env, "inet address argument is null."); |
|
163 |
return; |
|
164 |
} |
|
165 |
||
43100 | 166 |
if (NET_InetAddressToSockaddr(env, iaObj, port, &sa, &len, |
167 |
JNI_FALSE) != 0) { |
|
168 |
return; |
|
2 | 169 |
} |
170 |
||
43100 | 171 |
family = sa.sa.sa_family; |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
172 |
if (family != AF_INET) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
173 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
174 |
"Protocol family not supported"); |
2 | 175 |
} |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
176 |
if (fd == -1) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
177 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
178 |
"Destination unreachable"); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
179 |
return; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
180 |
} |
2 | 181 |
|
182 |
if (timeout <= 0) { |
|
43100 | 183 |
connect_res = connect(fd, &sa.sa, sizeof(SOCKETADDRESS)); |
2 | 184 |
if (connect_res == SOCKET_ERROR) { |
185 |
connect_res = WSAGetLastError(); |
|
186 |
} |
|
187 |
} else { |
|
188 |
int optval; |
|
189 |
int optlen = sizeof(optval); |
|
190 |
||
191 |
/* make socket non-blocking */ |
|
192 |
optval = 1; |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
193 |
ioctlsocket(fd, FIONBIO, &optval); |
2 | 194 |
|
195 |
/* initiate the connect */ |
|
43100 | 196 |
connect_res = connect(fd, &sa.sa, sizeof(SOCKETADDRESS)); |
2 | 197 |
if (connect_res == SOCKET_ERROR) { |
198 |
if (WSAGetLastError() != WSAEWOULDBLOCK) { |
|
199 |
connect_res = WSAGetLastError(); |
|
200 |
} else { |
|
201 |
fd_set wr, ex; |
|
202 |
struct timeval t; |
|
203 |
||
204 |
FD_ZERO(&wr); |
|
205 |
FD_ZERO(&ex); |
|
206 |
FD_SET(fd, &wr); |
|
207 |
FD_SET(fd, &ex); |
|
208 |
t.tv_sec = timeout / 1000; |
|
209 |
t.tv_usec = (timeout % 1000) * 1000; |
|
210 |
||
211 |
/* |
|
212 |
* Wait for timout, connection established or |
|
213 |
* connection failed. |
|
214 |
*/ |
|
215 |
connect_res = select(fd+1, 0, &wr, &ex, &t); |
|
216 |
||
217 |
/* |
|
218 |
* Timeout before connection is established/failed so |
|
219 |
* we throw exception and shutdown input/output to prevent |
|
220 |
* socket from being used. |
|
221 |
* The socket should be closed immediately by the caller. |
|
222 |
*/ |
|
223 |
if (connect_res == 0) { |
|
224 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
225 |
"connect timed out"); |
|
226 |
shutdown( fd, SD_BOTH ); |
|
227 |
||
228 |
/* make socket blocking again - just in case */ |
|
229 |
optval = 0; |
|
230 |
ioctlsocket( fd, FIONBIO, &optval ); |
|
231 |
return; |
|
232 |
} |
|
233 |
||
234 |
/* |
|
235 |
* We must now determine if the connection has been established |
|
236 |
* or if it has failed. The logic here is designed to work around |
|
237 |
* bug on Windows NT whereby using getsockopt to obtain the |
|
238 |
* last error (SO_ERROR) indicates there is no error. The workaround |
|
239 |
* on NT is to allow winsock to be scheduled and this is done by |
|
240 |
* yielding and retrying. As yielding is problematic in heavy |
|
241 |
* load conditions we attempt up to 3 times to get the error reason. |
|
242 |
*/ |
|
243 |
if (!FD_ISSET(fd, &ex)) { |
|
244 |
connect_res = 0; |
|
245 |
} else { |
|
246 |
int retry; |
|
247 |
for (retry=0; retry<3; retry++) { |
|
248 |
NET_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, |
|
249 |
(char*)&connect_res, &optlen); |
|
250 |
if (connect_res) { |
|
251 |
break; |
|
252 |
} |
|
253 |
Sleep(0); |
|
254 |
} |
|
255 |
||
256 |
if (connect_res == 0) { |
|
257 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
258 |
"Unable to establish connection"); |
|
259 |
return; |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
/* make socket blocking again */ |
|
266 |
optval = 0; |
|
267 |
ioctlsocket(fd, FIONBIO, &optval); |
|
268 |
} |
|
269 |
||
270 |
if (connect_res) { |
|
271 |
if (connect_res == WSAEADDRNOTAVAIL) { |
|
272 |
JNU_ThrowByName(env, JNU_JAVANETPKG "ConnectException", |
|
273 |
"connect: Address is invalid on local machine, or port is not valid on remote machine"); |
|
274 |
} else { |
|
275 |
NET_ThrowNew(env, connect_res, "connect"); |
|
276 |
} |
|
277 |
return; |
|
278 |
} |
|
279 |
||
280 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, (int)fd); |
|
281 |
||
282 |
/* set the remote peer address and port */ |
|
283 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
284 |
(*env)->SetIntField(env, this, psi_portID, port); |
|
285 |
||
286 |
/* |
|
287 |
* we need to initialize the local port field if bind was called |
|
288 |
* previously to the connect (by the client) then localport field |
|
289 |
* will already be initialized |
|
290 |
*/ |
|
291 |
if (localport == 0) { |
|
292 |
/* Now that we're a connected socket, let's extract the port number |
|
293 |
* that the system chose for us and store it in the Socket object. |
|
294 |
*/ |
|
295 |
u_short port; |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
296 |
int len = sizeof(SOCKETADDRESS); |
43100 | 297 |
if (getsockname(fd, &sa.sa, &len) == -1) { |
2 | 298 |
if (WSAGetLastError() == WSAENOTSOCK) { |
299 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
300 |
"Socket closed"); |
2 | 301 |
} else { |
302 |
NET_ThrowCurrent(env, "getsockname failed"); |
|
303 |
} |
|
304 |
return; |
|
305 |
} |
|
43100 | 306 |
port = ntohs((u_short)GET_PORT(&sa)); |
2 | 307 |
(*env)->SetIntField(env, this, psi_localportID, (int) port); |
308 |
} |
|
309 |
} |
|
310 |
||
311 |
/* |
|
312 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
313 |
* Method: socketBind |
|
314 |
* Signature: (Ljava/net/InetAddress;I)V |
|
315 |
*/ |
|
316 |
JNIEXPORT void JNICALL |
|
317 |
Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this, |
|
18192 | 318 |
jobject iaObj, jint localport, |
319 |
jboolean exclBind) { |
|
2 | 320 |
|
321 |
/* fdObj is the FileDescriptor field on this */ |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
322 |
jobject fdObj; |
2 | 323 |
/* fd is an int field on fdObj */ |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
324 |
int fd, len = 0; |
2 | 325 |
|
326 |
/* family is an int field of iaObj */ |
|
327 |
int family; |
|
328 |
int rv; |
|
329 |
||
43100 | 330 |
SOCKETADDRESS sa; |
2 | 331 |
|
332 |
fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
333 |
||
16870 | 334 |
family = getInetAddress_family(env, iaObj); |
2 | 335 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
336 |
if (family != java_net_InetAddress_IPv4) { |
2 | 337 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
338 |
"Protocol family not supported"); |
|
339 |
return; |
|
340 |
} |
|
341 |
||
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
342 |
if (IS_NULL(fdObj)) { |
2 | 343 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
344 |
"Socket closed"); |
|
345 |
return; |
|
346 |
} else { |
|
347 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
348 |
} |
|
349 |
if (IS_NULL(iaObj)) { |
|
350 |
JNU_ThrowNullPointerException(env, "inet address argument"); |
|
351 |
return; |
|
352 |
} |
|
353 |
||
43100 | 354 |
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
|
355 |
JNI_FALSE) != 0) { |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
356 |
return; |
2 | 357 |
} |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
358 |
rv = NET_WinBind(fd, &sa, len, exclBind); |
2 | 359 |
|
360 |
if (rv == -1) { |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
361 |
NET_ThrowCurrent(env, "NET_Bind"); |
2 | 362 |
return; |
363 |
} |
|
364 |
||
365 |
/* set the address */ |
|
366 |
(*env)->SetObjectField(env, this, psi_addressID, iaObj); |
|
367 |
||
368 |
/* intialize the local port */ |
|
369 |
if (localport == 0) { |
|
370 |
/* Now that we're a bound socket, let's extract the port number |
|
371 |
* that the system chose for us and store it in the Socket object. |
|
372 |
*/ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
373 |
int len = sizeof(SOCKETADDRESS); |
2 | 374 |
u_short port; |
375 |
||
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
376 |
if (getsockname(fd, &sa.sa, &len) == -1) { |
2 | 377 |
NET_ThrowCurrent(env, "getsockname in plain socketBind"); |
378 |
return; |
|
379 |
} |
|
43100 | 380 |
port = ntohs((u_short) GET_PORT (&sa)); |
2 | 381 |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
382 |
(*env)->SetIntField(env, this, psi_localportID, (int)port); |
2 | 383 |
} else { |
384 |
(*env)->SetIntField(env, this, psi_localportID, localport); |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
/* |
|
389 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
390 |
* Method: socketListen |
|
391 |
* Signature: (I)V |
|
392 |
*/ |
|
393 |
JNIEXPORT void JNICALL |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
394 |
Java_java_net_TwoStacksPlainSocketImpl_socketListen |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
395 |
(JNIEnv *env, jobject this, jint count) |
2 | 396 |
{ |
397 |
/* this FileDescriptor fd field */ |
|
398 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
399 |
jobject address; |
|
400 |
/* fdObj's int fd field */ |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
401 |
int fd = INVALID_SOCKET; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
402 |
SOCKETADDRESS addr; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
403 |
int addrlen; |
2 | 404 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
405 |
if (IS_NULL(fdObj)) { |
2 | 406 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
407 |
"socket closed"); |
|
408 |
return; |
|
409 |
} |
|
410 |
||
411 |
if (!IS_NULL(fdObj)) { |
|
412 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
413 |
} |
|
414 |
address = (*env)->GetObjectField(env, this, psi_addressID); |
|
415 |
if (IS_NULL(address)) { |
|
416 |
JNU_ThrowNullPointerException(env, "socket address"); |
|
417 |
return; |
|
418 |
} |
|
43100 | 419 |
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
|
420 |
JNI_FALSE) != 0) { |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
421 |
return; |
2 | 422 |
} |
423 |
||
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
424 |
if (addr.sa.sa_family == AF_INET) { |
2 | 425 |
/* listen on v4 */ |
426 |
if (listen(fd, count) == -1) { |
|
427 |
NET_ThrowCurrent(env, "listen failed"); |
|
428 |
} |
|
429 |
} else { |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
430 |
NET_SocketClose(fd); |
2 | 431 |
(*env)->SetObjectField(env, this, psi_fdID, NULL); |
432 |
} |
|
433 |
} |
|
434 |
||
435 |
/* |
|
436 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
437 |
* Method: socketAccept |
|
438 |
* Signature: (Ljava/net/SocketImpl;)V |
|
439 |
*/ |
|
440 |
JNIEXPORT void JNICALL |
|
441 |
Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this, |
|
442 |
jobject socket) |
|
443 |
{ |
|
444 |
/* fields on this */ |
|
445 |
jint port; |
|
446 |
jint timeout = (*env)->GetIntField(env, this, psi_timeoutID); |
|
447 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
448 |
||
449 |
/* the FileDescriptor field on socket */ |
|
450 |
jobject socketFdObj; |
|
451 |
||
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
452 |
/* cache the Inet4 class */ |
2 | 453 |
static jclass inet4Cls; |
454 |
||
455 |
/* the InetAddress field on socket */ |
|
456 |
jobject socketAddressObj; |
|
457 |
||
458 |
/* the fd int field on fdObj */ |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
459 |
jint fd=-1; |
2 | 460 |
|
43100 | 461 |
SOCKETADDRESS sa; |
2 | 462 |
jint len; |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
463 |
int ret; |
2 | 464 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
465 |
if (IS_NULL(fdObj)) { |
2 | 466 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
467 |
"Socket closed"); |
|
468 |
return; |
|
469 |
} |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
470 |
|
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
471 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
472 |
|
2 | 473 |
if (IS_NULL(socket)) { |
474 |
JNU_ThrowNullPointerException(env, "socket is null"); |
|
475 |
return; |
|
476 |
} else { |
|
477 |
socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID); |
|
478 |
socketAddressObj = (*env)->GetObjectField(env, socket, psi_addressID); |
|
479 |
} |
|
480 |
if ((IS_NULL(socketAddressObj)) || (IS_NULL(socketFdObj))) { |
|
481 |
JNU_ThrowNullPointerException(env, "socket address or fd obj"); |
|
482 |
return; |
|
483 |
} |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
484 |
|
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
485 |
len = sizeof(struct sockaddr_in); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
486 |
if (timeout) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
487 |
ret = NET_Timeout(fd, timeout); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
488 |
if (ret == 0) { |
2 | 489 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
490 |
"Accept timed out"); |
|
491 |
return; |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
492 |
} else if (ret == -1) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
493 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
494 |
/* REMIND: SOCKET CLOSED PROBLEM */ |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
495 |
/* NET_ThrowCurrent(env, "Accept failed"); */ |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
496 |
return; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
497 |
} else if (ret == -2) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
498 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
499 |
"operation interrupted"); |
2 | 500 |
return; |
501 |
} |
|
502 |
} |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
503 |
|
43100 | 504 |
fd = accept(fd, &sa.sa, &len); |
2 | 505 |
if (fd < 0) { |
506 |
/* REMIND: SOCKET CLOSED PROBLEM */ |
|
507 |
if (fd == -2) { |
|
508 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
509 |
"operation interrupted"); |
|
510 |
} else { |
|
511 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
512 |
"socket closed"); |
|
513 |
} |
|
514 |
return; |
|
515 |
} |
|
28681
fc0698dfbb23
8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows
chegar
parents:
28059
diff
changeset
|
516 |
SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, 0); |
2 | 517 |
(*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd); |
518 |
||
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
519 |
if (sa.sa.sa_family != AF_INET) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
520 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
521 |
"Protocol family not supported"); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
522 |
return; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
523 |
} |
2 | 524 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
525 |
if (inet4Cls == NULL) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
526 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
527 |
if (c != NULL) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
528 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
529 |
(*env)->DeleteLocalRef(env, c); |
2 | 530 |
} |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
531 |
} |
2 | 532 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
533 |
/* |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
534 |
* fill up the remote peer port and address in the new socket structure |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
535 |
*/ |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
536 |
if (inet4Cls != NULL) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
537 |
socketAddressObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
2 | 538 |
} else { |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
539 |
socketAddressObj = NULL; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
540 |
} |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
541 |
if (socketAddressObj == NULL) { |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
542 |
/* |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
543 |
* FindClass or NewObject failed so close connection and |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
544 |
* exit (there will be a pending exception). |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
545 |
*/ |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
546 |
NET_SocketClose(fd); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
547 |
return; |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
548 |
} |
2 | 549 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
550 |
setInetAddress_addr(env, socketAddressObj, ntohl(sa.sa4.sin_addr.s_addr)); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
551 |
setInetAddress_family(env, socketAddressObj, java_net_InetAddress_IPv4); |
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
552 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); |
43100 | 553 |
port = ntohs ((u_short)GET_PORT(&sa)); |
2 | 554 |
(*env)->SetIntField(env, socket, psi_portID, (int)port); |
555 |
port = (*env)->GetIntField(env, this, psi_localportID); |
|
556 |
(*env)->SetIntField(env, socket, psi_localportID, port); |
|
557 |
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); |
|
558 |
} |
|
559 |
||
560 |
/* |
|
561 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
562 |
* Method: socketAvailable |
|
563 |
* Signature: ()I |
|
564 |
*/ |
|
565 |
JNIEXPORT jint JNICALL |
|
566 |
Java_java_net_TwoStacksPlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) { |
|
567 |
||
568 |
jint available = -1; |
|
569 |
jint res; |
|
570 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
571 |
jint fd; |
|
572 |
||
573 |
if (IS_NULL(fdObj)) { |
|
574 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
575 |
return -1; |
|
576 |
} else { |
|
577 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
578 |
} |
|
579 |
res = ioctlsocket(fd, FIONREAD, &available); |
|
580 |
/* if result isn't 0, it means an error */ |
|
581 |
if (res != 0) { |
|
582 |
NET_ThrowNew(env, res, "socket available"); |
|
583 |
} |
|
584 |
return available; |
|
585 |
} |
|
586 |
||
587 |
/* |
|
588 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
589 |
* Method: socketClose |
|
590 |
* Signature: ()V |
|
591 |
*/ |
|
592 |
JNIEXPORT void JNICALL |
|
593 |
Java_java_net_TwoStacksPlainSocketImpl_socketClose0(JNIEnv *env, jobject this, |
|
594 |
jboolean useDeferredClose) { |
|
595 |
||
596 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
597 |
jint fd=-1; |
2 | 598 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
599 |
if (IS_NULL(fdObj)) { |
2 | 600 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
601 |
"socket already closed"); |
|
602 |
return; |
|
603 |
} |
|
604 |
if (!IS_NULL(fdObj)) { |
|
605 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
606 |
} |
|
607 |
if (fd != -1) { |
|
608 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
609 |
NET_SocketClose(fd); |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
/* |
|
614 |
* Socket options for plainsocketImpl |
|
615 |
* |
|
616 |
* |
|
617 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
18192 | 618 |
* Method: socketNativeSetOption |
2 | 619 |
* Signature: (IZLjava/lang/Object;)V |
620 |
*/ |
|
621 |
JNIEXPORT void JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
622 |
Java_java_net_TwoStacksPlainSocketImpl_socketNativeSetOption |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
623 |
(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
|
624 |
{ |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
625 |
int fd; |
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
|
626 |
int level = 0, optname = 0, optlen = 0; |
2 | 627 |
union { |
628 |
int i; |
|
629 |
struct linger ling; |
|
630 |
} optval; |
|
631 |
||
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
|
632 |
memset((char *)&optval, 0, sizeof(optval)); |
2 | 633 |
/* |
634 |
* Get SOCKET and check that it hasn't been closed |
|
635 |
*/ |
|
636 |
fd = getFD(env, this); |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
637 |
if (fd < 0) { |
2 | 638 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
639 |
return; |
|
640 |
} |
|
641 |
||
642 |
/* |
|
643 |
* SO_TIMEOUT is the socket option used to specify the timeout |
|
644 |
* for ServerSocket.accept and Socket.getInputStream().read. |
|
645 |
* It does not typically map to a native level socket option. |
|
646 |
* For Windows we special-case this and use the SOL_SOCKET/SO_RCVTIMEO |
|
647 |
* socket option to specify a receive timeout on the socket. This |
|
648 |
* receive timeout is applicable to Socket only and the socket |
|
649 |
* option should not be set on ServerSocket. |
|
650 |
*/ |
|
651 |
if (cmd == java_net_SocketOptions_SO_TIMEOUT) { |
|
652 |
||
653 |
/* |
|
654 |
* Don't enable the socket option on ServerSocket as it's |
|
655 |
* meaningless (we don't receive on a ServerSocket). |
|
656 |
*/ |
|
657 |
jobject ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID); |
|
658 |
if (ssObj != NULL) { |
|
659 |
return; |
|
660 |
} |
|
661 |
||
662 |
/* |
|
663 |
* SO_RCVTIMEO is only supported on Microsoft's implementation |
|
664 |
* of Windows Sockets so if WSAENOPROTOOPT returned then |
|
665 |
* reset flag and timeout will be implemented using |
|
666 |
* select() -- see SocketInputStream.socketRead. |
|
667 |
*/ |
|
668 |
if (isRcvTimeoutSupported) { |
|
669 |
jclass iCls = (*env)->FindClass(env, "java/lang/Integer"); |
|
670 |
jfieldID i_valueID; |
|
671 |
jint timeout; |
|
672 |
||
673 |
CHECK_NULL(iCls); |
|
674 |
i_valueID = (*env)->GetFieldID(env, iCls, "value", "I"); |
|
675 |
CHECK_NULL(i_valueID); |
|
676 |
timeout = (*env)->GetIntField(env, value, i_valueID); |
|
677 |
||
678 |
/* |
|
679 |
* Disable SO_RCVTIMEO if timeout is <= 5 second. |
|
680 |
*/ |
|
681 |
if (timeout <= 5000) { |
|
682 |
timeout = 0; |
|
683 |
} |
|
684 |
||
685 |
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, |
|
686 |
sizeof(timeout)) < 0) { |
|
687 |
if (WSAGetLastError() == WSAENOPROTOOPT) { |
|
688 |
isRcvTimeoutSupported = JNI_FALSE; |
|
689 |
} else { |
|
690 |
NET_ThrowCurrent(env, "setsockopt SO_RCVTIMEO"); |
|
41594
e54e81e9be7b
8168405: Pending exceptions in java.base/windows/native
prappo
parents:
41380
diff
changeset
|
691 |
return; |
2 | 692 |
} |
693 |
} |
|
694 |
} |
|
695 |
return; |
|
696 |
} |
|
697 |
||
698 |
/* |
|
699 |
* 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
|
700 |
* level and option name. |
2 | 701 |
*/ |
702 |
if (NET_MapSocketOption(cmd, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
703 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 704 |
return; |
705 |
} |
|
706 |
||
707 |
switch (cmd) { |
|
708 |
||
709 |
case java_net_SocketOptions_TCP_NODELAY : |
|
710 |
case java_net_SocketOptions_SO_OOBINLINE : |
|
711 |
case java_net_SocketOptions_SO_KEEPALIVE : |
|
712 |
case java_net_SocketOptions_SO_REUSEADDR : |
|
713 |
optval.i = (on ? 1 : 0); |
|
714 |
optlen = sizeof(optval.i); |
|
715 |
break; |
|
716 |
||
717 |
case java_net_SocketOptions_SO_SNDBUF : |
|
718 |
case java_net_SocketOptions_SO_RCVBUF : |
|
719 |
case java_net_SocketOptions_IP_TOS : |
|
720 |
{ |
|
721 |
jclass cls; |
|
722 |
jfieldID fid; |
|
723 |
||
724 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
725 |
CHECK_NULL(cls); |
|
726 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
727 |
CHECK_NULL(fid); |
|
728 |
||
729 |
optval.i = (*env)->GetIntField(env, value, fid); |
|
730 |
optlen = sizeof(optval.i); |
|
731 |
} |
|
732 |
break; |
|
733 |
||
734 |
case java_net_SocketOptions_SO_LINGER : |
|
735 |
{ |
|
736 |
jclass cls; |
|
737 |
jfieldID fid; |
|
738 |
||
739 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
740 |
CHECK_NULL(cls); |
|
741 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
742 |
CHECK_NULL(fid); |
|
743 |
||
744 |
if (on) { |
|
745 |
optval.ling.l_onoff = 1; |
|
910 | 746 |
optval.ling.l_linger = |
747 |
(unsigned short)(*env)->GetIntField(env, value, fid); |
|
2 | 748 |
} else { |
749 |
optval.ling.l_onoff = 0; |
|
750 |
optval.ling.l_linger = 0; |
|
751 |
} |
|
752 |
optlen = sizeof(optval.ling); |
|
753 |
} |
|
754 |
break; |
|
755 |
||
756 |
default: /* shouldn't get here */ |
|
757 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
758 |
"Option not supported by TwoStacksPlainSocketImpl"); |
|
759 |
return; |
|
760 |
} |
|
761 |
||
762 |
if (fd != -1) { |
|
763 |
if (NET_SetSockOpt(fd, level, optname, (void *)&optval, optlen) < 0) { |
|
764 |
NET_ThrowCurrent(env, "setsockopt"); |
|
765 |
} |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
||
770 |
/* |
|
771 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
772 |
* Method: socketGetOption |
|
773 |
* Signature: (I)I |
|
774 |
*/ |
|
775 |
JNIEXPORT jint JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
776 |
Java_java_net_TwoStacksPlainSocketImpl_socketGetOption |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
777 |
(JNIEnv *env, jobject this, jint opt, jobject iaContainerObj) |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
778 |
{ |
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
779 |
int fd; |
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
|
780 |
int level = 0, optname = 0, optlen = 0; |
2 | 781 |
union { |
782 |
int i; |
|
783 |
struct linger ling; |
|
784 |
} optval; |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
785 |
|
2 | 786 |
/* |
787 |
* Get SOCKET and check it hasn't been closed |
|
788 |
*/ |
|
789 |
fd = getFD(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
|
790 |
memset((char *)&optval, 0, sizeof(optval)); |
2 | 791 |
|
48911
edaa989b4e67
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
792 |
if (fd < 0) { |
2 | 793 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
794 |
return -1; |
|
795 |
} |
|
796 |
||
797 |
/* |
|
798 |
* SO_BINDADDR isn't a socket option |
|
799 |
*/ |
|
800 |
if (opt == java_net_SocketOptions_SO_BINDADDR) { |
|
43100 | 801 |
SOCKETADDRESS sa; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
802 |
int len = sizeof(SOCKETADDRESS); |
2 | 803 |
int port; |
804 |
jobject iaObj; |
|
805 |
jclass iaCntrClass; |
|
806 |
jfieldID iaFieldID; |
|
807 |
||
43100 | 808 |
memset((char *)&sa, 0, len); |
2 | 809 |
|
43100 | 810 |
if (getsockname(fd, &sa.sa, &len) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
28681
diff
changeset
|
811 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
28681
diff
changeset
|
812 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 813 |
return -1; |
814 |
} |
|
43100 | 815 |
iaObj = NET_SockaddrToInetAddress(env, &sa, &port); |
2 | 816 |
CHECK_NULL_RETURN(iaObj, -1); |
817 |
||
818 |
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj); |
|
819 |
iaFieldID = (*env)->GetFieldID(env, iaCntrClass, "addr", "Ljava/net/InetAddress;"); |
|
820 |
CHECK_NULL_RETURN(iaFieldID, -1); |
|
821 |
(*env)->SetObjectField(env, iaContainerObj, iaFieldID, iaObj); |
|
822 |
return 0; /* notice change from before */ |
|
823 |
} |
|
824 |
||
825 |
/* |
|
826 |
* Map the Java level socket option to the platform specific |
|
827 |
* level and option name. |
|
828 |
*/ |
|
829 |
if (NET_MapSocketOption(opt, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
830 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 831 |
return -1; |
832 |
} |
|
833 |
||
834 |
/* |
|
835 |
* Args are int except for SO_LINGER |
|
836 |
*/ |
|
837 |
if (opt == java_net_SocketOptions_SO_LINGER) { |
|
838 |
optlen = sizeof(optval.ling); |
|
839 |
} else { |
|
840 |
optlen = sizeof(optval.i); |
|
841 |
optval.i = 0; |
|
842 |
} |
|
843 |
||
844 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { |
|
845 |
NET_ThrowCurrent(env, "getsockopt"); |
|
846 |
return -1; |
|
847 |
} |
|
848 |
||
849 |
switch (opt) { |
|
850 |
case java_net_SocketOptions_SO_LINGER: |
|
851 |
return (optval.ling.l_onoff ? optval.ling.l_linger: -1); |
|
852 |
||
853 |
case java_net_SocketOptions_SO_SNDBUF: |
|
854 |
case java_net_SocketOptions_SO_RCVBUF: |
|
855 |
case java_net_SocketOptions_IP_TOS: |
|
856 |
return optval.i; |
|
857 |
||
858 |
case java_net_SocketOptions_TCP_NODELAY : |
|
859 |
case java_net_SocketOptions_SO_OOBINLINE : |
|
860 |
case java_net_SocketOptions_SO_KEEPALIVE : |
|
861 |
case java_net_SocketOptions_SO_REUSEADDR : |
|
862 |
return (optval.i == 0) ? -1 : 1; |
|
863 |
||
864 |
default: /* shouldn't get here */ |
|
865 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
866 |
"Option not supported by TwoStacksPlainSocketImpl"); |
|
867 |
return -1; |
|
868 |
} |
|
869 |
} |
|
870 |
||
871 |
/* |
|
872 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
873 |
* Method: socketShutdown |
|
874 |
* Signature: (I)V |
|
875 |
*/ |
|
876 |
JNIEXPORT void JNICALL |
|
877 |
Java_java_net_TwoStacksPlainSocketImpl_socketShutdown(JNIEnv *env, jobject this, |
|
878 |
jint howto) |
|
879 |
{ |
|
880 |
||
881 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
882 |
jint fd; |
|
883 |
||
884 |
/* |
|
885 |
* WARNING: THIS NEEDS LOCKING. ALSO: SHOULD WE CHECK for fd being |
|
886 |
* -1 already? |
|
887 |
*/ |
|
888 |
if (IS_NULL(fdObj)) { |
|
889 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
890 |
"socket already closed"); |
|
891 |
return; |
|
892 |
} else { |
|
893 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
894 |
} |
|
895 |
shutdown(fd, howto); |
|
896 |
} |
|
897 |
||
898 |
/* |
|
899 |
* Class: java_net_TwoStacksPlainSocketImpl |
|
900 |
* Method: socketSendUrgentData |
|
901 |
* Signature: (B)V |
|
902 |
*/ |
|
903 |
JNIEXPORT void JNICALL |
|
904 |
Java_java_net_TwoStacksPlainSocketImpl_socketSendUrgentData(JNIEnv *env, jobject this, |
|
905 |
jint data) { |
|
906 |
/* The fd field */ |
|
907 |
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); |
|
908 |
int n, fd; |
|
909 |
unsigned char d = data & 0xff; |
|
910 |
||
911 |
if (IS_NULL(fdObj)) { |
|
912 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
913 |
return; |
|
914 |
} else { |
|
915 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
916 |
/* 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
|
917 |
* was closed (sysCloseFD), the file descriptor is set to -1. |
2 | 918 |
*/ |
919 |
if (fd == -1) { |
|
920 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
921 |
return; |
|
922 |
} |
|
923 |
||
924 |
} |
|
925 |
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
|
926 |
if (n == -1) { |
2 | 927 |
NET_ThrowCurrent(env, "send"); |
928 |
return; |
|
929 |
} |
|
930 |
} |