src/solaris/native/java/net/Inet4AddressImpl.c

Print this page




  94             hp = gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET,
  95                                  &res2, buf2, sizeof(buf2), &h_error);
  96 #endif
  97             if (hp) {
  98                 /*
  99                  * If gethostbyaddr_r() found a fully qualified host name,
 100                  * returns that name. Otherwise, returns the hostname
 101                  * found by gethostname().
 102                  */
 103                 char *p = hp->h_name;
 104                 if ((strlen(hp->h_name) > strlen(hostname))
 105                     && (strncmp(hostname, hp->h_name, strlen(hostname)) == 0)
 106                     && (*(p + strlen(hostname)) == '.'))
 107                     strcpy(hostname, hp->h_name);
 108             }
 109         }
 110     }
 111     return (*env)->NewStringUTF(env, hostname);
 112 }
 113 
 114 static jclass ni_iacls;
 115 static jclass ni_ia4cls;
 116 static jmethodID ni_ia4ctrID;
 117 static jfieldID ni_iaaddressID;
 118 static jfieldID ni_iahostID;
 119 static jfieldID ni_iafamilyID;
 120 static int initialized = 0;
 121 
 122 /*
 123  * Find an internet address for a given hostname.  Note that this
 124  * code only works for addresses of type INET. The translation
 125  * of %d.%d.%d.%d to an address (int) occurs in java now, so the
 126  * String "host" shouldn't *ever* be a %d.%d.%d.%d string
 127  *
 128  * Class:     java_net_Inet4AddressImpl
 129  * Method:    lookupAllHostAddr
 130  * Signature: (Ljava/lang/String;)[[B
 131  */
 132 
 133 JNIEXPORT jobjectArray JNICALL
 134 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
 135                                                 jstring host) {
 136     const char *hostname;
 137     jobjectArray ret = 0;
 138     struct hostent res, *hp = 0;
 139     char buf[HENT_BUF_SIZE];
 140 
 141     /* temporary buffer, on the off chance we need to expand */
 142     char *tmp = NULL;
 143     int h_error=0;
 144 
 145     if (!initialized) {
 146       ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
 147       ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
 148       ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
 149       ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
 150       ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
 151       ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
 152       ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
 153       ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
 154       initialized = 1;
 155     }
 156 
 157     if (IS_NULL(host)) {
 158         JNU_ThrowNullPointerException(env, "host is null");
 159         return 0;
 160     }
 161     hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
 162     CHECK_NULL_RETURN(hostname, NULL);
 163 
 164 #ifdef __solaris__
 165     /*
 166      * Workaround for Solaris bug 4160367 - if a hostname contains a
 167      * white space then 0.0.0.0 is returned
 168      */
 169     if (isspace((unsigned char)hostname[0])) {
 170         JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
 171                         (char *)hostname);
 172         JNU_ReleaseStringPlatformChars(env, host, hostname);
 173         return NULL;
 174     }
 175 #endif


 189     if (hp == NULL && errno == ERANGE) {
 190         if ((tmp = (char*)malloc(BIG_HENT_BUF_SIZE))) {
 191 #ifdef __GLIBC__
 192             gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE,
 193                             &hp, &h_error);
 194 #else
 195             hp = gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE,
 196                                  &h_error);
 197 #endif
 198         }
 199     }
 200     if (hp != NULL) {
 201         struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
 202         int i = 0;
 203 
 204         while (*addrp != (struct in_addr *) 0) {
 205             i++;
 206             addrp++;
 207         }
 208 
 209         ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
 210         if (IS_NULL(ret)) {
 211             /* we may have memory to free at the end of this */
 212             goto cleanupAndReturn;
 213         }
 214         addrp = (struct in_addr **) hp->h_addr_list;
 215         i = 0;
 216         while (*addrp) {
 217           jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
 218           if (IS_NULL(iaObj)) {
 219             ret = NULL;
 220             goto cleanupAndReturn;
 221           }
 222           (*env)->SetIntField(env, iaObj, ni_iaaddressID,
 223                               ntohl((*addrp)->s_addr));
 224           (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
 225           (*env)->SetObjectArrayElement(env, ret, i, iaObj);
 226           addrp++;
 227           i++;
 228         }
 229     } else {
 230         JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
 231                         (char *)hostname);
 232         ret = NULL;
 233     }
 234 
 235 cleanupAndReturn:
 236     JNU_ReleaseStringPlatformChars(env, host, hostname);
 237     if (tmp != NULL) {
 238         free(tmp);
 239     }
 240     return ret;
 241 }
 242 
 243 /*
 244  * Class:     java_net_Inet4AddressImpl




  94             hp = gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET,
  95                                  &res2, buf2, sizeof(buf2), &h_error);
  96 #endif
  97             if (hp) {
  98                 /*
  99                  * If gethostbyaddr_r() found a fully qualified host name,
 100                  * returns that name. Otherwise, returns the hostname
 101                  * found by gethostname().
 102                  */
 103                 char *p = hp->h_name;
 104                 if ((strlen(hp->h_name) > strlen(hostname))
 105                     && (strncmp(hostname, hp->h_name, strlen(hostname)) == 0)
 106                     && (*(p + strlen(hostname)) == '.'))
 107                     strcpy(hostname, hp->h_name);
 108             }
 109         }
 110     }
 111     return (*env)->NewStringUTF(env, hostname);
 112 }
 113 








 114 /*
 115  * Find an internet address for a given hostname.  Note that this
 116  * code only works for addresses of type INET. The translation
 117  * of %d.%d.%d.%d to an address (int) occurs in java now, so the
 118  * String "host" shouldn't *ever* be a %d.%d.%d.%d string
 119  *
 120  * Class:     java_net_Inet4AddressImpl
 121  * Method:    lookupAllHostAddr
 122  * Signature: (Ljava/lang/String;)[[B
 123  */
 124 
 125 JNIEXPORT jobjectArray JNICALL
 126 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
 127                                                 jstring host) {
 128     const char *hostname;
 129     jobjectArray ret = 0;
 130     struct hostent res, *hp = 0;
 131     char buf[HENT_BUF_SIZE];
 132 
 133     /* temporary buffer, on the off chance we need to expand */
 134     char *tmp = NULL;
 135     int h_error=0;
 136 
 137     init(env);










 138 
 139     if (IS_NULL(host)) {
 140         JNU_ThrowNullPointerException(env, "host is null");
 141         return 0;
 142     }
 143     hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
 144     CHECK_NULL_RETURN(hostname, NULL);
 145 
 146 #ifdef __solaris__
 147     /*
 148      * Workaround for Solaris bug 4160367 - if a hostname contains a
 149      * white space then 0.0.0.0 is returned
 150      */
 151     if (isspace((unsigned char)hostname[0])) {
 152         JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
 153                         (char *)hostname);
 154         JNU_ReleaseStringPlatformChars(env, host, hostname);
 155         return NULL;
 156     }
 157 #endif


 171     if (hp == NULL && errno == ERANGE) {
 172         if ((tmp = (char*)malloc(BIG_HENT_BUF_SIZE))) {
 173 #ifdef __GLIBC__
 174             gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE,
 175                             &hp, &h_error);
 176 #else
 177             hp = gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE,
 178                                  &h_error);
 179 #endif
 180         }
 181     }
 182     if (hp != NULL) {
 183         struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
 184         int i = 0;
 185 
 186         while (*addrp != (struct in_addr *) 0) {
 187             i++;
 188             addrp++;
 189         }
 190 
 191         ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
 192         if (IS_NULL(ret)) {
 193             /* we may have memory to free at the end of this */
 194             goto cleanupAndReturn;
 195         }
 196         addrp = (struct in_addr **) hp->h_addr_list;
 197         i = 0;
 198         while (*addrp) {
 199           jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 200           if (IS_NULL(iaObj)) {
 201             ret = NULL;
 202             goto cleanupAndReturn;
 203           }
 204           (*env)->SetIntField(env, iaObj, ia_addressID,
 205                               ntohl((*addrp)->s_addr));
 206           (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
 207           (*env)->SetObjectArrayElement(env, ret, i, iaObj);
 208           addrp++;
 209           i++;
 210         }
 211     } else {
 212         JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
 213                         (char *)hostname);
 214         ret = NULL;
 215     }
 216 
 217 cleanupAndReturn:
 218     JNU_ReleaseStringPlatformChars(env, host, hostname);
 219     if (tmp != NULL) {
 220         free(tmp);
 221     }
 222     return ret;
 223 }
 224 
 225 /*
 226  * Class:     java_net_Inet4AddressImpl