/soc/2012/michael/android: 6e9fafca8684: Added basic SSL ops imp...

Michael Zangl michael at soc.pidgin.im
Wed Jul 25 07:23:39 EDT 2012


Changeset: 6e9fafca8684ad6b36e0e1d8f7ac96295ea62ae2
Author:	 Michael Zangl <michael at soc.pidgin.im>
Date:	 2012-07-25 13:22 +0200
Branch:	 soc.2012.android
URL: http://hg.pidgin.im/soc/2012/michael/android/rev/6e9fafca8684

Description:

Added basic SSL ops implementation.

diffstat:

 android/workspace/im.pidgin.libpurple/native/SslConnection.c                              |   39 ++
 android/workspace/im.pidgin.libpurple/native/SslOps.c                                     |  185 ++++++++++
 android/workspace/im.pidgin.libpurple/native/build.ant                                    |    5 +-
 android/workspace/im.pidgin.libpurple/native/helpers.c                                    |   14 +
 android/workspace/im.pidgin.libpurple/native/helpers.h                                    |    2 +
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java       |    2 +
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java |  101 +++++
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslOps.java        |   10 +
 8 files changed, 357 insertions(+), 1 deletions(-)

diffs (truncated from 425 to 300 lines):

diff --git a/android/workspace/im.pidgin.libpurple/native/SslConnection.c b/android/workspace/im.pidgin.libpurple/native/SslConnection.c
new file mode 100644
--- /dev/null
+++ b/android/workspace/im.pidgin.libpurple/native/SslConnection.c
@@ -0,0 +1,39 @@
+#include "SslConnection.h"
+#include "helpers.h"
+#include <libpurple/sslconn.h>
+
+static PurpleSslConnection *
+getNativeConnection(JNIEnv *env, jobject connection)
+{
+	return getNativePointer(env, connection);
+}
+
+/*
+ * Class:     im_pidgin_libpurple_core_ssl_SslConnection
+ * Method:    getHost_native
+ * Signature: ()Ljava/lang/String;
+ */JNIEXPORT jstring JNICALL
+Java_im_pidgin_libpurple_core_ssl_SslConnection_getHost_1native(JNIEnv *env,
+		jobject connection)
+{
+	PurpleSslConnection* connection_native;
+
+	connection_native = getNativeConnection(env, connection);
+
+	return (*env)->NewStringUTF(env, connection_native->host);
+}
+
+/*
+ * Class:     im_pidgin_libpurple_core_ssl_SslConnection
+ * Method:    getPort_native
+ * Signature: ()I
+ */JNIEXPORT jint JNICALL
+Java_im_pidgin_libpurple_core_ssl_SslConnection_getPort_1native(JNIEnv *env,
+		jobject connection)
+{
+	PurpleSslConnection* connection_native;
+
+	connection_native = getNativeConnection(env, connection);
+
+	return connection_native->port;
+}
diff --git a/android/workspace/im.pidgin.libpurple/native/SslOps.c b/android/workspace/im.pidgin.libpurple/native/SslOps.c
new file mode 100644
--- /dev/null
+++ b/android/workspace/im.pidgin.libpurple/native/SslOps.c
@@ -0,0 +1,185 @@
+#include "SslOps.h"
+#include "helpers.h"
+#include <libpurple/sslconn.h>
+
+JavaObjectReference ssl = JAVA_NULL_OBJECT_REF;
+
+static JavaObjectReference*
+getJavaObject(PurpleSslConnection* gsc)
+{
+	return (JavaObjectReference*) gsc->private_data;
+}
+
+static gboolean
+init()
+{
+	return TRUE;
+}
+
+static void
+uninit()
+{
+}
+
+static void
+registerConnection(PurpleSslConnection* gsc)
+{
+	static JavaMethodIDCache fdConstructor = METHOD_CACHE("<init>", "()V");
+	static JavaMethodIDCache socketConstructor =
+			METHOD_CACHE("<init>", "(Ljava/io/FileDescriptor;)V");
+	static JavaMethodIDCache connectionConstructor =
+			METHOD_CACHE("<init>", "(JLjava/net/Socket;)V");
+	static JavaMethodIDCache connectMethod = METHOD_CACHE("connect", "()Z");
+
+	CALLBACK_START_VOID(&ssl);
+
+	jobject fd;
+	jclass fdClass;
+	jmethodID fdConstructorMid;
+	jfieldID fdField;
+	jobject socket;
+	jclass socketClass;
+	jmethodID socketConstructorMid;
+	jobject connection;
+	jmethodID connectionConstructorMid;
+	jclass connectionClass;
+	jmethodID connectMid;
+
+	gsc->private_data = g_new(JavaObjectReference, 1);
+	g_return_if_fail(gsc->private_data != NULL);
+
+	fdClass = (*env)->FindClass(env, "java/io/FileDescriptor");
+	fdConstructorMid = getMethodIDCachedReferenced(env, fdClass,
+			&fdConstructor);
+	g_return_if_fail(fdConstructorMid != NULL);
+	fd = (*env)->NewObject(env, fdClass, fdConstructorMid);
+	fdField = (*env)->GetFieldID(env, fdClass, "descriptor", "I");
+	(*env)->SetIntField(env, fdClass, fdField, gsc->fd);
+
+	socketClass = (*env)->FindClass(env, "android/net/LocalSocketImpl");
+	socketConstructorMid = getMethodIDCachedReferenced(env, socketClass,
+			&socketConstructor);
+	g_return_if_fail(socketConstructorMid != NULL);
+	socket = (*env)->NewObject(env, socketClass, socketConstructorMid, fd);
+
+	connectionClass = (*env)->FindClass(env,
+			"im/pidgin/libpurple.core.ssl.SslConnection");
+	connectionConstructorMid = getMethodIDCachedReferenced(env, connectionClass,
+			&connectionConstructor);
+	g_return_if_fail(connectionConstructorMid != NULL);
+	connection = (*env)->NewObject(env, connectionClass,
+			connectionConstructorMid, pToLong(gsc), socket);
+
+	setJavaObject(getJavaObject(gsc), env, connection);
+
+	connectMid = getMethodIDCachedReferenced(env, connectionClass,
+			&connectMethod);
+	if (!(*env)->CallBooleanMethod(env, connection, connectMid)) {
+		/* TODO error. */
+	}
+}
+
+static void
+closeConnection(PurpleSslConnection* gsc)
+{
+	static JavaMethodIDCache methodCache = METHOD_CACHE("close", "()V");
+
+	JavaObjectReference *connection;
+	jmethodID mid;
+
+	connection = getJavaObject(gsc);
+	g_return_if_fail(connection != NULL);
+
+	CALLBACK_START_VOID(connection);
+
+	mid = getMethodIDCachedReferenced(env, connection->java_class,
+			&methodCache);
+	if (mid != NULL) {
+		(*env)->CallVoidMethod(env, connection->handlerObject, mid);
+	}
+
+	unsetJavaObject(connection, env);
+	g_free(connection);
+}
+
+static size_t
+readConnection(PurpleSslConnection* gsc, void* data, size_t len)
+{
+	static JavaMethodIDCache methodCache = METHOD_CACHE("read", "([BI)I");
+
+	JavaObjectReference *connection;
+	jmethodID mid;
+	jbyteArray data_array;
+	jlong result = -1;
+
+	connection = getJavaObject(gsc);
+	g_return_val_if_fail(connection != NULL, -1);
+
+	CALLBACK_START(connection, -1);
+
+	data_array = (*env)->NewByteArray(env, len);
+	g_return_val_if_fail(data_array != NULL, -1);
+
+	mid = getMethodIDCachedReferenced(env, connection->java_class,
+			&methodCache);
+	if (mid != NULL) {
+		result = (*env)->CallLongMethod(env, connection->handlerObject, mid,
+				data_array, (jlong) len);
+		(*env)->GetByteArrayRegion(env, data_array, 0, len, data);
+	}
+
+	(*env)->DeleteLocalRef(env, data_array);
+	return (size_t) result;
+}
+
+static size_t
+writeConnection(PurpleSslConnection* gsc, const void* data, size_t len)
+{
+	static JavaMethodIDCache methodCache = METHOD_CACHE("write", "([BI)I");
+
+	JavaObjectReference *connection;
+	jmethodID mid;
+	jbyteArray data_array;
+	jlong result = -1;
+
+	connection = getJavaObject(gsc);
+	g_return_val_if_fail(connection != NULL, -1);
+
+	CALLBACK_START(connection, -1);
+
+	data_array = (*env)->NewByteArray(env, len);
+	g_return_val_if_fail(data_array != NULL, -1);
+	(*env)->SetByteArrayRegion(env, data_array, 0, len, data);
+
+	mid = getMethodIDCachedReferenced(env, connection->java_class,
+			&methodCache);
+	if (mid != NULL) {
+		result = (*env)->CallLongMethod(env, connection->handlerObject, mid,
+				data_array, (jlong) len);
+	}
+
+	(*env)->DeleteLocalRef(env, data_array);
+	return (size_t) result;
+}
+
+static GList *
+get_peer_certificates(PurpleSslConnection* gsc)
+{
+	return NULL;
+}
+
+PurpleSslOps sslOps =
+		{ init, uninit, registerConnection, closeConnection, readConnection,
+				writeConnection, get_peer_certificates, NULL, NULL, NULL };
+
+/*
+ * Class:     im_pidgin_libpurple_core_ssl_SslOps
+ * Method:    register_native
+ * Signature: ()V
+ */JNIEXPORT void JNICALL
+Java_im_pidgin_libpurple_core_ssl_SslOps_register_1native(JNIEnv *env,
+		jobject object)
+{
+	setJavaObject(&ssl, env, object);
+	purple_ssl_set_ops(&sslOps);
+}
diff --git a/android/workspace/im.pidgin.libpurple/native/build.ant b/android/workspace/im.pidgin.libpurple/native/build.ant
--- a/android/workspace/im.pidgin.libpurple/native/build.ant
+++ b/android/workspace/im.pidgin.libpurple/native/build.ant
@@ -53,7 +53,10 @@
 		<javah classdefinition="im.pidgin.libpurple.core.dns.DnsQueryData" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.core.dns.DnsQueryFailedCallback" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.core.dns.DnsQueryResolvedCallback" to="${javah.out}" />
