android: Added a Java part to the Android IMC
authorTobias Brunner <tobias@strongswan.org>
Wed, 24 Apr 2013 14:24:14 +0000 (16:24 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 8 Jul 2013 16:49:26 +0000 (18:49 +0200)
src/frontends/android/jni/libandroidbridge/byod/imc_android.c
src/frontends/android/jni/libandroidbridge/charonservice.c
src/frontends/android/src/org/strongswan/android/logic/imc/AndroidImc.java [new file with mode: 0644]

index 06edf35..f648456 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include "imc_android_state.h"
+#include "../android_jni.h"
 
 #include <tnc/tnc.h>
 #include <libpts.h>
@@ -46,6 +47,16 @@ static pen_type_t msg_types[] = {
 static imc_agent_t *imc_android;
 
 /**
+ * AndroidImc object accessed via JNI
+ */
+static jobject android_imc;
+
+/**
+ * AndroidImc class object
+ */
+static jclass android_imc_cls;
+
+/**
  * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
  */
 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
@@ -382,13 +393,59 @@ TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
 bool imc_android_register(plugin_t *plugin, plugin_feature_t *feature,
                                                  bool reg, void *data)
 {
+       JNIEnv *env;
+       jmethodID method_id;
+       jobject obj, context = (jobject)data;
+       jclass cls;
+       bool success = TRUE;
+
+       androidjni_attach_thread(&env);
        if (reg)
        {
-               return tnc->imcs->load_from_functions(tnc->imcs, "android",
+               cls = (*env)->FindClass(env, JNI_PACKAGE_STRING "/imc/AndroidImc");
+               if (!cls)
+               {
+                       goto failed;
+               }
+               android_imc_cls = (*env)->NewGlobalRef(env, cls);
+               method_id = (*env)->GetMethodID(env, cls, "<init>",
+                                                                               "(Landroid/content/Context;)V");
+               if (!method_id)
+               {
+                       goto failed;
+               }
+               obj = (*env)->NewObject(env, cls, method_id, context);
+               if (!obj)
+               {
+                       goto failed;
+               }
+               android_imc = (*env)->NewGlobalRef(env, obj);
+               androidjni_detach_thread();
+
+               if (tnc->imcs->load_from_functions(tnc->imcs, "Android",
                                                        TNC_IMC_Initialize, TNC_IMC_NotifyConnectionChange,
                                                        TNC_IMC_BeginHandshake, TNC_IMC_ReceiveMessage,
                                                        TNC_IMC_ReceiveMessageLong, TNC_IMC_BatchEnding,
-                                                       TNC_IMC_Terminate, TNC_IMC_ProvideBindFunction);
+                                                       TNC_IMC_Terminate, TNC_IMC_ProvideBindFunction))
+               {
+                       return TRUE;
+               }
+failed:
+               DBG1(DBG_IMC, "initialization of Android IMC failed");
+               androidjni_exception_occurred(env);
+               success = FALSE;
+       }
+
+       if (android_imc)
+       {
+               (*env)->DeleteGlobalRef(env, android_imc);
+               android_imc = NULL;
+       }
+       if (android_imc_cls)
+       {
+               (*env)->DeleteGlobalRef(env, android_imc_cls);
+               android_imc_cls = NULL;
        }
-       return TRUE;
+       androidjni_detach_thread();
+       return success;
 }
index fbbda55..1c62e28 100644 (file)
@@ -462,14 +462,6 @@ static void charonservice_init(JNIEnv *env, jobject service, jobject builder)
                        PLUGIN_PROVIDE(CUSTOM, "android-backend"),
                                PLUGIN_DEPENDS(CUSTOM, "libcharon"),
        };
-#ifdef USE_BYOD
-       static plugin_feature_t byod_features[] = {
-               PLUGIN_CALLBACK(imc_android_register, NULL),
-                       PLUGIN_PROVIDE(CUSTOM, "android-imc"),
-                               PLUGIN_DEPENDS(CUSTOM, "android-backend"),
-                               PLUGIN_DEPENDS(CUSTOM, "imc-manager"),
-       };
-#endif
 
        INIT(this,
                .public = {
@@ -494,8 +486,17 @@ static void charonservice_init(JNIEnv *env, jobject service, jobject builder)
                                                                          countof(features), TRUE);
 
 #ifdef USE_BYOD
-       lib->plugins->add_static_features(lib->plugins, "android-byod",
+       {
+               plugin_feature_t byod_features[] = {
+                       PLUGIN_CALLBACK(imc_android_register, this->vpn_service),
+                               PLUGIN_PROVIDE(CUSTOM, "android-imc"),
+                                       PLUGIN_DEPENDS(CUSTOM, "android-backend"),
+                                       PLUGIN_DEPENDS(CUSTOM, "imc-manager"),
+               };
+
+               lib->plugins->add_static_features(lib->plugins, "android-byod",
                                                                byod_features, countof(byod_features), TRUE);
+       }
 #endif
 }
 
diff --git a/src/frontends/android/src/org/strongswan/android/logic/imc/AndroidImc.java b/src/frontends/android/src/org/strongswan/android/logic/imc/AndroidImc.java
new file mode 100644 (file)
index 0000000..3067707
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.logic.imc;
+
+import android.content.Context;
+
+public class AndroidImc
+{
+       private final Context mContext;
+
+       public AndroidImc(Context context)
+       {
+               mContext = context;
+       }
+}