android: Properly handle failures while initializing charon
authorTobias Brunner <tobias@strongswan.org>
Fri, 20 Sep 2013 08:30:02 +0000 (10:30 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 23 Sep 2013 09:49:52 +0000 (11:49 +0200)
src/frontends/android/jni/libandroidbridge/charonservice.c
src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java

index 41fc4d6..60485b6 100644 (file)
@@ -589,7 +589,7 @@ static void segv_handler(int signal)
 /**
  * Initialize charon and the libraries via JNI
  */
-JNI_METHOD(CharonVpnService, initializeCharon, void,
+JNI_METHOD(CharonVpnService, initializeCharon, jboolean,
        jobject builder, jstring jlogfile, jboolean byod)
 {
        struct sigaction action;
@@ -603,7 +603,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
        if (!library_init(NULL))
        {
                library_deinit();
-               return;
+               return FALSE;
        }
 
        /* set options before initializing other libraries that might read them */
@@ -615,7 +615,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
        {
                libhydra_deinit();
                library_deinit();
-               return;
+               return FALSE;
        }
 
        if (!libipsec_init())
@@ -623,7 +623,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
                libipsec_deinit();
                libhydra_deinit();
                library_deinit();
-               return;
+               return FALSE;
        }
 
        if (!libcharon_init("charon"))
@@ -632,7 +632,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
                libipsec_deinit();
                libhydra_deinit();
                library_deinit();
-               return;
+               return FALSE;
        }
 
        charon->load_loggers(charon, NULL, FALSE);
@@ -664,7 +664,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
                libipsec_deinit();
                libhydra_deinit();
                library_deinit();
-               return;
+               return FALSE;
        }
        lib->plugins->status(lib->plugins, LEVEL_CTRL);
 
@@ -680,6 +680,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
 
        /* start daemon (i.e. the threads in the thread-pool) */
        charon->start(charon);
+       return TRUE;
 }
 
 /**
index a7b8a8c..305ee80 100644 (file)
@@ -214,12 +214,20 @@ public class CharonVpnService extends VpnService implements Runnable
                                                mIsDisconnecting = false;
 
                                                BuilderAdapter builder = new BuilderAdapter(mCurrentProfile.getName());
-                                               initializeCharon(builder, mLogFile, mCurrentProfile.getVpnType().getEnableBYOD());
-                                               Log.i(TAG, "charon started");
-
-                                               initiate(mCurrentProfile.getVpnType().getIdentifier(),
-                                                                mCurrentProfile.getGateway(), mCurrentProfile.getUsername(),
-                                                                mCurrentProfile.getPassword());
+                                               if (initializeCharon(builder, mLogFile, mCurrentProfile.getVpnType().getEnableBYOD()))
+                                               {
+                                                       Log.i(TAG, "charon started");
+                                                       initiate(mCurrentProfile.getVpnType().getIdentifier(),
+                                                                        mCurrentProfile.getGateway(), mCurrentProfile.getUsername(),
+                                                                        mCurrentProfile.getPassword());
+                                               }
+                                               else
+                                               {
+                                                       Log.e(TAG, "failed to start charon");
+                                                       setError(ErrorState.GENERIC_ERROR);
+                                                       setState(State.DISABLED);
+                                                       mCurrentProfile = null;
+                                               }
                                        }
                                }
                                catch (InterruptedException ex)
@@ -517,8 +525,9 @@ public class CharonVpnService extends VpnService implements Runnable
         * @param builder BuilderAdapter for this connection
         * @param logfile absolute path to the logfile
         * @param boyd enable BYOD features
+        * @return TRUE if initialization was successful
         */
-       public native void initializeCharon(BuilderAdapter builder, String logfile, boolean byod);
+       public native boolean initializeCharon(BuilderAdapter builder, String logfile, boolean byod);
 
        /**
         * Deinitialize charon, provided by libandroidbridge.so