jdk/src/solaris/native/java/net/Inet6AddressImpl.c
changeset 22956 322ce28a7e20
parent 22646 5fa3669fd35d
child 23015 73b21ab36615
equal deleted inserted replaced
22955:3a17c4db457b 22956:322ce28a7e20
    64  * Method:    getLocalHostName
    64  * Method:    getLocalHostName
    65  * Signature: ()Ljava/lang/String;
    65  * Signature: ()Ljava/lang/String;
    66  */
    66  */
    67 JNIEXPORT jstring JNICALL
    67 JNIEXPORT jstring JNICALL
    68 Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
    68 Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
       
    69     int ret;
    69     char hostname[NI_MAXHOST+1];
    70     char hostname[NI_MAXHOST+1];
    70 
    71 
    71     hostname[0] = '\0';
    72     hostname[0] = '\0';
    72     if (JVM_GetHostName(hostname, sizeof(hostname))) {
    73     ret = JVM_GetHostName(hostname, sizeof(hostname));
       
    74     if (ret) {
    73         /* Something went wrong, maybe networking is not setup? */
    75         /* Something went wrong, maybe networking is not setup? */
    74         strcpy(hostname, "localhost");
    76         strcpy(hostname, "localhost");
    75     } else {
    77     } else {
    76         // ensure null-terminated
    78         // ensure null-terminated
    77         hostname[NI_MAXHOST] = '\0';
    79         hostname[NI_MAXHOST] = '\0';
    78 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
    80     }
    79         /* On Linux/FreeBSD gethostname() says "host.domain.sun.com".  On
    81 
    80          * Solaris gethostname() says "host", so extra work is needed.
    82 #if defined(__solaris__) && defined(AF_INET6)
    81          */
    83     if (ret == 0) {
    82 #else
       
    83         /* Solaris doesn't want to give us a fully qualified domain name.
    84         /* Solaris doesn't want to give us a fully qualified domain name.
    84          * We do a reverse lookup to try and get one.  This works
    85          * We do a reverse lookup to try and get one.  This works
    85          * if DNS occurs before NIS in /etc/resolv.conf, but fails
    86          * if DNS occurs before NIS in /etc/resolv.conf, but fails
    86          * if NIS comes first (it still gets only a partial name).
    87          * if NIS comes first (it still gets only a partial name).
    87          * We use thread-safe system calls.
    88          * We use thread-safe system calls.
    88          */
    89          */
    89 #ifdef AF_INET6
       
    90         struct addrinfo  hints, *res;
    90         struct addrinfo  hints, *res;
    91         int error;
    91         int error;
    92 
    92 
    93         memset(&hints, 0, sizeof(hints));
    93         memset(&hints, 0, sizeof(hints));
    94         hints.ai_flags = AI_CANONNAME;
    94         hints.ai_flags = AI_CANONNAME;
   109             /* if getnameinfo fails hostname is still the value
   109             /* if getnameinfo fails hostname is still the value
   110                from gethostname */
   110                from gethostname */
   111 
   111 
   112             freeaddrinfo(res);
   112             freeaddrinfo(res);
   113         }
   113         }
   114 #endif /* AF_INET6 */
   114     }
   115 #endif /* __linux__ || _ALLBSD_SOURCE */
   115 #endif
   116     }
   116 
   117     return (*env)->NewStringUTF(env, hostname);
   117     return (*env)->NewStringUTF(env, hostname);
   118 }
   118 }
   119 
   119 
   120 #ifdef MACOSX
   120 #ifdef MACOSX
   121 /* also called from Inet4AddressImpl.c */
   121 /* also called from Inet4AddressImpl.c */
   400             inetIndex = 0;
   400             inetIndex = 0;
   401             inet6Index = inetCount;
   401             inet6Index = inetCount;
   402         }
   402         }
   403 
   403 
   404         while (iterator != NULL) {
   404         while (iterator != NULL) {
   405             int ret1;
   405             jboolean ret1;
   406             if (iterator->ai_family == AF_INET) {
   406             if (iterator->ai_family == AF_INET) {
   407                 jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
   407                 jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
   408                 if (IS_NULL(iaObj)) {
   408                 if (IS_NULL(iaObj)) {
   409                     ret = NULL;
   409                     ret = NULL;
   410                     goto cleanupAndReturn;
   410                     goto cleanupAndReturn;
   420                 if (IS_NULL(iaObj)) {
   420                 if (IS_NULL(iaObj)) {
   421                     ret = NULL;
   421                     ret = NULL;
   422                     goto cleanupAndReturn;
   422                     goto cleanupAndReturn;
   423                 }
   423                 }
   424                 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
   424                 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
   425                 if (!ret1) {
   425                 if (ret1 == JNI_FALSE) {
   426                     ret = NULL;
   426                     ret = NULL;
   427                     goto cleanupAndReturn;
   427                     goto cleanupAndReturn;
   428                 }
   428                 }
   429 
   429 
   430                 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
   430                 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;