diff --git a/security/jss/org/mozilla/jss/ssl/SSLServerSocket.java b/security/jss/org/mozilla/jss/ssl/SSLServerSocket.java index 89ee0ae830b5..da4d0afd23a6 100644 --- a/security/jss/org/mozilla/jss/ssl/SSLServerSocket.java +++ b/security/jss/org/mozilla/jss/ssl/SSLServerSocket.java @@ -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(); + } } diff --git a/security/jss/org/mozilla/jss/ssl/SSLSocket.c b/security/jss/org/mozilla/jss/ssl/SSLSocket.c index ec0d95796a6f..892031ef5d5a 100644 --- a/security/jss/org/mozilla/jss/ssl/SSLSocket.c +++ b/security/jss/org/mozilla/jss/ssl/SSLSocket.c @@ -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) diff --git a/security/jss/org/mozilla/jss/ssl/SSLSocket.java b/security/jss/org/mozilla/jss/ssl/SSLSocket.java index 01d7fa2ba9c8..a53e826a25d2 100644 --- a/security/jss/org/mozilla/jss/ssl/SSLSocket.java +++ b/security/jss/org/mozilla/jss/ssl/SSLSocket.java @@ -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; diff --git a/security/jss/org/mozilla/jss/ssl/SocketBase.java b/security/jss/org/mozilla/jss/ssl/SocketBase.java index f5fc13a5bbb1..8affce1d531d 100644 --- a/security/jss/org/mozilla/jss/ssl/SocketBase.java +++ b/security/jss/org/mozilla/jss/ssl/SocketBase.java @@ -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 null 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(); diff --git a/security/jss/org/mozilla/jss/ssl/common.c b/security/jss/org/mozilla/jss/ssl/common.c index 503e0e0afbff..c15000629f21 100644 --- a/security/jss/org/mozilla/jss/ssl/common.c +++ b/security/jss/org/mozilla/jss/ssl/common.c @@ -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)