Class: DHCP::Option

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

Overview

General object to capture DHCP options. Every option of the protocol has three fields: an option type, a defined length and a payload

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Option

Create a DHCP option object with the given type and payload. params must be an array containing at least these two keys: :type and :payload The length is calculated with the size of the payload



33
34
35
36
37
38
39
40
41
42
# File 'lib/net/dhcp/options.rb', line 33

def initialize(params = {})
  # We need a type, and a payload
  if (([:type, :payload] & params.keys).size != 2)
    raise ArgumentError, 'you need to specify values for :type and :payload'
  end

  self.type = params[:type]
  self.payload = params[:payload]
  self.len = params.fetch(:len, self.payload.size)
end

Instance Attribute Details

#lenObject

Returns the value of attribute len.



28
29
30
# File 'lib/net/dhcp/options.rb', line 28

def len
  @len
end

#payloadObject

Returns the value of attribute payload.



28
29
30
# File 'lib/net/dhcp/options.rb', line 28

def payload
  @payload
end

#typeObject

Returns the value of attribute type.



28
29
30
# File 'lib/net/dhcp/options.rb', line 28

def type
  @type
end

Instance Method Details

#eql?(obj) ⇒ Boolean Also known as: ==

Check wether a given option is equivalent (protocol level) to this one.

Returns:

  • (Boolean)


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

def eql?(obj)
  return false unless (self.class == obj.class)

  vars = self.instance_variables
  # check all the other instance vairables
  vars.each do |var|
    return false unless (self.instance_variable_get(var) == obj.instance_variable_get(var))
  end
  return true
end

#packObject

Return the option packed as a binary string.



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

def pack
  (self.to_a).pack('C*')
end

#to_aObject

Return the option packed as an array of bytes. The first two elements are the type and length of this option. The payload follows afterwards.



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

def to_a
  return [self.type, self.len] + self.payload
end

#to_sObject



68
69
70
# File 'lib/net/dhcp/options.rb', line 68

def to_s
  "to_s NOT implemented for option type: #{self.type}"
end