class

Tuntap::Device

Inherits Tuntap::Interface / Reference / Object

Describes a TUN/TAP device. Some methods may need additional permissions, please make sure to read them before something "doesn't work".

Constants

CLONE_DEVICE = "/dev/net/tun"

Path to the clone device. May be different for non-Linux systems.

Constructors

new(handle : IO::FileDescriptor, name : String, flags : LibC::IfReqFlags)
Source
open(device_name : String | Nil = nil, flags = LibC::IfReqFlags::Tun) : self

Creates a brand new device or opens an existing one. To create a new one, the user needs to have one of these:

  • root permissions (The effective UID is 0, or user is a system user)
  • the CAP_NET_ADMIN permission

To open an existing device, the requirements are these:

  • The device already exists
  • The device is owned by this process's UNIX user
  • The user has read/write permissions on /dev/net/tun
  • The flags match those used to create the device

If creation fails, an Errno is raised.

If the device_name is nil, one is automatically chosen by the system. If given, its length must be <= 16. As for the flags, exactly one of Tun or Tap must be set. If any of these are violated, an ArgumentError is raised.

Source

Instance methods

flags

The flags this device created with

Source
handle

The handle of the tunnel device, which can be read from and written to.

Source
read(buffer = Bytes.new(@mtu)) : Bytes

Reads from the device, putting the data into buffer. Returns the slice trimmed down to the size of the received packet.

Source
read_packet

Reads a packet from the device

Source
write(packet : Bytes)

Writes the packet into the tunnel device.

Source
write(packet : IpPacket)

ditto

Source