Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VpnService.Builder.establish() sporadically null

When I try to create the tunnel interface for my VpnService, I'm getting the following error:

Attempt to invoke virtual method 'java.lang.String android.os.ParcelFileDescriptor.toString()' on a null object reference

The only resolve I currently have is to restart the device when this happens. If I do not, I'm unable to create the tunnel at all.

My code for creating the tunnel:

// This is inside my VpnService implementation 

private ParcelFileDescriptor configureTunnelWithPushOpts(PushOpts popts)
{
    VpnService.Builder builder = this.new Builder();

    builder.setMtu       ( currentServerPrefs.SERVER_MTU );
    builder.addAddress   ( popts.ip, 32                  );
    builder.addDnsServer ( popts.dns1                    );
    builder.addDnsServer ( popts.dns2                    );
    builder.addRoute     ( "0.0.0.0", 0                  );


    // Note: Blocking mode is now enabled in native
    // code under the setFileDescriptor function.
    // builder.setBlocking(true);

    builder.setConfigureIntent(
            PendingIntent.getActivity(
                    this,
                    0,
                    new Intent(
                            this,
                            MainActivity.class
                    ),
            PendingIntent.FLAG_UPDATE_CURRENT)
    );

    final ParcelFileDescriptor vpnInterface;

    synchronized (this) {
        builder.setSession(currentServerPrefs.SERVER_ADDRESS);
        vpnInterface = builder.establish();
    }

    Logger.info("New interface: " + vpnInterface.toString(), this);
    return vpnInterface;
}

Edit:

According to the documentation, the establish function will return null if the VpnService has not been prepared, however I am using this to prepare it prior to running the above function.

// This is inside my MainActivity class

Intent intent = VpnService.prepare(getApplicationContext());
if (intent != null) {
    startActivityForResult(intent, 0);
} else {
    onActivityResult(0, RESULT_OK, null);
}

. . . 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Intent intent = new Intent(this, MyVpnService.class);
        startService(intent);
    }
}

Phone I'm using for debugging

Google Nexus 5 Running Android 5.0.1

Edit

I figured out how to replicate it so that it's no longer sporadic. Basically, if I uninstall the application while the VPN is connected, and then reinstall it and try to start it back up, I receive a null response when i run builder.establish(). I fear this could pose potential issues when the application is updated through the Google Play Store.

Aside from that, Please do not mark this question as a duplicate. Every other question on this matter has been given the answer that the service needs to be prepared before the builder is established, however I am preparing it in my case and have a different cause for my problem. Any help would be appreciated.

like image 954
Nathan F. Avatar asked Oct 29 '22 04:10

Nathan F.


1 Answers

Thanks a lot for the hint, it brought me to the solution!

I had the same problem in Android 5.0.2: Everything worked fine, and I installed the vpn app I am developing. The next day, suddenly, an unresolvable NullPointerException, although prepare was properly called.

The issue could be solved in my environment with the following steps:

  • Uninstall the app. It does not matter, if the app has still working VPN functionality or not.
  • Restart your phone. Make sure that it was shut off completely.
  • After restart, make sure the app is not installed anymore (I had some strange development issues where an app was reinstalled after rebooting my phone).
  • Install your VPN app again. It should receive a vpn interface now, and not anymore null.
like image 95
Martin Bories Avatar answered Nov 11 '22 13:11

Martin Bories