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