android: Create a new VpnService.Builder after VPN has been established
authorTobias Brunner <tobias@strongswan.org>
Wed, 5 Sep 2012 09:36:00 +0000 (11:36 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 6 Sep 2012 09:25:24 +0000 (11:25 +0200)
src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java

index 9b502e8..3a91c2f 100644 (file)
@@ -519,12 +519,19 @@ public class CharonVpnService extends VpnService implements Runnable
         */
        public class BuilderAdapter
        {
         */
        public class BuilderAdapter
        {
-               VpnService.Builder builder;
+               private final String mName;
+               private VpnService.Builder mBuilder;
 
                public BuilderAdapter(String name)
                {
 
                public BuilderAdapter(String name)
                {
-                       builder = new CharonVpnService.Builder();
-                       builder.setSession(name);
+                       mName = name;
+                       mBuilder = createBuilder(name);
+               }
+
+               private VpnService.Builder createBuilder(String name)
+               {
+                       VpnService.Builder builder = new CharonVpnService.Builder();
+                       builder.setSession(mName);
 
                        /* even though the option displayed in the system dialog says "Configure"
                         * we just use our main Activity */
 
                        /* even though the option displayed in the system dialog says "Configure"
                         * we just use our main Activity */
@@ -533,13 +540,14 @@ public class CharonVpnService extends VpnService implements Runnable
                        PendingIntent pending = PendingIntent.getActivity(context, 0, intent,
                                                                                                                          Intent.FLAG_ACTIVITY_NEW_TASK);
                        builder.setConfigureIntent(pending);
                        PendingIntent pending = PendingIntent.getActivity(context, 0, intent,
                                                                                                                          Intent.FLAG_ACTIVITY_NEW_TASK);
                        builder.setConfigureIntent(pending);
+                       return builder;
                }
 
                public synchronized boolean addAddress(String address, int prefixLength)
                {
                        try
                        {
                }
 
                public synchronized boolean addAddress(String address, int prefixLength)
                {
                        try
                        {
-                               builder.addAddress(address, prefixLength);
+                               mBuilder.addAddress(address, prefixLength);
                        }
                        catch (IllegalArgumentException ex)
                        {
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -552,7 +560,7 @@ public class CharonVpnService extends VpnService implements Runnable
                {
                        try
                        {
                {
                        try
                        {
-                               builder.addDnsServer(address);
+                               mBuilder.addDnsServer(address);
                        }
                        catch (IllegalArgumentException ex)
                        {
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -565,7 +573,7 @@ public class CharonVpnService extends VpnService implements Runnable
                {
                        try
                        {
                {
                        try
                        {
-                               builder.addRoute(address, prefixLength);
+                               mBuilder.addRoute(address, prefixLength);
                        }
                        catch (IllegalArgumentException ex)
                        {
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -578,7 +586,7 @@ public class CharonVpnService extends VpnService implements Runnable
                {
                        try
                        {
                {
                        try
                        {
-                               builder.addSearchDomain(domain);
+                               mBuilder.addSearchDomain(domain);
                        }
                        catch (IllegalArgumentException ex)
                        {
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -591,7 +599,7 @@ public class CharonVpnService extends VpnService implements Runnable
                {
                        try
                        {
                {
                        try
                        {
-                               builder.setMtu(mtu);
+                               mBuilder.setMtu(mtu);
                        }
                        catch (IllegalArgumentException ex)
                        {
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -605,7 +613,7 @@ public class CharonVpnService extends VpnService implements Runnable
                        ParcelFileDescriptor fd;
                        try
                        {
                        ParcelFileDescriptor fd;
                        try
                        {
-                               fd = builder.establish();
+                               fd = mBuilder.establish();
                        }
                        catch (Exception ex)
                        {
                        }
                        catch (Exception ex)
                        {
@@ -616,6 +624,9 @@ public class CharonVpnService extends VpnService implements Runnable
                        {
                                return -1;
                        }
                        {
                                return -1;
                        }
+                       /* now that the TUN device is created we don't need the current
+                        * builder anymore, but we might need another when reestablishing */
+                       mBuilder = createBuilder(mName);
                        return fd.detachFd();
                }
        }
                        return fd.detachFd();
                }
        }