Class: Xlat::Adapters::LinuxTun

Inherits:
Object
  • Object
show all
Defined in:
lib/xlat/adapters/linux_tun.rb

Constant Summary collapse

DEV_TUN =
-'/dev/net/tun'
IFF_TUN =
0x0001
IFF_MULTI_QUEUE =
0x0100
IFF_NO_PI =
0x1000
TUNSETIFF =
0x400454ca
SIOCSIFMTU =
0x8922

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ifname, multiqueue: false) ⇒ LinuxTun

Returns a new instance of LinuxTun.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/xlat/adapters/linux_tun.rb', line 14

def initialize(ifname, multiqueue: false)
  unless ifname.bytesize < Socket::IFNAMSIZ  # maxlen including the terminating NUL
    raise ArgumentError, "Too long interface name: #{ifname}"
  end

  @ifname = ifname
  @mtu = 1500
  @io = File.open(DEV_TUN, 'r+:BINARY')
  options = IFF_TUN | (multiqueue ? IFF_MULTI_QUEUE : 0) | IFF_NO_PI
  @io.ioctl(TUNSETIFF, [@ifname, options].pack("a#{Socket::IFNAMSIZ}s!"))
end

Instance Attribute Details

#mtuObject

Returns the value of attribute mtu.



26
27
28
# File 'lib/xlat/adapters/linux_tun.rb', line 26

def mtu
  @mtu
end

Class Method Details

.openObject



47
48
49
50
51
52
# File 'lib/xlat/adapters/linux_tun.rb', line 47

def self.open(...)
  tun = new(...)
  yield tun
ensure
  tun&.close
end

Instance Method Details

#closeObject



43
44
45
# File 'lib/xlat/adapters/linux_tun.rb', line 43

def close
  @io.close
end

#read(buf) ⇒ Object



35
36
37
# File 'lib/xlat/adapters/linux_tun.rb', line 35

def read(buf)
  IOBufferExt.readv(@io, [buf])
end

#write(*bufs) ⇒ Object



39
40
41
# File 'lib/xlat/adapters/linux_tun.rb', line 39

def write(*bufs)
  IOBufferExt.writev(@io, bufs)
end