package org.kne.cloud.network.tun;
import one.papachi.tuntap4j.TunDevice;

import java.io.IOException;
import java.nio.ByteBuffer;

class App {

    public static void main(String[] args) throws IOException {
        String deviceName = "klalb0"; // it can be any name e.g. "mytun"
        TunDevice device = new TunDevice(deviceName); // initialize a TUN device
        device.open(); // open device for reading and writing
        device.setStatus(true); // set device status to "up"
        device.setIPAddress("10.0.0.0", "255.0.0.0"); // set IPv4 address and mask
        int mtu = device.getMTU(); // get MTU of the device
        ByteBuffer buffer = ByteBuffer.allocate(mtu); // allocate ByteBuffer to use in read operation (direct ByteBuffer is supported)
        while (true) {
            device.read(buffer.clear()); // read from the device into ByteBuffer
            buffer.flip(); // prepare ByteBuffer for reading
            // TODO process IP packet
        }
    }

}