8244202: Implementation of JEP 418: Internet-Address Resolution SPI

Co-authored-by: Chris Hegarty <chegar@openjdk.org>
Co-authored-by: Daniel Fuchs <dfuchs@openjdk.org>
Co-authored-by: Alan Bateman <alanb@openjdk.org>
Reviewed-by: dfuchs, alanb, michaelm, chegar
This commit is contained in:
Aleksei Efimov 2021-11-11 14:33:58 +00:00
parent c29cab8ab4
commit 2ca4ff87b7
56 changed files with 2986 additions and 293 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* 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
@ -36,9 +36,11 @@
#include "net_util.h"
#include "java_net_Inet4AddressImpl.h"
#include "java_net_spi_InetAddressResolver_LookupPolicy.h"
#if defined(MACOSX)
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6,
int addressesOrder);
#endif
#define SET_NONBLOCKING(fd) { \
@ -111,7 +113,9 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
if (error) {
#if defined(MACOSX)
// If getaddrinfo fails try getifaddrs, see bug 8170910.
ret = lookupIfLocalhost(env, hostname, JNI_FALSE);
// java_net_spi_InetAddressResolver_LookupPolicy_IPV4_FIRST and no ordering is ok
// here since only AF_INET addresses will be returned.
ret = lookupIfLocalhost(env, hostname, JNI_FALSE, java_net_spi_InetAddressResolver_LookupPolicy_IPV4);
if (ret != NULL || (*env)->ExceptionCheck(env)) {
goto cleanupAndReturn;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* 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
@ -41,6 +41,8 @@
#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); \
@ -74,7 +76,7 @@ Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
#if defined(MACOSX)
/* also called from Inet4AddressImpl.c */
__private_extern__ jobjectArray
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6, int characteristics)
{
jobjectArray result = NULL;
char myhostname[NI_MAXHOST + 1];
@ -151,7 +153,7 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL);
if (!result) goto done;
if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) {
if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV6_FIRST) != 0) {
i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
j = 0;
} else {
@ -204,7 +206,7 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
*/
JNIEXPORT jobjectArray JNICALL
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jstring host) {
jstring host, jint characteristics) {
jobjectArray ret = NULL;
const char *hostname;
int error = 0;
@ -224,14 +226,14 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
// try once, with our static buffer
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_UNSPEC;
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);
ret = lookupIfLocalhost(env, hostname, JNI_TRUE, characteristics);
if (ret != NULL || (*env)->ExceptionCheck(env)) {
goto cleanupAndReturn;
}
@ -242,8 +244,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
} 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
@ -322,13 +322,13 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
goto cleanupAndReturn;
}
if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV6_FIRST) != 0) {
inetIndex = inet6Count;
inet6Index = 0;
} else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
} else if ((characteristics & java_net_spi_InetAddressResolver_LookupPolicy_IPV4_FIRST) != 0) {
inetIndex = 0;
inet6Index = inetCount;
} else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
} else {
inetIndex = inet6Index = originalIndex = 0;
}
@ -371,7 +371,8 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
(*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
inet6Index++;
}
if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
// Check if addresses are requested to be returned in SYSTEM order
if (addressesInSystemOrder(characteristics)) {
originalIndex++;
inetIndex = inet6Index = 0;
}