author | michaelm |
Thu, 13 Jun 2019 09:10:51 +0100 | |
changeset 55375 | 96c7427456f9 |
parent 54627 | 22323f20401b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53422
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41578
diff
changeset
|
25 |
#include "net_util.h" |
2 | 26 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41578
diff
changeset
|
27 |
#include "java_net_InetAddress.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41578
diff
changeset
|
28 |
#include "java_net_SocketOptions.h" |
2 | 29 |
|
27178
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
30 |
// Taken from mstcpip.h in Windows SDK 8.0 or newer. |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
31 |
#define SIO_LOOPBACK_FAST_PATH _WSAIOW(IOC_VENDOR,16) |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
32 |
|
2 | 33 |
#ifndef IPTOS_TOS_MASK |
34 |
#define IPTOS_TOS_MASK 0x1e |
|
35 |
#endif |
|
36 |
#ifndef IPTOS_PREC_MASK |
|
37 |
#define IPTOS_PREC_MASK 0xe0 |
|
38 |
#endif |
|
39 |
||
40 |
/* true if SO_RCVTIMEO is supported */ |
|
41 |
jboolean isRcvTimeoutSupported = JNI_TRUE; |
|
42 |
||
43 |
/* |
|
44 |
* Table of Windows Sockets errors, the specific exception we |
|
45 |
* throw for the error, and the error text. |
|
46 |
* |
|
47 |
* Note that this table excludes OS dependent errors. |
|
48 |
* |
|
49 |
* Latest list of Windows Sockets errors can be found at :- |
|
50 |
* http://msdn.microsoft.com/library/psdk/winsock/errors_3wc2.htm |
|
51 |
*/ |
|
52 |
static struct { |
|
53 |
int errCode; |
|
54 |
const char *exc; |
|
55 |
const char *errString; |
|
56 |
} const winsock_errors[] = { |
|
57 |
{ WSAEACCES, 0, "Permission denied" }, |
|
58 |
{ WSAEADDRINUSE, "BindException", "Address already in use" }, |
|
59 |
{ WSAEADDRNOTAVAIL, "BindException", "Cannot assign requested address" }, |
|
60 |
{ WSAEAFNOSUPPORT, 0, "Address family not supported by protocol family" }, |
|
61 |
{ WSAEALREADY, 0, "Operation already in progress" }, |
|
62 |
{ WSAECONNABORTED, 0, "Software caused connection abort" }, |
|
63 |
{ WSAECONNREFUSED, "ConnectException", "Connection refused" }, |
|
64 |
{ WSAECONNRESET, 0, "Connection reset by peer" }, |
|
65 |
{ WSAEDESTADDRREQ, 0, "Destination address required" }, |
|
66 |
{ WSAEFAULT, 0, "Bad address" }, |
|
67 |
{ WSAEHOSTDOWN, 0, "Host is down" }, |
|
68 |
{ WSAEHOSTUNREACH, "NoRouteToHostException", "No route to host" }, |
|
69 |
{ WSAEINPROGRESS, 0, "Operation now in progress" }, |
|
70 |
{ WSAEINTR, 0, "Interrupted function call" }, |
|
71 |
{ WSAEINVAL, 0, "Invalid argument" }, |
|
72 |
{ WSAEISCONN, 0, "Socket is already connected" }, |
|
73 |
{ WSAEMFILE, 0, "Too many open files" }, |
|
74 |
{ WSAEMSGSIZE, 0, "The message is larger than the maximum supported by the underlying transport" }, |
|
75 |
{ WSAENETDOWN, 0, "Network is down" }, |
|
76 |
{ WSAENETRESET, 0, "Network dropped connection on reset" }, |
|
77 |
{ WSAENETUNREACH, 0, "Network is unreachable" }, |
|
78 |
{ WSAENOBUFS, 0, "No buffer space available (maximum connections reached?)" }, |
|
79 |
{ WSAENOPROTOOPT, 0, "Bad protocol option" }, |
|
80 |
{ WSAENOTCONN, 0, "Socket is not connected" }, |
|
81 |
{ WSAENOTSOCK, 0, "Socket operation on nonsocket" }, |
|
82 |
{ WSAEOPNOTSUPP, 0, "Operation not supported" }, |
|
83 |
{ WSAEPFNOSUPPORT, 0, "Protocol family not supported" }, |
|
84 |
{ WSAEPROCLIM, 0, "Too many processes" }, |
|
85 |
{ WSAEPROTONOSUPPORT, 0, "Protocol not supported" }, |
|
86 |
{ WSAEPROTOTYPE, 0, "Protocol wrong type for socket" }, |
|
87 |
{ WSAESHUTDOWN, 0, "Cannot send after socket shutdown" }, |
|
88 |
{ WSAESOCKTNOSUPPORT, 0, "Socket type not supported" }, |
|
89 |
{ WSAETIMEDOUT, "ConnectException", "Connection timed out" }, |
|
90 |
{ WSATYPE_NOT_FOUND, 0, "Class type not found" }, |
|
91 |
{ WSAEWOULDBLOCK, 0, "Resource temporarily unavailable" }, |
|
92 |
{ WSAHOST_NOT_FOUND, 0, "Host not found" }, |
|
93 |
{ WSA_NOT_ENOUGH_MEMORY, 0, "Insufficient memory available" }, |
|
94 |
{ WSANOTINITIALISED, 0, "Successful WSAStartup not yet performed" }, |
|
95 |
{ WSANO_DATA, 0, "Valid name, no data record of requested type" }, |
|
96 |
{ WSANO_RECOVERY, 0, "This is a nonrecoverable error" }, |
|
97 |
{ WSASYSNOTREADY, 0, "Network subsystem is unavailable" }, |
|
98 |
{ WSATRY_AGAIN, 0, "Nonauthoritative host not found" }, |
|
99 |
{ WSAVERNOTSUPPORTED, 0, "Winsock.dll version out of range" }, |
|
100 |
{ WSAEDISCON, 0, "Graceful shutdown in progress" }, |
|
101 |
{ WSA_OPERATION_ABORTED, 0, "Overlapped operation aborted" }, |
|
102 |
}; |
|
103 |
||
104 |
/* |
|
105 |
* Initialize Windows Sockets API support |
|
106 |
*/ |
|
107 |
BOOL WINAPI |
|
108 |
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) |
|
109 |
{ |
|
110 |
WSADATA wsadata; |
|
111 |
||
112 |
switch (reason) { |
|
113 |
case DLL_PROCESS_ATTACH: |
|
114 |
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) { |
|
115 |
return FALSE; |
|
116 |
} |
|
117 |
break; |
|
118 |
||
119 |
case DLL_PROCESS_DETACH: |
|
120 |
WSACleanup(); |
|
121 |
break; |
|
122 |
||
123 |
default: |
|
124 |
break; |
|
125 |
} |
|
126 |
return TRUE; |
|
127 |
} |
|
128 |
||
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
20821
diff
changeset
|
129 |
void platformInit() {} |
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
20821
diff
changeset
|
130 |
void parseExclusiveBindProperty(JNIEnv *env) {} |
2 | 131 |
|
132 |
/* |
|
133 |
* Since winsock doesn't have the equivalent of strerror(errno) |
|
134 |
* use table to lookup error text for the error. |
|
135 |
*/ |
|
136 |
JNIEXPORT void JNICALL |
|
137 |
NET_ThrowNew(JNIEnv *env, int errorNum, char *msg) |
|
138 |
{ |
|
139 |
int i; |
|
140 |
int table_size = sizeof(winsock_errors) / |
|
141 |
sizeof(winsock_errors[0]); |
|
142 |
char exc[256]; |
|
143 |
char fullMsg[256]; |
|
144 |
char *excP = NULL; |
|
145 |
||
146 |
/* |
|
147 |
* If exception already throw then don't overwrite it. |
|
148 |
*/ |
|
149 |
if ((*env)->ExceptionOccurred(env)) { |
|
150 |
return; |
|
151 |
} |
|
152 |
||
153 |
/* |
|
154 |
* Default message text if not provided |
|
155 |
*/ |
|
156 |
if (!msg) { |
|
157 |
msg = "no further information"; |
|
158 |
} |
|
159 |
||
160 |
/* |
|
161 |
* Check table for known winsock errors |
|
162 |
*/ |
|
163 |
i=0; |
|
164 |
while (i < table_size) { |
|
165 |
if (errorNum == winsock_errors[i].errCode) { |
|
166 |
break; |
|
167 |
} |
|
168 |
i++; |
|
169 |
} |
|
170 |
||
171 |
/* |
|
172 |
* If found get pick the specific exception and error |
|
173 |
* message corresponding to this error. |
|
174 |
*/ |
|
175 |
if (i < table_size) { |
|
176 |
excP = (char *)winsock_errors[i].exc; |
|
177 |
jio_snprintf(fullMsg, sizeof(fullMsg), "%s: %s", |
|
178 |
(char *)winsock_errors[i].errString, msg); |
|
179 |
} else { |
|
180 |
jio_snprintf(fullMsg, sizeof(fullMsg), |
|
181 |
"Unrecognized Windows Sockets error: %d: %s", |
|
182 |
errorNum, msg); |
|
183 |
||
184 |
} |
|
185 |
||
186 |
/* |
|
187 |
* Throw SocketException if no specific exception for this |
|
188 |
* error. |
|
189 |
*/ |
|
190 |
if (excP == NULL) { |
|
191 |
excP = "SocketException"; |
|
192 |
} |
|
193 |
sprintf(exc, "%s%s", JNU_JAVANETPKG, excP); |
|
194 |
JNU_ThrowByName(env, exc, fullMsg); |
|
195 |
} |
|
196 |
||
197 |
void |
|
198 |
NET_ThrowCurrent(JNIEnv *env, char *msg) |
|
199 |
{ |
|
200 |
NET_ThrowNew(env, WSAGetLastError(), msg); |
|
201 |
} |
|
202 |
||
203 |
void |
|
204 |
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, |
|
205 |
const char *defaultDetail) { |
|
41578 | 206 |
JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail); |
2 | 207 |
} |
208 |
||
209 |
jfieldID |
|
210 |
NET_GetFileDescriptorID(JNIEnv *env) |
|
211 |
{ |
|
212 |
jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
213 |
CHECK_NULL_RETURN(cls, NULL); |
|
214 |
return (*env)->GetFieldID(env, cls, "fd", "I"); |
|
215 |
} |
|
216 |
||
54627
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
217 |
jint IPv4_supported() |
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
218 |
{ |
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
219 |
/* TODO: properly check for IPv4 support on Windows */ |
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
220 |
return JNI_TRUE; |
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
221 |
} |
22323f20401b
8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents:
53422
diff
changeset
|
222 |
|
2 | 223 |
jint IPv6_supported() |
224 |
{ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
225 |
SOCKET s = socket(AF_INET6, SOCK_STREAM, 0) ; |
9019 | 226 |
if (s == INVALID_SOCKET) { |
2 | 227 |
return JNI_FALSE; |
228 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
229 |
closesocket(s); |
2 | 230 |
|
231 |
return JNI_TRUE; |
|
232 |
} |
|
233 |
||
36115 | 234 |
jint reuseport_supported() |
235 |
{ |
|
49892
8bed781a8d9c
8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents:
49440
diff
changeset
|
236 |
/* SO_REUSEPORT is not supported on Windows */ |
36115 | 237 |
return JNI_FALSE; |
238 |
} |
|
2 | 239 |
|
240 |
/* call NET_MapSocketOptionV6 for the IPv6 fd only |
|
241 |
* and NET_MapSocketOption for the IPv4 fd |
|
242 |
*/ |
|
243 |
JNIEXPORT int JNICALL |
|
244 |
NET_MapSocketOptionV6(jint cmd, int *level, int *optname) { |
|
245 |
||
246 |
switch (cmd) { |
|
247 |
case java_net_SocketOptions_IP_MULTICAST_IF: |
|
248 |
case java_net_SocketOptions_IP_MULTICAST_IF2: |
|
249 |
*level = IPPROTO_IPV6; |
|
250 |
*optname = IPV6_MULTICAST_IF; |
|
251 |
return 0; |
|
252 |
||
253 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
254 |
*level = IPPROTO_IPV6; |
|
255 |
*optname = IPV6_MULTICAST_LOOP; |
|
256 |
return 0; |
|
257 |
} |
|
258 |
return NET_MapSocketOption (cmd, level, optname); |
|
259 |
} |
|
260 |
||
261 |
/* |
|
262 |
* Map the Java level socket option to the platform specific |
|
263 |
* level and option name. |
|
264 |
*/ |
|
265 |
||
266 |
JNIEXPORT int JNICALL |
|
267 |
NET_MapSocketOption(jint cmd, int *level, int *optname) { |
|
268 |
||
269 |
typedef struct { |
|
270 |
jint cmd; |
|
271 |
int level; |
|
272 |
int optname; |
|
273 |
} sockopts; |
|
274 |
||
275 |
static sockopts opts[] = { |
|
276 |
{ java_net_SocketOptions_TCP_NODELAY, IPPROTO_TCP, TCP_NODELAY }, |
|
277 |
{ java_net_SocketOptions_SO_OOBINLINE, SOL_SOCKET, SO_OOBINLINE }, |
|
278 |
{ java_net_SocketOptions_SO_LINGER, SOL_SOCKET, SO_LINGER }, |
|
279 |
{ java_net_SocketOptions_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF }, |
|
280 |
{ java_net_SocketOptions_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF }, |
|
281 |
{ java_net_SocketOptions_SO_KEEPALIVE, SOL_SOCKET, SO_KEEPALIVE }, |
|
282 |
{ java_net_SocketOptions_SO_REUSEADDR, SOL_SOCKET, SO_REUSEADDR }, |
|
283 |
{ java_net_SocketOptions_SO_BROADCAST, SOL_SOCKET, SO_BROADCAST }, |
|
284 |
{ java_net_SocketOptions_IP_MULTICAST_IF, IPPROTO_IP, IP_MULTICAST_IF }, |
|
285 |
{ java_net_SocketOptions_IP_MULTICAST_LOOP, IPPROTO_IP, IP_MULTICAST_LOOP }, |
|
286 |
{ java_net_SocketOptions_IP_TOS, IPPROTO_IP, IP_TOS }, |
|
287 |
||
288 |
}; |
|
289 |
||
290 |
||
291 |
int i; |
|
292 |
||
293 |
/* |
|
294 |
* Map the Java level option to the native level |
|
295 |
*/ |
|
296 |
for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) { |
|
297 |
if (cmd == opts[i].cmd) { |
|
298 |
*level = opts[i].level; |
|
299 |
*optname = opts[i].optname; |
|
300 |
return 0; |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
/* not found */ |
|
305 |
return -1; |
|
306 |
} |
|
307 |
||
308 |
||
309 |
/* |
|
310 |
* Wrapper for setsockopt dealing with Windows specific issues :- |
|
311 |
* |
|
312 |
* IP_TOS and IP_MULTICAST_LOOP can't be set on some Windows |
|
313 |
* editions. |
|
314 |
* |
|
315 |
* The value for the type-of-service (TOS) needs to be masked |
|
316 |
* to get consistent behaviour with other operating systems. |
|
317 |
*/ |
|
318 |
JNIEXPORT int JNICALL |
|
319 |
NET_SetSockOpt(int s, int level, int optname, const void *optval, |
|
320 |
int optlen) |
|
321 |
{ |
|
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:
25388
diff
changeset
|
322 |
int rv = 0; |
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:
25388
diff
changeset
|
323 |
int parg = 0; |
18192 | 324 |
int plen = sizeof(parg); |
2 | 325 |
|
326 |
if (level == IPPROTO_IP && optname == IP_TOS) { |
|
327 |
int *tos = (int *)optval; |
|
328 |
*tos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK); |
|
329 |
} |
|
330 |
||
18192 | 331 |
if (optname == SO_REUSEADDR) { |
332 |
/* |
|
333 |
* Do not set SO_REUSEADDE if SO_EXCLUSIVEADDUSE is already set |
|
334 |
*/ |
|
335 |
rv = NET_GetSockOpt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *)&parg, &plen); |
|
336 |
if (rv == 0 && parg == 1) { |
|
337 |
return rv; |
|
338 |
} |
|
339 |
} |
|
340 |
||
2 | 341 |
rv = setsockopt(s, level, optname, optval, optlen); |
342 |
||
343 |
if (rv == SOCKET_ERROR) { |
|
344 |
/* |
|
345 |
* IP_TOS & IP_MULTICAST_LOOP can't be set on some versions |
|
346 |
* of Windows. |
|
347 |
*/ |
|
348 |
if ((WSAGetLastError() == WSAENOPROTOOPT) && |
|
349 |
(level == IPPROTO_IP) && |
|
350 |
(optname == IP_TOS || optname == IP_MULTICAST_LOOP)) { |
|
351 |
rv = 0; |
|
352 |
} |
|
353 |
||
354 |
/* |
|
355 |
* IP_TOS can't be set on unbound UDP sockets. |
|
356 |
*/ |
|
357 |
if ((WSAGetLastError() == WSAEINVAL) && |
|
358 |
(level == IPPROTO_IP) && |
|
359 |
(optname == IP_TOS)) { |
|
360 |
rv = 0; |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
return rv; |
|
365 |
} |
|
366 |
||
367 |
/* |
|
368 |
* Wrapper for setsockopt dealing with Windows specific issues :- |
|
369 |
* |
|
370 |
* IP_TOS is not supported on some versions of Windows so |
|
371 |
* instead return the default value for the OS. |
|
372 |
*/ |
|
373 |
JNIEXPORT int JNICALL |
|
374 |
NET_GetSockOpt(int s, int level, int optname, void *optval, |
|
375 |
int *optlen) |
|
376 |
{ |
|
377 |
int rv; |
|
378 |
||
25388
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
379 |
if (level == IPPROTO_IPV6 && optname == IPV6_TCLASS) { |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
380 |
int *intopt = (int *)optval; |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
381 |
*intopt = 0; |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
382 |
*optlen = sizeof(*intopt); |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
383 |
return 0; |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
384 |
} |
65f81ea02187
8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents:
23010
diff
changeset
|
385 |
|
2 | 386 |
rv = getsockopt(s, level, optname, optval, optlen); |
387 |
||
388 |
||
389 |
/* |
|
390 |
* IPPROTO_IP/IP_TOS is not supported on some Windows |
|
391 |
* editions so return the default type-of-service |
|
392 |
* value. |
|
393 |
*/ |
|
394 |
if (rv == SOCKET_ERROR) { |
|
395 |
||
396 |
if (WSAGetLastError() == WSAENOPROTOOPT && |
|
397 |
level == IPPROTO_IP && optname == IP_TOS) { |
|
398 |
||
49892
8bed781a8d9c
8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents:
49440
diff
changeset
|
399 |
*((int *)optval) = 0; |
2 | 400 |
rv = 0; |
401 |
} |
|
402 |
} |
|
403 |
||
404 |
return rv; |
|
405 |
} |
|
406 |
||
53422
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
407 |
JNIEXPORT int JNICALL |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
408 |
NET_SocketAvailable(int s, int *pbytes) { |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
409 |
u_long arg; |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
410 |
if (ioctlsocket((SOCKET)s, FIONREAD, &arg) == SOCKET_ERROR) { |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
411 |
return -1; |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
412 |
} else { |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
413 |
*pbytes = (int) arg; |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
414 |
return 0; |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
415 |
} |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
416 |
} |
6f02e036630e
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents:
52499
diff
changeset
|
417 |
|
2 | 418 |
/* |
18192 | 419 |
* Sets SO_ECLUSIVEADDRUSE if SO_REUSEADDR is not already set. |
420 |
*/ |
|
421 |
void setExclusiveBind(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:
25388
diff
changeset
|
422 |
int parg = 0; |
18192 | 423 |
int plen = sizeof(parg); |
424 |
int rv = 0; |
|
425 |
rv = NET_GetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&parg, &plen); |
|
426 |
if (rv == 0 && parg == 0) { |
|
427 |
parg = 1; |
|
428 |
rv = NET_SetSockOpt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&parg, plen); |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
/* |
|
2 | 433 |
* Wrapper for bind winsock call - transparent converts an |
434 |
* error related to binding to a port that has exclusive access |
|
435 |
* into an error indicating the port is in use (facilitates |
|
436 |
* better error reporting). |
|
18192 | 437 |
* |
438 |
* Should be only called by the wrapper method NET_WinBind |
|
2 | 439 |
*/ |
440 |
JNIEXPORT int JNICALL |
|
43100 | 441 |
NET_Bind(int s, SOCKETADDRESS *sa, int len) |
2 | 442 |
{ |
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:
25388
diff
changeset
|
443 |
int rv = 0; |
43100 | 444 |
rv = bind(s, &sa->sa, len); |
2 | 445 |
|
446 |
if (rv == SOCKET_ERROR) { |
|
447 |
/* |
|
448 |
* If bind fails with WSAEACCES it means that a privileged |
|
449 |
* process has done an exclusive bind (NT SP4/2000/XP only). |
|
450 |
*/ |
|
451 |
if (WSAGetLastError() == WSAEACCES) { |
|
452 |
WSASetLastError(WSAEADDRINUSE); |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
return rv; |
|
457 |
} |
|
458 |
||
18192 | 459 |
/* |
460 |
* Wrapper for NET_Bind call. Sets SO_EXCLUSIVEADDRUSE |
|
461 |
* if required, and then calls NET_BIND |
|
462 |
*/ |
|
463 |
JNIEXPORT int JNICALL |
|
43100 | 464 |
NET_WinBind(int s, SOCKETADDRESS *sa, int len, jboolean exclBind) |
18192 | 465 |
{ |
466 |
if (exclBind == JNI_TRUE) |
|
467 |
setExclusiveBind(s); |
|
43100 | 468 |
return NET_Bind(s, sa, len); |
18192 | 469 |
} |
470 |
||
2 | 471 |
JNIEXPORT int JNICALL |
472 |
NET_SocketClose(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:
25388
diff
changeset
|
473 |
struct linger l = {0, 0}; |
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:
25388
diff
changeset
|
474 |
int ret = 0; |
2 | 475 |
int len = sizeof (l); |
476 |
if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) { |
|
477 |
if (l.l_onoff == 0) { |
|
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
47216
diff
changeset
|
478 |
shutdown(fd, SD_SEND); |
2 | 479 |
} |
480 |
} |
|
481 |
ret = closesocket (fd); |
|
482 |
return ret; |
|
483 |
} |
|
484 |
||
485 |
JNIEXPORT int JNICALL |
|
486 |
NET_Timeout(int fd, long timeout) { |
|
487 |
int ret; |
|
488 |
fd_set tbl; |
|
489 |
struct timeval t; |
|
490 |
t.tv_sec = timeout / 1000; |
|
491 |
t.tv_usec = (timeout % 1000) * 1000; |
|
492 |
FD_ZERO(&tbl); |
|
493 |
FD_SET(fd, &tbl); |
|
494 |
ret = select (fd + 1, &tbl, 0, 0, &t); |
|
495 |
return ret; |
|
496 |
} |
|
497 |
||
498 |
||
499 |
/* |
|
500 |
* differs from NET_Timeout() as follows: |
|
501 |
* |
|
502 |
* If timeout = -1, it blocks forever. |
|
503 |
* |
|
504 |
* returns 1 or 2 depending if only one or both sockets |
|
505 |
* fire at same time. |
|
506 |
* |
|
507 |
* *fdret is (one of) the active fds. If both sockets |
|
508 |
* fire at same time, *fdret = fd always. |
|
509 |
*/ |
|
510 |
JNIEXPORT int JNICALL |
|
511 |
NET_Timeout2(int fd, int fd1, long timeout, int *fdret) { |
|
512 |
int ret; |
|
513 |
fd_set tbl; |
|
514 |
struct timeval t, *tP = &t; |
|
515 |
if (timeout == -1) { |
|
516 |
tP = 0; |
|
517 |
} else { |
|
518 |
t.tv_sec = timeout / 1000; |
|
519 |
t.tv_usec = (timeout % 1000) * 1000; |
|
520 |
} |
|
521 |
FD_ZERO(&tbl); |
|
522 |
FD_SET(fd, &tbl); |
|
523 |
FD_SET(fd1, &tbl); |
|
524 |
ret = select (0, &tbl, 0, 0, tP); |
|
525 |
switch (ret) { |
|
526 |
case 0: |
|
527 |
return 0; /* timeout */ |
|
528 |
case 1: |
|
529 |
if (FD_ISSET (fd, &tbl)) { |
|
530 |
*fdret= fd; |
|
531 |
} else { |
|
532 |
*fdret= fd1; |
|
533 |
} |
|
534 |
return 1; |
|
535 |
case 2: |
|
536 |
*fdret= fd; |
|
537 |
return 2; |
|
538 |
} |
|
539 |
return -1; |
|
540 |
} |
|
541 |
||
542 |
||
543 |
void dumpAddr (char *str, void *addr) { |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41578
diff
changeset
|
544 |
struct sockaddr_in6 *a = (struct sockaddr_in6 *)addr; |
2 | 545 |
int family = a->sin6_family; |
546 |
printf ("%s\n", str); |
|
547 |
if (family == AF_INET) { |
|
548 |
struct sockaddr_in *him = (struct sockaddr_in *)addr; |
|
549 |
printf ("AF_INET: port %d: %x\n", ntohs(him->sin_port), |
|
550 |
ntohl(him->sin_addr.s_addr)); |
|
551 |
} else { |
|
552 |
int i; |
|
553 |
struct in6_addr *in = &a->sin6_addr; |
|
554 |
printf ("AF_INET6 "); |
|
555 |
printf ("port %d ", ntohs (a->sin6_port)); |
|
556 |
printf ("flow %d ", a->sin6_flowinfo); |
|
557 |
printf ("addr "); |
|
558 |
for (i=0; i<7; i++) { |
|
559 |
printf ("%04x:", ntohs(in->s6_words[i])); |
|
560 |
} |
|
561 |
printf ("%04x", ntohs(in->s6_words[7])); |
|
562 |
printf (" scope %d\n", a->sin6_scope_id); |
|
563 |
} |
|
564 |
} |
|
565 |
||
566 |
/* Macro, which cleans-up the iv6bind structure, |
|
567 |
* closes the two sockets (if open), |
|
568 |
* and returns SOCKET_ERROR. Used in NET_BindV6 only. |
|
569 |
*/ |
|
570 |
||
26458
d6a5aed9204b
7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents:
25859
diff
changeset
|
571 |
#define CLOSE_SOCKETS_AND_RETURN do { \ |
2 | 572 |
if (fd != -1) { \ |
573 |
closesocket (fd); \ |
|
574 |
fd = -1; \ |
|
575 |
} \ |
|
576 |
if (ofd != -1) { \ |
|
577 |
closesocket (ofd); \ |
|
578 |
ofd = -1; \ |
|
579 |
} \ |
|
580 |
if (close_fd != -1) { \ |
|
581 |
closesocket (close_fd); \ |
|
582 |
close_fd = -1; \ |
|
583 |
} \ |
|
584 |
if (close_ofd != -1) { \ |
|
585 |
closesocket (close_ofd); \ |
|
586 |
close_ofd = -1; \ |
|
587 |
} \ |
|
588 |
b->ipv4_fd = b->ipv6_fd = -1; \ |
|
589 |
return SOCKET_ERROR; \ |
|
26458
d6a5aed9204b
7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents:
25859
diff
changeset
|
590 |
} while(0) |
2 | 591 |
|
592 |
/* |
|
593 |
* if ipv6 is available, call NET_BindV6 to bind to the required address/port. |
|
594 |
* Because the same port number may need to be reserved in both v4 and v6 space, |
|
595 |
* this may require socket(s) to be re-opened. Therefore, all of this information |
|
596 |
* is passed in and returned through the ipv6bind structure. |
|
597 |
* |
|
598 |
* If the request is to bind to a specific address, then this (by definition) means |
|
599 |
* only bind in either v4 or v6, and this is just the same as normal. ie. a single |
|
600 |
* call to bind() will suffice. The other socket is closed in this case. |
|
601 |
* |
|
602 |
* The more complicated case is when the requested address is ::0 or 0.0.0.0. |
|
603 |
* |
|
604 |
* Two further cases: |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52073
diff
changeset
|
605 |
* 2. If the requested port is 0 (ie. any port) then we try to bind in v4 space |
2 | 606 |
* first with a wild-card port argument. We then try to bind in v6 space |
607 |
* using the returned port number. If this fails, we repeat the process |
|
608 |
* until a free port common to both spaces becomes available. |
|
609 |
* |
|
610 |
* 3. If the requested port is a specific port, then we just try to get that |
|
611 |
* port in both spaces, and if it is not free in both, then the bind fails. |
|
612 |
* |
|
613 |
* On failure, sockets are closed and an error returned with CLOSE_SOCKETS_AND_RETURN |
|
614 |
*/ |
|
615 |
||
616 |
JNIEXPORT int JNICALL |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
617 |
NET_BindV6(struct ipv6bind *b, jboolean exclBind) { |
2 | 618 |
int fd=-1, ofd=-1, rv, len; |
619 |
/* need to defer close until new sockets created */ |
|
620 |
int close_fd=-1, close_ofd=-1; |
|
621 |
SOCKETADDRESS oaddr; /* other address to bind */ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
622 |
int family = b->addr->sa.sa_family; |
2 | 623 |
int ofamily; |
624 |
u_short port; /* requested port parameter */ |
|
625 |
u_short bound_port; |
|
626 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
627 |
if (family == AF_INET && (b->addr->sa4.sin_addr.s_addr != INADDR_ANY)) { |
2 | 628 |
/* bind to v4 only */ |
629 |
int ret; |
|
43100 | 630 |
ret = NET_WinBind((int)b->ipv4_fd, b->addr, |
631 |
sizeof(SOCKETADDRESS), exclBind); |
|
2 | 632 |
if (ret == SOCKET_ERROR) { |
633 |
CLOSE_SOCKETS_AND_RETURN; |
|
634 |
} |
|
635 |
closesocket (b->ipv6_fd); |
|
636 |
b->ipv6_fd = -1; |
|
637 |
return 0; |
|
638 |
} |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
639 |
if (family == AF_INET6 && (!IN6_IS_ADDR_ANY(&b->addr->sa6.sin6_addr))) { |
2 | 640 |
/* bind to v6 only */ |
641 |
int ret; |
|
43100 | 642 |
ret = NET_WinBind((int)b->ipv6_fd, b->addr, |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
643 |
sizeof(SOCKETADDRESS), exclBind); |
2 | 644 |
if (ret == SOCKET_ERROR) { |
645 |
CLOSE_SOCKETS_AND_RETURN; |
|
646 |
} |
|
647 |
closesocket (b->ipv4_fd); |
|
648 |
b->ipv4_fd = -1; |
|
649 |
return 0; |
|
650 |
} |
|
651 |
||
652 |
/* We need to bind on both stacks, with the same port number */ |
|
653 |
||
654 |
memset (&oaddr, 0, sizeof(oaddr)); |
|
655 |
if (family == AF_INET) { |
|
656 |
ofamily = AF_INET6; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
657 |
fd = (int)b->ipv4_fd; |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
658 |
ofd = (int)b->ipv6_fd; |
2 | 659 |
port = (u_short)GET_PORT (b->addr); |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
660 |
IN6ADDR_SETANY(&oaddr.sa6); |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
661 |
oaddr.sa6.sin6_port = port; |
2 | 662 |
} else { |
663 |
ofamily = AF_INET; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
664 |
ofd = (int)b->ipv4_fd; |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
665 |
fd = (int)b->ipv6_fd; |
2 | 666 |
port = (u_short)GET_PORT (b->addr); |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
667 |
oaddr.sa4.sin_family = AF_INET; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
668 |
oaddr.sa4.sin_port = port; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
669 |
oaddr.sa4.sin_addr.s_addr = INADDR_ANY; |
2 | 670 |
} |
671 |
||
43100 | 672 |
rv = NET_WinBind(fd, b->addr, sizeof(SOCKETADDRESS), exclBind); |
2 | 673 |
if (rv == SOCKET_ERROR) { |
674 |
CLOSE_SOCKETS_AND_RETURN; |
|
675 |
} |
|
676 |
||
677 |
/* get the port and set it in the other address */ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
678 |
len = sizeof(SOCKETADDRESS); |
2 | 679 |
if (getsockname(fd, (struct sockaddr *)b->addr, &len) == -1) { |
680 |
CLOSE_SOCKETS_AND_RETURN; |
|
681 |
} |
|
682 |
bound_port = GET_PORT (b->addr); |
|
683 |
SET_PORT (&oaddr, bound_port); |
|
43100 | 684 |
if ((rv = NET_WinBind(ofd, &oaddr, |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
685 |
sizeof(SOCKETADDRESS), exclBind)) == SOCKET_ERROR) { |
2 | 686 |
int retries; |
687 |
int sotype, arglen=sizeof(sotype); |
|
688 |
||
689 |
/* no retries unless, the request was for any free port */ |
|
690 |
||
691 |
if (port != 0) { |
|
692 |
CLOSE_SOCKETS_AND_RETURN; |
|
693 |
} |
|
694 |
||
695 |
getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen); |
|
696 |
||
697 |
#define SOCK_RETRIES 50 |
|
698 |
/* 50 is an arbitrary limit, just to ensure that this |
|
699 |
* cannot be an endless loop. Would expect socket creation to |
|
700 |
* succeed sooner. |
|
701 |
*/ |
|
702 |
for (retries = 0; retries < SOCK_RETRIES; retries ++) { |
|
703 |
int len; |
|
704 |
close_fd = fd; fd = -1; |
|
705 |
close_ofd = ofd; ofd = -1; |
|
706 |
b->ipv4_fd = SOCKET_ERROR; |
|
707 |
b->ipv6_fd = SOCKET_ERROR; |
|
708 |
||
709 |
/* create two new sockets */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
710 |
fd = (int)socket (family, sotype, 0); |
2 | 711 |
if (fd == SOCKET_ERROR) { |
712 |
CLOSE_SOCKETS_AND_RETURN; |
|
713 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
714 |
ofd = (int)socket (ofamily, sotype, 0); |
2 | 715 |
if (ofd == SOCKET_ERROR) { |
716 |
CLOSE_SOCKETS_AND_RETURN; |
|
717 |
} |
|
718 |
||
719 |
/* bind random port on first socket */ |
|
720 |
SET_PORT (&oaddr, 0); |
|
43100 | 721 |
rv = NET_WinBind(ofd, &oaddr, sizeof(SOCKETADDRESS), exclBind); |
2 | 722 |
if (rv == SOCKET_ERROR) { |
723 |
CLOSE_SOCKETS_AND_RETURN; |
|
724 |
} |
|
725 |
/* close the original pair of sockets before continuing */ |
|
726 |
closesocket (close_fd); |
|
727 |
closesocket (close_ofd); |
|
728 |
close_fd = close_ofd = -1; |
|
729 |
||
730 |
/* bind new port on second socket */ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
731 |
len = sizeof(SOCKETADDRESS); |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41210
diff
changeset
|
732 |
if (getsockname(ofd, &oaddr.sa, &len) == -1) { |
2 | 733 |
CLOSE_SOCKETS_AND_RETURN; |
734 |
} |
|
735 |
bound_port = GET_PORT (&oaddr); |
|
736 |
SET_PORT (b->addr, bound_port); |
|
43100 | 737 |
rv = NET_WinBind(fd, b->addr, sizeof(SOCKETADDRESS), exclBind); |
2 | 738 |
|
739 |
if (rv != SOCKET_ERROR) { |
|
740 |
if (family == AF_INET) { |
|
741 |
b->ipv4_fd = fd; |
|
742 |
b->ipv6_fd = ofd; |
|
743 |
} else { |
|
744 |
b->ipv4_fd = ofd; |
|
745 |
b->ipv6_fd = fd; |
|
746 |
} |
|
747 |
return 0; |
|
748 |
} |
|
749 |
} |
|
750 |
CLOSE_SOCKETS_AND_RETURN; |
|
751 |
} |
|
752 |
return 0; |
|
753 |
} |
|
754 |
||
27178
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
755 |
/** |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
756 |
* Enables SIO_LOOPBACK_FAST_PATH |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
757 |
*/ |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
758 |
JNIEXPORT jint JNICALL |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
759 |
NET_EnableFastTcpLoopback(int fd) { |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
760 |
int enabled = 1; |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
761 |
DWORD result_byte_count = -1; |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
762 |
int result = WSAIoctl(fd, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
763 |
SIO_LOOPBACK_FAST_PATH, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
764 |
&enabled, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
765 |
sizeof(enabled), |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
766 |
NULL, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
767 |
0, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
768 |
&result_byte_count, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
769 |
NULL, |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
770 |
NULL); |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
771 |
return result == SOCKET_ERROR ? WSAGetLastError() : 0; |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
772 |
} |
885f4428b501
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents:
26458
diff
changeset
|
773 |
|
43100 | 774 |
/** |
775 |
* See net_util.h for documentation |
|
776 |
*/ |
|
2 | 777 |
JNIEXPORT int JNICALL |
43100 | 778 |
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, |
779 |
SOCKETADDRESS *sa, int *len, |
|
780 |
jboolean v4MappedAddress) |
|
781 |
{ |
|
782 |
jint family = getInetAddress_family(env, iaObj); |
|
51151 | 783 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
43100 | 784 |
memset((char *)sa, 0, sizeof(SOCKETADDRESS)); |
785 |
||
786 |
if (ipv6_available() && |
|
787 |
!(family == java_net_InetAddress_IPv4 && |
|
788 |
v4MappedAddress == JNI_FALSE)) |
|
789 |
{ |
|
2 | 790 |
jbyte caddr[16]; |
43100 | 791 |
jint address; |
55375 | 792 |
unsigned int scopeid = 0; |
2 | 793 |
|
43100 | 794 |
if (family == java_net_InetAddress_IPv4) { |
795 |
// convert to IPv4-mapped address |
|
796 |
memset((char *)caddr, 0, 16); |
|
16870 | 797 |
address = getInetAddress_addr(env, iaObj); |
51151 | 798 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
2 | 799 |
if (address == INADDR_ANY) { |
800 |
/* we would always prefer IPv6 wildcard address |
|
43100 | 801 |
* caddr[10] = 0xff; |
802 |
* caddr[11] = 0xff; */ |
|
2 | 803 |
} else { |
804 |
caddr[10] = 0xff; |
|
805 |
caddr[11] = 0xff; |
|
806 |
caddr[12] = ((address >> 24) & 0xff); |
|
807 |
caddr[13] = ((address >> 16) & 0xff); |
|
808 |
caddr[14] = ((address >> 8) & 0xff); |
|
809 |
caddr[15] = (address & 0xff); |
|
810 |
} |
|
811 |
} else { |
|
20821 | 812 |
getInet6Address_ipaddress(env, iaObj, (char *)caddr); |
813 |
scopeid = getInet6Address_scopeid(env, iaObj); |
|
2 | 814 |
} |
43100 | 815 |
sa->sa6.sin6_port = (u_short)htons((u_short)port); |
816 |
memcpy((void *)&sa->sa6.sin6_addr, caddr, sizeof(struct in6_addr)); |
|
817 |
sa->sa6.sin6_family = AF_INET6; |
|
55375 | 818 |
sa->sa6.sin6_scope_id = scopeid; |
43100 | 819 |
if (len != NULL) { |
820 |
*len = sizeof(struct sockaddr_in6); |
|
821 |
} |
|
2 | 822 |
} else { |
823 |
jint address; |
|
43100 | 824 |
if (family != java_net_InetAddress_IPv4) { |
825 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable"); |
|
826 |
return -1; |
|
2 | 827 |
} |
16870 | 828 |
address = getInetAddress_addr(env, iaObj); |
51151 | 829 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
43100 | 830 |
sa->sa4.sin_port = htons((short)port); |
831 |
sa->sa4.sin_addr.s_addr = (u_long)htonl(address); |
|
832 |
sa->sa4.sin_family = AF_INET; |
|
833 |
if (len != NULL) { |
|
834 |
*len = sizeof(struct sockaddr_in); |
|
835 |
} |
|
2 | 836 |
} |
837 |
return 0; |
|
838 |
} |
|
839 |
||
840 |
int |
|
841 |
NET_IsIPv4Mapped(jbyte* caddr) { |
|
842 |
int i; |
|
843 |
for (i = 0; i < 10; i++) { |
|
844 |
if (caddr[i] != 0x00) { |
|
845 |
return 0; /* false */ |
|
846 |
} |
|
847 |
} |
|
848 |
||
849 |
if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) { |
|
850 |
return 1; /* true */ |
|
851 |
} |
|
852 |
return 0; /* false */ |
|
853 |
} |
|
854 |
||
855 |
int |
|
856 |
NET_IPv4MappedToIPv4(jbyte* caddr) { |
|
857 |
return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8) |
|
858 |
| (caddr[15] & 0xff); |
|
859 |
} |
|
860 |
||
861 |
int |
|
862 |
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) { |
|
863 |
int i; |
|
864 |
for (i = 0; i < 16; i++) { |
|
865 |
if (caddr1[i] != caddr2[i]) { |
|
866 |
return 0; /* false */ |
|
867 |
} |
|
868 |
} |
|
869 |
return 1; |
|
870 |
} |
|
871 |
||
872 |
/** |
|
873 |
* Wrapper for select/poll with timeout on a single file descriptor. |
|
874 |
* |
|
875 |
* flags (defined in net_util_md.h can be any combination of |
|
876 |
* NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT. |
|
877 |
* |
|
878 |
* The function will return when either the socket is ready for one |
|
879 |
* of the specified operation or the timeout expired. |
|
880 |
* |
|
881 |
* It returns the time left from the timeout, or -1 if it expired. |
|
882 |
*/ |
|
883 |
||
52073
1c8745e31fa3
8212000: Verify exported symbols in java.base (libnet, libnio/ch)
prappo
parents:
51151
diff
changeset
|
884 |
jint |
2 | 885 |
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout) |
886 |
{ |
|
887 |
jlong prevTime = JVM_CurrentTimeMillis(env, 0); |
|
888 |
jint read_rv; |
|
889 |
||
890 |
while (1) { |
|
891 |
jlong newTime; |
|
892 |
fd_set rd, wr, ex; |
|
893 |
struct timeval t; |
|
894 |
||
895 |
t.tv_sec = timeout / 1000; |
|
896 |
t.tv_usec = (timeout % 1000) * 1000; |
|
897 |
||
898 |
FD_ZERO(&rd); |
|
899 |
FD_ZERO(&wr); |
|
900 |
FD_ZERO(&ex); |
|
901 |
if (flags & NET_WAIT_READ) { |
|
902 |
FD_SET(fd, &rd); |
|
903 |
} |
|
904 |
if (flags & NET_WAIT_WRITE) { |
|
905 |
FD_SET(fd, &wr); |
|
906 |
} |
|
907 |
if (flags & NET_WAIT_CONNECT) { |
|
908 |
FD_SET(fd, &wr); |
|
909 |
FD_SET(fd, &ex); |
|
910 |
} |
|
911 |
||
912 |
errno = 0; |
|
913 |
read_rv = select(fd+1, &rd, &wr, &ex, &t); |
|
914 |
||
915 |
newTime = JVM_CurrentTimeMillis(env, 0); |
|
910 | 916 |
timeout -= (jint)(newTime - prevTime); |
2 | 917 |
if (timeout <= 0) { |
918 |
return read_rv > 0 ? 0 : -1; |
|
919 |
} |
|
920 |
newTime = prevTime; |
|
921 |
||
922 |
if (read_rv > 0) { |
|
923 |
break; |
|
924 |
} |
|
925 |
||
926 |
||
927 |
} /* while */ |
|
928 |
||
929 |
return timeout; |
|
930 |
} |
|
931 |
||
932 |
int NET_Socket (int domain, int type, int protocol) { |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
933 |
SOCKET sock; |
2 | 934 |
sock = socket (domain, type, protocol); |
935 |
if (sock != INVALID_SOCKET) { |
|
936 |
SetHandleInformation((HANDLE)(uintptr_t)sock, HANDLE_FLAG_INHERIT, FALSE); |
|
937 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
938 |
return (int)sock; |
2 | 939 |
} |