8000724: Improve networking serialization

Delegate InetAddress fields to a holder object

Reviewed-by: alanb, chegar
This commit is contained in:
Michael McMahon 2013-02-13 10:40:31 +00:00
parent 9f917b11d6
commit 724cfc1897
24 changed files with 289 additions and 219 deletions

View File

@ -122,7 +122,7 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
* not connected already. * not connected already.
*/ */
protected void disconnect() { protected void disconnect() {
disconnect0(connectedAddress.family); disconnect0(connectedAddress.holder().getFamily());
connected = false; connected = false;
connectedAddress = null; connectedAddress = null;
connectedPort = -1; connectedPort = -1;

View File

@ -100,27 +100,28 @@ class Inet4Address extends InetAddress {
Inet4Address() { Inet4Address() {
super(); super();
hostName = null; holder().hostName = null;
address = 0; holder().address = 0;
family = IPv4; holder().family = IPv4;
} }
Inet4Address(String hostName, byte addr[]) { Inet4Address(String hostName, byte addr[]) {
this.hostName = hostName; holder().hostName = hostName;
this.family = IPv4; holder().family = IPv4;
if (addr != null) { if (addr != null) {
if (addr.length == INADDRSZ) { if (addr.length == INADDRSZ) {
address = addr[3] & 0xFF; int address = addr[3] & 0xFF;
address |= ((addr[2] << 8) & 0xFF00); address |= ((addr[2] << 8) & 0xFF00);
address |= ((addr[1] << 16) & 0xFF0000); address |= ((addr[1] << 16) & 0xFF0000);
address |= ((addr[0] << 24) & 0xFF000000); address |= ((addr[0] << 24) & 0xFF000000);
holder().address = address;
} }
} }
} }
Inet4Address(String hostName, int address) { Inet4Address(String hostName, int address) {
this.hostName = hostName; holder().hostName = hostName;
this.family = IPv4; holder().family = IPv4;
this.address = address; holder().address = address;
} }
/** /**
@ -134,8 +135,8 @@ class Inet4Address extends InetAddress {
private Object writeReplace() throws ObjectStreamException { private Object writeReplace() throws ObjectStreamException {
// will replace the to be serialized 'this' object // will replace the to be serialized 'this' object
InetAddress inet = new InetAddress(); InetAddress inet = new InetAddress();
inet.hostName = this.hostName; inet.holder().hostName = holder().getHostName();
inet.address = this.address; inet.holder().address = holder().getAddress();
/** /**
* Prior to 1.4 an InetAddress was created with a family * Prior to 1.4 an InetAddress was created with a family
@ -143,7 +144,7 @@ class Inet4Address extends InetAddress {
* For compatibility reasons we must therefore write the * For compatibility reasons we must therefore write the
* the InetAddress with this family. * the InetAddress with this family.
*/ */
inet.family = 2; inet.holder().family = 2;
return inet; return inet;
} }
@ -157,7 +158,7 @@ class Inet4Address extends InetAddress {
* @since JDK1.1 * @since JDK1.1
*/ */
public boolean isMulticastAddress() { public boolean isMulticastAddress() {
return ((address & 0xf0000000) == 0xe0000000); return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
} }
/** /**
@ -167,7 +168,7 @@ class Inet4Address extends InetAddress {
* @since 1.4 * @since 1.4
*/ */
public boolean isAnyLocalAddress() { public boolean isAnyLocalAddress() {
return address == 0; return holder().getAddress() == 0;
} }
/** /**
@ -195,6 +196,7 @@ class Inet4Address extends InetAddress {
// defined in "Documenting Special Use IPv4 Address Blocks // defined in "Documenting Special Use IPv4 Address Blocks
// that have been Registered with IANA" by Bill Manning // that have been Registered with IANA" by Bill Manning
// draft-manning-dsua-06.txt // draft-manning-dsua-06.txt
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 169) return (((address >>> 24) & 0xFF) == 169)
&& (((address >>> 16) & 0xFF) == 254); && (((address >>> 16) & 0xFF) == 254);
} }
@ -211,6 +213,7 @@ class Inet4Address extends InetAddress {
// 10/8 prefix // 10/8 prefix
// 172.16/12 prefix // 172.16/12 prefix
// 192.168/16 prefix // 192.168/16 prefix
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 10) return (((address >>> 24) & 0xFF) == 10)
|| ((((address >>> 24) & 0xFF) == 172) || ((((address >>> 24) & 0xFF) == 172)
&& (((address >>> 16) & 0xF0) == 16)) && (((address >>> 16) & 0xF0) == 16))
@ -257,6 +260,7 @@ class Inet4Address extends InetAddress {
*/ */
public boolean isMCLinkLocal() { public boolean isMCLinkLocal() {
// 224.0.0/24 prefix and ttl == 1 // 224.0.0/24 prefix and ttl == 1
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 224) return (((address >>> 24) & 0xFF) == 224)
&& (((address >>> 16) & 0xFF) == 0) && (((address >>> 16) & 0xFF) == 0)
&& (((address >>> 8) & 0xFF) == 0); && (((address >>> 8) & 0xFF) == 0);
@ -272,6 +276,7 @@ class Inet4Address extends InetAddress {
*/ */
public boolean isMCSiteLocal() { public boolean isMCSiteLocal() {
// 239.255/16 prefix or ttl < 32 // 239.255/16 prefix or ttl < 32
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 239) return (((address >>> 24) & 0xFF) == 239)
&& (((address >>> 16) & 0xFF) == 255); && (((address >>> 16) & 0xFF) == 255);
} }
@ -287,6 +292,7 @@ class Inet4Address extends InetAddress {
*/ */
public boolean isMCOrgLocal() { public boolean isMCOrgLocal() {
// 239.192 - 239.195 // 239.192 - 239.195
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 239) return (((address >>> 24) & 0xFF) == 239)
&& (((address >>> 16) & 0xFF) >= 192) && (((address >>> 16) & 0xFF) >= 192)
&& (((address >>> 16) & 0xFF) <= 195); && (((address >>> 16) & 0xFF) <= 195);
@ -300,6 +306,7 @@ class Inet4Address extends InetAddress {
* @return the raw IP address of this object. * @return the raw IP address of this object.
*/ */
public byte[] getAddress() { public byte[] getAddress() {
int address = holder().getAddress();
byte[] addr = new byte[INADDRSZ]; byte[] addr = new byte[INADDRSZ];
addr[0] = (byte) ((address >>> 24) & 0xFF); addr[0] = (byte) ((address >>> 24) & 0xFF);
@ -325,7 +332,7 @@ class Inet4Address extends InetAddress {
* @return a hash code value for this IP address. * @return a hash code value for this IP address.
*/ */
public int hashCode() { public int hashCode() {
return address; return holder().getAddress();
} }
/** /**
@ -346,7 +353,7 @@ class Inet4Address extends InetAddress {
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
return (obj != null) && (obj instanceof Inet4Address) && return (obj != null) && (obj instanceof Inet4Address) &&
(((InetAddress)obj).address == address); (((InetAddress)obj).holder().getAddress() == holder().getAddress());
} }
// Utilities // Utilities

View File

@ -40,7 +40,7 @@ class Inet4AddressImpl implements InetAddressImpl {
public synchronized InetAddress anyLocalAddress() { public synchronized InetAddress anyLocalAddress() {
if (anyLocalAddress == null) { if (anyLocalAddress == null) {
anyLocalAddress = new Inet4Address(); // {0x00,0x00,0x00,0x00} anyLocalAddress = new Inet4Address(); // {0x00,0x00,0x00,0x00}
anyLocalAddress.hostName = "0.0.0.0"; anyLocalAddress.holder().hostName = "0.0.0.0";
} }
return anyLocalAddress; return anyLocalAddress;
} }

View File

@ -210,18 +210,18 @@ class Inet6Address extends InetAddress {
Inet6Address() { Inet6Address() {
super(); super();
hostName = null; holder().hostName = null;
ipaddress = new byte[INADDRSZ]; ipaddress = new byte[INADDRSZ];
family = IPv6; holder().family = IPv6;
} }
/* checking of value for scope_id should be done by caller /* checking of value for scope_id should be done by caller
* scope_id must be >= 0, or -1 to indicate not being set * scope_id must be >= 0, or -1 to indicate not being set
*/ */
Inet6Address(String hostName, byte addr[], int scope_id) { Inet6Address(String hostName, byte addr[], int scope_id) {
this.hostName = hostName; holder().hostName = hostName;
if (addr.length == INADDRSZ) { // normal IPv6 address if (addr.length == INADDRSZ) { // normal IPv6 address
family = IPv6; holder().family = IPv6;
ipaddress = addr.clone(); ipaddress = addr.clone();
} }
if (scope_id >= 0) { if (scope_id >= 0) {
@ -335,9 +335,9 @@ class Inet6Address extends InetAddress {
private void initif(String hostName, byte addr[],NetworkInterface nif) private void initif(String hostName, byte addr[],NetworkInterface nif)
throws UnknownHostException throws UnknownHostException
{ {
this.hostName = hostName; holder().hostName = hostName;
if (addr.length == INADDRSZ) { // normal IPv6 address if (addr.length == INADDRSZ) { // normal IPv6 address
family = IPv6; holder().family = IPv6;
ipaddress = addr.clone(); ipaddress = addr.clone();
} }
if (nif != null) { if (nif != null) {
@ -420,6 +420,11 @@ class Inet6Address extends InetAddress {
*/ */
private void readObject(ObjectInputStream s) private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException { throws IOException, ClassNotFoundException {
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
s.defaultReadObject(); s.defaultReadObject();
if (ifname != null && !ifname.equals("")) { if (ifname != null && !ifname.equals("")) {
@ -447,7 +452,7 @@ class Inet6Address extends InetAddress {
ipaddress.length); ipaddress.length);
} }
if (family != IPv6) { if (holder().getFamily() != IPv6) {
throw new InvalidObjectException("invalid address family type"); throw new InvalidObjectException("invalid address family type");
} }
} }

View File

@ -81,7 +81,7 @@ class Inet6AddressImpl implements InetAddressImpl {
if (anyLocalAddress == null) { if (anyLocalAddress == null) {
if (InetAddress.preferIPv6Address) { if (InetAddress.preferIPv6Address) {
anyLocalAddress = new Inet6Address(); anyLocalAddress = new Inet6Address();
anyLocalAddress.hostName = "::"; anyLocalAddress.holder().hostName = "::";
} else { } else {
anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress(); anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();
} }

View File

@ -35,8 +35,12 @@ import java.util.ArrayList;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.security.AccessController; import java.security.AccessController;
import java.io.ObjectStreamException; import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectOutputStream;
import java.io.ObjectOutputStream.PutField;
import sun.security.action.*; import sun.security.action.*;
import sun.net.InetAddressCachePolicy; import sun.net.InetAddressCachePolicy;
import sun.net.util.IPAddressUtil; import sun.net.util.IPAddressUtil;
@ -199,26 +203,49 @@ class InetAddress implements java.io.Serializable {
/* Specify address family preference */ /* Specify address family preference */
static transient boolean preferIPv6Address = false; static transient boolean preferIPv6Address = false;
/** static class InetAddressHolder {
* @serial
*/ InetAddressHolder() {}
InetAddressHolder(String hostName, int address, int family) {
this.hostName = hostName;
this.address = address;
this.family = family;
}
String hostName; String hostName;
String getHostName() {
return hostName;
}
/** /**
* Holds a 32-bit IPv4 address. * Holds a 32-bit IPv4 address.
*
* @serial
*/ */
int address; int address;
int getAddress() {
return address;
}
/** /**
* Specifies the address family type, for instance, '1' for IPv4 * Specifies the address family type, for instance, '1' for IPv4
* addresses, and '2' for IPv6 addresses. * addresses, and '2' for IPv6 addresses.
*
* @serial
*/ */
int family; int family;
int getFamily() {
return family;
}
}
/* Used to store the serializable fields of InetAddress */
private final transient InetAddressHolder holder;
InetAddressHolder holder() {
return holder;
}
/* Used to store the name service provider */ /* Used to store the name service provider */
private static List<NameService> nameServices = null; private static List<NameService> nameServices = null;
@ -251,6 +278,7 @@ class InetAddress implements java.io.Serializable {
* put in the address cache, since it is not created by name. * put in the address cache, since it is not created by name.
*/ */
InetAddress() { InetAddress() {
holder = new InetAddressHolder();
} }
/** /**
@ -263,7 +291,7 @@ class InetAddress implements java.io.Serializable {
*/ */
private Object readResolve() throws ObjectStreamException { private Object readResolve() throws ObjectStreamException {
// will replace the deserialized 'this' object // will replace the deserialized 'this' object
return new Inet4Address(this.hostName, this.address); return new Inet4Address(holder().getHostName(), holder().getAddress());
} }
/** /**
@ -500,10 +528,10 @@ class InetAddress implements java.io.Serializable {
* @see SecurityManager#checkConnect * @see SecurityManager#checkConnect
*/ */
String getHostName(boolean check) { String getHostName(boolean check) {
if (hostName == null) { if (holder().getHostName() == null) {
hostName = InetAddress.getHostFromNameService(this, check); holder().hostName = InetAddress.getHostFromNameService(this, check);
} }
return hostName; return holder().getHostName();
} }
/** /**
@ -666,6 +694,7 @@ class InetAddress implements java.io.Serializable {
* @return a string representation of this IP address. * @return a string representation of this IP address.
*/ */
public String toString() { public String toString() {
String hostName = holder().getHostName();
return ((hostName != null) ? hostName : "") return ((hostName != null) ? hostName : "")
+ "/" + getHostAddress(); + "/" + getHostAddress();
} }
@ -1522,14 +1551,58 @@ class InetAddress implements java.io.Serializable {
} }
} }
private static final long FIELDS_OFFSET;
private static final sun.misc.Unsafe UNSAFE;
static {
try {
sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
FIELDS_OFFSET = unsafe.objectFieldOffset(
InetAddress.class.getDeclaredField("holder")
);
UNSAFE = unsafe;
} catch (ReflectiveOperationException e) {
throw new Error(e);
}
}
private void readObject (ObjectInputStream s) throws private void readObject (ObjectInputStream s) throws
IOException, ClassNotFoundException { IOException, ClassNotFoundException {
s.defaultReadObject ();
if (getClass().getClassLoader() != null) { if (getClass().getClassLoader() != null) {
hostName = null;
address = 0;
throw new SecurityException ("invalid address type"); throw new SecurityException ("invalid address type");
} }
GetField gf = s.readFields();
String host = (String)gf.get("hostName", null);
int address= gf.get("address", 0);
int family= gf.get("family", 0);
InetAddressHolder h = new InetAddressHolder(host, address, family);
UNSAFE.putObject(this, FIELDS_OFFSET, h);
}
/* needed because the serializable fields no longer exist */
/**
* @serialField hostName String
* @serialField address int
* @serialField family int
*/
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("hostName", String.class),
new ObjectStreamField("address", int.class),
new ObjectStreamField("family", int.class),
};
private void writeObject (ObjectOutputStream s) throws
IOException {
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
PutField pf = s.putFields();
pf.put("hostName", holder().getHostName());
pf.put("address", holder().getAddress());
pf.put("family", holder().getFamily());
s.writeFields();
s.flush();
} }
} }

View File

@ -87,8 +87,8 @@ public class InetSocketAddress
if (hostname != null) if (hostname != null)
return hostname; return hostname;
if (addr != null) { if (addr != null) {
if (addr.hostName != null) if (addr.holder().getHostName() != null)
return addr.hostName; return addr.holder().getHostName();
else else
return addr.getHostAddress(); return addr.getHostAddress();
} }

View File

@ -33,8 +33,11 @@
*/ */
jclass ia_class; jclass ia_class;
jfieldID ia_addressID; jclass iac_class;
jfieldID ia_familyID; jfieldID ia_holderID;
jfieldID iac_addressID;
jfieldID iac_familyID;
jfieldID iac_hostNameID;
jfieldID ia_preferIPv6AddressID; jfieldID ia_preferIPv6AddressID;
/* /*
@ -48,10 +51,18 @@ Java_java_net_InetAddress_init(JNIEnv *env, jclass cls) {
CHECK_NULL(c); CHECK_NULL(c);
ia_class = (*env)->NewGlobalRef(env, c); ia_class = (*env)->NewGlobalRef(env, c);
CHECK_NULL(ia_class); CHECK_NULL(ia_class);
ia_addressID = (*env)->GetFieldID(env, ia_class, "address", "I"); c = (*env)->FindClass(env,"java/net/InetAddress$InetAddressHolder");
CHECK_NULL(ia_addressID); CHECK_NULL(c);
ia_familyID = (*env)->GetFieldID(env, ia_class, "family", "I"); iac_class = (*env)->NewGlobalRef(env, c);
CHECK_NULL(ia_familyID); ia_holderID = (*env)->GetFieldID(env, ia_class, "holder", "Ljava/net/InetAddress$InetAddressHolder;");
CHECK_NULL(ia_holderID);
ia_preferIPv6AddressID = (*env)->GetStaticFieldID(env, ia_class, "preferIPv6Address", "Z"); ia_preferIPv6AddressID = (*env)->GetStaticFieldID(env, ia_class, "preferIPv6Address", "Z");
CHECK_NULL(ia_preferIPv6AddressID); CHECK_NULL(ia_preferIPv6AddressID);
iac_addressID = (*env)->GetFieldID(env, iac_class, "address", "I");
CHECK_NULL(iac_addressID);
iac_familyID = (*env)->GetFieldID(env, iac_class, "family", "I");
CHECK_NULL(iac_familyID);
iac_hostNameID = (*env)->GetFieldID(env, iac_class, "hostName", "Ljava/lang/String;");
CHECK_NULL(iac_hostNameID);
} }

View File

@ -84,6 +84,58 @@ void init(JNIEnv *env) {
} }
} }
/* The address, and family fields used to be in InetAddress
* but are now in an implementation object. So, there is an extra
* level of indirection to access them now.
*/
extern jclass iac_class;
extern jfieldID ia_holderID;
extern jfieldID iac_addressID;
extern jfieldID iac_familyID;
void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address) {
jobject holder;
init(env);
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;
init(env);
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;
init(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
(*env)->SetObjectField(env, holder, iac_hostNameID, host);
}
int getInetAddress_addr(JNIEnv *env, jobject iaObj) {
jobject holder;
init(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetIntField(env, holder, iac_addressID);
}
int getInetAddress_family(JNIEnv *env, jobject iaObj) {
jobject holder;
init(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetIntField(env, holder, iac_familyID);
}
jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj) {
jobject holder;
init(env);
holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
return (*env)->GetObjectField(env, holder, iac_hostNameID);
}
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
jobject iaObj; jobject iaObj;
@ -110,8 +162,8 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
CHECK_NULL_RETURN(iaObj, NULL); CHECK_NULL_RETURN(iaObj, NULL);
address = NET_IPv4MappedToIPv4(caddr); address = NET_IPv4MappedToIPv4(caddr);
(*env)->SetIntField(env, iaObj, ia_addressID, address); setInetAddress_addr(env, iaObj, address);
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); setInetAddress_family(env, iaObj, IPv4);
} else { } else {
static jclass inet6Cls = 0; static jclass inet6Cls = 0;
jint scope; jint scope;
@ -131,7 +183,7 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
(*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress); (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
(*env)->SetIntField(env, iaObj, ia_familyID, IPv6); setInetAddress_family(env, iaObj, IPv6);
scope = getScopeID(him); scope = getScopeID(him);
(*env)->SetIntField(env, iaObj, ia6_scopeidID, scope); (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
if (scope > 0) if (scope > 0)
@ -153,9 +205,8 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
} }
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
CHECK_NULL_RETURN(iaObj, NULL); CHECK_NULL_RETURN(iaObj, NULL);
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); setInetAddress_family(env, iaObj, IPv4);
(*env)->SetIntField(env, iaObj, ia_addressID, setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr));
ntohl(him4->sin_addr.s_addr));
*port = ntohs(him4->sin_port); *port = ntohs(him4->sin_port);
} }
return iaObj; return iaObj;
@ -167,8 +218,7 @@ NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
jint family = AF_INET; jint family = AF_INET;
#ifdef AF_INET6 #ifdef AF_INET6
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
AF_INET : AF_INET6;
if (him->sa_family == AF_INET6) { if (him->sa_family == AF_INET6) {
#ifdef WIN32 #ifdef WIN32
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
@ -183,7 +233,7 @@ NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
return JNI_FALSE; return JNI_FALSE;
} }
addrNew = NET_IPv4MappedToIPv4(caddrNew); addrNew = NET_IPv4MappedToIPv4(caddrNew);
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); addrCur = getInetAddress_addr(env, iaObj);
if (addrNew == addrCur) { if (addrNew == addrCur) {
return JNI_TRUE; return JNI_TRUE;
} else { } else {
@ -215,7 +265,7 @@ NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
return JNI_FALSE; return JNI_FALSE;
} }
addrNew = ntohl(him4->sin_addr.s_addr); addrNew = ntohl(him4->sin_addr.s_addr);
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); addrCur = getInetAddress_addr(env, iaObj);
if (addrNew == addrCur) { if (addrNew == addrCur) {
return JNI_TRUE; return JNI_TRUE;
} else { } else {

View File

@ -53,10 +53,18 @@
* i.e. psi_timeoutID is PlainSocketImpl's timeout field's ID. * i.e. psi_timeoutID is PlainSocketImpl's timeout field's ID.
*/ */
extern jclass ia_class; extern jclass ia_class;
extern jfieldID ia_addressID; extern jfieldID iac_addressID;
extern jfieldID ia_familyID; extern jfieldID iac_familyID;
extern jfieldID iac_hostNameID;
extern jfieldID ia_preferIPv6AddressID; extern jfieldID ia_preferIPv6AddressID;
extern void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address);
extern void setInetAddress_family(JNIEnv *env, jobject iaObj, int family);
extern void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject h);
extern int getInetAddress_addr(JNIEnv *env, jobject iaObj);
extern int getInetAddress_family(JNIEnv *env, jobject iaObj);
extern jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj);
extern jclass ia4_class; extern jclass ia4_class;
extern jmethodID ia4_ctrID; extern jmethodID ia4_ctrID;

View File

@ -135,9 +135,6 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<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;");
initialized = 1; initialized = 1;
} }
@ -238,9 +235,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)(iterator->ai_addr))->sin_addr.s_addr));
ntohl(((struct sockaddr_in*)(iterator->ai_addr))->sin_addr.s_addr)); setInetAddress_hostName(env, iaObj, name);
(*env)->SetObjectField(env, iaObj, ni_iahostID, name);
(*env)->SetObjectArrayElement(env, ret, retLen - i -1, iaObj); (*env)->SetObjectArrayElement(env, ret, retLen - i -1, iaObj);
i++; i++;
iterator = iterator->ai_next; iterator = iterator->ai_next;
@ -372,9 +368,6 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
static jclass ni_iacls; static jclass ni_iacls;
static jclass ni_ia4cls; static jclass ni_ia4cls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iahostID;
static jfieldID ni_iafamilyID;
static int initialized = 0; static int initialized = 0;
/* /*
@ -403,9 +396,6 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<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;");
initialized = 1; initialized = 1;
} }
@ -499,9 +489,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host);
(*env)->SetObjectArrayElement(env, ret, i++, iaObj); (*env)->SetObjectArrayElement(env, ret, i++, iaObj);
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }

View File

@ -120,9 +120,6 @@ static jclass ni_ia4cls;
static jclass ni_ia6cls; static jclass ni_ia6cls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID; static jmethodID ni_ia6ctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iahostID;
static jfieldID ni_iafamilyID;
static jfieldID ni_ia6ipaddressID; static jfieldID ni_ia6ipaddressID;
static int initialized = 0; static int initialized = 0;
@ -159,9 +156,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls); ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<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"); ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
initialized = 1; initialized = 1;
} }
@ -315,9 +309,8 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host);
(*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj); (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
inetIndex++; inetIndex++;
} else if (iterator->ai_family == AF_INET6) { } else if (iterator->ai_family == AF_INET6) {
@ -342,7 +335,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
} }
(*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress); (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj); (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
inet6Index++; inet6Index++;
} }

View File

@ -118,8 +118,6 @@ static jclass ni_ibcls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID; static jmethodID ni_ia6ctrID;
static jmethodID ni_ibctrID; static jmethodID ni_ibctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iafamilyID;
static jfieldID ni_ia6ipaddressID; static jfieldID ni_ia6ipaddressID;
static jfieldID ni_ibaddressID; static jfieldID ni_ibaddressID;
static jfieldID ni_ib4broadcastID; static jfieldID ni_ib4broadcastID;
@ -195,8 +193,6 @@ Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V"); ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V"); ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B"); ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;"); ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;"); ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
@ -300,7 +296,7 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
netif *ifs, *curr; netif *ifs, *curr;
#ifdef AF_INET6 #ifdef AF_INET6
int family = ( (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4 ) ? AF_INET : AF_INET6; int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
#else #else
int family = AF_INET; int family = AF_INET;
#endif #endif
@ -325,7 +321,7 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
if (family == addrP->family) { if (family == addrP->family) {
if (family == AF_INET) { if (family == AF_INET) {
int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr); int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
int address2 = (*env)->GetIntField(env, iaObj, ni_iaaddressID); int address2 = getInetAddress_addr(env, iaObj);
if (address1 == address2) { if (address1 == address2) {
match = JNI_TRUE; match = JNI_TRUE;
@ -651,7 +647,7 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
if (addrP->family == AF_INET) { if (addrP->family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID); iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (iaObj) { if (iaObj) {
(*env)->SetIntField(env, iaObj, ni_iaaddressID, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr)); setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
} }
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj) { if (ibObj) {
@ -660,8 +656,7 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
jobject ia2Obj = NULL; jobject ia2Obj = NULL;
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID); ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (ia2Obj) { if (ia2Obj) {
(*env)->SetIntField(env, ia2Obj, ni_iaaddressID, setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
(*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj); (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
(*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask); (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
} }

View File

@ -552,14 +552,13 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
#ifdef AF_INET6 #ifdef AF_INET6
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
AF_INET : AF_INET6;
#else #else
family = AF_INET; family = AF_INET;
#endif #endif
if (family == AF_INET) { /* this API can't handle IPV6 addresses */ if (family == AF_INET) { /* this API can't handle IPV6 addresses */
int address = (*env)->GetIntField(env, iaObj, ia_addressID); int address = getInetAddress_addr(env, iaObj);
(*env)->SetIntField(env, addressObj, ia_addressID, address); setInetAddress_addr(env, addressObj, address);
} }
return port; return port;
} }
@ -1028,23 +1027,18 @@ Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
*/ */
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) { static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) {
static jfieldID ni_addrsID; static jfieldID ni_addrsID;
static jfieldID ia_addressID;
struct in_addr in; struct in_addr in;
jobjectArray addrArray; jobjectArray addrArray;
jsize len; jsize len;
jobject addr; jobject addr;
int i; int i;
if (ni_addrsID == NULL || ia_addressID == NULL) { if (ni_addrsID == NULL ) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
CHECK_NULL(c); CHECK_NULL(c);
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
"[Ljava/net/InetAddress;"); "[Ljava/net/InetAddress;");
CHECK_NULL(ni_addrsID); CHECK_NULL(ni_addrsID);
c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL(c);
ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL(ia_addressID);
} }
addrArray = (*env)->GetObjectField(env, value, ni_addrsID); addrArray = (*env)->GetObjectField(env, value, ni_addrsID);
@ -1065,8 +1059,8 @@ static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject val
*/ */
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
addr = (*env)->GetObjectArrayElement(env, addrArray, i); addr = (*env)->GetObjectArrayElement(env, addrArray, i);
if ((*env)->GetIntField(env, addr, ia_familyID) == IPv4) { if (getInetAddress_family(env, addr) == IPv4) {
in.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); in.s_addr = htonl(getInetAddress_addr(env, addr));
break; break;
} }
} }
@ -1116,17 +1110,9 @@ static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject val
* Throw exception if failed. * Throw exception if failed.
*/ */
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) { static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) {
static jfieldID ia_addressID;
struct in_addr in; struct in_addr in;
if (ia_addressID == NULL) { in.s_addr = htonl( getInetAddress_addr(env, value) );
jclass c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL(c);
ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL(ia_addressID);
}
in.s_addr = htonl( (*env)->GetIntField(env, value, ia_addressID) );
if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
(const char*)&in, sizeof(in)) < 0) { (const char*)&in, sizeof(in)) < 0) {
@ -1456,7 +1442,6 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
if (isIPV4) { if (isIPV4) {
static jclass inet4_class; static jclass inet4_class;
static jmethodID inet4_ctrID; static jmethodID inet4_ctrID;
static jfieldID inet4_addrID;
static jclass ni_class; static jclass ni_class;
static jmethodID ni_ctrID; static jmethodID ni_ctrID;
@ -1486,15 +1471,13 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
CHECK_NULL_RETURN(c, NULL); CHECK_NULL_RETURN(c, NULL);
inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
CHECK_NULL_RETURN(inet4_ctrID, NULL); CHECK_NULL_RETURN(inet4_ctrID, NULL);
inet4_addrID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL_RETURN(inet4_addrID, NULL);
inet4_class = (*env)->NewGlobalRef(env, c); inet4_class = (*env)->NewGlobalRef(env, c);
CHECK_NULL_RETURN(inet4_class, NULL); CHECK_NULL_RETURN(inet4_class, NULL);
} }
addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
CHECK_NULL_RETURN(addr, NULL); CHECK_NULL_RETURN(addr, NULL);
(*env)->SetIntField(env, addr, inet4_addrID, ntohl(in.s_addr)); setInetAddress_addr(env, addr, ntohl(in.s_addr));
/* /*
* For IP_MULTICAST_IF return InetAddress * For IP_MULTICAST_IF return InetAddress
@ -1942,7 +1925,7 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
ipv6_join_leave = ipv6_available(); ipv6_join_leave = ipv6_available();
#ifdef __linux__ #ifdef __linux__
if ((*env)->GetIntField(env, iaObj, ia_familyID) == IPv4) { if (getInetAddress_family(env, iaObj) == IPv4) {
ipv6_join_leave = JNI_FALSE; ipv6_join_leave = JNI_FALSE;
} }
#endif #endif
@ -1989,7 +1972,7 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
CHECK_NULL(ni_indexID); CHECK_NULL(ni_indexID);
} }
mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
mname.imr_address.s_addr = 0; mname.imr_address.s_addr = 0;
mname.imr_ifindex = (*env)->GetIntField(env, niObj, ni_indexID); mname.imr_ifindex = (*env)->GetIntField(env, niObj, ni_indexID);
mname_len = sizeof(struct ip_mreqn); mname_len = sizeof(struct ip_mreqn);
@ -2007,11 +1990,11 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
} }
addr = (*env)->GetObjectArrayElement(env, addrArray, 0); addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
#ifdef __linux__ #ifdef __linux__
mname.imr_address.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); mname.imr_address.s_addr = htonl(getInetAddress_addr(env, addr));
#else #else
mname.imr_interface.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); mname.imr_interface.s_addr = htonl(getInetAddress_addr(env, addr));
#endif #endif
mname_len = sizeof(struct ip_mreq); mname_len = sizeof(struct ip_mreq);
} }
@ -2046,7 +2029,7 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
return; return;
} }
mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
mname.imr_address.s_addr = 0 ; mname.imr_address.s_addr = 0 ;
mname.imr_ifindex = index; mname.imr_ifindex = index;
mname_len = sizeof(struct ip_mreqn); mname_len = sizeof(struct ip_mreqn);
@ -2068,7 +2051,7 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
#else #else
mname.imr_interface.s_addr = in.s_addr; mname.imr_interface.s_addr = in.s_addr;
#endif #endif
mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
mname_len = sizeof(struct ip_mreq); mname_len = sizeof(struct ip_mreq);
} }
} }
@ -2133,10 +2116,10 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
jbyte caddr[16]; jbyte caddr[16];
jint family; jint family;
jint address; jint address;
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? AF_INET : AF_INET6; family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
if (family == AF_INET) { /* will convert to IPv4-mapped address */ if (family == AF_INET) { /* will convert to IPv4-mapped address */
memset((char *) caddr, 0, 16); memset((char *) caddr, 0, 16);
address = (*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
caddr[10] = 0xff; caddr[10] = 0xff;
caddr[11] = 0xff; caddr[11] = 0xff;

View File

@ -777,7 +777,7 @@ JNIEXPORT int JNICALL
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
int *len, jboolean v4MappedAddress) { int *len, jboolean v4MappedAddress) {
jint family; jint family;
family = (*env)->GetIntField(env, iaObj, ia_familyID); family = getInetAddress_family(env, iaObj);
#ifdef AF_INET6 #ifdef AF_INET6
/* needs work. 1. family 2. clean up him6 etc deallocate memory */ /* needs work. 1. family 2. clean up him6 etc deallocate memory */
if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) { if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) {
@ -789,7 +789,7 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
if (family == IPv4) { /* will convert to IPv4-mapped address */ if (family == IPv4) { /* will convert to IPv4-mapped address */
memset((char *) caddr, 0, 16); memset((char *) caddr, 0, 16);
address = (*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
if (address == INADDR_ANY) { if (address == INADDR_ANY) {
/* we would always prefer IPv6 wildcard address /* we would always prefer IPv6 wildcard address
caddr[10] = 0xff; caddr[10] = 0xff;
@ -898,7 +898,7 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
return -1; return -1;
} }
memset((char *) him4, 0, sizeof(struct sockaddr_in)); memset((char *) him4, 0, sizeof(struct sockaddr_in));
address = (*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
him4->sin_port = htons((short) port); him4->sin_port = htons((short) port);
him4->sin_addr.s_addr = (uint32_t) htonl(address); him4->sin_addr.s_addr = (uint32_t) htonl(address);
him4->sin_family = AF_INET; him4->sin_family = AF_INET;

View File

@ -102,7 +102,7 @@ class TwoStacksPlainDatagramSocketImpl extends AbstractPlainDatagramSocketImpl
if ((fd != null && fd1 != null) && !connected) { if ((fd != null && fd1 != null) && !connected) {
return anyLocalBoundAddr; return anyLocalBoundAddr;
} }
int family = connectedAddress == null ? -1 : connectedAddress.family; int family = connectedAddress == null ? -1 : connectedAddress.holder().getFamily();
return socketLocalAddress(family); return socketLocalAddress(family);
} else } else
return super.getOption(optID); return super.getOption(optID);

View File

@ -114,9 +114,6 @@ Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
static jclass ni_iacls; static jclass ni_iacls;
static jclass ni_ia4cls; static jclass ni_ia4cls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iahostID;
static jfieldID ni_iafamilyID;
static int initialized = 0; static int initialized = 0;
/* /*
@ -149,9 +146,6 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<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;");
initialized = 1; initialized = 1;
} }
@ -208,8 +202,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl(address));
ntohl(address));
(*env)->SetObjectArrayElement(env, ret, 0, iaObj); (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
JNU_ReleaseStringPlatformChars(env, host, hostname); JNU_ReleaseStringPlatformChars(env, host, hostname);
return ret; return ret;
@ -242,9 +235,8 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl((*addrp)->s_addr));
ntohl((*addrp)->s_addr)); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host);
(*env)->SetObjectArrayElement(env, ret, i, iaObj); (*env)->SetObjectArrayElement(env, ret, i, iaObj);
addrp++; addrp++;
i++; i++;

View File

@ -77,9 +77,6 @@ static jclass ni_ia4cls;
static jclass ni_ia6cls; static jclass ni_ia6cls;
static jmethodID ni_ia4ctrID; static jmethodID ni_ia4ctrID;
static jmethodID ni_ia6ctrID; static jmethodID ni_ia6ctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iahostID;
static jfieldID ni_iafamilyID;
static jfieldID ni_ia6ipaddressID; static jfieldID ni_ia6ipaddressID;
static int initialized = 0; static int initialized = 0;
@ -104,9 +101,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls); ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<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"); ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
initialized = 1; initialized = 1;
} }
@ -243,9 +237,8 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
(*env)->SetIntField(env, iaObj, ni_iaaddressID, setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host);
(*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj); (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
inetIndex ++; inetIndex ++;
} else if (iterator->ai_family == AF_INET6) { } else if (iterator->ai_family == AF_INET6) {
@ -269,7 +262,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
} }
(*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress); (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
(*env)->SetObjectField(env, iaObj, ni_iahostID, host); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj); (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
inet6Index ++; inet6Index ++;
} }

View File

@ -66,7 +66,6 @@ jfieldID ni_nameID; /* NetworkInterface.name */
jfieldID ni_displayNameID; /* NetworkInterface.displayName */ jfieldID ni_displayNameID; /* NetworkInterface.displayName */
jfieldID ni_childsID; /* NetworkInterface.childs */ jfieldID ni_childsID; /* NetworkInterface.childs */
jclass ni_iacls; /* InetAddress */ jclass ni_iacls; /* InetAddress */
jfieldID ni_iaAddr; /* InetAddress.address */
jclass ni_ia4cls; /* Inet4Address */ jclass ni_ia4cls; /* Inet4Address */
jmethodID ni_ia4Ctor; /* Inet4Address() */ jmethodID ni_ia4Ctor; /* Inet4Address() */
@ -480,7 +479,6 @@ Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls)
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress"); ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls); ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
ni_iaAddr = (*env)->GetFieldID(env, ni_iacls, "address", "I");
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
@ -568,7 +566,7 @@ jobject createNetworkInterface
} }
/* default ctor will set family to AF_INET */ /* default ctor will set family to AF_INET */
(*env)->SetIntField(env, iaObj, ni_iaAddr, ntohl(addrs->addr.him4.sin_addr.s_addr)); setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr));
if (addrs->mask != -1) { if (addrs->mask != -1) {
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj == NULL) { if (ibObj == NULL) {
@ -581,8 +579,7 @@ jobject createNetworkInterface
free_netaddr(netaddrP); free_netaddr(netaddrP);
return NULL; return NULL;
} }
(*env)->SetIntField(env, ia2Obj, ni_iaAddr, setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr));
ntohl(addrs->brdcast.him4.sin_addr.s_addr));
(*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj); (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
@ -736,7 +733,7 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
(JNIEnv *env, jclass cls, jobject iaObj) (JNIEnv *env, jclass cls, jobject iaObj)
{ {
netif *ifList, *curr; netif *ifList, *curr;
jint addr = (*env)->GetIntField(env, iaObj, ni_iaAddr); jint addr = getInetAddress_addr(env, iaObj);
jobject netifObj = NULL; jobject netifObj = NULL;
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack

View File

@ -71,7 +71,6 @@ extern jfieldID ni_displayNameID; /* NetworkInterface.displayName */
extern jfieldID ni_childsID; /* NetworkInterface.childs */ extern jfieldID ni_childsID; /* NetworkInterface.childs */
extern jclass ni_iacls; /* InetAddress */ extern jclass ni_iacls; /* InetAddress */
extern jfieldID ni_iaAddr; /* InetAddress.address */
extern jclass ni_ia4cls; /* Inet4Address */ extern jclass ni_ia4cls; /* Inet4Address */
extern jmethodID ni_ia4Ctor; /* Inet4Address() */ extern jmethodID ni_ia4Ctor; /* Inet4Address() */

View File

@ -33,6 +33,7 @@
#include "jni_util.h" #include "jni_util.h"
#include "NetworkInterface.h" #include "NetworkInterface.h"
#include "net_util.h"
/* /*
* Windows implementation of the java.net.NetworkInterface native methods. * Windows implementation of the java.net.NetworkInterface native methods.
@ -477,7 +478,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
} }
/* default ctor will set family to AF_INET */ /* default ctor will set family to AF_INET */
(*env)->SetIntField(env, iaObj, ni_iaAddr, ntohl(addrs->addr.him4.sin_addr.s_addr)); setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr));
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj == NULL) { if (ibObj == NULL) {
@ -490,8 +491,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
free_netaddr(netaddrP); free_netaddr(netaddrP);
return NULL; return NULL;
} }
(*env)->SetIntField(env, ia2Obj, ni_iaAddr, setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr));
ntohl(addrs->brdcast.him4.sin_addr.s_addr));
(*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj); (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);

