harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r988286 - in /harmony/enhanced/java/branches/omd/classlib/modules: luni/src/main/java/java/net/ x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/ x-net/src/main/native/jsse/shared/
Date Mon, 23 Aug 2010 19:55:57 GMT
Author: odeakin
Date: Mon Aug 23 19:55:57 2010
New Revision: 988286

URL: http://svn.apache.org/viewvc?rev=988286&view=rev
Log:
A few changes to fix x-net unit test failures:
 - Access the SocketImpl FileDescriptor using reflection so its getFileDescriptor() method
can be made protected again, as spec'ed.
 - Make SSLServerSocketImpl use SSLParameters to get/set its options instead of going via
the SSLSocket. The way this was implemented before meant that multiple accepts on the same
server socket were not possible.
 - Make the SSLSocketWrapper class function correctly by replacing its SocketImpl with the
one in the Socket it is being created with. This fixes behaviour when SSLSocketFactory is
used to create a SSLSocket from an existing socket.

Modified:
    harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketWrapper.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h

Modified: harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
Mon Aug 23 19:55:57 2010
@@ -159,7 +159,7 @@ public abstract class SocketImpl impleme
      * 
      * @return the file descriptor of this socket.
      */
-    public FileDescriptor getFileDescriptor() {
+    protected FileDescriptor getFileDescriptor() {
         return fd;
     }
 

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImpl.java?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImpl.java
Mon Aug 23 19:55:57 2010
@@ -31,7 +31,15 @@ import javax.net.ssl.SSLServerSocket;
  */
 public class SSLServerSocketImpl extends SSLServerSocket {
 
-    private final SSLSocketImpl sslSocket;
+    // the sslParameters object encapsulates all the info
+    // about supported and enabled cipher suites and protocols,
+    // as well as the information about client/server mode of
+    // ssl socket, whether it require/want client authentication or not,
+    // and controls whether new SSL sessions may be established by this
+    // socket or not.
+    private final SSLParameters sslParameters;
+
+    private long SSL = 0;
 
     // logger
     private Logger.Stream logger = Logger.getStream("ssocket");
@@ -44,7 +52,7 @@ public class SSLServerSocketImpl extends
     protected SSLServerSocketImpl(SSLParameters sslParameters)
         throws IOException {
         super();
-        sslSocket = new SSLSocketImpl(sslParameters);
+        this.sslParameters = sslParameters;
     }
 
     /**
@@ -56,7 +64,7 @@ public class SSLServerSocketImpl extends
     protected SSLServerSocketImpl(int port, SSLParameters sslParameters)
         throws IOException {
         super(port);
-        sslSocket = new SSLSocketImpl(sslParameters);
+        this.sslParameters = sslParameters;
     }
 
     /**
@@ -69,7 +77,7 @@ public class SSLServerSocketImpl extends
     protected SSLServerSocketImpl(int port, int backlog,
             SSLParameters sslParameters) throws IOException {
         super(port, backlog);
-        sslSocket = new SSLSocketImpl(sslParameters);
+        this.sslParameters = sslParameters;
     }
 
     /**
@@ -85,7 +93,7 @@ public class SSLServerSocketImpl extends
                                 SSLParameters sslParameters)
         throws IOException {
         super(port, backlog, iAddress);
-        sslSocket = new SSLSocketImpl(sslParameters);
+        this.sslParameters = sslParameters;
     }
 
     // --------------- SSLParameters based methods ---------------------
@@ -97,7 +105,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public String[] getSupportedCipherSuites() {
-        return sslSocket.getSupportedCipherSuites();
+        return sslParameters.getSupportedCipherSuites();
     }
 
     /**
@@ -107,7 +115,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public String[] getEnabledCipherSuites() {
-        return sslSocket.getEnabledCipherSuites();
+        return sslParameters.getEnabledCipherSuites();
     }
 
     /**
@@ -117,7 +125,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setEnabledCipherSuites(String[] suites) {
-        sslSocket.setEnabledCipherSuites(suites);
+        sslParameters.setEnabledCipherSuites(SSL, suites);
     }
 
     /**
@@ -127,7 +135,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public String[] getSupportedProtocols() {
-        return sslSocket.getSupportedProtocols();
+        return sslParameters.getSupportedProtocols();
     }
 
     /**
@@ -137,7 +145,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public String[] getEnabledProtocols() {
-        return sslSocket.getEnabledProtocols();
+        return sslParameters.getEnabledProtocols();
     }
 
     /**
@@ -147,7 +155,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setEnabledProtocols(String[] protocols) {
-        sslSocket.setEnabledProtocols(protocols);
+        sslParameters.setEnabledProtocols(SSL, protocols);
     }
 
     /**
@@ -157,7 +165,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setUseClientMode(boolean mode) {
-        sslSocket.setUseClientMode(mode);
+        sslParameters.setUseClientMode(mode);
     }
 
     /**
@@ -167,7 +175,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public boolean getUseClientMode() {
-        return sslSocket.getUseClientMode();
+        return sslParameters.getUseClientMode();
     }
 
     /**
@@ -177,7 +185,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setNeedClientAuth(boolean need) {
-        sslSocket.setNeedClientAuth(need);
+        sslParameters.setNeedClientAuth(SSL, need);
     }
 
     /**
@@ -187,7 +195,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public boolean getNeedClientAuth() {
-        return sslSocket.getNeedClientAuth();
+        return sslParameters.getNeedClientAuth();
     }
 
     /**
@@ -197,7 +205,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setWantClientAuth(boolean want) {
-        sslSocket.setWantClientAuth(want);
+        sslParameters.setWantClientAuth(SSL, want);
     }
 
     /**
@@ -207,7 +215,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public boolean getWantClientAuth() {
-        return sslSocket.getWantClientAuth();
+        return sslParameters.getWantClientAuth();
     }
 
     /**
@@ -217,7 +225,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public void setEnableSessionCreation(boolean flag) {
-        sslSocket.setEnableSessionCreation(flag);
+        sslParameters.setEnableSessionCreation(flag);
     }
 
     /**
@@ -227,7 +235,7 @@ public class SSLServerSocketImpl extends
      */
     @Override
     public boolean getEnableSessionCreation() {
-        return sslSocket.getEnableSessionCreation();
+        return sslParameters.getEnableSessionCreation();
     }
 
 
@@ -243,23 +251,32 @@ public class SSLServerSocketImpl extends
         if (logger != null) {
             logger.println("SSLServerSocketImpl.accept ..");
         }
-        implAccept(sslSocket);
+        SSLSocketImpl s = new SSLSocketImpl(
+                (SSLParameters) sslParameters.clone());
+        SSL = s.getSSL();
+        implAccept(s);
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             try {
-                sm.checkAccept(sslSocket.getInetAddress().getHostAddress(),
-                        sslSocket.getPort());
+                sm.checkAccept(s.getInetAddress().getHostAddress(),
+                        s.getPort());
             } catch(SecurityException e) {
-                sslSocket.close();
+                s.close();
                 throw e;
             }
         }
-        sslSocket.init();
-        sslSocket.startHandshake();
+        s.init();
+        s.startHandshake();
         if (logger != null) {
             logger.println("SSLServerSocketImpl: accepted, initialized");
         }
-        return sslSocket;
+        return s;
+    }
+
+    @Override
+    public void close() throws IOException {
+        super.close();
+        SSL = 0;
     }
 
     /**

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
Mon Aug 23 19:55:57 2010
@@ -24,11 +24,15 @@ import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.reflect.Field;
 import java.net.InetAddress;
+import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import javax.net.ssl.HandshakeCompletedEvent;
 import javax.net.ssl.HandshakeCompletedListener;
 import javax.net.ssl.SSLSession;
@@ -77,10 +81,43 @@ public class SSLSocketImpl extends SSLSo
     // Pointer to the OpenSSL SSL struct used for this connection
     private long SSL;
 
+    private static Field fdField;
+    private static Field descriptorField;
+    private FileDescriptor fd;
+
     // ----------------- Constructors and initializers --------------------
+    private static Field getField(final String className, final String fieldName) {
+        return AccessController.doPrivileged(new PrivilegedAction<Field>() {
+            public Field run() {
+                Field field = null;
+                try {
+                    Class clazz = Class.forName(className);
+                    field = clazz.getDeclaredField(fieldName);
+                    field.setAccessible(true);
+                } catch (NoSuchFieldException e) {
+                    throw new Error(e);
+                } catch (ClassNotFoundException e) {
+                    throw new Error(e);
+                }
+                return field;
+            }
+        });
+    }
+
+    {
+        fdField = getField("java.net.SocketImpl", "fd");
+        descriptorField = getField("java.io.FileDescriptor", "descriptor");
+    }
 
     private native long initImpl(long context);
 
+    SSLSocketImpl(Socket socket, boolean autoClose, SSLParameters sslParameters) throws IOException
{
+        super();
+
+        this.sslParameters = sslParameters;
+        init();
+    }
+
     /**
      * Constructor
      * @param   sslParameters:  SSLParameters
@@ -206,6 +243,10 @@ public class SSLSocketImpl extends SSLSo
         }
     }
 
+    long getSSL() {
+        return SSL;
+    }
+
     // --------------- SSLParameters based methods ---------------------
 
     /**
@@ -411,8 +452,8 @@ public class SSLSocketImpl extends SSLSo
         }
     }
 
-    private native void sslConnectImpl(long sslContextAddress, FileDescriptor fd);
-    private native void sslAcceptImpl(long sslContextAddress, FileDescriptor fd);
+    private native void sslConnectImpl(long sslContextAddress, long fd);
+    private native void sslAcceptImpl(long sslContextAddress, long fd);
 
     /**
      * Performs the handshake process over the SSL/TLS connection
@@ -437,17 +478,26 @@ public class SSLSocketImpl extends SSLSo
 
         if (!handshake_started) {
             handshake_started = true;
+
+            long descriptor;
+            try {
+                fd = (FileDescriptor) fdField.get(impl);
+                descriptor = descriptorField.getLong(fd);
+            } catch (IllegalAccessException e) {
+                throw new Error(e);
+            }
+
             if (sslParameters.getUseClientMode()) {
                 if (logger != null) {
                     logger.println("SSLSocketImpl: CLIENT connecting");
                 }
 
-                sslConnectImpl(SSL, impl.getFileDescriptor());
+                sslConnectImpl(SSL, descriptor);
             } else {
                 if (logger != null) {
                     logger.println("SSLSocketImpl: SERVER accepting connection");
                 }
-                sslAcceptImpl(SSL, impl.getFileDescriptor());
+                sslAcceptImpl(SSL, descriptor);
             }
         }
 
@@ -475,7 +525,6 @@ public class SSLSocketImpl extends SSLSo
         sslParameters.threadLocalParams.remove();
     }
 
-
     // ---------------- Socket's methods overridings -------------------
 
     /**
@@ -611,7 +660,7 @@ public class SSLSocketImpl extends SSLSo
     }
   
     protected int available() throws IOException {
-        return netImpl.availableStream(impl.getFileDescriptor());
+        return netImpl.availableStream(fd);
     }
     
     private native void writeAppDataImpl(long SSL, byte[] data, int offset, int len);

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketWrapper.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketWrapper.java?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketWrapper.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketWrapper.java
Mon Aug 23 19:55:57 2010
@@ -18,10 +18,14 @@
 package org.apache.harmony.xnet.provider.jsse;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.net.SocketImpl;
 import java.net.SocketAddress;
 import java.net.SocketException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 
 /**
  * This class wraps the SSL fuctionality over existing conneted socket.
@@ -31,12 +35,36 @@ public class SSLSocketWrapper extends SS
     private final Socket socket;
     private final boolean autoClose;
 
+    private static Field implField;
+
+    {
+        implField = AccessController.doPrivileged(new PrivilegedAction<Field>() {
+            public Field run() {
+                Field field = null;
+                try {
+                    field = Socket.class.getDeclaredField("impl");
+                    field.setAccessible(true);
+                } catch (NoSuchFieldException e) {
+                    throw new Error(e);
+                }
+                return field;
+            }
+        });
+    }
+
     protected SSLSocketWrapper(Socket socket, boolean autoClose, SSLParameters sslParameters)
throws IOException {
         super(sslParameters);
         if (!socket.isConnected()) {
             throw new SocketException("Socket is not connected.");
         }
         this.socket = socket;
+
+        try {
+            impl = (SocketImpl) implField.get(socket);
+        } catch (IllegalAccessException e) {
+            throw new Error(e);
+        }
+
         this.autoClose = autoClose;
         init();
     }

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
Mon Aug 23 19:55:57 2010
@@ -24,26 +24,8 @@
 #include "openssl/err.h"
 #include "errno.h"
 
-jlong getFD(JNIEnv * env, jobject fd) {
-    jclass descriptorCLS;
-    jfieldID descriptorFID;
-    hysocket_t hysocketP;
-
-    //TODO add to cache
-    descriptorCLS = (*env)->FindClass (env, "java/io/FileDescriptor");
-    if (NULL == descriptorCLS){
-        return 0;
-    }
-
-    descriptorFID = (*env)->GetFieldID (env, descriptorCLS, "descriptor", "J");
-    if (NULL == descriptorFID){
-        return 0;
-    }
-
-    hysocketP = (hysocket_t) ((IDATA)((*env)->GetLongField (env, fd, descriptorFID)));
-    if (NULL == hysocketP) {
-        return 0;
-    }
+jlong getFD(jlong fd) {
+    hysocket_t hysocketP = (hysocket_t) fd;
 
 #if defined(WIN32) || defined(WIN64)
     if (hysocketP->flags & SOCKET_IPV4_OPEN_MASK) {
@@ -64,8 +46,8 @@ JNIEXPORT jlong JNICALL Java_org_apache_
 }
 
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslAcceptImpl
-  (JNIEnv *env, jclass clazz, jlong jssl, jobject fd) {
-    jlong socket = getFD(env, fd);
+  (JNIEnv *env, jclass clazz, jlong jssl, jlong fd) {
+    jlong socket = getFD(fd);
     SSL *ssl = jlong2addr(SSL, jssl);
     BIO *bio;
     int ret;
@@ -87,8 +69,8 @@ JNIEXPORT void JNICALL Java_org_apache_h
 }
 
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslConnectImpl
-  (JNIEnv *env, jclass clazz, jlong jssl, jobject fd) {
-    jlong socket = getFD(env, fd);
+  (JNIEnv *env, jclass clazz, jlong jssl, jlong fd) {
+    jlong socket = getFD(fd);
     SSL *ssl = jlong2addr(SSL, jssl);
     BIO *bio;
     int ret;

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h?rev=988286&r1=988285&r2=988286&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
Mon Aug 23 19:55:57 2010
@@ -26,9 +26,9 @@ extern "C" {
 JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_initImpl
   (JNIEnv *, jclass, jlong);
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslAcceptImpl
-  (JNIEnv *, jclass, jlong, jobject);
+  (JNIEnv *, jclass, jlong, jlong);
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslConnectImpl
-  (JNIEnv *, jclass, jlong, jobject);
+  (JNIEnv *, jclass, jlong, jlong);
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_writeAppDataImpl
   (JNIEnv *, jclass, jlong, jbyteArray, jint, jint);
 JNIEXPORT jint JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_readAppDataImpl



Mime
View raw message