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, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, delete_field, fields, #fields, #force_binary, inherited, #inspect, #is_optional?, #is_present?, #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



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

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

#has_human_types?true

Deprecated.

Use #human_types? instead

Returns:

  • (true)

Since:

  • 2.2.0



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

def has_human_types?
  Deprecation.deprecated(self.class, __method__, 'human_types?')
  human_types?
end

#human_typeObject

Since:

  • 2.2.0



145
146
147
148
149
150
151
# File 'lib/packetgen/header/dhcp/option.rb', line 145

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



134
135
136
# File 'lib/packetgen/header/dhcp/option.rb', line 134

def human_types?
  true
end

#private_readObject

Since:

  • 2.2.0



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

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



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

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_humanObject

Since:

  • 2.2.0



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/packetgen/header/dhcp/option.rb', line 153

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