diff --git a/src/solaris/native/java/net/Inet4AddressImpl.c b/src/solaris/native/java/net/Inet4AddressImpl.c index ec4f97dfae11ccc5eeff6da0f1bc4e2f30c64b21..f6345efc60df6e58e7f2c02f81383b7ad7c0911c 100644 --- a/src/solaris/native/java/net/Inet4AddressImpl.c +++ b/src/solaris/native/java/net/Inet4AddressImpl.c @@ -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 { - struct addrinfo hints, *res; - int error; - - 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); + // 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; - /* if getnameinfo fails hostname is still the value - from gethostname */ + // make sure string is null-terminated + hostname[NI_MAXHOST] = '\0'; + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_CANONNAME; + hints.ai_family = AF_INET; - freeaddrinfo(res); + 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); } diff --git a/src/solaris/native/java/net/Inet6AddressImpl.c b/src/solaris/native/java/net/Inet6AddressImpl.c index e29a25daf52801e50baae5cc1eccb30c304ef32c..f14aca41722c6218311332624e193aecc1572cf2 100644 --- a/src/solaris/native/java/net/Inet6AddressImpl.c +++ b/src/solaris/native/java/net/Inet6AddressImpl.c @@ -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) - struct addrinfo hints, *res; - int error; + } 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; + // 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); }