< prev index next >

src/java.base/unix/native/libnet/Inet6AddressImpl.c

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

*** 39,10 ***
--- 39,12 ---
  #include "net_util.h"
  
  #include "java_net_InetAddress.h"
  #include "java_net_Inet4AddressImpl.h"
  #include "java_net_Inet6AddressImpl.h"
+ #include "java_net_spi_InetAddressResolver_LookupPolicy.h"
+ 
  
  #define SET_NONBLOCKING(fd) {       \
      int flags = fcntl(fd, F_GETFL); \
      flags |= O_NONBLOCK;            \
      fcntl(fd, F_SETFL, flags);      \

*** 72,11 ***
  }
  
  #if defined(MACOSX)
  /* also called from Inet4AddressImpl.c */
  __private_extern__ jobjectArray
! lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
  {
      jobjectArray result = NULL;
      char myhostname[NI_MAXHOST + 1];
      struct ifaddrs *ifa = NULL;
      int familyOrder = 0;
--- 74,11 ---
  }
  
  #if defined(MACOSX)
  /* also called from Inet4AddressImpl.c */
  __private_extern__ jobjectArray
! lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6, int characteristics)
  {
      jobjectArray result = NULL;
      char myhostname[NI_MAXHOST + 1];
      struct ifaddrs *ifa = NULL;
      int familyOrder = 0;

*** 149,11 ***
      int arraySize = addrs4 + addrs6 -
          (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
      result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL);
      if (!result) goto done;
  
!     if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) {
          i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
          j = 0;
      } else {
          i = 0;
          j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks);
--- 151,11 ---
      int arraySize = addrs4 + addrs6 -
          (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
      result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL);
      if (!result) goto done;
  
!     if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV6_FIRST) != 0) {
          i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
          j = 0;
      } else {
          i = 0;
          j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks);

*** 202,11 ***
   * Method:    lookupAllHostAddr
   * Signature: (Ljava/lang/String;)[[B
   */
  JNIEXPORT jobjectArray JNICALL
  Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
!                                                  jstring host) {
      jobjectArray ret = NULL;
      const char *hostname;
      int error = 0;
      struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
          *iterator;
--- 204,11 ---
   * Method:    lookupAllHostAddr
   * Signature: (Ljava/lang/String;)[[B
   */
  JNIEXPORT jobjectArray JNICALL
  Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
!                                                  jstring host, jint characteristics) {
      jobjectArray ret = NULL;
      const char *hostname;
      int error = 0;
      struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
          *iterator;

*** 222,30 ***
      CHECK_NULL_RETURN(hostname, NULL);
  
      // try once, with our static buffer
      memset(&hints, 0, sizeof(hints));
      hints.ai_flags = AI_CANONNAME;
!     hints.ai_family = AF_UNSPEC;
  
      error = getaddrinfo(hostname, NULL, &hints, &res);
  
      if (error) {
  #if defined(MACOSX)
          // if getaddrinfo fails try getifaddrs
!         ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
          if (ret != NULL || (*env)->ExceptionCheck(env)) {
              goto cleanupAndReturn;
          }
  #endif
          // report error
          NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
          goto cleanupAndReturn;
      } else {
          int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,
              inet6Index = 0, originalIndex = 0;
-         int addressPreference =
-             (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);;
          iterator = res;
          while (iterator != NULL) {
              // skip duplicates
              int skip = 0;
              struct addrinfo *iteratorNew = resNew;
--- 224,28 ---
      CHECK_NULL_RETURN(hostname, NULL);
  
      // try once, with our static buffer
      memset(&hints, 0, sizeof(hints));
      hints.ai_flags = AI_CANONNAME;
!     hints.ai_family = lookupCharacteristicsToAddressFamily(characteristics);
  
      error = getaddrinfo(hostname, NULL, &hints, &res);
  
      if (error) {
  #if defined(MACOSX)
          // if getaddrinfo fails try getifaddrs
!         ret = lookupIfLocalhost(env, hostname, JNI_TRUE, characteristics);
          if (ret != NULL || (*env)->ExceptionCheck(env)) {
              goto cleanupAndReturn;
          }
  #endif
          // report error
          NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
          goto cleanupAndReturn;
      } else {
          int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,
              inet6Index = 0, originalIndex = 0;
          iterator = res;
          while (iterator != NULL) {
              // skip duplicates
              int skip = 0;
              struct addrinfo *iteratorNew = resNew;

*** 320,17 ***
          if (IS_NULL(ret)) {
              /* we may have memory to free at the end of this */
              goto cleanupAndReturn;
          }
  
!         if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
              inetIndex = inet6Count;
              inet6Index = 0;
!         } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
              inetIndex = 0;
              inet6Index = inetCount;
!         } else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
              inetIndex = inet6Index = originalIndex = 0;
          }
  
          iterator = resNew;
          while (iterator != NULL) {
--- 320,17 ---
          if (IS_NULL(ret)) {
              /* we may have memory to free at the end of this */
              goto cleanupAndReturn;
          }
  
!         if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV6_FIRST) != 0) {
              inetIndex = inet6Count;
              inet6Index = 0;
!         } else if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV4_FIRST) != 0) {
              inetIndex = 0;
              inet6Index = inetCount;
!         } else {
              inetIndex = inet6Index = originalIndex = 0;
          }
  
          iterator = resNew;
          while (iterator != NULL) {

*** 369,11 ***
                  if ((*env)->ExceptionCheck(env))
                      goto cleanupAndReturn;
                  (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
                  inet6Index++;
              }
!             if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
                  originalIndex++;
                  inetIndex = inet6Index = 0;
              }
              iterator = iterator->ai_next;
          }
--- 369,12 ---
                  if ((*env)->ExceptionCheck(env))
                      goto cleanupAndReturn;
                  (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
                  inet6Index++;
              }
!             // Check if addresses are requested to be returned in SYSTEM order
+             if (addressesInSystemOrder(characteristics)) {
                  originalIndex++;
                  inetIndex = inet6Index = 0;
              }
              iterator = iterator->ai_next;
          }
< prev index next >