View File

@ -432,7 +432,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
int lcladdrlen; int lcladdrlen;
int address; int address;
family = (*env)->GetIntField(env, addressObj, ia_familyID); family = getInetAddress_family(env, addressObj);
if (family == IPv6 && !ipv6_supported) { if (family == IPv6 && !ipv6_supported) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
"Protocol family not supported"); "Protocol family not supported");
@ -452,7 +452,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
JNU_ThrowNullPointerException(env, "argument address"); JNU_ThrowNullPointerException(env, "argument address");
return; return;
} else { } else {
address = (*env)->GetIntField(env, addressObj, ia_addressID); address = getInetAddress_addr(env, addressObj);
} }
if (NET_InetAddressToSockaddr(env, addressObj, port, (struct sockaddr *)&lcladdr, &lcladdrlen, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, addressObj, port, (struct sockaddr *)&lcladdr, &lcladdrlen, JNI_FALSE) != 0) {
@ -552,9 +552,9 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_connect0(JNIEnv *env, jobject thi
return; return;
} }
addr = (*env)->GetIntField(env, address, ia_addressID); addr = getInetAddress_addr(env, address);
family = (*env)->GetIntField(env, address, ia_familyID); family = getInetAddress_family(env, address);
if (family == IPv6 && !ipv6_supported) { if (family == IPv6 && !ipv6_supported) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
"Protocol family not supported"); "Protocol family not supported");
@ -670,7 +670,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
return; return;
} }
family = (*env)->GetIntField(env, iaObj, ia_familyID); family = getInetAddress_family(env, iaObj);
if (family == IPv4) { if (family == IPv4) {
fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
} else { } else {
@ -714,7 +714,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
if (!w2k_or_later) { /* avoid this check on Win 2K or better. Does not work with IPv6. if (!w2k_or_later) { /* avoid this check on Win 2K or better. Does not work with IPv6.
* Check is not necessary on these OSes */ * Check is not necessary on these OSes */
if (connected) { if (connected) {
address = (*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
} else { } else {
address = ntohl(rmtaddr.him4.sin_addr.s_addr); address = ntohl(rmtaddr.him4.sin_addr.s_addr);
} }
@ -823,7 +823,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
if (IS_NULL(addressObj)) { if (IS_NULL(addressObj)) {
JNU_ThrowNullPointerException(env, "Null address in peek()"); JNU_ThrowNullPointerException(env, "Null address in peek()");
} else { } else {
address = (*env)->GetIntField(env, addressObj, ia_addressID); address = getInetAddress_addr(env, addressObj);
/* We only handle IPv4 for now. Will support IPv6 once its in the os */ /* We only handle IPv4 for now. Will support IPv6 once its in the os */
family = AF_INET; family = AF_INET;
} }
@ -905,9 +905,8 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", 0); JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", 0);
return 0; return 0;
} }
(*env)->SetIntField(env, addressObj, ia_addressID, setInetAddress_addr(env, addressObj, ntohl(remote_addr.sin_addr.s_addr));
ntohl(remote_addr.sin_addr.s_addr)); setInetAddress_family(env, addressObj, IPv4);
(*env)->SetIntField(env, addressObj, ia_familyID, IPv4);
/* return port */ /* return port */
return ntohs(remote_addr.sin_port); return ntohs(remote_addr.sin_port);
@ -1574,21 +1573,16 @@ static int getInetAddrFromIf (JNIEnv *env, int family, jobject nif, jobject *iad
{ {
jobjectArray addrArray; jobjectArray addrArray;
static jfieldID ni_addrsID=0; static jfieldID ni_addrsID=0;
static jfieldID ia_familyID=0;
jsize len; jsize len;
jobject addr; jobject addr;
int i; int i;
if (ni_addrsID == NULL || ia_familyID == NULL) { if (ni_addrsID == NULL ) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
CHECK_NULL_RETURN (c, -1); CHECK_NULL_RETURN (c, -1);
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
"[Ljava/net/InetAddress;"); "[Ljava/net/InetAddress;");
CHECK_NULL_RETURN (ni_addrsID, -1); CHECK_NULL_RETURN (ni_addrsID, -1);
c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL_RETURN (c, -1);
ia_familyID = (*env)->GetFieldID(env, c, "family", "I");
CHECK_NULL_RETURN (ia_familyID, -1);
} }
addrArray = (*env)->GetObjectField(env, nif, ni_addrsID); addrArray = (*env)->GetObjectField(env, nif, ni_addrsID);
@ -1606,7 +1600,7 @@ static int getInetAddrFromIf (JNIEnv *env, int family, jobject nif, jobject *iad
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
int fam; int fam;
addr = (*env)->GetObjectArrayElement(env, addrArray, i); addr = (*env)->GetObjectArrayElement(env, addrArray, i);
fam = (*env)->GetIntField(env, addr, ia_familyID); fam = getInetAddress_family(env, addr);
if (fam == family) { if (fam == family) {
*iaddr = addr; *iaddr = addr;
return 0; return 0;
@ -1618,20 +1612,13 @@ static int getInetAddrFromIf (JNIEnv *env, int family, jobject nif, jobject *iad
static int getInet4AddrFromIf (JNIEnv *env, jobject nif, struct in_addr *iaddr) static int getInet4AddrFromIf (JNIEnv *env, jobject nif, struct in_addr *iaddr)
{ {
jobject addr; jobject addr;
static jfieldID ia_addressID;
int ret = getInetAddrFromIf (env, IPv4, nif, &addr); int ret = getInetAddrFromIf (env, IPv4, nif, &addr);
if (ret == -1) { if (ret == -1) {
return -1; return -1;
} }
if (ia_addressID == 0) { iaddr->s_addr = htonl(getInetAddress_addr(env, addr));
jclass c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL_RETURN (c, -1);
ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL_RETURN (ia_addressID, -1);
}
iaddr->s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
return 0; return 0;
} }
@ -1706,17 +1693,9 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
} }
opt = java_net_SocketOptions_IP_MULTICAST_IF2; opt = java_net_SocketOptions_IP_MULTICAST_IF2;
} else { } else {
static jfieldID ia_addressID;
struct in_addr in; struct in_addr in;
if (ia_addressID == NULL) { in.s_addr = htonl(getInetAddress_addr(env, value));
jclass c = (*env)->FindClass(env,"java/net/InetAddress");
CHECK_NULL(c);
ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL(ia_addressID);
}
in.s_addr = htonl((*env)->GetIntField(env, value, ia_addressID));
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
(const char*)&in, sizeof(in)) < 0) { (const char*)&in, sizeof(in)) < 0) {
@ -1945,7 +1924,6 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint o
if (isIPV4) { if (isIPV4) {
static jclass inet4_class; static jclass inet4_class;
static jmethodID inet4_ctrID; static jmethodID inet4_ctrID;
static jfieldID inet4_addrID;
static jclass ni_class; static jclass ni_class;
static jmethodID ni_ctrID; static jmethodID ni_ctrID;
@ -1975,15 +1953,13 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint o
CHECK_NULL_RETURN(c, NULL); CHECK_NULL_RETURN(c, NULL);
inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
CHECK_NULL_RETURN(inet4_ctrID, NULL); CHECK_NULL_RETURN(inet4_ctrID, NULL);
inet4_addrID = (*env)->GetFieldID(env, c, "address", "I");
CHECK_NULL_RETURN(inet4_addrID, NULL);
inet4_class = (*env)->NewGlobalRef(env, c); inet4_class = (*env)->NewGlobalRef(env, c);
CHECK_NULL_RETURN(inet4_class, NULL); CHECK_NULL_RETURN(inet4_class, NULL);
} }
addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
CHECK_NULL_RETURN(addr, NULL); CHECK_NULL_RETURN(addr, NULL);
(*env)->SetIntField(env, addr, inet4_addrID, ntohl(in.s_addr)); setInetAddress_addr(env, addr, ntohl(in.s_addr));
/* /*
* For IP_MULTICAST_IF return InetAddress * For IP_MULTICAST_IF return InetAddress

View File

@ -411,7 +411,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
fdObj = (*env)->GetObjectField(env, this, psi_fdID); fdObj = (*env)->GetObjectField(env, this, psi_fdID);
fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
family = (*env)->GetIntField(env, iaObj, ia_familyID); family = getInetAddress_family(env, iaObj);
if (family == IPv6 && !ipv6_supported) { if (family == IPv6 && !ipv6_supported) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -724,9 +724,8 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
return; return;
} }
(*env)->SetIntField(env, socketAddressObj, ia_addressID, setInetAddress_addr(env, socketAddressObj, ntohl(him.him4.sin_addr.s_addr));
ntohl(him.him4.sin_addr.s_addr)); setInetAddress_family(env, socketAddressObj, IPv4);
(*env)->SetIntField(env, socketAddressObj, ia_familyID, IPv4);
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
} else { } else {
jbyteArray addr; jbyteArray addr;
@ -754,7 +753,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
} }
addr = (*env)->GetObjectField (env, socketAddressObj, ia6_ipaddressID); addr = (*env)->GetObjectField (env, socketAddressObj, ia6_ipaddressID);
(*env)->SetByteArrayRegion (env, addr, 0, 16, (const char *)&him.him6.sin6_addr); (*env)->SetByteArrayRegion (env, addr, 0, 16, (const char *)&him.him6.sin6_addr);
(*env)->SetIntField(env, socketAddressObj, ia_familyID, IPv6); setInetAddress_family(env, socketAddressObj, IPv6);
scope = him.him6.sin6_scope_id; scope = him.him6.sin6_scope_id;
(*env)->SetIntField(env, socketAddressObj, ia6_scopeidID, scope); (*env)->SetIntField(env, socketAddressObj, ia6_scopeidID, scope);
if(scope>0) { if(scope>0) {

View File

@ -804,7 +804,7 @@ JNIEXPORT int JNICALL
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
int *len, jboolean v4MappedAddress) { int *len, jboolean v4MappedAddress) {
jint family, iafam; jint family, iafam;
iafam = (*env)->GetIntField(env, iaObj, ia_familyID); iafam = getInetAddress_family(env, iaObj);
family = (iafam == IPv4)? AF_INET : AF_INET6; family = (iafam == IPv4)? AF_INET : AF_INET6;
if (ipv6_available() && !(family == AF_INET && v4MappedAddress == JNI_FALSE)) { if (ipv6_available() && !(family == AF_INET && v4MappedAddress == JNI_FALSE)) {
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
@ -815,7 +815,7 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
if (family == AF_INET) { /* will convert to IPv4-mapped address */ if (family == AF_INET) { /* will convert to IPv4-mapped address */
memset((char *) caddr, 0, 16); memset((char *) caddr, 0, 16);
address = (*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
if (address == INADDR_ANY) { if (address == INADDR_ANY) {
/* we would always prefer IPv6 wildcard address /* we would always prefer IPv6 wildcard address
caddr[10] = 0xff; caddr[10] = 0xff;
@ -854,7 +854,7 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
return -1; return -1;
} }
memset((char *) him4, 0, sizeof(struct sockaddr_in)); memset((char *) him4, 0, sizeof(struct sockaddr_in));
address = (int)(*env)->GetIntField(env, iaObj, ia_addressID); address = getInetAddress_addr(env, iaObj);
him4->sin_port = htons((short) port); him4->sin_port = htons((short) port);
him4->sin_addr.s_addr = (u_long) htonl(address); him4->sin_addr.s_addr = (u_long) htonl(address);
him4->sin_family = AF_INET; him4->sin_family = AF_INET;