src/windows/native/java/net/Inet6AddressImpl.c
Print this page
@@ -70,20 +70,10 @@
strcpy (hostname, "localhost");
}
return JNU_NewStringPlatform (env, hostname);
}
-static jclass ni_iacls;
-static jclass ni_ia4cls;
-static jclass ni_ia6cls;
-static jmethodID ni_ia4ctrID;
-static jmethodID ni_ia6ctrID;
-static jfieldID ni_iaaddressID;
-static jfieldID ni_iahostID;
-static jfieldID ni_iafamilyID;
-static jfieldID ni_ia6ipaddressID;
-static int initialized = 0;
JNIEXPORT jobjectArray JNICALL
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jstring host) {
const char *hostname;
@@ -92,45 +82,20 @@
jboolean preferIPv6Address;
int error=0;
struct addrinfo hints, *res, *resNew = NULL;
- if (!initialized) {
- ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
- ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
- ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
- ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
- ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
- ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
- ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
- ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
- ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
- ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
- ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
- ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
- initialized = 1;
- }
+ init(env);
+
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null");
return 0;
}
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
CHECK_NULL_RETURN(hostname, NULL);
if (NET_addrtransAvailable()) {
- static jfieldID ia_preferIPv6AddressID;
- if (ia_preferIPv6AddressID == NULL) {
- jclass c = (*env)->FindClass(env,"java/net/InetAddress");
- if (c) {
- ia_preferIPv6AddressID =
- (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
- }
- if (ia_preferIPv6AddressID == NULL) {
- JNU_ReleaseStringPlatformChars(env, host, hostname);
- return NULL;
- }
- }
/* get the address preference */
preferIPv6Address
= (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
/* Try once, with our static buffer. */
@@ -220,11 +185,11 @@
iterator = iterator->ai_next;
}
retLen = i;
iterator = resNew;
i = 0;
- ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
+ ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
if (IS_NULL(ret)) {
/* we may have memory to free at the end of this */
goto cleanupAndReturn;
}
@@ -237,24 +202,24 @@
inet6Index = inetCount;
}
while (iterator != NULL) {
if (iterator->ai_family == AF_INET) {
- jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
+ jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (IS_NULL(iaObj)) {
ret = NULL;
goto cleanupAndReturn;
}
- (*env)->SetIntField(env, iaObj, ni_iaaddressID,
+ (*env)->SetIntField(env, iaObj, ia_addressID,
ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
- (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
+ (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
(*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
inetIndex ++;
} else if (iterator->ai_family == AF_INET6) {
jint scope = 0;
jbyteArray ipaddress;
- jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
+ jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (IS_NULL(iaObj)) {
ret = NULL;
goto cleanupAndReturn;
}
ipaddress = (*env)->NewByteArray(env, 16);
@@ -267,12 +232,12 @@
scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
if (scope != 0) { /* zero is default value, no need to set */
(*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
}
- (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
- (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
+ (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
+ (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
(*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
inet6Index ++;
}
iterator = iterator->ai_next;
}