Class: PacketGen::Header::DHCP::Option

Inherits:
Types::TLV show all
Defined in:
lib/packetgen/header/dhcp/option.rb

Overview

DHCP option

A DHCP option is a TLV, so it has:

Author:

  • Sylvain Daubert

Since:

  • 2.2.0

Constant Summary collapse

TYPES =

Option types

Since:

  • 2.2.0

Instance Attribute Summary

Attributes inherited from Types::TLV

#length, #type, #value

Instance Method Summary collapse

Methods inherited from Types::TLV

#old_type=

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(options = {}) ⇒ Option

Returns a new instance of Option.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :type (Integer)
  • :length (Integer)
  • :value (String)

Since:

  • 2.2.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/packetgen/header/dhcp/option.rb', line 95

def initialize(options={})
  super
  return unless DHCP_OPTIONS.key?(self.type)

  h = DHCP_OPTIONS[self.type].last
  return unless h.is_a? Hash

  h.each do |k, v|
    self.length = v if k == :length
    if k == :v
      self[:value] = v.new
      self.value = options[:value] if options[:value]
    end
  end
end

Instance Method Details

#human_typeString

Get human-readable type

Returns:

  • (String)

Since:

  • 2.2.0



138
139
140
141
142
143
144
# File 'lib/packetgen/header/dhcp/option.rb', line 138

def human_type
  if DHCP_OPTIONS.key?(type)
    DHCP_OPTIONS[type].first.dup
  else
    type.to_s
  end
end

#human_types?true

Returns:

  • (true)

Since:

  • 2.7.0



132
133
134
# File 'lib/packetgen/header/dhcp/option.rb', line 132

def human_types?
  true
end

#private_readObject

Since:

  • 2.2.0



112
# File 'lib/packetgen/header/dhcp/option.rb', line 112

alias private_read read

#read(str) ⇒ Option, ...

Populate object from a binary string

Parameters:

  • str (String)

Returns:

  • (Option, End, Pad)

    may return another object than itself

Since:

  • 2.2.0



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/packetgen/header/dhcp/option.rb', line 117

def read(str)
  read_type = str[0].unpack('C').first
  if read_type.zero?
    Pad.new.read(str)
  elsif read_type == 255
    End.new.read(str)
  elsif DHCP_OPTIONS.key?(read_type)
    Option.new(DHCP_OPTIONS[read_type][1] || {}).private_read(str)
  else
    super
  end
end

#to_humanString

Returns:

  • (String)

Since:

  • 2.2.0



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/packetgen/header/dhcp/option.rb', line 147

def to_human
  s = human_type
  if length > 0
    s << if value.respond_to? :to_human
           ":#{value.to_human}"
         elsif self[:value].is_a? Types::Int
           ":#{self.value.to_i}"
         else
           ":#{value.inspect}"
         end
  end
  s
end