class Tuntap::Device

Overview

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

Defined in:

tuntap/device.cr

Constant Summary

CLONE_DEVICE = "/dev/net/tun"

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

Constructors

Instance Method Summary

Instance methods inherited from class Tuntap::Interface

add_address(address : String) : Nil
add_address(address : LibC::Sockaddr) : Nil
add_address
, add_netmask(address : String) : Nil
add_netmask(address : LibC::Sockaddr) : Nil
add_netmask
, add_route(destination : String, gateway : String = UNSET_ADDR, mask : String = UNSET_ADDR, metric = DEFAULT_METRIC, mtu = DEFAULT_MTU, window = 0, flags : LibC::RtEntryFlags = LibC::RtEntryFlags::Up) add_route, close close, down! : Nil down!, fd : IO::FileDescriptor fd, ioctl(command, argument) : Int32 ioctl, mtu : Int32 mtu, mtu=(mtu : Int32) mtu=, name : String name, up! : Nil up!

Constructor methods inherited from class Tuntap::Interface

new(fd : IO::FileDescriptor, name : String) new

Class methods inherited from class Tuntap::Interface

names : Array(String) names, open(name : String) open

Constructor Detail

def self.new(handle : IO::FileDescriptor, name : String, flags : LibC::IfReqFlags) #

[View source]
def self.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.


[View source]

Instance Method Detail

def flags : LibC::IfReqFlags #

The flags this device created with


[View source]
def handle : IO::FileDescriptor #

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


[View source]
def 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.


[View source]
def read_packet : IpPacket #

Reads a packet from the device


[View source]
def write(packet : Bytes) #

Writes the packet into the tunnel device.


[View source]
def write(packet : IpPacket) #

ditto


[View source]