提交 52e58b86 编写于 作者: R robm

8026245: InetAddress.getLocalHost crash if IPv6 disabled (macosx)

Reviewed-by: chegar, alanb
上级 6016e36e
...@@ -70,6 +70,8 @@ static void initializeInetClasses(JNIEnv *env) ...@@ -70,6 +70,8 @@ static void initializeInetClasses(JNIEnv *env)
#if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R) #if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
/* Use getaddrinfo(3), which is thread safe */ /* Use getaddrinfo(3), which is thread safe */
/************************************************************************ /************************************************************************
* Inet4AddressImpl * Inet4AddressImpl
...@@ -378,8 +380,6 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { ...@@ -378,8 +380,6 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
return (*env)->NewStringUTF(env, hostname); return (*env)->NewStringUTF(env, hostname);
} }
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
/* /*
* Find an internet address for a given hostname. Note that this * Find an internet address for a given hostname. Note that this
* code only works for addresses of type INET. The translation * code only works for addresses of type INET. The translation
......
...@@ -122,9 +122,11 @@ static jclass ni_ia4cls; ...@@ -122,9 +122,11 @@ static jclass ni_ia4cls;
static jclass ni_ia6cls; static jclass ni_ia6cls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID; static jmethodID ni_ia6ctrID;
static jboolean preferIPv6Address;
static void initializeInetClasses(JNIEnv *env) static void initializeInetClasses(JNIEnv *env)
{ {
jfieldID ni_preferIPv6AddressID;
static int initialized = 0; static int initialized = 0;
if (!initialized) { if (!initialized) {
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress"); ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
...@@ -135,6 +137,10 @@ static void initializeInetClasses(JNIEnv *env) ...@@ -135,6 +137,10 @@ static void initializeInetClasses(JNIEnv *env)
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls); ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V"); ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
ni_preferIPv6AddressID =
(*env)->GetStaticFieldID(env, ni_iacls, "preferIPv6Address", "Z");
preferIPv6Address =
(*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
initialized = 1; initialized = 1;
} }
} }
...@@ -145,7 +151,6 @@ __private_extern__ jobjectArray ...@@ -145,7 +151,6 @@ __private_extern__ jobjectArray
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6) lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
{ {
jobjectArray result = NULL; jobjectArray result = NULL;
jboolean preferIPv6Address;
char myhostname[NI_MAXHOST+1]; char myhostname[NI_MAXHOST+1];
struct ifaddrs *ifa = NULL; struct ifaddrs *ifa = NULL;
int familyOrder = 0; int familyOrder = 0;
...@@ -157,9 +162,6 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6) ...@@ -157,9 +162,6 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
// Make sure static variables we need are set. // Make sure static variables we need are set.
initializeInetClasses(env); initializeInetClasses(env);
/* get the address preference */
preferIPv6Address = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
/* If the requested name matches this host's hostname, return IP addresses /* If the requested name matches this host's hostname, return IP addresses
* from all attached interfaces. (#2844683 et al) This prevents undesired * from all attached interfaces. (#2844683 et al) This prevents undesired
* PPP dialup, but may return addresses that don't actually correspond to * PPP dialup, but may return addresses that don't actually correspond to
...@@ -278,7 +280,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, ...@@ -278,7 +280,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
const char *hostname; const char *hostname;
jobjectArray ret = 0; jobjectArray ret = 0;
int retLen = 0; int retLen = 0;
jboolean preferIPv6Address;
int error=0; int error=0;
#ifdef AF_INET6 #ifdef AF_INET6
...@@ -296,36 +297,18 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, ...@@ -296,36 +297,18 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
#ifdef MACOSX #ifdef MACOSX
/* /*
* If getaddrinfo has failed and we're looking up the local machine, we * If we're looking up the local machine, attempt to get the address
* attempt to get the address from getifaddrs. This ensures we get an * from getifaddrs. This ensures we get an IPv6 address for the local
* IPv6 address for the local machine. * machine.
*/ */
if (error) { ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
ret = lookupIfLocalhost(env, hostname, JNI_TRUE); if (ret != NULL || (*env)->ExceptionCheck(env)) {
if (ret != NULL || (*env)->ExceptionCheck(env)) { JNU_ReleaseStringPlatformChars(env, host, hostname);
JNU_ReleaseStringPlatformChars(env, host, hostname); return ret;
return ret;
}
} }
#endif #endif
#ifdef AF_INET6 #ifdef AF_INET6
static jfieldID ia_preferIPv6AddressID;
if (ia_preferIPv6AddressID == NULL) {
jclass c = (*env)->FindClass(env,"java/net/InetAddress");
if (c) {
ia_preferIPv6AddressID =
(*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
}
if (ia_preferIPv6AddressID == NULL) {
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
}
}
/* get the address preference */
preferIPv6Address
= (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
/* Try once, with our static buffer. */ /* Try once, with our static buffer. */
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME; hints.ai_flags = AI_CANONNAME;
......
/* /*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,9 +23,11 @@ ...@@ -23,9 +23,11 @@
/** /**
* @test * @test
* @bug 4531817 * @bug 4531817 8026245
* @summary Inet[46]Address.localHost need doPrivileged * @summary Inet[46]Address.localHost need doPrivileged
* @run main/othervm GetLocalHostWithSM * @run main/othervm GetLocalHostWithSM
* @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalHostWithSM
* @run main/othervm -Djava.net.preferIPv6Addresses=true GetLocalHostWithSM
* files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file * files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file
*/ */
......
/* /*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,7 +23,10 @@ ...@@ -23,7 +23,10 @@
/* /*
* @test * @test
* @bug 4106601 * @bug 4106601 8026245
* @run main/othervm GetLocalAddress
* @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalAddress
* @run main/othervm -Djava.net.preferIPv6Addresses=true GetLocalAddress
* @summary Test the java.net.socket.GetLocalAddress method * @summary Test the java.net.socket.GetLocalAddress method
* *
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册