提交 59bb9bb5 编写于 作者: C clanger

8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only

Reviewed-by: clanger, chegar
Contributed-by: sshamaia@in.ibm.com
上级 cb0ed89e
......@@ -67,38 +67,36 @@ extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolea
*/
JNIEXPORT jstring JNICALL
Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
char hostname[NI_MAXHOST+1];
char hostname[NI_MAXHOST + 1];
hostname[0] = '\0';
if (JVM_GetHostName(hostname, NI_MAXHOST)) {
/* Something went wrong, maybe networking is not setup? */
strcpy(hostname, "localhost");
#if defined(__solaris__)
} else {
// try to resolve hostname via nameservice
// if it is known but getnameinfo fails, hostname will still be the
// value from gethostname
struct addrinfo hints, *res;
int error;
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_UNSPEC;
error = getaddrinfo(hostname, NULL, &hints, &res);
if (error == 0) {
/* host is known to name service */
error = getnameinfo(res->ai_addr,
res->ai_addrlen,
hostname,
NI_MAXHOST,
NULL,
0,
NI_NAMEREQD);
/* if getnameinfo fails hostname is still the value
from gethostname */
hints.ai_family = AF_INET;
if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
NULL, 0, NI_NAMEREQD);
freeaddrinfo(res);
}
}
#else
} else {
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
}
#endif
return (*env)->NewStringUTF(env, hostname);
}
......
......@@ -66,49 +66,36 @@
*/
JNIEXPORT jstring JNICALL
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
char hostname[NI_MAXHOST+1];
char hostname[NI_MAXHOST + 1];
hostname[0] = '\0';
if (JVM_GetHostName(hostname, sizeof(hostname))) {
/* Something went wrong, maybe networking is not setup? */
strcpy(hostname, "localhost");
} else {
// ensure null-terminated
hostname[NI_MAXHOST] = '\0';
/* Solaris doesn't want to give us a fully qualified domain name.
* We do a reverse lookup to try and get one. This works
* if DNS occurs before NIS in /etc/resolv.conf, but fails
* if NIS comes first (it still gets only a partial name).
* We use thread-safe system calls.
*/
#if defined(__solaris__) && defined(AF_INET6)
} else {
// try to resolve hostname via nameservice
// if it is known but getnameinfo fails, hostname will still be the
// value from gethostname
struct addrinfo hints, *res;
int error;
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_UNSPEC;
error = getaddrinfo(hostname, NULL, &hints, &res);
if (error == 0) {
/* host is known to name service */
error = getnameinfo(res->ai_addr,
res->ai_addrlen,
hostname,
NI_MAXHOST,
NULL,
0,
NI_NAMEREQD);
/* if getnameinfo fails hostname is still the value
from gethostname */
if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
NULL, 0, NI_NAMEREQD);
freeaddrinfo(res);
}
#endif
}
#else
} else {
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
}
#endif
return (*env)->NewStringUTF(env, hostname);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册