-
+		
+		<javah classdefinition="im.pidgin.libpurple.core.ssl.SslConnection" to="${javah.out}" />
+		<javah classdefinition="im.pidgin.libpurple.core.ssl.SslOps" to="${javah.out}"/>
+		
 		<javah classdefinition="im.pidgin.libpurple.glib.GHashTableReference" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.glib.GListReference" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.glib.GSourceFunctionCall" to="${javah.out}" />
diff --git a/android/workspace/im.pidgin.libpurple/native/helpers.c b/android/workspace/im.pidgin.libpurple/native/helpers.c
--- a/android/workspace/im.pidgin.libpurple/native/helpers.c
+++ b/android/workspace/im.pidgin.libpurple/native/helpers.c
@@ -36,6 +36,20 @@ setJavaObject(JavaObjectReference *ref, 
 	return TRUE;
 }
 
+void
+unsetJavaObject(JavaObjectReference *ref, JNIEnv *env)
+{
+	if (ref->java_class != NULL) {
+		(*env)->DeleteGlobalRef(env, ref->java_class);
+	}
+	if (ref->handlerObject != NULL) {
+		(*env)->DeleteGlobalRef(env, ref->handlerObject);
+	}
+	ref->java_class = NULL;
+	ref->handlerObject = NULL;
+	ref->jvm = NULL;
+}
+
 /**
  * Gets the peer field of an object of the peer class.
  */
