How to use VPN in Android?

Sudarshan picture Sudarshan · Nov 5, 2012 · Viewed 8.6k times · Source

I am exploring VPN Connectivity in Android. I am new to this section.

  1. I want to know how to setup a VPN connection in Android using an application?

  2. I came across a sample code namely ToyVpn.I ran the application but i dont know what datas i need to give in the form shown by that application, Consider iam having a VPN server with IP address (say 10.162.1.2), what do I need to do in that application to make that app work?

  3. If I try to use any public VPN server, what do I need to do?

  4. I had downloaded Openvpn source from here and I compiled and ran in my ICS device, but i don't know how to configure the data that need to be entered. Please provide the exact way to use this app.

Answer

Paul Lammertsma picture Paul Lammertsma · Nov 28, 2013

I want to know how to setup a VPN connection in Android using an application?

The Android SDK comes bundled with a sample; see ToyVpn in the android-16 samples directory. Also consult the documentation for VpnService and VpnService.Builder.

I came across a sample code namely ToyVpn.I ran the application but i dont know what datas i need to give in the form shown by that application, Consider iam having a VPN server with ip-address (say 10.162.1.2), What i need to do in that application to make that app work .

Your VPN will need to create a new socket, protect the socket from being routed back into the VPN using VpnService.protect(Socket), and connect the socket to 10.162.1.2. Having set up a tunnel connection to the VPN server, you should proceed to writing the input stream of the VpnService's interface into the tunnel's output stream, and in turn write the tunnel response back into the interface output stream.

If i try to use any public VPN server, What i need to do?

There is no standard mechanism in place for setting up a connection to a VPN server. ToyVpn simply communicates over a socket using raw TCP packets. You can either implement an existing protocol (see RFC 4026) like OpenVPN has done or write something yourself.