Class VpnTunnel
The packet loop, written once for both platforms.
public class MyTunnel extends VpnTunnel {
protected void onStart(TunnelConfiguration cfg) { }
protected void onPacket(PacketBuffer p) { forward(p); }
protected void onStop(TunnelStopReason r) { }
}
Where this runs
On Android, in the app's own process, inside the port's VpnService.
Nowhere else: Tunnels.isSupported() is false on iOS, where a packet
tunnel would have to run in a Network Extension and the translation that
would give that process a virtual machine has not been written.
Write the tunnel as though it ran somewhere else anyway. Everything it
needs travels in TunnelConfiguration.getData(); a static the app set
happens to be there on Android and is not a thing to rely on.
What is expensive
A host process for a tunnel runs under a memory budget far below an
app's, and is killed rather than warned when it exceeds it. The buffers
handed to onPacket(PacketBuffer) are reused for exactly that reason, so copying
every packet -- or holding one past the call -- gives back the headroom
the pooling was for. See PacketBuffer.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidforward(byte[] packet, int offset, int length) Sends bytes the tunnel produced itself.protected final voidforward(PacketBuffer packet) Sends a packet back to the device.protected abstract voidonPacket(PacketBuffer packet) One packet arrived from the device, bound for the far end.protected abstract voidonStart(TunnelConfiguration configuration) The tunnel is up and the platform has configured the link.protected abstract voidonStop(TunnelStopReason reason) The tunnel is going down, for the given reason.
-
Constructor Details
-
VpnTunnel
public VpnTunnel()
-
-
Method Details
-
onStart
The tunnel is up and the platform has configured the link. -
onPacket
One packet arrived from the device, bound for the far end.
The buffer is REUSED once this returns; see
PacketBuffer. -
onStop
The tunnel is going down, for the given reason. -
forward
Sends a packet back to the device.
Safe to call with the buffer
onPacketwas given, which is the ordinary pass-through case and copies nothing. -
forward
protected final void forward(byte[] packet, int offset, int length) Sends bytes the tunnel produced itself.
-