@@ -332,37 +332,33 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
332332 */
333333JNIEXPORT jstring JNICALL
334334Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv * env , jobject this ) {
335- char hostname [NI_MAXHOST + 1 ];
335+ char hostname [NI_MAXHOST + 1 ];
336336
337337 hostname [0 ] = '\0' ;
338338 if (JVM_GetHostName (hostname , sizeof (hostname ))) {
339- /* Something went wrong, maybe networking is not setup? */
340339 strcpy (hostname , "localhost" );
341340 } else {
341+ #if defined(__solaris__ )
342+ // try to resolve hostname via nameservice
343+ // if it is known but getnameinfo fails, hostname will still be the
344+ // value from gethostname
342345 struct addrinfo hints , * res ;
343- int error ;
344346
347+ // make sure string is null-terminated
345348 hostname [NI_MAXHOST ] = '\0' ;
346349 memset (& hints , 0 , sizeof (hints ));
347350 hints .ai_flags = AI_CANONNAME ;
348351 hints .ai_family = AF_INET ;
349352
350- error = getaddrinfo (hostname , NULL , & hints , & res );
351-
352- if (error == 0 ) {/* host is known to name service */
353- getnameinfo (res -> ai_addr ,
354- res -> ai_addrlen ,
355- hostname ,
356- NI_MAXHOST ,
357- NULL ,
358- 0 ,
359- NI_NAMEREQD );
360-
361- /* if getnameinfo fails hostname is still the value
362- from gethostname */
363-
353+ if (getaddrinfo (hostname , NULL , & hints , & res ) == 0 ) {
354+ getnameinfo (res -> ai_addr , res -> ai_addrlen , hostname , NI_MAXHOST ,
355+ NULL , 0 , NI_NAMEREQD );
364356 freeaddrinfo (res );
365357 }
358+ #else
359+ // make sure string is null-terminated
360+ hostname [NI_MAXHOST ] = '\0' ;
361+ #endif
366362 }
367363 return (* env )-> NewStringUTF (env , hostname );
368364}
0 commit comments