class Tuntap::Device
- Tuntap::Device
- Tuntap::Interface
- Reference
- Object
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.crConstant Summary
-
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)
-
.open(device_name : String | Nil = nil, flags = LibC::IfReqFlags::Tun) : self
Creates a brand new device or opens an existing one.
Instance Method Summary
-
#flags : LibC::IfReqFlags
The flags this device created with
-
#handle : IO::FileDescriptor
The handle of the tunnel device, which can be read from and written to.
-
#read(buffer = Bytes.new(@mtu)) : Bytes
Reads from the device, putting the data into buffer.
-
#read_packet : IpPacket
Reads a packet from the device
-
#write(packet : Bytes)
Writes the packet into the tunnel device.
-
#write(packet : IpPacket)
ditto
Instance methods inherited from class Tuntap::Interface
add_address(address : String) : Niladd_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
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.
Instance Method Detail
The handle of the tunnel device, which can be read from and written to.
Reads from the device, putting the data into buffer. Returns the slice trimmed down to the size of the received packet.