Class: Lib::DHCP::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lib/dhcp/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Options

Returns a new instance of Options.



17
18
19
20
# File 'lib/lib/dhcp/options.rb', line 17

def initialize(*options)
  @options = []
  options.each { |option| self.add option }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/lib/dhcp/options.rb', line 11

def options
  @options
end

Class Method Details

.unpack(packet) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lib/dhcp/options.rb', line 56

def self.unpack(packet)
  size = packet.size
  offset = 0
  options = self.new
  while offset < size
    payload = packet.unpack("@#{offset}a*").first
    option = Lib::DHCP::Option.unpack(payload)
    options << option
    break if option.oid == Lib::DHCP::Option::END_OPTION
    offset += (option.len.to_i + 2)
    #(option.oid == 0 or option.oid == 255) ? offset += (option.len.to_i + 1) : offset += (option.len.to_i + 2)
  end
  options
end

Instance Method Details

#[]=(index, option) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/lib/dhcp/options.rb', line 30

def []=(index, option)
  option = create_option(option)
  if include?(option) and @options[index].oid != option.oid
    @options[index] = option if option.oid == 0
  else
    @options[index] = create_option(option)
  end

end

#add(option) ⇒ Object Also known as: <<



26
27
28
# File 'lib/lib/dhcp/options.rb', line 26

def add(option)
  @options << create_option(option)
end

#del(oid) ⇒ Object



50
51
52
# File 'lib/lib/dhcp/options.rb', line 50

def del(oid)
  @options.delete_if{|option| option.oid.to_i == oid.to_i}
end

#include?(option) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/lib/dhcp/options.rb', line 40

def include?(option)
  return ! select(option.oid).empty? if option.is_a?(Option)
  return ! select(option).empty? if option.is_a?(Integer)
  false
end

#packObject



22
23
24
# File 'lib/lib/dhcp/options.rb', line 22

def pack
  self.map{|option| option.pack}.join('')
end

#select(oid) ⇒ Object



46
47
48
# File 'lib/lib/dhcp/options.rb', line 46

def select(oid)
  @options.select {|option| option.oid.to_i == oid.to_i}
end