# Class VpnTunnel

java.lang.Object
com.codename1.vpn.tunnel.VpnTunnel

public abstract class VpnTunnel extends Object

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
  Constructor
  Description
  `VpnTunnel()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected final void`
  `forward(byte[] packet, int offset, int length)`
  Sends bytes the tunnel produced itself.
  `protected final void`
  `forward(PacketBuffer packet)`
  Sends a packet back to the device.
  `protected abstract void`
  `onPacket(PacketBuffer packet)`
  One packet arrived from the device, bound for the far end.
  `protected abstract void`
  `onStart(TunnelConfiguration configuration)`
  The tunnel is up and the platform has configured the link.
  `protected abstract void`
  `onStop(TunnelStopReason reason)`
  The tunnel is going down, for the given reason.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### VpnTunnel

    public VpnTunnel()

- ## Method Details

  - ### onStart

    protected abstract void onStart(TunnelConfiguration configuration)
    The tunnel is up and the platform has configured the link.

  - ### onPacket

    protected abstract void onPacket(PacketBuffer packet)

    One packet arrived from the device, bound for the far end.

    The buffer is REUSED once this returns; see `PacketBuffer`.

  - ### onStop

    protected abstract void onStop(TunnelStopReason reason)
    The tunnel is going down, for the given reason.

  - ### forward

    protected final void forward(PacketBuffer packet)

    Sends a packet back to the device.

    Safe to call with the buffer `onPacket` was 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.