tun

The tun module (built on songgao/water) creates a TUN virtual network interface and reads/writes raw IP packets — the building block for VPNs and custom tunnels. It requires root privileges; open fails without them. Import it with import tun.

Functions

FunctionSignatureDescription
opentun.open(opts?) → intCreate a TUN interface; returns a handle (-1 on error)
readtun.read(h: int, maxBytes: int) → stringRead raw packet bytes ("" on EOF/error)
writetun.write(h: int, bytes: string) → boolWrite a raw packet
nametun.name(h: int) → stringInterface name, e.g. "utun5" or "tun0"
closetun.close(h: int) → boolClose the interface

Example

import tun

let h = tun.open()
if h == -1 {
    println("need root to open a TUN device")
    exit(1)
}

println("interface: " + tun.name(h))

while true {
    let packet = tun.read(h, 2048)
    if packet == "" { break }
    // inspect or forward the raw IP packet
}

tun.close(h)
Standard library · View as Markdown · llms-full.txt