提交 fbfea144 编写于 作者: C chegar

8025306: Inet[4|6]Address class and fieldID initialization in networking native code

Reviewed-by: alanb, michaelm
上级 094bb042
......@@ -112,6 +112,7 @@ SUNWprivate_1.1 {
NET_EnableFastTcpLoopback;
NET_ThrowNew;
ipv6_available;
initInetAddressIDs;
local:
*;
......
......@@ -34,6 +34,8 @@
jclass ia4_class;
jmethodID ia4_ctrID;
static int ia4_initialized = 0;
/*
* Class: java_net_Inet4Address
* Method: init
......@@ -41,9 +43,13 @@ jmethodID ia4_ctrID;
*/
JNIEXPORT void JNICALL
Java_java_net_Inet4Address_init(JNIEnv *env, jclass cls) {
if (!ia4_initialized) {
jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL(c);
ia4_class = (*env)->NewGlobalRef(env, c);
CHECK_NULL(ia4_class);
ia4_ctrID = (*env)->GetMethodID(env, ia4_class, "<init>", "()V");
CHECK_NULL(ia4_ctrID);
ia4_initialized = 1;
}
}
......@@ -42,6 +42,8 @@ jfieldID ia6_scopeidsetID;
jfieldID ia6_scopeifnameID;
jmethodID ia6_ctrID;
static int ia6_initialized = 0;
/*
* Class: java_net_Inet6Address
* Method: init
......@@ -49,6 +51,7 @@ jmethodID ia6_ctrID;
*/
JNIEXPORT void JNICALL
Java_java_net_Inet6Address_init(JNIEnv *env, jclass cls) {
if (!ia6_initialized) {
jclass ia6h_class;
jclass c = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL(c);
......@@ -69,4 +72,7 @@ Java_java_net_Inet6Address_init(JNIEnv *env, jclass cls) {
ia6_scopeifnameID = (*env)->GetFieldID(env, ia6h_class, "scope_ifname", "Ljava/net/NetworkInterface;");
CHECK_NULL(ia6_scopeifnameID);
ia6_ctrID = (*env)->GetMethodID(env, ia6_class, "<init>", "()V");
CHECK_NULL(ia6_ctrID);
ia6_initialized = 1;
}
}
......@@ -41,6 +41,8 @@ jfieldID iac_hostNameID;
jfieldID iac_origHostNameID;
jfieldID ia_preferIPv6AddressID;
static int ia_initialized = 0;
/*
* Class: java_net_InetAddress
* Method: init
......@@ -48,6 +50,7 @@ jfieldID ia_preferIPv6AddressID;
*/
JNIEXPORT void JNICALL
Java_java_net_InetAddress_init(JNIEnv *env, jclass cls) {
if (!ia_initialized) {
jclass c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL(c);
ia_class = (*env)->NewGlobalRef(env, c);
......@@ -67,4 +70,7 @@ Java_java_net_InetAddress_init(JNIEnv *env, jclass cls) {
iac_hostNameID = (*env)->GetFieldID(env, iac_class, "hostName", "Ljava/lang/String;");
CHECK_NULL(iac_hostNameID);
iac_origHostNameID = (*env)->GetFieldID(env, iac_class, "originalHostName", "Ljava/lang/String;");
CHECK_NULL(iac_origHostNameID);
ia_initialized = 1;
}
}
......@@ -75,11 +75,14 @@ JNI_OnLoad(JavaVM *vm, void *reserved)
static int initialized = 0;
static void initInetAddrs(JNIEnv *env) {
JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env) {
if (!initialized) {
Java_java_net_InetAddress_init(env, 0);
JNU_CHECK_EXCEPTION(env);
Java_java_net_Inet4Address_init(env, 0);
JNU_CHECK_EXCEPTION(env);
Java_java_net_Inet6Address_init(env, 0);
JNU_CHECK_EXCEPTION(env);
initialized = 1;
}
}
......@@ -100,47 +103,32 @@ extern jfieldID iac_familyID;
* get_ methods that return objects return NULL on error.
*/
jobject getInet6Address_scopeifname(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, NULL);
return (*env)->GetObjectField(env, holder, ia6_scopeifnameID);
}
int setInet6Address_scopeifname(JNIEnv *env, jobject iaObj, jobject scopeifname) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, JNI_FALSE);
(*env)->SetObjectField(env, holder, ia6_scopeifnameID, scopeifname);
return JNI_TRUE;
}
int getInet6Address_scopeid_set(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, -1);
return (*env)->GetBooleanField(env, holder, ia6_scopeidsetID);
}
int getInet6Address_scopeid(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, -1);
return (*env)->GetIntField(env, holder, ia6_scopeidID);
}
int setInet6Address_scopeid(JNIEnv *env, jobject iaObj, int scopeid) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, JNI_FALSE);
(*env)->SetIntField(env, holder, ia6_scopeidID, scopeid);
if (scopeid > 0) {
......@@ -154,7 +142,6 @@ int getInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *dest) {
jobject holder, addr;
jbyteArray barr;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, JNI_FALSE);
addr = (*env)->GetObjectField(env, holder, ia6_ipaddressID);
......@@ -167,7 +154,6 @@ int setInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *address) {
jobject holder;
jbyteArray addr;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
CHECK_NULL_RETURN(holder, JNI_FALSE);
addr = (jbyteArray)(*env)->GetObjectField(env, holder, ia6_ipaddressID);
......@@ -181,53 +167,39 @@ int setInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *address) {
}
void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
(*env)->SetIntField(env, holder, iac_addressID, address);
}
void setInetAddress_family(JNIEnv *env, jobject iaObj, int family) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
(*env)->SetIntField(env, holder, iac_familyID, family);
}
void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject host) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
(*env)->SetObjectField(env, holder, iac_hostNameID, host);
(*env)->SetObjectField(env, holder, iac_origHostNameID, host);
}
int getInetAddress_addr(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetIntField(env, holder, iac_addressID);
}
int getInetAddress_family(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetIntField(env, holder, iac_familyID);
}
jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj) {
jobject holder;
initInetAddrs(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetObjectField(env, holder, iac_hostNameID);
}
JNIEXPORT jobject JNICALL
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
jobject iaObj;
initInetAddrs(env);
#ifdef AF_INET6
if (him->sa_family == AF_INET6) {
jbyteArray ipaddress;
......@@ -239,31 +211,15 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
jbyte *caddr = (jbyte *)&(him6->sin6_addr);
if (NET_IsIPv4Mapped(caddr)) {
int address;
static jclass inet4Cls = 0;
if (inet4Cls == 0) {
jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(c, NULL);
inet4Cls = (*env)->NewGlobalRef(env, c);
CHECK_NULL_RETURN(inet4Cls, NULL);
(*env)->DeleteLocalRef(env, c);
}
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
CHECK_NULL_RETURN(iaObj, NULL);
address = NET_IPv4MappedToIPv4(caddr);
setInetAddress_addr(env, iaObj, address);
setInetAddress_family(env, iaObj, IPv4);
} else {
static jclass inet6Cls = 0;
jint scope;
int ret;
if (inet6Cls == 0) {
jclass c = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL_RETURN(c, NULL);
inet6Cls = (*env)->NewGlobalRef(env, c);
CHECK_NULL_RETURN(inet6Cls, NULL);
(*env)->DeleteLocalRef(env, c);
}
iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID);
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
CHECK_NULL_RETURN(iaObj, NULL);
ret = setInet6Address_ipaddress(env, iaObj, (char *)&(him6->sin6_addr));
CHECK_NULL_RETURN(ret, NULL);
......@@ -276,16 +232,7 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
#endif /* AF_INET6 */
{
struct sockaddr_in *him4 = (struct sockaddr_in *)him;
static jclass inet4Cls = 0;
if (inet4Cls == 0) {
jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(c, NULL);
inet4Cls = (*env)->NewGlobalRef(env, c);
CHECK_NULL_RETURN(inet4Cls, NULL);
(*env)->DeleteLocalRef(env, c);
}
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
CHECK_NULL_RETURN(iaObj, NULL);
setInetAddress_family(env, iaObj, IPv4);
setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr));
......
......@@ -56,6 +56,8 @@ extern jfieldID iac_hostNameID;
extern jfieldID iac_origHostNameID;
extern jfieldID ia_preferIPv6AddressID;
JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env);
/** (Inet6Address accessors)
* set_ methods return JNI_TRUE on success JNI_FALSE on error
* get_ methods that return int/boolean, return -1 on error
......
......@@ -51,29 +51,6 @@
#define HAS_GLIBC_GETHOSTBY_R 1
#endif
static jclass ni_iacls;
static jclass ni_ia4cls;
static jmethodID ni_ia4ctrID;
static jboolean initializeInetClasses(JNIEnv *env)
{
static int initialized = 0;
if (!initialized) {
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia4ctrID, JNI_FALSE);
initialized = 1;
}
return JNI_TRUE;
}
#if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
......@@ -147,8 +124,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
int getaddrinfo_error=0;
struct addrinfo hints, *res, *resNew = NULL;
if (!initializeInetClasses(env))
return NULL;
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null");
......@@ -242,7 +219,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
goto cleanupAndReturn;
}
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;
......@@ -252,7 +229,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
/* We need 4 bytes to store ipv4 address; */
int len = 4;
jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (IS_NULL(iaObj)) {
/* we may have memory to free at the end of this */
ret = NULL;
......@@ -408,8 +385,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
int error = 0;
struct addrinfo hints, *res, *resNew = NULL;
if (!initializeInetClasses(env))
return NULL;
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null");
......@@ -487,7 +464,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
retLen = i;
iterator = resNew;
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 */
......@@ -496,7 +473,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
i = 0;
while (iterator != NULL) {
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;
......
......@@ -117,44 +117,6 @@ Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
return (*env)->NewStringUTF(env, hostname);
}
static jclass ni_iacls;
static jclass ni_ia4cls;
static jclass ni_ia6cls;
static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID;
static jboolean preferIPv6Address;
static jboolean initializeInetClasses(JNIEnv *env)
{
jfieldID ni_preferIPv6AddressID;
static int initialized = 0;
if (!initialized) {
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL_RETURN(ni_ia6cls, JNI_FALSE);
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
CHECK_NULL_RETURN(ni_ia6cls, JNI_FALSE);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia4ctrID, JNI_FALSE);
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia6ctrID, JNI_FALSE);
ni_preferIPv6AddressID =
(*env)->GetStaticFieldID(env, ni_iacls, "preferIPv6Address", "Z");
CHECK_NULL_RETURN(ni_preferIPv6AddressID, JNI_FALSE);
preferIPv6Address =
(*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
initialized = 1;
}
return JNI_TRUE;
}
#ifdef MACOSX
/* also called from Inet4AddressImpl.c */
__private_extern__ jobjectArray
......@@ -169,9 +131,8 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
jboolean includeLoopback = JNI_FALSE;
jobject name;
// Make sure static variables we need are set.
if (!initializeInetClasses(env))
return NULL;
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
/* If the requested name matches this host's hostname, return IP addresses
* from all attached interfaces. (#2844683 et al) This prevents undesired
......@@ -234,10 +195,10 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
/* Create and fill the Java array. */
int arraySize = addrs4 + addrs6 -
(includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
result = (*env)->NewObjectArray(env, arraySize, ni_iacls, NULL);
result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL);
if (!result) goto done;
if (preferIPv6Address) {
if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) {
i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
j = 0;
} else {
......@@ -301,8 +262,8 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
struct addrinfo hints, *res, *resNew = NULL;
#endif /* AF_INET6 */
if (!initializeInetClasses(env))
return NULL;
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null");
......@@ -429,14 +390,14 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
retLen = i;
iterator = resNew;
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;
}
if (preferIPv6Address) {
if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) {
/* AF_INET addresses will be offset by inet6Count */
inetIndex = inet6Count;
inet6Index = 0;
......@@ -449,7 +410,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
while (iterator != NULL) {
int ret1;
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;
......@@ -461,7 +422,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
} else if (iterator->ai_family == AF_INET6) {
jint scope = 0;
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;
......@@ -555,7 +516,7 @@ Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
if (!error) {
ret = (*env)->NewStringUTF(env, host);
CHECK_NULL(ret);
CHECK_NULL_RETURN(ret, NULL);
}
#endif /* AF_INET6 */
......
......@@ -133,12 +133,7 @@ jfieldID ni_parentID;
jfieldID ni_defaultIndexID;
jmethodID ni_ctrID;
static jclass ni_iacls;
static jclass ni_ia4cls;
static jclass ni_ia6cls;
static jclass ni_ibcls;
static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID;
static jmethodID ni_ibctrID;
static jfieldID ni_ibaddressID;
static jfieldID ni_ib4broadcastID;
......@@ -215,26 +210,10 @@ JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
CHECK_NULL(ni_parentID);
ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
CHECK_NULL(ni_ctrID);
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL(ni_iacls);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL(ni_iacls);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL(ni_ia4cls);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL(ni_ia4cls);
ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL(ni_ia6cls);
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
CHECK_NULL(ni_ia6cls);
ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
CHECK_NULL(ni_ibcls);
ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
CHECK_NULL(ni_ibcls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL(ni_ia4ctrID);
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
CHECK_NULL(ni_ia6ctrID);
ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
CHECK_NULL(ni_ibctrID);
ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address",
......@@ -247,6 +226,9 @@ JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
CHECK_NULL(ni_ib4maskID);
ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
"I");
CHECK_NULL(ni_defaultIndexID);
initInetAddressIDs(env);
}
/*
......@@ -700,7 +682,7 @@ static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
}
// create the array of InetAddresses
addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
addrArr = (*env)->NewObjectArray(env, addr_count, ia_class, NULL);
if (addrArr == NULL) {
return NULL;
}
......@@ -717,7 +699,7 @@ static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
jobject ibObj = NULL;
if (addrP->family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (iaObj) {
setInetAddress_addr(env, iaObj, htonl(
((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
......@@ -729,7 +711,7 @@ static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
if (addrP->brdcast) {
jobject ia2Obj = NULL;
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (ia2Obj) {
setInetAddress_addr(env, ia2Obj, htonl(
((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
......@@ -747,7 +729,7 @@ static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
#if defined(AF_INET6)
if (addrP->family == AF_INET6) {
int scope=0;
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (iaObj) {
jboolean ret = setInet6Address_ipaddress(env, iaObj,
(char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
......
......@@ -174,9 +174,8 @@ Java_java_net_PlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) {
IO_fd_fdID = NET_GetFileDescriptorID(env);
CHECK_NULL(IO_fd_fdID);
Java_java_net_InetAddress_init(env, 0);
Java_java_net_Inet4Address_init(env, 0);
Java_java_net_Inet6Address_init(env, 0);
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION(env);
Java_java_net_NetworkInterface_init(env, 0);
}
......
......@@ -162,6 +162,9 @@ Java_java_net_PlainSocketImpl_initProto(JNIEnv *env, jclass cls) {
IO_fd_fdID = NET_GetFileDescriptorID(env);
CHECK_NULL(IO_fd_fdID);
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION(env);
/* Create the marker fd used for dup2 */
marker_fd = getMarkerFD();
}
......
......@@ -35,6 +35,7 @@
#include "java_lang_Integer.h"
#include "nio.h"
#include "nio_util.h"
#include "net_util.h"
static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */
......@@ -44,6 +45,7 @@ Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
{
CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
initInetAddressIDs(env);
}
JNIEXPORT jboolean JNICALL
......
......@@ -157,6 +157,7 @@ Java_sun_nio_ch_sctp_SctpNet_init
}
preCloseFD = sp[0];
close(sp[1]);
initInetAddressIDs(env);
}
/*
......
......@@ -114,11 +114,6 @@ Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
return JNU_NewStringPlatform(env, hostname);
}
static jclass ni_iacls;
static jclass ni_ia4cls;
static jmethodID ni_ia4ctrID;
static int initialized = 0;
/*
* Find an internet address for a given hostname. Not this this
* code only works for addresses of type INET. The translation
......@@ -143,19 +138,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jobjectArray ret = NULL;
if (!initialized) {
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL_RETURN(ni_iacls, NULL);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL_RETURN(ni_iacls, NULL);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(ni_ia4cls, NULL);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL_RETURN(ni_ia4cls, NULL);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia4ctrID, NULL);
initialized = 1;
}
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host argument");
......@@ -199,13 +183,13 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
address |= (addr[1]<<8) & 0xff00;
address |= addr[0];
ret = (*env)->NewObjectArray(env, 1, ni_iacls, NULL);
ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
if (IS_NULL(ret)) {
goto cleanupAndReturn;
}
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (IS_NULL(iaObj)) {
ret = NULL;
goto cleanupAndReturn;
......@@ -229,7 +213,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
addrp++;
}
ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
if (IS_NULL(ret)) {
goto cleanupAndReturn;
......@@ -238,7 +222,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
addrp = (struct in_addr **) hp->h_addr_list;
i = 0;
while (*addrp != (struct in_addr *) 0) {
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;
......
......@@ -74,13 +74,6 @@ Java_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
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 int initialized = 0;
JNIEXPORT jobjectArray JNICALL
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jstring host) {
......@@ -88,30 +81,13 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jobjectArray ret = 0;
int retLen = 0;
jboolean preferIPv6Address;
static jfieldID ia_preferIPv6AddressID;
int error=0;
struct addrinfo hints, *res, *resNew = NULL;
if (!initialized) {
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL_RETURN(ni_iacls, NULL);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL_RETURN(ni_iacls, NULL);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL_RETURN(ni_ia4cls, NULL);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL_RETURN(ni_ia4cls, NULL);
ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL_RETURN(ni_ia6cls, NULL);
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
CHECK_NULL_RETURN(ni_ia6cls, NULL);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia4ctrID, NULL);
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
CHECK_NULL_RETURN(ni_ia6ctrID, NULL);
initialized = 1;
}
initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null");
return 0;
......@@ -119,17 +95,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
CHECK_NULL_RETURN(hostname, NULL);
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);
......@@ -231,7 +196,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
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 */
......@@ -248,7 +213,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
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;
......@@ -259,7 +224,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
inetIndex ++;
} else if (iterator->ai_family == AF_INET6) {
jint scope = 0, ret1;
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;
......@@ -349,7 +314,7 @@ Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
if (!error) {
ret = (*env)->NewStringUTF(env, host);
CHECK_NULL(ret);
CHECK_NULL_RETURN(ret, NULL);
}
if (ret == NULL) {
......
......@@ -65,13 +65,6 @@ jfieldID ni_bindsID; /* NetworkInterface.bindings */
jfieldID ni_nameID; /* NetworkInterface.name */
jfieldID ni_displayNameID; /* NetworkInterface.displayName */
jfieldID ni_childsID; /* NetworkInterface.childs */
jclass ni_iacls; /* InetAddress */
jclass ni_ia4cls; /* Inet4Address */
jmethodID ni_ia4Ctor; /* Inet4Address() */
jclass ni_ia6cls; /* Inet6Address */
jmethodID ni_ia6ctrID; /* Inet6Address() */
jclass ni_ibcls; /* InterfaceAddress */
jmethodID ni_ibctrID; /* InterfaceAddress() */
......@@ -515,26 +508,6 @@ Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls)
CHECK_NULL(ni_childsID);
ni_ctor = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
CHECK_NULL(ni_ctor);
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
CHECK_NULL(ni_iacls);
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
CHECK_NULL(ni_iacls);
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
CHECK_NULL(ni_ia4cls);
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
CHECK_NULL(ni_ia4cls);
ni_ia4Ctor = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
CHECK_NULL(ni_ia4Ctor);
ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
CHECK_NULL(ni_ia6cls);
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
CHECK_NULL(ni_ia6cls);
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
CHECK_NULL(ni_ia6ctrID);
ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
CHECK_NULL(ni_ibcls);
ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
......@@ -546,6 +519,9 @@ Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls)
ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
CHECK_NULL(ni_ibbroadcastID);
ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
CHECK_NULL(ni_ibmaskID);
initInetAddressIDs(env);
}
/*
......@@ -591,7 +567,7 @@ jobject createNetworkInterface
return NULL;
}
}
addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
addrArr = (*env)->NewObjectArray(env, netaddrCount, ia_class, NULL);
if (addrArr == NULL) {
free_netaddr(netaddrP);
return NULL;
......@@ -609,7 +585,7 @@ jobject createNetworkInterface
jobject iaObj, ia2Obj;
jobject ibObj = NULL;
if (addrs->addr.him.sa_family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (iaObj == NULL) {
free_netaddr(netaddrP);
return NULL;
......@@ -624,7 +600,7 @@ jobject createNetworkInterface
return NULL;
}
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (ia2Obj == NULL) {
free_netaddr(netaddrP);
return NULL;
......@@ -636,7 +612,7 @@ jobject createNetworkInterface
}
} else /* AF_INET6 */ {
int scope;
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (iaObj) {
int ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
if (ret == JNI_FALSE) {
......
......@@ -70,16 +70,6 @@ extern jfieldID ni_nameID; /* NetworkInterface.name */
extern jfieldID ni_displayNameID; /* NetworkInterface.displayName */
extern jfieldID ni_childsID; /* NetworkInterface.childs */
extern jclass ni_iacls; /* InetAddress */
extern jclass ni_ia4cls; /* Inet4Address */
extern jmethodID ni_ia4Ctor; /* Inet4Address() */
extern jclass ni_ia6cls; /* Inet6Address */
extern jmethodID ni_ia6ctrID; /* Inet6Address() */
extern jfieldID ni_ia6ipaddressID;
extern jfieldID ni_ia6ipaddressID;
extern jclass ni_ibcls; /* InterfaceAddress */
extern jmethodID ni_ibctrID; /* InterfaceAddress() */
extern jfieldID ni_ibaddressID; /* InterfaceAddress.address */
......
......@@ -513,7 +513,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
}
}
addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
addrArr = (*env)->NewObjectArray(env, netaddrCount, ia_class, NULL);
if (addrArr == NULL) {
return NULL;
}
......@@ -531,7 +531,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
jobject iaObj, ia2Obj;
jobject ibObj = NULL;
if (addrs->addr.him.sa_family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (iaObj == NULL) {
return NULL;
}
......@@ -545,7 +545,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
return NULL;
}
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (ia2Obj == NULL) {
free_netaddr(netaddrP);
return NULL;
......@@ -556,7 +556,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
} else /* AF_INET6 */ {
int scope;
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (iaObj) {
int ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
if (ret == JNI_FALSE) {
......
......@@ -33,6 +33,7 @@
#include "nio.h"
#include "nio_util.h"
#include "net_util.h"
#include "sun_nio_ch_IOUtil.h"
/* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */
......@@ -55,6 +56,7 @@ Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
CHECK_NULL(handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J"));
initInetAddressIDs(env);
}
/**************************************************************
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册