author | coffeys |
Thu, 17 May 2012 12:21:16 +0100 | |
changeset 13043 | 10fa4f21ea0f |
parent 9019 | 03920d69bb93 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, 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 |
||
26 |
#include <winsock2.h> |
|
27 |
#include <ws2tcpip.h> |
|
28 |
||
29 |
#include "net_util.h" |
|
30 |
#include "jni.h" |
|
31 |
||
32 |
#ifndef IPTOS_TOS_MASK |
|
33 |
#define IPTOS_TOS_MASK 0x1e |
|
34 |
#endif |
|
35 |
#ifndef IPTOS_PREC_MASK |
|
36 |
#define IPTOS_PREC_MASK 0xe0 |
|
37 |
#endif |
|
38 |
||
39 |
/* true if SO_RCVTIMEO is supported */ |
|
40 |
jboolean isRcvTimeoutSupported = JNI_TRUE; |
|
41 |
||
42 |
/* |
|
43 |
* Table of Windows Sockets errors, the specific exception we |
|
44 |
* throw for the error, and the error text. |
|
45 |
* |
|
46 |
* Note that this table excludes OS dependent errors. |
|
47 |
* |
|
48 |
* Latest list of Windows Sockets errors can be found at :- |
|
49 |
* http://msdn.microsoft.com/library/psdk/winsock/errors_3wc2.htm |
|
50 |
*/ |
|
51 |
static struct { |
|
52 |
int errCode; |
|
53 |
const char *exc; |
|
54 |
const char *errString; |
|
55 |
} const winsock_errors[] = { |
|
56 |
{ WSAEACCES, 0, "Permission denied" }, |
|
57 |
{ WSAEADDRINUSE, "BindException", "Address already in use" }, |
|
58 |
{ WSAEADDRNOTAVAIL, "BindException", "Cannot assign requested address" }, |
|
59 |
{ WSAEAFNOSUPPORT, 0, "Address family not supported by protocol family" }, |
|
60 |
{ WSAEALREADY, 0, "Operation already in progress" }, |
|
61 |
{ WSAECONNABORTED, 0, "Software caused connection abort" }, |
|
62 |
{ WSAECONNREFUSED, "ConnectException", "Connection refused" }, |
|
63 |
{ WSAECONNRESET, 0, "Connection reset by peer" }, |
|
64 |
{ WSAEDESTADDRREQ, 0, "Destination address required" }, |
|
65 |
{ WSAEFAULT, 0, "Bad address" }, |
|
66 |
{ WSAEHOSTDOWN, 0, "Host is down" }, |
|
67 |
{ WSAEHOSTUNREACH, "NoRouteToHostException", "No route to host" }, |
|
68 |
{ WSAEINPROGRESS, 0, "Operation now in progress" }, |
|
69 |
{ WSAEINTR, 0, "Interrupted function call" }, |
|
70 |
{ WSAEINVAL, 0, "Invalid argument" }, |
|
71 |
{ WSAEISCONN, 0, "Socket is already connected" }, |
|
72 |
{ WSAEMFILE, 0, "Too many open files" }, |
|
73 |
{ WSAEMSGSIZE, 0, "The message is larger than the maximum supported by the underlying transport" }, |
|
74 |
{ WSAENETDOWN, 0, "Network is down" }, |
|
75 |
{ WSAENETRESET, 0, "Network dropped connection on reset" }, |
|
76 |
{ WSAENETUNREACH, 0, "Network is unreachable" }, |
|
77 |
{ WSAENOBUFS, 0, "No buffer space available (maximum connections reached?)" }, |
|
78 |
{ WSAENOPROTOOPT, 0, "Bad protocol option" }, |
|
79 |
{ WSAENOTCONN, 0, "Socket is not connected" }, |
|
80 |
{ WSAENOTSOCK, 0, "Socket operation on nonsocket" }, |
|
81 |
{ WSAEOPNOTSUPP, 0, "Operation not supported" }, |
|
82 |
{ WSAEPFNOSUPPORT, 0, "Protocol family not supported" }, |
|
83 |
{ WSAEPROCLIM, 0, "Too many processes" }, |
|
84 |
{ WSAEPROTONOSUPPORT, 0, "Protocol not supported" }, |
|
85 |
{ WSAEPROTOTYPE, 0, "Protocol wrong type for socket" }, |
|
86 |
{ WSAESHUTDOWN, 0, "Cannot send after socket shutdown" }, |
|
87 |
{ WSAESOCKTNOSUPPORT, 0, "Socket type not supported" }, |
|
88 |
{ WSAETIMEDOUT, "ConnectException", "Connection timed out" }, |
|
89 |
{ WSATYPE_NOT_FOUND, 0, "Class type not found" }, |
|
90 |
{ WSAEWOULDBLOCK, 0, "Resource temporarily unavailable" }, |
|
91 |
{ WSAHOST_NOT_FOUND, 0, "Host not found" }, |
|
92 |
{ WSA_NOT_ENOUGH_MEMORY, 0, "Insufficient memory available" }, |
|
93 |
{ WSANOTINITIALISED, 0, "Successful WSAStartup not yet performed" }, |
|
94 |
{ WSANO_DATA, 0, "Valid name, no data record of requested type" }, |
|
95 |
{ WSANO_RECOVERY, 0, "This is a nonrecoverable error" }, |
|
96 |
{ WSASYSNOTREADY, 0, "Network subsystem is unavailable" }, |
|
97 |
{ WSATRY_AGAIN, 0, "Nonauthoritative host not found" }, |
|
98 |
{ WSAVERNOTSUPPORTED, 0, "Winsock.dll version out of range" }, |
|
99 |
{ WSAEDISCON, 0, "Graceful shutdown in progress" }, |
|
100 |
{ WSA_OPERATION_ABORTED, 0, "Overlapped operation aborted" }, |
|
101 |
}; |
|
102 |
||
103 |
/* |
|
104 |
* Initialize Windows Sockets API support |
|
105 |
*/ |
|
106 |
BOOL WINAPI |
|
107 |
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) |
|
108 |
{ |
|
109 |
WSADATA wsadata; |
|
110 |
||
111 |
switch (reason) { |
|
112 |
case DLL_PROCESS_ATTACH: |
|
113 |
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) { |
|
114 |
return FALSE; |
|
115 |
} |
|
116 |
break; |
|
117 |
||
118 |
case DLL_PROCESS_DETACH: |
|
119 |
WSACleanup(); |
|
120 |
break; |
|
121 |
||
122 |
default: |
|
123 |
break; |
|
124 |
} |
|
125 |
return TRUE; |
|
126 |
} |
|
127 |
||
128 |
void initLocalAddrTable () {} |
|
13043
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
9019
diff
changeset
|
129 |
void parseExclusiveBindProperty (JNIEnv *env) {} |
2 | 130 |
|
131 |
/* |
|
132 |
* Since winsock doesn't have the equivalent of strerror(errno) |
|
133 |
* use table to lookup error text for the error. |
|
134 |
*/ |
|
135 |
JNIEXPORT void JNICALL |
|
136 |
NET_ThrowNew(JNIEnv *env, int errorNum, char *msg) |
|
137 |
{ |
|
138 |
int i; |
|
139 |
int table_size = sizeof(winsock_errors) / |
|
140 |
sizeof(winsock_errors[0]); |
|
141 |
char exc[256]; |
|
142 |
char fullMsg[256]; |
|
143 |
char *excP = NULL; |
|
144 |
||
145 |
/* |
|
146 |
* If exception already throw then don't overwrite it. |
|
147 |
*/ |
|
148 |
if ((*env)->ExceptionOccurred(env)) { |
|
149 |
return; |
|
150 |
} |
|
151 |
||
152 |
/* |
|
153 |
* Default message text if not provided |
|
154 |
*/ |
|
155 |
if (!msg) { |
|
156 |
msg = "no further information"; |
|
157 |
} |
|
158 |
||
159 |
/* |
|
160 |
* Check table for known winsock errors |
|
161 |
*/ |
|
162 |
i=0; |
|
163 |
while (i < table_size) { |
|
164 |
if (errorNum == winsock_errors[i].errCode) { |
|
165 |
break; |
|
166 |
} |
|
167 |
i++; |
|
168 |
} |
|
169 |
||
170 |
/* |
|
171 |
* If found get pick the specific exception and error |
|
172 |
* message corresponding to this error. |
|
173 |
*/ |
|
174 |
if (i < table_size) { |
|
175 |
excP = (char *)winsock_errors[i].exc; |
|
176 |
jio_snprintf(fullMsg, sizeof(fullMsg), "%s: %s", |
|
177 |
(char *)winsock_errors[i].errString, msg); |
|
178 |
} else { |
|
179 |
jio_snprintf(fullMsg, sizeof(fullMsg), |
|
180 |
"Unrecognized Windows Sockets error: %d: %s", |
|
181 |
errorNum, msg); |
|
182 |
||
183 |
} |
|
184 |
||
185 |
/* |
|
186 |
* Throw SocketException if no specific exception for this |
|
187 |
* error. |
|
188 |
*/ |
|
189 |
if (excP == NULL) { |
|
190 |
excP = "SocketException"; |
|
191 |
} |
|
192 |
sprintf(exc, "%s%s", JNU_JAVANETPKG, excP); |
|
193 |
JNU_ThrowByName(env, exc, fullMsg); |
|
194 |
} |
|
195 |
||
196 |
void |
|
197 |
NET_ThrowCurrent(JNIEnv *env, char *msg) |
|
198 |
{ |
|
199 |
NET_ThrowNew(env, WSAGetLastError(), msg); |
|
200 |
} |
|
201 |
||
202 |
void |
|
203 |
NET_ThrowSocketException(JNIEnv *env, char* msg) |
|
204 |
{ |
|
205 |
static jclass cls = NULL; |
|
206 |
if (cls == NULL) { |
|
207 |
cls = (*env)->FindClass(env, "java/net/SocketException"); |
|
208 |
CHECK_NULL(cls); |
|
209 |
cls = (*env)->NewGlobalRef(env, cls); |
|
210 |
CHECK_NULL(cls); |
|
211 |
} |
|
212 |
(*env)->ThrowNew(env, cls, msg); |
|
213 |
} |
|
214 |
||
215 |
void |
|
216 |
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, |
|
217 |
const char *defaultDetail) { |
|
218 |
char errmsg[255]; |
|
219 |
sprintf(errmsg, "errno: %d, error: %s\n", WSAGetLastError(), defaultDetail); |
|
220 |
JNU_ThrowByNameWithLastError(env, name, errmsg); |
|
221 |
} |
|
222 |
||
223 |
jfieldID |
|
224 |
NET_GetFileDescriptorID(JNIEnv *env) |
|
225 |
{ |
|
226 |
jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
227 |
CHECK_NULL_RETURN(cls, NULL); |
|
228 |
return (*env)->GetFieldID(env, cls, "fd", "I"); |
|
229 |
} |
|
230 |
||
231 |
jint IPv6_supported() |
|
232 |
{ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
233 |
SOCKET s = socket(AF_INET6, SOCK_STREAM, 0) ; |
9019 | 234 |
if (s == INVALID_SOCKET) { |
2 | 235 |
return JNI_FALSE; |
236 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
237 |
closesocket(s); |
2 | 238 |
|
239 |
return JNI_TRUE; |
|
240 |
} |
|
241 |
||
242 |
/* |
|
243 |
* Return the default TOS value |
|
244 |
*/ |
|
245 |
int NET_GetDefaultTOS() { |
|
246 |
static int default_tos = -1; |
|
247 |
OSVERSIONINFO ver; |
|
248 |
HKEY hKey; |
|
249 |
LONG ret; |
|
250 |
||
251 |
/* |
|
252 |
* If default ToS already determined then return it |
|
253 |
*/ |
|
254 |
if (default_tos >= 0) { |
|
255 |
return default_tos; |
|
256 |
} |
|
257 |
||
258 |
/* |
|
259 |
* Assume default is "normal service" |
|
260 |
*/ |
|
261 |
default_tos = 0; |
|
262 |
||
263 |
/* |
|
264 |
* Which OS is this? |
|
265 |
*/ |
|
266 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
267 |
GetVersionEx(&ver); |
|
268 |
||
269 |
/* |
|
270 |
* If 2000 or greater then no default ToS in registry |
|
271 |
*/ |
|
272 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) { |
|
273 |
if (ver.dwMajorVersion >= 5) { |
|
274 |
return default_tos; |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
/* |
|
279 |
* Query the registry to see if a Default ToS has been set. |
|
280 |
* Different registry entry for NT vs 95/98/ME. |
|
281 |
*/ |
|
282 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) { |
|
283 |
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
|
284 |
"SYSTEM\\CurrentControlSet\\Services\\Tcp\\Parameters", |
|
285 |
0, KEY_READ, (PHKEY)&hKey); |
|
286 |
} else { |
|
287 |
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
|
288 |
"SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP\\Parameters", |
|
289 |
0, KEY_READ, (PHKEY)&hKey); |
|
290 |
} |
|
291 |
if (ret == ERROR_SUCCESS) { |
|
292 |
DWORD dwLen; |
|
293 |
DWORD dwDefaultTOS; |
|
294 |
ULONG ulType; |
|
295 |
dwLen = sizeof(dwDefaultTOS); |
|
296 |
||
297 |
ret = RegQueryValueEx(hKey, "DefaultTOS", NULL, &ulType, |
|
298 |
(LPBYTE)&dwDefaultTOS, &dwLen); |
|
299 |
RegCloseKey(hKey); |
|
300 |
if (ret == ERROR_SUCCESS) { |
|
301 |
default_tos = (int)dwDefaultTOS; |
|
302 |
} |
|
303 |
} |
|
304 |
return default_tos; |
|
305 |
} |
|
306 |
||
307 |
/* call NET_MapSocketOptionV6 for the IPv6 fd only |
|
308 |
* and NET_MapSocketOption for the IPv4 fd |
|
309 |
*/ |
|
310 |
JNIEXPORT int JNICALL |
|
311 |
NET_MapSocketOptionV6(jint cmd, int *level, int *optname) { |
|
312 |
||
313 |
switch (cmd) { |
|
314 |
case java_net_SocketOptions_IP_MULTICAST_IF: |
|
315 |
case java_net_SocketOptions_IP_MULTICAST_IF2: |
|
316 |
*level = IPPROTO_IPV6; |
|
317 |
*optname = IPV6_MULTICAST_IF; |
|
318 |
return 0; |
|
319 |
||
320 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
321 |
*level = IPPROTO_IPV6; |
|
322 |
*optname = IPV6_MULTICAST_LOOP; |
|
323 |
return 0; |
|
324 |
} |
|
325 |
return NET_MapSocketOption (cmd, level, optname); |
|
326 |
} |
|
327 |
||
328 |
/* |
|
329 |
* Map the Java level socket option to the platform specific |
|
330 |
* level and option name. |
|
331 |
*/ |
|
332 |
||
333 |
JNIEXPORT int JNICALL |
|
334 |
NET_MapSocketOption(jint cmd, int *level, int *optname) { |
|
335 |
||
336 |
typedef struct { |
|
337 |
jint cmd; |
|
338 |
int level; |
|
339 |
int optname; |
|
340 |
} sockopts; |
|
341 |
||
342 |
static sockopts opts[] = { |
|
343 |
{ java_net_SocketOptions_TCP_NODELAY, IPPROTO_TCP, TCP_NODELAY }, |
|
344 |
{ java_net_SocketOptions_SO_OOBINLINE, SOL_SOCKET, SO_OOBINLINE }, |
|
345 |
{ java_net_SocketOptions_SO_LINGER, SOL_SOCKET, SO_LINGER }, |
|
346 |
{ java_net_SocketOptions_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF }, |
|
347 |
{ java_net_SocketOptions_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF }, |
|
348 |
{ java_net_SocketOptions_SO_KEEPALIVE, SOL_SOCKET, SO_KEEPALIVE }, |
|
349 |
{ java_net_SocketOptions_SO_REUSEADDR, SOL_SOCKET, SO_REUSEADDR }, |
|
350 |
{ java_net_SocketOptions_SO_BROADCAST, SOL_SOCKET, SO_BROADCAST }, |
|
351 |
{ java_net_SocketOptions_IP_MULTICAST_IF, IPPROTO_IP, IP_MULTICAST_IF }, |
|
352 |
{ java_net_SocketOptions_IP_MULTICAST_LOOP, IPPROTO_IP, IP_MULTICAST_LOOP }, |
|
353 |
{ java_net_SocketOptions_IP_TOS, IPPROTO_IP, IP_TOS }, |
|
354 |
||
355 |
}; |
|
356 |
||
357 |
||
358 |
int i; |
|
359 |
||
360 |
/* |
|
361 |
* Map the Java level option to the native level |
|
362 |
*/ |
|
363 |
for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) { |
|
364 |
if (cmd == opts[i].cmd) { |
|
365 |
*level = opts[i].level; |
|
366 |
*optname = opts[i].optname; |
|
367 |
return 0; |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
/* not found */ |
|
372 |
return -1; |
|
373 |
} |
|
374 |
||
375 |
||
376 |
/* |
|
377 |
* Wrapper for setsockopt dealing with Windows specific issues :- |
|
378 |
* |
|
379 |
* IP_TOS and IP_MULTICAST_LOOP can't be set on some Windows |
|
380 |
* editions. |
|
381 |
* |
|
382 |
* The value for the type-of-service (TOS) needs to be masked |
|
383 |
* to get consistent behaviour with other operating systems. |
|
384 |
*/ |
|
385 |
JNIEXPORT int JNICALL |
|
386 |
NET_SetSockOpt(int s, int level, int optname, const void *optval, |
|
387 |
int optlen) |
|
388 |
{ |
|
389 |
int rv; |
|
390 |
||
391 |
if (level == IPPROTO_IP && optname == IP_TOS) { |
|
392 |
int *tos = (int *)optval; |
|
393 |
*tos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK); |
|
394 |
} |
|
395 |
||
396 |
rv = setsockopt(s, level, optname, optval, optlen); |
|
397 |
||
398 |
if (rv == SOCKET_ERROR) { |
|
399 |
/* |
|
400 |
* IP_TOS & IP_MULTICAST_LOOP can't be set on some versions |
|
401 |
* of Windows. |
|
402 |
*/ |
|
403 |
if ((WSAGetLastError() == WSAENOPROTOOPT) && |
|
404 |
(level == IPPROTO_IP) && |
|
405 |
(optname == IP_TOS || optname == IP_MULTICAST_LOOP)) { |
|
406 |
rv = 0; |
|
407 |
} |
|
408 |
||
409 |
/* |
|
410 |
* IP_TOS can't be set on unbound UDP sockets. |
|
411 |
*/ |
|
412 |
if ((WSAGetLastError() == WSAEINVAL) && |
|
413 |
(level == IPPROTO_IP) && |
|
414 |
(optname == IP_TOS)) { |
|
415 |
rv = 0; |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
return rv; |
|
420 |
} |
|
421 |
||
422 |
/* |
|
423 |
* Wrapper for setsockopt dealing with Windows specific issues :- |
|
424 |
* |
|
425 |
* IP_TOS is not supported on some versions of Windows so |
|
426 |
* instead return the default value for the OS. |
|
427 |
*/ |
|
428 |
JNIEXPORT int JNICALL |
|
429 |
NET_GetSockOpt(int s, int level, int optname, void *optval, |
|
430 |
int *optlen) |
|
431 |
{ |
|
432 |
int rv; |
|
433 |
||
434 |
rv = getsockopt(s, level, optname, optval, optlen); |
|
435 |
||
436 |
||
437 |
/* |
|
438 |
* IPPROTO_IP/IP_TOS is not supported on some Windows |
|
439 |
* editions so return the default type-of-service |
|
440 |
* value. |
|
441 |
*/ |
|
442 |
if (rv == SOCKET_ERROR) { |
|
443 |
||
444 |
if (WSAGetLastError() == WSAENOPROTOOPT && |
|
445 |
level == IPPROTO_IP && optname == IP_TOS) { |
|
446 |
||
447 |
int *tos; |
|
448 |
tos = (int *)optval; |
|
449 |
*tos = NET_GetDefaultTOS(); |
|
450 |
||
451 |
rv = 0; |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
return rv; |
|
456 |
} |
|
457 |
||
458 |
/* |
|
459 |
* Wrapper for bind winsock call - transparent converts an |
|
460 |
* error related to binding to a port that has exclusive access |
|
461 |
* into an error indicating the port is in use (facilitates |
|
462 |
* better error reporting). |
|
463 |
*/ |
|
464 |
JNIEXPORT int JNICALL |
|
465 |
NET_Bind(int s, struct sockaddr *him, int len) |
|
466 |
{ |
|
467 |
int rv = bind(s, him, len); |
|
468 |
||
469 |
if (rv == SOCKET_ERROR) { |
|
470 |
/* |
|
471 |
* If bind fails with WSAEACCES it means that a privileged |
|
472 |
* process has done an exclusive bind (NT SP4/2000/XP only). |
|
473 |
*/ |
|
474 |
if (WSAGetLastError() == WSAEACCES) { |
|
475 |
WSASetLastError(WSAEADDRINUSE); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
return rv; |
|
480 |
} |
|
481 |
||
482 |
JNIEXPORT int JNICALL |
|
483 |
NET_SocketClose(int fd) { |
|
484 |
struct linger l; |
|
485 |
int ret; |
|
486 |
int len = sizeof (l); |
|
487 |
if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) { |
|
488 |
if (l.l_onoff == 0) { |
|
489 |
WSASendDisconnect(fd, NULL); |
|
490 |
} |
|
491 |
} |
|
492 |
ret = closesocket (fd); |
|
493 |
return ret; |
|
494 |
} |
|
495 |
||
496 |
JNIEXPORT int JNICALL |
|
497 |
NET_Timeout(int fd, long timeout) { |
|
498 |
int ret; |
|
499 |
fd_set tbl; |
|
500 |
struct timeval t; |
|
501 |
t.tv_sec = timeout / 1000; |
|
502 |
t.tv_usec = (timeout % 1000) * 1000; |
|
503 |
FD_ZERO(&tbl); |
|
504 |
FD_SET(fd, &tbl); |
|
505 |
ret = select (fd + 1, &tbl, 0, 0, &t); |
|
506 |
return ret; |
|
507 |
} |
|
508 |
||
509 |
||
510 |
/* |
|
511 |
* differs from NET_Timeout() as follows: |
|
512 |
* |
|
513 |
* If timeout = -1, it blocks forever. |
|
514 |
* |
|
515 |
* returns 1 or 2 depending if only one or both sockets |
|
516 |
* fire at same time. |
|
517 |
* |
|
518 |
* *fdret is (one of) the active fds. If both sockets |
|
519 |
* fire at same time, *fdret = fd always. |
|
520 |
*/ |
|
521 |
JNIEXPORT int JNICALL |
|
522 |
NET_Timeout2(int fd, int fd1, long timeout, int *fdret) { |
|
523 |
int ret; |
|
524 |
fd_set tbl; |
|
525 |
struct timeval t, *tP = &t; |
|
526 |
if (timeout == -1) { |
|
527 |
tP = 0; |
|
528 |
} else { |
|
529 |
t.tv_sec = timeout / 1000; |
|
530 |
t.tv_usec = (timeout % 1000) * 1000; |
|
531 |
} |
|
532 |
FD_ZERO(&tbl); |
|
533 |
FD_SET(fd, &tbl); |
|
534 |
FD_SET(fd1, &tbl); |
|
535 |
ret = select (0, &tbl, 0, 0, tP); |
|
536 |
switch (ret) { |
|
537 |
case 0: |
|
538 |
return 0; /* timeout */ |
|
539 |
case 1: |
|
540 |
if (FD_ISSET (fd, &tbl)) { |
|
541 |
*fdret= fd; |
|
542 |
} else { |
|
543 |
*fdret= fd1; |
|
544 |
} |
|
545 |
return 1; |
|
546 |
case 2: |
|
547 |
*fdret= fd; |
|
548 |
return 2; |
|
549 |
} |
|
550 |
return -1; |
|
551 |
} |
|
552 |
||
553 |
||
554 |
void dumpAddr (char *str, void *addr) { |
|
555 |
struct SOCKADDR_IN6 *a = (struct SOCKADDR_IN6 *)addr; |
|
556 |
int family = a->sin6_family; |
|
557 |
printf ("%s\n", str); |
|
558 |
if (family == AF_INET) { |
|
559 |
struct sockaddr_in *him = (struct sockaddr_in *)addr; |
|
560 |
printf ("AF_INET: port %d: %x\n", ntohs(him->sin_port), |
|
561 |
ntohl(him->sin_addr.s_addr)); |
|
562 |
} else { |
|
563 |
int i; |
|
564 |
struct in6_addr *in = &a->sin6_addr; |
|
565 |
printf ("AF_INET6 "); |
|
566 |
printf ("port %d ", ntohs (a->sin6_port)); |
|
567 |
printf ("flow %d ", a->sin6_flowinfo); |
|
568 |
printf ("addr "); |
|
569 |
for (i=0; i<7; i++) { |
|
570 |
printf ("%04x:", ntohs(in->s6_words[i])); |
|
571 |
} |
|
572 |
printf ("%04x", ntohs(in->s6_words[7])); |
|
573 |
printf (" scope %d\n", a->sin6_scope_id); |
|
574 |
} |
|
575 |
} |
|
576 |
||
577 |
/* Macro, which cleans-up the iv6bind structure, |
|
578 |
* closes the two sockets (if open), |
|
579 |
* and returns SOCKET_ERROR. Used in NET_BindV6 only. |
|
580 |
*/ |
|
581 |
||
582 |
#define CLOSE_SOCKETS_AND_RETURN { \ |
|
583 |
if (fd != -1) { \ |
|
584 |
closesocket (fd); \ |
|
585 |
fd = -1; \ |
|
586 |
} \ |
|
587 |
if (ofd != -1) { \ |
|
588 |
closesocket (ofd); \ |
|
589 |
ofd = -1; \ |
|
590 |
} \ |
|
591 |
if (close_fd != -1) { \ |
|
592 |
closesocket (close_fd); \ |
|
593 |
close_fd = -1; \ |
|
594 |
} \ |
|
595 |
if (close_ofd != -1) { \ |
|
596 |
closesocket (close_ofd); \ |
|
597 |
close_ofd = -1; \ |
|
598 |
} \ |
|
599 |
b->ipv4_fd = b->ipv6_fd = -1; \ |
|
600 |
return SOCKET_ERROR; \ |
|
601 |
} |
|
602 |
||
603 |
/* |
|
604 |
* if ipv6 is available, call NET_BindV6 to bind to the required address/port. |
|
605 |
* Because the same port number may need to be reserved in both v4 and v6 space, |
|
606 |
* this may require socket(s) to be re-opened. Therefore, all of this information |
|
607 |
* is passed in and returned through the ipv6bind structure. |
|
608 |
* |
|
609 |
* If the request is to bind to a specific address, then this (by definition) means |
|
610 |
* only bind in either v4 or v6, and this is just the same as normal. ie. a single |
|
611 |
* call to bind() will suffice. The other socket is closed in this case. |
|
612 |
* |
|
613 |
* The more complicated case is when the requested address is ::0 or 0.0.0.0. |
|
614 |
* |
|
615 |
* Two further cases: |
|
616 |
* 2. If the reqeusted port is 0 (ie. any port) then we try to bind in v4 space |
|
617 |
* first with a wild-card port argument. We then try to bind in v6 space |
|
618 |
* using the returned port number. If this fails, we repeat the process |
|
619 |
* until a free port common to both spaces becomes available. |
|
620 |
* |
|
621 |
* 3. If the requested port is a specific port, then we just try to get that |
|
622 |
* port in both spaces, and if it is not free in both, then the bind fails. |
|
623 |
* |
|
624 |
* On failure, sockets are closed and an error returned with CLOSE_SOCKETS_AND_RETURN |
|
625 |
*/ |
|
626 |
||
627 |
JNIEXPORT int JNICALL |
|
628 |
NET_BindV6(struct ipv6bind* b) { |
|
629 |
int fd=-1, ofd=-1, rv, len; |
|
630 |
/* need to defer close until new sockets created */ |
|
631 |
int close_fd=-1, close_ofd=-1; |
|
632 |
SOCKETADDRESS oaddr; /* other address to bind */ |
|
633 |
int family = b->addr->him.sa_family; |
|
634 |
int ofamily; |
|
635 |
u_short port; /* requested port parameter */ |
|
636 |
u_short bound_port; |
|
637 |
||
638 |
if (family == AF_INET && (b->addr->him4.sin_addr.s_addr != INADDR_ANY)) { |
|
639 |
/* bind to v4 only */ |
|
640 |
int ret; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
641 |
ret = NET_Bind ((int)b->ipv4_fd, (struct sockaddr *)b->addr, |
2 | 642 |
sizeof (struct sockaddr_in)); |
643 |
if (ret == SOCKET_ERROR) { |
|
644 |
CLOSE_SOCKETS_AND_RETURN; |
|
645 |
} |
|
646 |
closesocket (b->ipv6_fd); |
|
647 |
b->ipv6_fd = -1; |
|
648 |
return 0; |
|
649 |
} |
|
650 |
if (family == AF_INET6 && (!IN6_IS_ADDR_ANY(&b->addr->him6.sin6_addr))) { |
|
651 |
/* bind to v6 only */ |
|
652 |
int ret; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
653 |
ret = NET_Bind ((int)b->ipv6_fd, (struct sockaddr *)b->addr, |
2 | 654 |
sizeof (struct SOCKADDR_IN6)); |
655 |
if (ret == SOCKET_ERROR) { |
|
656 |
CLOSE_SOCKETS_AND_RETURN; |
|
657 |
} |
|
658 |
closesocket (b->ipv4_fd); |
|
659 |
b->ipv4_fd = -1; |
|
660 |
return 0; |
|
661 |
} |
|
662 |
||
663 |
/* We need to bind on both stacks, with the same port number */ |
|
664 |
||
665 |
memset (&oaddr, 0, sizeof(oaddr)); |
|
666 |
if (family == AF_INET) { |
|
667 |
ofamily = AF_INET6; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
668 |
fd = (int)b->ipv4_fd; |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
669 |
ofd = (int)b->ipv6_fd; |
2 | 670 |
port = (u_short)GET_PORT (b->addr); |
671 |
IN6ADDR_SETANY (&oaddr.him6); |
|
672 |
oaddr.him6.sin6_port = port; |
|
673 |
} else { |
|
674 |
ofamily = AF_INET; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
675 |
ofd = (int)b->ipv4_fd; |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
676 |
fd = (int)b->ipv6_fd; |
2 | 677 |
port = (u_short)GET_PORT (b->addr); |
678 |
oaddr.him4.sin_family = AF_INET; |
|
679 |
oaddr.him4.sin_port = port; |
|
680 |
oaddr.him4.sin_addr.s_addr = INADDR_ANY; |
|
681 |
} |
|
682 |
||
683 |
rv = NET_Bind (fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr)); |
|
684 |
if (rv == SOCKET_ERROR) { |
|
685 |
CLOSE_SOCKETS_AND_RETURN; |
|
686 |
} |
|
687 |
||
688 |
/* get the port and set it in the other address */ |
|
689 |
len = SOCKETADDRESS_LEN(b->addr); |
|
690 |
if (getsockname(fd, (struct sockaddr *)b->addr, &len) == -1) { |
|
691 |
CLOSE_SOCKETS_AND_RETURN; |
|
692 |
} |
|
693 |
bound_port = GET_PORT (b->addr); |
|
694 |
SET_PORT (&oaddr, bound_port); |
|
695 |
if ((rv=NET_Bind (ofd, (struct sockaddr *) &oaddr, |
|
696 |
SOCKETADDRESS_LEN (&oaddr))) == SOCKET_ERROR) { |
|
697 |
int retries; |
|
698 |
int sotype, arglen=sizeof(sotype); |
|
699 |
||
700 |
/* no retries unless, the request was for any free port */ |
|
701 |
||
702 |
if (port != 0) { |
|
703 |
CLOSE_SOCKETS_AND_RETURN; |
|
704 |
} |
|
705 |
||
706 |
getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen); |
|
707 |
||
708 |
#define SOCK_RETRIES 50 |
|
709 |
/* 50 is an arbitrary limit, just to ensure that this |
|
710 |
* cannot be an endless loop. Would expect socket creation to |
|
711 |
* succeed sooner. |
|
712 |
*/ |
|
713 |
for (retries = 0; retries < SOCK_RETRIES; retries ++) { |
|
714 |
int len; |
|
715 |
close_fd = fd; fd = -1; |
|
716 |
close_ofd = ofd; ofd = -1; |
|
717 |
b->ipv4_fd = SOCKET_ERROR; |
|
718 |
b->ipv6_fd = SOCKET_ERROR; |
|
719 |
||
720 |
/* create two new sockets */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
721 |
fd = (int)socket (family, sotype, 0); |
2 | 722 |
if (fd == SOCKET_ERROR) { |
723 |
CLOSE_SOCKETS_AND_RETURN; |
|
724 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
725 |
ofd = (int)socket (ofamily, sotype, 0); |
2 | 726 |
if (ofd == SOCKET_ERROR) { |
727 |
CLOSE_SOCKETS_AND_RETURN; |
|
728 |
} |
|
729 |
||
730 |
/* bind random port on first socket */ |
|
731 |
SET_PORT (&oaddr, 0); |
|
732 |
rv = NET_Bind (ofd, (struct sockaddr *)&oaddr, SOCKETADDRESS_LEN(&oaddr)); |
|
733 |
if (rv == SOCKET_ERROR) { |
|
734 |
CLOSE_SOCKETS_AND_RETURN; |
|
735 |
} |
|
736 |
/* close the original pair of sockets before continuing */ |
|
737 |
closesocket (close_fd); |
|
738 |
closesocket (close_ofd); |
|
739 |
close_fd = close_ofd = -1; |
|
740 |
||
741 |
/* bind new port on second socket */ |
|
742 |
len = SOCKETADDRESS_LEN(&oaddr); |
|
743 |
if (getsockname(ofd, (struct sockaddr *)&oaddr, &len) == -1) { |
|
744 |
CLOSE_SOCKETS_AND_RETURN; |
|
745 |
} |
|
746 |
bound_port = GET_PORT (&oaddr); |
|
747 |
SET_PORT (b->addr, bound_port); |
|
748 |
rv = NET_Bind (fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr)); |
|
749 |
||
750 |
if (rv != SOCKET_ERROR) { |
|
751 |
if (family == AF_INET) { |
|
752 |
b->ipv4_fd = fd; |
|
753 |
b->ipv6_fd = ofd; |
|
754 |
} else { |
|
755 |
b->ipv4_fd = ofd; |
|
756 |
b->ipv6_fd = fd; |
|
757 |
} |
|
758 |
return 0; |
|
759 |
} |
|
760 |
} |
|
761 |
CLOSE_SOCKETS_AND_RETURN; |
|
762 |
} |
|
763 |
return 0; |
|
764 |
} |
|
765 |
||
766 |
/* |
|
767 |
* Determine the default interface for an IPv6 address. |
|
768 |
* |
|
769 |
* Returns :- |
|
770 |
* 0 if error |
|
771 |
* > 0 interface index to use |
|
772 |
*/ |
|
773 |
jint getDefaultIPv6Interface(JNIEnv *env, struct SOCKADDR_IN6 *target_addr) |
|
774 |
{ |
|
775 |
int ret; |
|
776 |
DWORD b; |
|
777 |
struct sockaddr_in6 route; |
|
778 |
SOCKET fd = socket(AF_INET6, SOCK_STREAM, 0); |
|
9019 | 779 |
if (fd == INVALID_SOCKET) { |
2 | 780 |
return 0; |
781 |
} |
|
782 |
||
783 |
ret = WSAIoctl(fd, SIO_ROUTING_INTERFACE_QUERY, |
|
784 |
(void *)target_addr, sizeof(struct sockaddr_in6), |
|
785 |
(void *)&route, sizeof(struct sockaddr_in6), |
|
786 |
&b, 0, 0); |
|
9019 | 787 |
if (ret == SOCKET_ERROR) { |
2 | 788 |
// error |
789 |
closesocket(fd); |
|
790 |
return 0; |
|
791 |
} else { |
|
792 |
closesocket(fd); |
|
793 |
return route.sin6_scope_id; |
|
794 |
} |
|
795 |
} |
|
796 |
||
797 |
/* If address types is IPv6, then IPv6 must be available. Otherwise |
|
798 |
* no address can be generated. In the case of an IPv4 Inetaddress this |
|
799 |
* method will return an IPv4 mapped address where IPv6 is available and |
|
800 |
* v4MappedAddress is TRUE. Otherwise it will return a sockaddr_in |
|
801 |
* structure for an IPv4 InetAddress. |
|
802 |
*/ |
|
803 |
JNIEXPORT int JNICALL |
|
804 |
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, |
|
805 |
int *len, jboolean v4MappedAddress) { |
|
806 |
jint family, iafam; |
|
807 |
iafam = (*env)->GetIntField(env, iaObj, ia_familyID); |
|
808 |
family = (iafam == IPv4)? AF_INET : AF_INET6; |
|
809 |
if (ipv6_available() && !(family == AF_INET && v4MappedAddress == JNI_FALSE)) { |
|
810 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
811 |
jbyteArray ipaddress; |
|
812 |
jbyte caddr[16]; |
|
813 |
jint address, scopeid = 0; |
|
814 |
jint cached_scope_id = 0; |
|
815 |
||
816 |
if (family == AF_INET) { /* will convert to IPv4-mapped address */ |
|
817 |
memset((char *) caddr, 0, 16); |
|
818 |
address = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
819 |
if (address == INADDR_ANY) { |
|
820 |
/* we would always prefer IPv6 wildcard address |
|
821 |
caddr[10] = 0xff; |
|
822 |
caddr[11] = 0xff; */ |
|
823 |
} else { |
|
824 |
caddr[10] = 0xff; |
|
825 |
caddr[11] = 0xff; |
|
826 |
caddr[12] = ((address >> 24) & 0xff); |
|
827 |
caddr[13] = ((address >> 16) & 0xff); |
|
828 |
caddr[14] = ((address >> 8) & 0xff); |
|
829 |
caddr[15] = (address & 0xff); |
|
830 |
} |
|
831 |
} else { |
|
832 |
ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID); |
|
833 |
scopeid = (jint)(*env)->GetIntField(env, iaObj, ia6_scopeidID); |
|
834 |
cached_scope_id = (jint)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID); |
|
835 |
(*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr); |
|
836 |
} |
|
837 |
||
838 |
memset((char *)him6, 0, sizeof(struct SOCKADDR_IN6)); |
|
839 |
him6->sin6_port = (u_short) htons((u_short)port); |
|
840 |
memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) ); |
|
841 |
him6->sin6_family = AF_INET6; |
|
842 |
if ((family == AF_INET6) && IN6_IS_ADDR_LINKLOCAL( &(him6->sin6_addr) ) |
|
843 |
&& (!scopeid && !cached_scope_id)) { |
|
844 |
cached_scope_id = getDefaultIPv6Interface(env, him6); |
|
845 |
(*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id); |
|
846 |
} |
|
847 |
him6->sin6_scope_id = scopeid != 0 ? scopeid : cached_scope_id; |
|
848 |
*len = sizeof(struct SOCKADDR_IN6) ; |
|
849 |
} else { |
|
850 |
struct sockaddr_in *him4 = (struct sockaddr_in*)him; |
|
851 |
jint address; |
|
852 |
if (family != AF_INET) { |
|
853 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable"); |
|
854 |
return -1; |
|
855 |
} |
|
856 |
memset((char *) him4, 0, sizeof(struct sockaddr_in)); |
|
857 |
address = (int)(*env)->GetIntField(env, iaObj, ia_addressID); |
|
858 |
him4->sin_port = htons((short) port); |
|
859 |
him4->sin_addr.s_addr = (u_long) htonl(address); |
|
860 |
him4->sin_family = AF_INET; |
|
861 |
*len = sizeof(struct sockaddr_in); |
|
862 |
} |
|
863 |
return 0; |
|
864 |
} |
|
865 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
1090
diff
changeset
|
866 |
JNIEXPORT jint JNICALL |
2 | 867 |
NET_GetPortFromSockaddr(struct sockaddr *him) { |
868 |
if (him->sa_family == AF_INET6) { |
|
869 |
return ntohs(((struct sockaddr_in6*)him)->sin6_port); |
|
870 |
} else { |
|
871 |
return ntohs(((struct sockaddr_in*)him)->sin_port); |
|
872 |
} |
|
873 |
} |
|
874 |
||
875 |
int |
|
876 |
NET_IsIPv4Mapped(jbyte* caddr) { |
|
877 |
int i; |
|
878 |
for (i = 0; i < 10; i++) { |
|
879 |
if (caddr[i] != 0x00) { |
|
880 |
return 0; /* false */ |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) { |
|
885 |
return 1; /* true */ |
|
886 |
} |
|
887 |
return 0; /* false */ |
|
888 |
} |
|
889 |
||
890 |
int |
|
891 |
NET_IPv4MappedToIPv4(jbyte* caddr) { |
|
892 |
return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8) |
|
893 |
| (caddr[15] & 0xff); |
|
894 |
} |
|
895 |
||
896 |
int |
|
897 |
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) { |
|
898 |
int i; |
|
899 |
for (i = 0; i < 16; i++) { |
|
900 |
if (caddr1[i] != caddr2[i]) { |
|
901 |
return 0; /* false */ |
|
902 |
} |
|
903 |
} |
|
904 |
return 1; |
|
905 |
} |
|
906 |
||
907 |
int getScopeID (struct sockaddr *him) { |
|
908 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
909 |
return him6->sin6_scope_id; |
|
910 |
} |
|
911 |
||
912 |
int cmpScopeID (unsigned int scope, struct sockaddr *him) { |
|
913 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
914 |
return him6->sin6_scope_id == scope; |
|
915 |
} |
|
916 |
||
917 |
/** |
|
918 |
* Wrapper for select/poll with timeout on a single file descriptor. |
|
919 |
* |
|
920 |
* flags (defined in net_util_md.h can be any combination of |
|
921 |
* NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT. |
|
922 |
* |
|
923 |
* The function will return when either the socket is ready for one |
|
924 |
* of the specified operation or the timeout expired. |
|
925 |
* |
|
926 |
* It returns the time left from the timeout, or -1 if it expired. |
|
927 |
*/ |
|
928 |
||
929 |
jint |
|
930 |
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout) |
|
931 |
{ |
|
932 |
jlong prevTime = JVM_CurrentTimeMillis(env, 0); |
|
933 |
jint read_rv; |
|
934 |
||
935 |
while (1) { |
|
936 |
jlong newTime; |
|
937 |
fd_set rd, wr, ex; |
|
938 |
struct timeval t; |
|
939 |
||
940 |
t.tv_sec = timeout / 1000; |
|
941 |
t.tv_usec = (timeout % 1000) * 1000; |
|
942 |
||
943 |
FD_ZERO(&rd); |
|
944 |
FD_ZERO(&wr); |
|
945 |
FD_ZERO(&ex); |
|
946 |
if (flags & NET_WAIT_READ) { |
|
947 |
FD_SET(fd, &rd); |
|
948 |
} |
|
949 |
if (flags & NET_WAIT_WRITE) { |
|
950 |
FD_SET(fd, &wr); |
|
951 |
} |
|
952 |
if (flags & NET_WAIT_CONNECT) { |
|
953 |
FD_SET(fd, &wr); |
|
954 |
FD_SET(fd, &ex); |
|
955 |
} |
|
956 |
||
957 |
errno = 0; |
|
958 |
read_rv = select(fd+1, &rd, &wr, &ex, &t); |
|
959 |
||
960 |
newTime = JVM_CurrentTimeMillis(env, 0); |
|
910 | 961 |
timeout -= (jint)(newTime - prevTime); |
2 | 962 |
if (timeout <= 0) { |
963 |
return read_rv > 0 ? 0 : -1; |
|
964 |
} |
|
965 |
newTime = prevTime; |
|
966 |
||
967 |
if (read_rv > 0) { |
|
968 |
break; |
|
969 |
} |
|
970 |
||
971 |
||
972 |
} /* while */ |
|
973 |
||
974 |
return timeout; |
|
975 |
} |
|
976 |
||
977 |
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
|
978 |
SOCKET sock; |
2 | 979 |
sock = socket (domain, type, protocol); |
980 |
if (sock != INVALID_SOCKET) { |
|
981 |
SetHandleInformation((HANDLE)(uintptr_t)sock, HANDLE_FLAG_INHERIT, FALSE); |
|
982 |
} |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
983 |
return (int)sock; |
2 | 984 |
} |