1 /* 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 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 * 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. 24 */ 25 26 #include <errno.h> 27 #include <sys/time.h> 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <netinet/in.h> 31 #include <netdb.h> 32 #include <string.h> 33 #include <strings.h> 34 #include <stdlib.h> 35 #include <ctype.h> 36 #ifdef MACOSX 37 #include <ifaddrs.h> 38 #include <net/if.h> 39 #include <unistd.h> /* gethostname */ 40 #endif 41 42 #include "jvm.h" 43 #include "jni_util.h" 44 #include "net_util.h" 45 #ifndef IPV6_DEFS_H 46 #include <netinet/icmp6.h> 47 #endif 48 49 #include "java_net_Inet4AddressImpl.h" 50 #include "java_net_Inet6AddressImpl.h" 51 52 /* the initial size of our hostent buffers */ 53 #ifndef NI_MAXHOST 54 #define NI_MAXHOST 1025 55 #endif 56 57 58 /************************************************************************ 59 * Inet6AddressImpl 60 */ 61 62 /* 63 * Class: java_net_Inet6AddressImpl 64 * Method: getLocalHostName 65 * Signature: ()Ljava/lang/String; 66 */ 67 JNIEXPORT jstring JNICALL 68 Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { 69 char hostname[NI_MAXHOST+1]; 70 71 hostname[0] = '\0'; 72 if (JVM_GetHostName(hostname, sizeof(hostname))) { 73 /* Something went wrong, maybe networking is not setup? */ 74 strcpy(hostname, "localhost"); 75 } else { 76 // ensure null-terminated 77 hostname[NI_MAXHOST] = '\0'; 78 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 79 /* On Linux/FreeBSD gethostname() says "host.domain.sun.com". On 80 * Solaris gethostname() says "host", so extra work is needed. 81 */ 82 #else 83 /* Solaris doesn't want to give us a fully qualified domain name. 84 * We do a reverse lookup to try and get one. This works 85 * if DNS occurs before NIS in /etc/resolv.conf, but fails 86 * if NIS comes first (it still gets only a partial name). 87 * We use thread-safe system calls. 88 */ 89 #ifdef AF_INET6 90 struct addrinfo hints, *res; 91 int error; 92 93 memset(&hints, 0, sizeof(hints)); 94 hints.ai_flags = AI_CANONNAME; 95 hints.ai_family = AF_UNSPEC; 96 97 error = getaddrinfo(hostname, NULL, &hints, &res); 98 99 if (error == 0) { 100 /* host is known to name service */ 101 error = getnameinfo(res->ai_addr, 102 res->ai_addrlen, 103 hostname, 104 NI_MAXHOST, 105 NULL, 106 0, 107 NI_NAMEREQD); 108 109 /* if getnameinfo fails hostname is still the value 110 from gethostname */ 111 112 freeaddrinfo(res); 113 } 114 #endif /* AF_INET6 */ 115 #endif /* __linux__ || _ALLBSD_SOURCE */ 116 } 117 return (*env)->NewStringUTF(env, hostname); 118 } 119 120 static jclass ni_iacls; 121 static jclass ni_ia4cls; 122 static jclass ni_ia6cls; 123 static jmethodID ni_ia4ctrID; 124 static jmethodID ni_ia6ctrID; 125 static jboolean preferIPv6Address; 126 127 static void initializeInetClasses(JNIEnv *env) 128 { 129 jfieldID ni_preferIPv6AddressID; 130 static int initialized = 0; 131 if (!initialized) { 132 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress"); 133 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls); 134 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); 135 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); 136 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address"); 137 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls); 138 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); 139 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V"); 140 ni_preferIPv6AddressID = 141 (*env)->GetStaticFieldID(env, ni_iacls, "preferIPv6Address", "Z"); 142 preferIPv6Address = 143 (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID); 144 initialized = 1; 145 } 146 } 147 148 #ifdef MACOSX 149 /* also called from Inet4AddressImpl.c */ 150 __private_extern__ jobjectArray 151 lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6) 152 { 153 jobjectArray result = NULL; 154 char myhostname[NI_MAXHOST+1]; 155 struct ifaddrs *ifa = NULL; 156 int familyOrder = 0; 157 int count = 0, i, j; 158 int addrs4 = 0, addrs6 = 0, numV4Loopbacks = 0, numV6Loopbacks = 0; 159 jboolean includeLoopback = JNI_FALSE; 160 jobject name; 161 162 // Make sure static variables we need are set. 163 initializeInetClasses(env); 164 165 /* If the requested name matches this host's hostname, return IP addresses 166 * from all attached interfaces. (#2844683 et al) This prevents undesired 167 * PPP dialup, but may return addresses that don't actually correspond to 168 * the name (if the name actually matches something in DNS etc. 169 */ 170 myhostname[0] = '\0'; 171 if (JVM_GetHostName(myhostname, NI_MAXHOST)) { 172 /* Something went wrong, maybe networking is not setup? */ 173 return NULL; 174 } 175 myhostname[NI_MAXHOST] = '\0'; 176 177 if (strcmp(myhostname, hostname) != 0) { 178 // Non-self lookup 179 return NULL; 180 } 181 182 if (getifaddrs(&ifa) != 0) { 183 NET_ThrowNew(env, errno, "Can't get local interface addresses"); 184 return NULL; 185 } 186 187 name = (*env)->NewStringUTF(env, hostname); 188 189 /* Iterate over the interfaces, and total up the number of IPv4 and IPv6 190 * addresses we have. Also keep a count of loopback addresses. We need to 191 * exclude them in the normal case, but return them if we don't get an IP 192 * address. 193 */ 194 struct ifaddrs *iter = ifa; 195 while (iter) { 196 int family = iter->ifa_addr->sa_family; 197 if (iter->ifa_name[0] != '\0' && iter->ifa_addr) 198 { 199 jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK; 200 if (family == AF_INET) { 201 addrs4++; 202 if (isLoopback) numV4Loopbacks++; 203 } else if (family == AF_INET6 && includeV6) { 204 addrs6++; 205 if (isLoopback) numV6Loopbacks++; 206 } else { 207 /* We don't care e.g. AF_LINK */ 208 } 209 } 210 iter = iter->ifa_next; 211 } 212 213 if (addrs4 == numV4Loopbacks && addrs6 == numV6Loopbacks) { 214 // We don't have a real IP address, just loopback. We need to include 215 // loopback in our results. 216 includeLoopback = JNI_TRUE; 217 } 218 219 /* Create and fill the Java array. */ 220 int arraySize = addrs4 + addrs6 - 221 (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks)); 222 result = (*env)->NewObjectArray(env, arraySize, ni_iacls, NULL); 223 if (!result) goto done; 224 225 if (preferIPv6Address) { 226 i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks); 227 j = 0; 228 } else { 229 i = 0; 230 j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks); 231 } 232 233 // Now loop around the ifaddrs 234 iter = ifa; 235 while (iter != NULL) { 236 jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK; 237 int family = iter->ifa_addr->sa_family; 238 239 if (iter->ifa_name[0] != '\0' && iter->ifa_addr 240 && (family == AF_INET || (family == AF_INET6 && includeV6)) 241 && (!isLoopback || includeLoopback)) 242 { 243 int port; 244 int index = (family == AF_INET) ? i++ : j++; 245 jobject o = NET_SockaddrToInetAddress(env, iter->ifa_addr, &port); 246 if (!o) { 247 freeifaddrs(ifa); 248 if (!(*env)->ExceptionCheck(env)) 249 JNU_ThrowOutOfMemoryError(env, "Object allocation failed"); 250 return NULL; 251 } 252 setInetAddress_hostName(env, o, name); 253 (*env)->SetObjectArrayElement(env, result, index, o); 254 (*env)->DeleteLocalRef(env, o); 255 } 256 iter = iter->ifa_next; 257 } 258 259 done: 260 freeifaddrs(ifa); 261 262 return result; 263 } 264 #endif 265 266 /* 267 * Find an internet address for a given hostname. Note that this 268 * code only works for addresses of type INET. The translation 269 * of %d.%d.%d.%d to an address (int) occurs in java now, so the 270 * String "host" shouldn't *ever* be a %d.%d.%d.%d string 271 * 272 * Class: java_net_Inet6AddressImpl 273 * Method: lookupAllHostAddr 274 * Signature: (Ljava/lang/String;)[[B 275 */ 276 277 JNIEXPORT jobjectArray JNICALL 278 Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, 279 jstring host) { 280 const char *hostname; 281 jobjectArray ret = 0; 282 int retLen = 0; 283 284 int error=0; 285 #ifdef AF_INET6 286 struct addrinfo hints, *res, *resNew = NULL; 287 #endif /* AF_INET6 */ 288 289 initializeInetClasses(env); 290 291 if (IS_NULL(host)) { 292 JNU_ThrowNullPointerException(env, "host is null"); 293 return 0; 294 } 295 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); 296 CHECK_NULL_RETURN(hostname, NULL); 297 298 #ifdef MACOSX 299 /* 300 * If we're looking up the local machine, attempt to get the address 301 * from getifaddrs. This ensures we get an IPv6 address for the local 302 * machine. 303 */ 304 ret = lookupIfLocalhost(env, hostname, JNI_TRUE); 305 if (ret != NULL || (*env)->ExceptionCheck(env)) { 306 JNU_ReleaseStringPlatformChars(env, host, hostname); 307 return ret; 308 } 309 #endif 310 311 #ifdef AF_INET6 312 /* Try once, with our static buffer. */ 313 memset(&hints, 0, sizeof(hints)); 314 hints.ai_flags = AI_CANONNAME; 315 hints.ai_family = AF_UNSPEC; 316 317 #ifdef __solaris__ 318 /* 319 * Workaround for Solaris bug 4160367 - if a hostname contains a 320 * white space then 0.0.0.0 is returned 321 */ 322 if (isspace((unsigned char)hostname[0])) { 323 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 324 hostname); 325 JNU_ReleaseStringPlatformChars(env, host, hostname); 326 return NULL; 327 } 328 #endif 329 330 error = getaddrinfo(hostname, NULL, &hints, &res); 331 332 if (error) { 333 /* report error */ 334 ThrowUnknownHostExceptionWithGaiError(env, hostname, error); 335 JNU_ReleaseStringPlatformChars(env, host, hostname); 336 return NULL; 337 } else { 338 int i = 0; 339 int inetCount = 0, inet6Count = 0, inetIndex, inet6Index; 340 struct addrinfo *itr, *last = NULL, *iterator = res; 341 while (iterator != NULL) { 342 int skip = 0; 343 itr = resNew; 344 while (itr != NULL) { 345 if (iterator->ai_family == itr->ai_family && 346 iterator->ai_addrlen == itr->ai_addrlen) { 347 if (itr->ai_family == AF_INET) { /* AF_INET */ 348 struct sockaddr_in *addr1, *addr2; 349 addr1 = (struct sockaddr_in *)iterator->ai_addr; 350 addr2 = (struct sockaddr_in *)itr->ai_addr; 351 if (addr1->sin_addr.s_addr == 352 addr2->sin_addr.s_addr) { 353 skip = 1; 354 break; 355 } 356 } else { 357 int t; 358 struct sockaddr_in6 *addr1, *addr2; 359 addr1 = (struct sockaddr_in6 *)iterator->ai_addr; 360 addr2 = (struct sockaddr_in6 *)itr->ai_addr; 361 362 for (t = 0; t < 16; t++) { 363 if (addr1->sin6_addr.s6_addr[t] != 364 addr2->sin6_addr.s6_addr[t]) { 365 break; 366 } 367 } 368 if (t < 16) { 369 itr = itr->ai_next; 370 continue; 371 } else { 372 skip = 1; 373 break; 374 } 375 } 376 } else if (iterator->ai_family != AF_INET && 377 iterator->ai_family != AF_INET6) { 378 /* we can't handle other family types */ 379 skip = 1; 380 break; 381 } 382 itr = itr->ai_next; 383 } 384 385 if (!skip) { 386 struct addrinfo *next 387 = (struct addrinfo*) malloc(sizeof(struct addrinfo)); 388 if (!next) { 389 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); 390 ret = NULL; 391 goto cleanupAndReturn; 392 } 393 memcpy(next, iterator, sizeof(struct addrinfo)); 394 next->ai_next = NULL; 395 if (resNew == NULL) { 396 resNew = next; 397 } else { 398 last->ai_next = next; 399 } 400 last = next; 401 i++; 402 if (iterator->ai_family == AF_INET) { 403 inetCount ++; 404 } else if (iterator->ai_family == AF_INET6) { 405 inet6Count ++; 406 } 407 } 408 iterator = iterator->ai_next; 409 } 410 retLen = i; 411 iterator = resNew; 412 413 ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL); 414 415 if (IS_NULL(ret)) { 416 /* we may have memory to free at the end of this */ 417 goto cleanupAndReturn; 418 } 419 420 if (preferIPv6Address) { 421 /* AF_INET addresses will be offset by inet6Count */ 422 inetIndex = inet6Count; 423 inet6Index = 0; 424 } else { 425 /* AF_INET6 addresses will be offset by inetCount */ 426 inetIndex = 0; 427 inet6Index = inetCount; 428 } 429 430 while (iterator != NULL) { 431 int ret1; 432 if (iterator->ai_family == AF_INET) { 433 jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID); 434 if (IS_NULL(iaObj)) { 435 ret = NULL; 436 goto cleanupAndReturn; 437 } 438 setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); 439 setInetAddress_hostName(env, iaObj, host); 440 (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj); 441 inetIndex++; 442 } else if (iterator->ai_family == AF_INET6) { 443 jint scope = 0; 444 445 jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID); 446 if (IS_NULL(iaObj)) { 447 ret = NULL; 448 goto cleanupAndReturn; 449 } 450 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr)); 451 if (!ret1) { 452 ret = NULL; 453 goto cleanupAndReturn; 454 } 455 456 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id; 457 if (scope != 0) { /* zero is default value, no need to set */ 458 setInet6Address_scopeid(env, iaObj, scope); 459 } 460 setInetAddress_hostName(env, iaObj, host); 461 (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj); 462 inet6Index++; 463 } 464 iterator = iterator->ai_next; 465 } 466 } 467 468 cleanupAndReturn: 469 { 470 struct addrinfo *iterator, *tmp; 471 iterator = resNew; 472 while (iterator != NULL) { 473 tmp = iterator; 474 iterator = iterator->ai_next; 475 free(tmp); 476 } 477 JNU_ReleaseStringPlatformChars(env, host, hostname); 478 } 479 480 freeaddrinfo(res); 481 #endif /* AF_INET6 */ 482 483 return ret; 484 } 485 486 /* 487 * Class: java_net_Inet6AddressImpl 488 * Method: getHostByAddr 489 * Signature: (I)Ljava/lang/String; 490 */ 491 JNIEXPORT jstring JNICALL 492 Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this, 493 jbyteArray addrArray) { 494 495 jstring ret = NULL; 496 497 #ifdef AF_INET6 498 char host[NI_MAXHOST+1]; 499 int error = 0; 500 int len = 0; 501 jbyte caddr[16]; 502 503 struct sockaddr_in him4; 504 struct sockaddr_in6 him6; 505 struct sockaddr *sa; 506 507 /* 508 * For IPv4 addresses construct a sockaddr_in structure. 509 */ 510 if ((*env)->GetArrayLength(env, addrArray) == 4) { 511 jint addr; 512 (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); 513 addr = ((caddr[0]<<24) & 0xff000000); 514 addr |= ((caddr[1] <<16) & 0xff0000); 515 addr |= ((caddr[2] <<8) & 0xff00); 516 addr |= (caddr[3] & 0xff); 517 memset((void *) &him4, 0, sizeof(him4)); 518 him4.sin_addr.s_addr = (uint32_t) htonl(addr); 519 him4.sin_family = AF_INET; 520 sa = (struct sockaddr *) &him4; 521 len = sizeof(him4); 522 } else { 523 /* 524 * For IPv6 address construct a sockaddr_in6 structure. 525 */ 526 (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); 527 memset((void *) &him6, 0, sizeof(him6)); 528 memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) ); 529 him6.sin6_family = AF_INET6; 530 sa = (struct sockaddr *) &him6 ; 531 len = sizeof(him6) ; 532 } 533 534 error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, 535 NI_NAMEREQD); 536 537 if (!error) { 538 ret = (*env)->NewStringUTF(env, host); 539 } 540 #endif /* AF_INET6 */ 541 542 if (ret == NULL) { 543 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL); 544 } 545 546 return ret; 547 } 548 549 #define SET_NONBLOCKING(fd) { \ 550 int flags = fcntl(fd, F_GETFL); \ 551 flags |= O_NONBLOCK; \ 552 fcntl(fd, F_SETFL, flags); \ 553 } 554 555 #ifdef AF_INET6 556 static jboolean 557 ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout, 558 struct sockaddr_in6* netif, jint ttl) { 559 jint size; 560 jint n; 561 socklen_t len; 562 char sendbuf[1500]; 563 unsigned char recvbuf[1500]; 564 struct icmp6_hdr *icmp6; 565 struct sockaddr_in6 sa_recv; 566 jbyte *caddr, *recv_caddr; 567 jchar pid; 568 jint tmout2, seq = 1; 569 struct timeval tv; 570 size_t plen; 571 572 #ifdef __linux__ 573 { 574 int csum_offset; 575 /** 576 * For some strange reason, the linux kernel won't calculate the 577 * checksum of ICMPv6 packets unless you set this socket option 578 */ 579 csum_offset = 2; 580 setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int)); 581 } 582 #endif 583 584 caddr = (jbyte *)&(him->sin6_addr); 585 586 /* icmp_id is a 16 bit data type, therefore down cast the pid */ 587 pid = (jchar)getpid(); 588 size = 60*1024; 589 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); 590 if (ttl > 0) { 591 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); 592 } 593 if (netif != NULL) { 594 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) { 595 NET_ThrowNew(env, errno, "Can't bind socket"); 596 close(fd); 597 return JNI_FALSE; 598 } 599 } 600 SET_NONBLOCKING(fd); 601 602 do { 603 icmp6 = (struct icmp6_hdr *) sendbuf; 604 icmp6->icmp6_type = ICMP6_ECHO_REQUEST; 605 icmp6->icmp6_code = 0; 606 /* let's tag the ECHO packet with our pid so we can identify it */ 607 icmp6->icmp6_id = htons(pid); 608 icmp6->icmp6_seq = htons(seq); 609 seq++; 610 icmp6->icmp6_cksum = 0; 611 gettimeofday(&tv, NULL); 612 memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv)); 613 plen = sizeof(struct icmp6_hdr) + sizeof(tv); 614 n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6)); 615 if (n < 0 && errno != EINPROGRESS) { 616 #ifdef __linux__ 617 if (errno != EINVAL && errno != EHOSTUNREACH) 618 /* 619 * On some Linux versions, when a socket is bound to the 620 * loopback interface, sendto will fail and errno will be 621 * set to EINVAL or EHOSTUNREACH. 622 * When that happens, don't throw an exception, just return false. 623 */ 624 #endif /*__linux__ */ 625 NET_ThrowNew(env, errno, "Can't send ICMP packet"); 626 close(fd); 627 return JNI_FALSE; 628 } 629 630 tmout2 = timeout > 1000 ? 1000 : timeout; 631 do { 632 tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); 633 634 if (tmout2 >= 0) { 635 len = sizeof(sa_recv); 636 n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len); 637 icmp6 = (struct icmp6_hdr *) (recvbuf); 638 recv_caddr = (jbyte *)&(sa_recv.sin6_addr); 639 /* 640 * We did receive something, but is it what we were expecting? 641 * I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and 642 * from the host that we are trying to determine is reachable. 643 */ 644 if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY && 645 (ntohs(icmp6->icmp6_id) == pid)) { 646 if (NET_IsEqual(caddr, recv_caddr)) { 647 close(fd); 648 return JNI_TRUE; 649 } 650 if (NET_IsZeroAddr(caddr)) { 651 close(fd); 652 return JNI_TRUE; 653 } 654 } 655 } 656 } while (tmout2 > 0); 657 timeout -= 1000; 658 } while (timeout > 0); 659 close(fd); 660 return JNI_FALSE; 661 } 662 #endif /* AF_INET6 */ 663 664 /* 665 * Class: java_net_Inet6AddressImpl 666 * Method: isReachable0 667 * Signature: ([bII[bI)Z 668 */ 669 JNIEXPORT jboolean JNICALL 670 Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this, 671 jbyteArray addrArray, 672 jint scope, 673 jint timeout, 674 jbyteArray ifArray, 675 jint ttl, jint if_scope) { 676 #ifdef AF_INET6 677 jbyte caddr[16]; 678 jint fd, sz; 679 struct sockaddr_in6 him6; 680 struct sockaddr_in6 inf6; 681 struct sockaddr_in6* netif = NULL; 682 int len = 0; 683 int connect_rv = -1; 684 685 /* 686 * If IPv6 is not enable, then we can't reach an IPv6 address, can we? 687 */ 688 if (!ipv6_available()) { 689 return JNI_FALSE; 690 } 691 /* 692 * If it's an IPv4 address, ICMP won't work with IPv4 mapped address, 693 * therefore, let's delegate to the Inet4Address method. 694 */ 695 sz = (*env)->GetArrayLength(env, addrArray); 696 if (sz == 4) { 697 return Java_java_net_Inet4AddressImpl_isReachable0(env, this, 698 addrArray, 699 timeout, 700 ifArray, ttl); 701 } 702 703 memset((void *) caddr, 0, 16); 704 memset((void *) &him6, 0, sizeof(him6)); 705 (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); 706 memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) ); 707 him6.sin6_family = AF_INET6; 708 #ifdef __linux__ 709 if (scope > 0) 710 him6.sin6_scope_id = scope; 711 else 712 him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr)); 713 len = sizeof(struct sockaddr_in6); 714 #else 715 if (scope > 0) 716 him6.sin6_scope_id = scope; 717 len = sizeof(struct sockaddr_in6); 718 #endif 719 /* 720 * If a network interface was specified, let's create the address 721 * for it. 722 */ 723 if (!(IS_NULL(ifArray))) { 724 memset((void *) caddr, 0, 16); 725 memset((void *) &inf6, 0, sizeof(inf6)); 726 (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr); 727 memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) ); 728 inf6.sin6_family = AF_INET6; 729 inf6.sin6_scope_id = if_scope; 730 netif = &inf6; 731 } 732 /* 733 * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST 734 * otherwise we'll try a tcp socket to the Echo port (7). 735 * Note that this is empiric, and not connecting could mean it's blocked 736 * or the echo service has been disabled. 737 */ 738 739 fd = JVM_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); 740 741 if (fd != -1) { /* Good to go, let's do a ping */ 742 return ping6(env, fd, &him6, timeout, netif, ttl); 743 } 744 745 /* No good, let's fall back on TCP */ 746 fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0); 747 if (fd == JVM_IO_ERR) { 748 /* note: if you run out of fds, you may not be able to load 749 * the exception class, and get a NoClassDefFoundError 750 * instead. 751 */ 752 NET_ThrowNew(env, errno, "Can't create socket"); 753 return JNI_FALSE; 754 } 755 if (ttl > 0) { 756 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); 757 } 758 759 /* 760 * A network interface was specified, so let's bind to it. 761 */ 762 if (netif != NULL) { 763 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) { 764 NET_ThrowNew(env, errno, "Can't bind socket"); 765 close(fd); 766 return JNI_FALSE; 767 } 768 } 769 SET_NONBLOCKING(fd); 770 771 /* no need to use NET_Connect as non-blocking */ 772 him6.sin6_port = htons((short) 7); /* Echo port */ 773 connect_rv = JVM_Connect(fd, (struct sockaddr *)&him6, len); 774 775 /** 776 * connection established or refused immediately, either way it means 777 * we were able to reach the host! 778 */ 779 if (connect_rv == 0 || errno == ECONNREFUSED) { 780 close(fd); 781 return JNI_TRUE; 782 } else { 783 int optlen; 784 785 switch (errno) { 786 case ENETUNREACH: /* Network Unreachable */ 787 case EAFNOSUPPORT: /* Address Family not supported */ 788 case EADDRNOTAVAIL: /* address is not available on the remote machine */ 789 #ifdef __linux__ 790 case EINVAL: 791 case EHOSTUNREACH: 792 /* 793 * On some Linux versions, when a socket is bound to the 794 * loopback interface, connect will fail and errno will 795 * be set to EINVAL or EHOSTUNREACH. When that happens, 796 * don't throw an exception, just return false. 797 */ 798 #endif /* __linux__ */ 799 close(fd); 800 return JNI_FALSE; 801 } 802 803 if (errno != EINPROGRESS) { 804 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", 805 "connect failed"); 806 close(fd); 807 return JNI_FALSE; 808 } 809 810 timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); 811 812 if (timeout >= 0) { 813 /* has connection been established */ 814 optlen = sizeof(connect_rv); 815 if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, 816 &optlen) <0) { 817 connect_rv = errno; 818 } 819 if (connect_rv == 0 || ECONNREFUSED) { 820 close(fd); 821 return JNI_TRUE; 822 } 823 } 824 close(fd); 825 return JNI_FALSE; 826 } 827 #else /* AF_INET6 */ 828 return JNI_FALSE; 829 #endif /* AF_INET6 */ 830 } --- EOF ---