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