Class: VirtualBox::Net::Dhcp

Inherits:
Object
  • Object
show all
Defined in:
lib/virtual_box/net/dhcp.rb

Overview

Specification for a virtual DHCP server.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dhcp

Creates a DHCP server specification based on the given attributes.

The DHCP server is not automatically added to VirtualBox.

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})

    ActiveRecord-style initial values for attributes; can be used together with Net#to_hash to save and restore



51
52
53
# File 'lib/virtual_box/net/dhcp.rb', line 51

def initialize(options = {})
  options.each { |k, v| self.send :"#{k}=", v }
end

Instance Attribute Details

#end_ipString

The last IP address in this DHCP server’s address pool.

Returns:

  • (String)


21
22
23
# File 'lib/virtual_box/net/dhcp.rb', line 21

def end_ip
  @end_ip
end

#ipString

This DHCP server’s IP address on the virtual network that it serves.

Returns:

  • (String)


9
10
11
# File 'lib/virtual_box/net/dhcp.rb', line 9

def ip
  @ip
end

#netmaskString

The network mask reported by this DHCP server.

Returns:

  • (String)


13
14
15
# File 'lib/virtual_box/net/dhcp.rb', line 13

def netmask
  @netmask
end

#start_ipString

The first IP address in this DHCP server’s address pool.

Returns:

  • (String)


17
18
19
# File 'lib/virtual_box/net/dhcp.rb', line 17

def start_ip
  @start_ip
end

Class Method Details

.allHash<String, VirtualBox::Dhcp>

The DHCP servers added to with VirtualBox.

Returns:

  • (Hash<String, VirtualBox::Dhcp>)

    all the DHCP servers added to VirtualBox, indexed by the name of the virtual network that they serve



104
105
106
107
108
109
110
111
112
# File 'lib/virtual_box/net/dhcp.rb', line 104

def self.all
  output = VirtualBox.run_command! ['VBoxManage', '--nologo', 'list',
                                    '--long', 'dhcpservers']
  Hash[output.split("\n\n").map { |dhcp_info|
    dhcp = new
    if_name = dhcp.from_dhcp_info(dhcp_info)
    [if_name, dhcp]
  }]
end

.ip_btos(ip_number) ⇒ String

Converts an IP number into a string.

Parameters:

  • ip_number (Integer)

    a 32-bit big-endian number holding an IP address

Returns:

  • (String)

    the IP address, encoded using the dot (.) notation



137
138
139
# File 'lib/virtual_box/net/dhcp.rb', line 137

def self.ip_btos(ip_number)
  [ip_number].pack('N').unpack('C*').join('.')
end

.ip_stob(ip_string) ⇒ Integer

Converts an IP string into a number.

Parameters:

  • ip_string (String)

    an IP address using the dot (.) notation

Returns:

  • (Integer)

    the IP adddres, encoded as a 32-bit big-endian number



145
146
147
# File 'lib/virtual_box/net/dhcp.rb', line 145

def self.ip_stob(ip_string)
  ip_string.split('.').map(&:to_i).pack('C*').unpack('N').first
end

Instance Method Details

#add(net_or_name) ⇒ VirtualBox::Net::Dhcp

Adds this DHCP server to VirtualBox.

Parameters:

  • net_or_name (String, VirtualBox::Net)

    the name of the VirtualBox virtual network that this server will be connected to

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/virtual_box/net/dhcp.rb', line 69

def add(net_or_name)
  command = ['VBoxManage', 'dhcpserver', 'add', '--ip', ip,
      '--netmask', netmask, '--lowerip', start_ip, '--upperip', end_ip,
      '--enable']
  if net_or_name.kind_of? VirtualBox::Net
    command.push '--ifname', net_or_name.name
  else
    command.push '--netname', net_or_name
  end

  VirtualBox.run_command! command
  self
end

#from_dhcp_info(dhcp_info) ⇒ String

Parses information about a DHCP server returned by VirtualBox.

The parsed information is used to replace this network’s specification.

Parameters:

  • dhcp_info (String)

    output from “VBoxManage list –long dhcpservers” for one server

Returns:

  • (String)

    the name of the virtual network served by this DHCP server



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/virtual_box/net/dhcp.rb', line 120

def from_dhcp_info(dhcp_info)
  info = Hash[dhcp_info.split("\n").map { |line|
    line.split(':', 2).map(&:strip)
  }]

  self.ip = info['IP']
  self.netmask = info['NetworkMask']
  self.start_ip = info['lowerIPAddress']
  self.end_ip = info['upperIPAddress']

  info['NetworkName']
end

#remove(net_or_name) ⇒ VirtualBox::Net::Dhcp

Removes this DHCP server from VirtualBox.

Parameters:

  • net_or_name (String, VirtualBox::Net)

    the name of the VirtualBox virtual network that this server was connected to

Returns:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/virtual_box/net/dhcp.rb', line 88

def remove(net_or_name)
  command = ['VBoxManage', 'dhcpserver', 'remove']
  if net_or_name.kind_of? VirtualBox::Net
    command.push '--ifname', net_or_name.name
  else
    command.push '--netname', net_or_name
  end

  VirtualBox.run_command command
  self
end

#to_hashHash<Symbol, Object>

Hash capturing this specification. Can be passed to Dhcp#new.

Returns:

  • (Hash<Symbol, Object>)

    Ruby-friendly Hash that can be used to re-create this DHCP server specification



59
60
61
62
# File 'lib/virtual_box/net/dhcp.rb', line 59

def to_hash
  { :net_name => 'net_name', :ip => ip, :netmask => netmask,
    :start_ip => start_ip, :end_ip => end_ip }
end