title: How to extend VpnService to open an L2TP/IPSEC connection in android programmatically?
body: <p>I am looking to implement a solution to open a <code>L2TP/IPSEC</code> connection from the android application so I can simply pass the <code>server ip</code>, <code>pre-shared key</code>, <code>username</code> and <code>password</code> and so the device is connected to this VPN server</p>
  
  <p>I looked here <a href="https://developer.android.com/reference/android/net/VpnService.html" rel="nofollow noreferrer">https://developer.android.com/reference/android/net/VpnService.html</a> that we need to extend and build our own VPN solutions to do that.</p>
  
  <p>Is there any available solutions for that so we can use it instead of reinventing the wheel?</p>
  
  <p>I tried the below implementations of <code>VpnService</code> but none of them worked for me</p>
  
  <ul>
  <li><a href="https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn" rel="nofollow noreferrer">https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn</a></li>
  <li><a href="https://pomgit.com/limeysoft/L2TP-VPN/tree/master/app" rel="nofollow noreferrer">https://pomgit.com/limeysoft/L2TP-VPN/tree/master/app</a></li>
  </ul>
  
  <p>And tried this</p>
  
  <ul>
  <li><a href="https://alvinalexander.com/java/jwarehouse/android/vpn/java/android/net/vpn/" rel="nofollow noreferrer">https://alvinalexander.com/java/jwarehouse/android/vpn/java/android/net/vpn/</a></li>
  </ul>
  
  <p>and got the below error</p>
  
  <blockquote>
    <p>mtpd: Start VPN daemon: mtpd W/libc: Set property "ctl.start" to
    "mtpd"
            Unable to set property "ctl.start" to "mtpd": connection failed; errno=13 (Permission denied) E/SysPropJNI:
    SystemProperties_set key:ctl.start,val:mtpd W/System.err:
    java.io.IOException: cannot start service: mtpd W/System.err:     at
    com.mega.speed.vpn.DaemonProxy.start(DaemonProxy.java:58)
    W/System.err:     at
    com.mega.speed.vpn.VpnDaemons.startDaemon(VpnDaemons.java:92)
                      at com.mega.speed.vpn.VpnDaemons.startMtpd(VpnDaemons.java:113)
                      at com.mega.speed.vpn.VpnDaemons.startL2tp(VpnDaemons.java:36)
    W/System.err:     at
    com.mega.speed.vpn.L2tpService.connect(L2tpService.java:16)
                      at com.mega.speed.fragments.HotspotFragment.onCreateView(HotspotFragment.java:125)
    W/System.err:     at
    android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    W/System.err:     at
    android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    W/System.err:     at
    android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    W/System.err:     at
    android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    W/System.err:     at
    android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
                      at android.os.Handler.handleCallback(Handler.java:808) W/System.err:<br>
    at android.os.Handler.dispatchMessage(Handler.java:101)
                      at android.os.Looper.loop(Looper.java:166) W/System.err:     at
    android.app.ActivityThread.main(ActivityThread.java:7529)
    W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    W/System.err:     at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)</p>
  </blockquote>