Fix 143256: SSLServerSocket.getInetAddress returns the wrong address.

Implement toString() for both socket types.
This commit is contained in:
nicolson%netscape.com 2002-05-09 17:10:06 +00:00
Родитель 5741fadaf3
Коммит a9fb648aec
5 изменённых файлов: 91 добавлений и 39 удалений

Просмотреть файл

@ -292,10 +292,10 @@ public class SSLServerSocket extends java.net.ServerSocket {
}
/**
* @return The remote peer's IP address.
* @Return the local address of this server socket.
*/
public InetAddress getInetAddress() {
return base.getInetAddress();
return base.getLocalAddress();
}
/**
@ -334,4 +334,17 @@ public class SSLServerSocket extends java.net.ServerSocket {
public void useCache(boolean b) throws SocketException {
base.useCache(b);
}
/**
* Returns the addresses and ports of this socket.
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("SSLServerSocket[addr=");
buf.append(getInetAddress());
buf.append(",port=0,localport=");
buf.append(getLocalPort());
buf.append("]");
return buf.toString();
}
}

Просмотреть файл

@ -363,6 +363,10 @@ finish:
return retval;
}
/*
* This function is only here for binary compatibility. See
* http://bugzilla.mozilla.org/show_bug.cgi?id=143254
*/
JNIEXPORT jint JNICALL
Java_org_mozilla_jss_ssl_SSLSocket_getLocalAddressNative(JNIEnv *env,
jobject self)

Просмотреть файл

@ -270,27 +270,8 @@ public class SSLSocket extends java.net.Socket {
* @return The local IP address.
*/
public InetAddress getLocalAddress() {
try {
int intAddr = getLocalAddressNative();
InetAddress in;
int[] addr = new int[4];
addr[0] = ((intAddr >>> 24) & 0xff);
addr[1] = ((intAddr >>> 16) & 0xff);
addr[2] = ((intAddr >>> 8) & 0xff);
addr[3] = ((intAddr ) & 0xff);
try {
in = InetAddress.getByName(
addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3] );
} catch (java.net.UnknownHostException e) {
in = null;
}
return in;
} catch(SocketException e) {
e.printStackTrace();
return null;
}
return base.getLocalAddress();
}
private native int getLocalAddressNative() throws SocketException;
/**
* @return The local port.
@ -697,6 +678,22 @@ public class SSLSocket extends java.net.Socket {
private static native void setCipherPolicyNative(int policyEnum)
throws SocketException;
/**
* Returns the addresses and ports of this socket.
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("SSLSocket[addr=");
buf.append(getInetAddress());
buf.append(getLocalAddress());
buf.append(",port=");
buf.append(getPort());
buf.append(",localport=");
buf.append(getLocalPort());
buf.append("]");
return buf.toString();
}
public final static int SSL2_RC4_128_WITH_MD5 = 0xFF01;
public final static int SSL2_RC4_128_EXPORT40_WITH_MD5 = 0xFF02;
public final static int SSL2_RC2_128_CBC_WITH_MD5 = 0xFF03;

Просмотреть файл

@ -138,33 +138,58 @@ class SocketBase {
native void setSSLOption(int option, int on)
throws SocketException;
/**
* Converts a host-ordered 4-byte internet address into an InetAddress.
* Unfortunately InetAddress provides no more efficient means
* of construction than getByName(), and it is final.
*
* @return The InetAddress corresponding to the given integer,
* or <tt>null</tt> if the InetAddress could not be constructed.
*/
private static InetAddress
convertIntToInetAddress(int intAddr) {
InetAddress in;
int[] addr = new int[4];
addr[0] = ((intAddr >>> 24) & 0xff);
addr[1] = ((intAddr >>> 16) & 0xff);
addr[2] = ((intAddr >>> 8) & 0xff);
addr[3] = ((intAddr ) & 0xff);
try {
in = InetAddress.getByName(
addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3] );
} catch (java.net.UnknownHostException e) {
in = null;
}
return in;
}
/**
* @Return the InetAddress of the peer end of the socket.
*/
InetAddress getInetAddress()
{
try {
int intAddr = getPeerAddressNative();
int[] addr = new int[4];
InetAddress in = null;
addr[0] = ((intAddr >>> 24) & 0xff);
addr[1] = ((intAddr >>> 16) & 0xff);
addr[2] = ((intAddr >>> 8) & 0xff);
addr[3] = ((intAddr ) & 0xff);
try {
in = InetAddress.getByName(
addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3] );
} catch (java.net.UnknownHostException e) {
e.printStackTrace();
in = null;
}
return in;
return convertIntToInetAddress( getPeerAddressNative() );
} catch(SocketException e) {
e.printStackTrace();
return null;
}
}
private native int getPeerAddressNative() throws SocketException;
/**
* @return The local IP address.
*/
InetAddress getLocalAddress() {
try {
return convertIntToInetAddress( getLocalAddressNative() );
} catch(SocketException e) {
e.printStackTrace();
return null;
}
}
private native int getLocalAddressNative() throws SocketException;
public int getLocalPort() {
try {
return getLocalPortNative();

Просмотреть файл

@ -426,6 +426,19 @@ Java_org_mozilla_jss_ssl_SocketBase_getPeerAddressNative
}
}
JNIEXPORT jint JNICALL
Java_org_mozilla_jss_ssl_SocketBase_getLocalAddressNative(JNIEnv *env,
jobject self)
{
PRNetAddr addr;
if( JSSL_getSockAddr(env, self, &addr, LOCAL_SOCK) == PR_SUCCESS ) {
return ntohl(addr.inet.ip);
} else {
return 0;
}
}
JNIEXPORT jint JNICALL
Java_org_mozilla_jss_ssl_SocketBase_getLocalPortNative(JNIEnv *env,
jobject self)