Print this page


Split Close
Expand all
Collapse all
          --- old/src/windows/native/java/net/Inet4AddressImpl.c
          +++ new/src/windows/native/java/net/Inet4AddressImpl.c
↓ open down ↓ 103 lines elided ↑ open up ↑
 104  104  JNIEXPORT jstring JNICALL
 105  105  Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
 106  106      char hostname[256];
 107  107  
 108  108      if (gethostname(hostname, sizeof hostname) == -1) {
 109  109          strcpy(hostname, "localhost");
 110  110      }
 111  111      return JNU_NewStringPlatform(env, hostname);
 112  112  }
 113  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  114  /*
 123  115   * Find an internet address for a given hostname.  Not this this
 124  116   * code only works for addresses of type INET. The translation
 125  117   * of %d.%d.%d.%d to an address (int) occurs in java now, so the
 126  118   * String "host" shouldn't be a %d.%d.%d.%d string. The only
 127  119   * exception should be when any of the %d are out of range and
 128  120   * we fallback to a lookup.
 129  121   *
 130  122   * Class:     java_net_Inet4AddressImpl
 131  123   * Method:    lookupAllHostAddr
↓ open down ↓ 4 lines elided ↑ open up ↑
 136  128  
 137  129  JNIEXPORT jobjectArray JNICALL
 138  130  Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
 139  131                                                  jstring host) {
 140  132      const char *hostname;
 141  133      struct hostent *hp;
 142  134      unsigned int addr[4];
 143  135  
 144  136      jobjectArray ret = NULL;
 145  137  
 146      -    if (!initialized) {
 147      -      ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
 148      -      ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
 149      -      ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
 150      -      ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
 151      -      ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
 152      -      ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
 153      -      ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
 154      -      ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
 155      -      initialized = 1;
 156      -    }
      138 +    init(env);
 157  139  
 158  140      if (IS_NULL(host)) {
 159  141          JNU_ThrowNullPointerException(env, "host argument");
 160  142          return NULL;
 161  143      }
 162  144      hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
 163  145      CHECK_NULL_RETURN(hostname, NULL);
 164  146  
 165  147      /*
 166  148       * The NT/2000 resolver tolerates a space in front of localhost. This
↓ open down ↓ 23 lines elided ↑ open up ↑
 190  172          }
 191  173  
 192  174          /*
 193  175           * Return an byte array with the populated address.
 194  176           */
 195  177          address = (addr[3]<<24) & 0xff000000;
 196  178          address |= (addr[2]<<16) & 0xff0000;
 197  179          address |= (addr[1]<<8) & 0xff00;
 198  180          address |= addr[0];
 199  181  
 200      -        ret = (*env)->NewObjectArray(env, 1, ni_iacls, NULL);
      182 +        ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
 201  183  
 202  184          if (IS_NULL(ret)) {
 203  185              goto cleanupAndReturn;
 204  186          }
 205  187  
 206      -        iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
      188 +        iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 207  189          if (IS_NULL(iaObj)) {
 208  190            ret = NULL;
 209  191            goto cleanupAndReturn;
 210  192          }
 211      -        (*env)->SetIntField(env, iaObj, ni_iaaddressID,
      193 +        (*env)->SetIntField(env, iaObj, ia_addressID,
 212  194                              ntohl(address));
 213  195          (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
 214  196          JNU_ReleaseStringPlatformChars(env, host, hostname);
 215  197          return ret;
 216  198      }
 217  199  
 218  200      /*
 219  201       * Perform the lookup
 220  202       */
 221  203      if ((hp = gethostbyname((char*)hostname)) != NULL) {
 222  204          struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
 223  205          int len = sizeof(struct in_addr);
 224  206          int i = 0;
 225  207  
 226  208          while (*addrp != (struct in_addr *) 0) {
 227  209              i++;
 228  210              addrp++;
 229  211          }
 230  212  
 231      -        ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
      213 +        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
 232  214  
 233  215          if (IS_NULL(ret)) {
 234  216              goto cleanupAndReturn;
 235  217          }
 236  218  
 237  219          addrp = (struct in_addr **) hp->h_addr_list;
 238  220          i = 0;
 239  221          while (*addrp != (struct in_addr *) 0) {
 240      -          jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
      222 +          jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 241  223            if (IS_NULL(iaObj)) {
 242  224              ret = NULL;
 243  225              goto cleanupAndReturn;
 244  226            }
 245      -          (*env)->SetIntField(env, iaObj, ni_iaaddressID,
      227 +          (*env)->SetIntField(env, iaObj, ia_addressID,
 246  228                                ntohl((*addrp)->s_addr));
 247      -          (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
      229 +          (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
 248  230            (*env)->SetObjectArrayElement(env, ret, i, iaObj);
 249  231            addrp++;
 250  232            i++;
 251  233          }
 252  234      } else {
 253  235          JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
 254  236      }
 255  237  
 256  238  cleanupAndReturn:
 257  239      JNU_ReleaseStringPlatformChars(env, host, hostname);
↓ open down ↓ 320 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX