implement minimal NIF to create and read from a TUN device

This commit is contained in:
lilly 2026-05-20 18:00:28 +02:00
commit 7b4d355d1a
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
12 changed files with 963 additions and 1 deletions

View file

@ -0,0 +1,33 @@
defmodule P2pChat.Transport.TunTap do
use GenServer
defstruct [:tun_fd]
@impl true
def init(args) do
{:ok, io_device} = setup_tap_device()
{:ok, {}}
end
@impl true
def terminate(_reason, state) do
end
def setup_tap_device(name \\ "tunP2P") do
{:ok, io_device} = :file.open(~c"/dev/net/tun", [:read, :write, :raw])
:ok = :file.close(io_device)
:ok
end
def teardown_tap_device(io_device) do
end
#
# Client API
#
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
end

View file

@ -0,0 +1,8 @@
defmodule P2pChat.Transport.TunTapPort do
use Rustler, otp_app: :p2p_chat, crate: "p2pchat_transport_tuntapport"
def make_tun_device(), do: :erlang.nif_error(:nif_not_loaded)
def read(_handle, _length \\ 2**16), do: :erlang.nif_error(:nif_not_loaded)
end