diff --git a/android/workspace/im.pidgin.libpurple/native/helpers.h b/android/workspace/im.pidgin.libpurple/native/helpers.h
--- a/android/workspace/im.pidgin.libpurple/native/helpers.h
+++ b/android/workspace/im.pidgin.libpurple/native/helpers.h
@@ -81,6 +81,8 @@ typedef struct {
 
 gboolean setJavaObject(JavaObjectReference *ref, JNIEnv *env, jobject object);
 
+void unsetJavaObject(JavaObjectReference *ref, JNIEnv *env);
+
 void *getNativePointer(JNIEnv *env, jobject peered);
 
 jmethodID getMethodIDCachedReferenced(JNIEnv *env, jclass class, JavaMethodIDCache *cache);
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
@@ -3,6 +3,7 @@ package im.pidgin.libpurple.core;
 import im.pidgin.libpurple.account.PurpleAccountManager;
 import im.pidgin.libpurple.blist.PurpleBlistManager;
 import im.pidgin.libpurple.conversation.ConversationManager;
+import im.pidgin.libpurple.core.ssl.SslOps;
 import im.pidgin.libpurple.core.thread.PurpleThread;
 import im.pidgin.libpurple.plugin.PurplePluginList;
 import im.pidgin.libpurple.plugin.PurplePluginManager;
@@ -60,6 +61,7 @@ public class CoreManager {
 		accountList.register();
 		blist.register();
 		conversationManager.register();



More information about the Commits mailing list