简体   繁体   中英

IllegalState when creating interface with Android 4.0's VpnService.Builder

I am experimenting with the VpnService that was added to Android in SDK 4.0. I have gotten as far as starting the VpnService, however, I am unable to create the virtual interface. My code throws an IllegalStateException and I am not able to figure out why. The code is inside a Thread, which is an inner-class of my VPNService-class.

My cut down example looks like this:

        ParcelFileDescriptor pfd = null;

        VpnService.Builder b = new VpnService.Builder();
        try{
            b.addAddress("10.0.6.2", 24);
            b.setMtu(1460);
            b.setSession("TunnelTest");
        } catch(IllegalArgumentException e){
            Log.v("Service", "Address is invalid!");
            stopSelf();
            return;
        }

        try{
            pfd = b.establish();
        } catch (IllegalArgumentException e){
            Log.v("Service", "Interface was provided with an illegal argument");
        } catch (IllegalStateException e){
            e.printStackTrace();
            Log.v("Service", "Illegal state");
        } catch (SecurityException e){
            Log.v("Service", "Security exception thrown");

What I try to do here is to create a virtual interface with IP 10.0.6.2, belonging to the 10.6.0.0/24 subnet. The exception is thrown by the b.establish() call.

This is the full stack trace:

11-30 17:51:10.863: W/System.err(2741): java.lang.IllegalStateException: Cannot create interface
11-30 17:51:10.875: W/System.err(2741):     at android.os.Parcel.readException(Parcel.java:1335)
11-30 17:51:10.875: W/System.err(2741):     at android.os.Parcel.readException(Parcel.java:1281)
11-30 17:51:10.875: W/System.err(2741):     at android.net.IConnectivityManager$Stub$Proxy.establishVpn(IConnectivityManager.java:1321)
11-30 17:51:10.875: W/System.err(2741):     at android.net.VpnService$Builder.establish(VpnService.java:472)
11-30 17:51:10.875: W/System.err(2741):     at no.simtel.TestVpnService$TestVpn.run(TestVpnService.java:37)

您可以使用adb logcat来验证您是否遇到了此问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM