Print this page


Split Close
Expand all
Collapse all
          --- old/src/windows/native/java/net/Inet6AddressImpl.c
          +++ new/src/windows/native/java/net/Inet6AddressImpl.c
↓ open down ↓ 64 lines elided ↑ open up ↑
  65   65  JNIEXPORT jstring JNICALL
  66   66  Java_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
  67   67      char hostname [256];
  68   68  
  69   69      if (gethostname (hostname, sizeof (hostname)) == -1) {
  70   70          strcpy (hostname, "localhost");
  71   71      }
  72   72      return JNU_NewStringPlatform (env, hostname);
  73   73  }
  74   74  
  75      -static jclass ni_iacls;
  76      -static jclass ni_ia4cls;
  77      -static jclass ni_ia6cls;
  78      -static jmethodID ni_ia4ctrID;
  79      -static jmethodID ni_ia6ctrID;
  80      -static jfieldID ni_iaaddressID;
  81      -static jfieldID ni_iahostID;
  82      -static jfieldID ni_iafamilyID;
  83      -static jfieldID ni_ia6ipaddressID;
  84      -static int initialized = 0;
  85   75  
  86   76  JNIEXPORT jobjectArray JNICALL
  87   77  Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
  88   78                                                  jstring host) {
  89   79      const char *hostname;
  90   80      jobjectArray ret = 0;
  91   81      int retLen = 0;
  92   82      jboolean preferIPv6Address;
  93   83  
  94   84      int error=0;
  95   85      struct addrinfo hints, *res, *resNew = NULL;
  96   86  
  97      -    if (!initialized) {
  98      -      ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
  99      -      ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
 100      -      ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
 101      -      ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
 102      -      ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
 103      -      ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
 104      -      ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
 105      -      ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
 106      -      ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
 107      -      ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
 108      -      ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
 109      -      ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
 110      -      initialized = 1;
 111      -    }
       87 +    init(env);
       88 +
 112   89      if (IS_NULL(host)) {
 113   90          JNU_ThrowNullPointerException(env, "host is null");
 114   91          return 0;
 115   92      }
 116   93      hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
 117   94      CHECK_NULL_RETURN(hostname, NULL);
 118   95  
 119   96      if (NET_addrtransAvailable()) {
 120      -        static jfieldID ia_preferIPv6AddressID;
 121      -        if (ia_preferIPv6AddressID == NULL) {
 122      -            jclass c = (*env)->FindClass(env,"java/net/InetAddress");
 123      -            if (c)  {
 124      -                ia_preferIPv6AddressID =
 125      -                    (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
 126      -            }
 127      -            if (ia_preferIPv6AddressID == NULL) {
 128      -                JNU_ReleaseStringPlatformChars(env, host, hostname);
 129      -                return NULL;
 130      -            }
 131      -        }
 132   97          /* get the address preference */
 133   98          preferIPv6Address
 134   99              = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
 135  100  
 136  101          /* Try once, with our static buffer. */
 137  102          memset(&hints, 0, sizeof(hints));
 138  103          hints.ai_flags = AI_CANONNAME;
 139  104          hints.ai_family = AF_UNSPEC;
 140  105  
 141  106          error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
↓ open down ↓ 73 lines elided ↑ open up ↑
 215  180                          inetCount ++;
 216  181                      } else if (iterator->ai_family == AF_INET6) {
 217  182                          inet6Count ++;
 218  183                      }
 219  184                  }
 220  185                  iterator = iterator->ai_next;
 221  186              }
 222  187              retLen = i;
 223  188              iterator = resNew;
 224  189              i = 0;
 225      -            ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
      190 +            ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
 226  191  
 227  192              if (IS_NULL(ret)) {
 228  193                  /* we may have memory to free at the end of this */
 229  194                  goto cleanupAndReturn;
 230  195              }
 231  196  
 232  197              if (preferIPv6Address) {
 233  198                  inetIndex = inet6Count;
 234  199                  inet6Index = 0;
 235  200              } else {
 236  201                  inetIndex = 0;
 237  202                  inet6Index = inetCount;
 238  203              }
 239  204  
 240  205              while (iterator != NULL) {
 241  206                  if (iterator->ai_family == AF_INET) {
 242      -                  jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
      207 +                  jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 243  208                    if (IS_NULL(iaObj)) {
 244  209                      ret = NULL;
 245  210                      goto cleanupAndReturn;
 246  211                    }
 247      -                  (*env)->SetIntField(env, iaObj, ni_iaaddressID,
      212 +                  (*env)->SetIntField(env, iaObj, ia_addressID,
 248  213                                        ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
 249      -                  (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
      214 +                  (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
 250  215                    (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
 251  216                      inetIndex ++;
 252  217                  } else if (iterator->ai_family == AF_INET6) {
 253  218                    jint scope = 0;
 254  219                    jbyteArray ipaddress;
 255      -                  jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
      220 +                  jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 256  221                    if (IS_NULL(iaObj)) {
 257  222                      ret = NULL;
 258  223                      goto cleanupAndReturn;
 259  224                    }
 260  225                    ipaddress = (*env)->NewByteArray(env, 16);
 261  226                    if (IS_NULL(ipaddress)) {
 262  227                      ret = NULL;
 263  228                      goto cleanupAndReturn;
 264  229                    }
 265  230                    (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
 266  231                                               (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
 267  232                    scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
 268  233                    if (scope != 0) { /* zero is default value, no need to set */
 269  234                      (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
 270  235                      (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
 271  236                    }
 272      -                  (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
 273      -                  (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
      237 +                  (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
      238 +                  (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
 274  239                    (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
 275  240                    inet6Index ++;
 276  241                  }
 277  242                  iterator = iterator->ai_next;
 278  243              }
 279  244          }
 280  245      }
 281  246  
 282  247  cleanupAndReturn:
 283  248      {
↓ open down ↓ 407 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX