Class: PacketGen::Header::DHCPv6::Option

Inherits:
Types::Fields show all
Includes:
Types::Fieldable
Defined in:
lib/packetgen/header/dhcpv6/option.rb

Overview

A DHCPv6 consists of:

Subclasses handles known options. These subclasses may remove #data field to replace it by specific option field(s).

Author:

  • Sylvain Daubert

Since:

  • 2.5.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::Fieldable

#format_inspect, #read, #sz, #to_s, #type_name

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?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(options = {}) ⇒ Option

Create an Option

Parameters:

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

Since:

  • 2.5.0



77
78
79
80
81
# File 'lib/packetgen/header/dhcpv6/option.rb', line 77

def initialize(options={})
  options[:length] = options[:data].to_s.size if options[:data]
  super
  self.length = self.sz - 4 if options[:data].nil?
end

Instance Attribute Details

#dataString

variable length option data.

Returns:

  • (String)


34
35
# File 'lib/packetgen/header/dhcpv6/option.rb', line 34

define_field :data, Types::String,
builder: ->(h, t) { t.new(length_from: h[:length]) }

#lengthInteger

16-bit option length

Returns:

  • (Integer)


30
# File 'lib/packetgen/header/dhcpv6/option.rb', line 30

define_field :length, Types::Int16

#typeInteger

16-bit option type

Returns:

  • (Integer)


26
# File 'lib/packetgen/header/dhcpv6/option.rb', line 26

define_field :type, Types::Int16

Class Method Details

.new(options = {}) ⇒ Option

Create a new Option object (or a subclass)

Parameters:

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

Returns:

Since:

  • 2.5.0



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/packetgen/header/dhcpv6/option.rb', line 56

def new(options={})
  return super unless self == Option

  case options[:type]
  when Integer
    klass = Option.subclasses[options[:type]]
    klass&.new(options)
  when String
    if DHCPv6.const_defined?(options[:type])
      klass = DHCPv6.const_get(options[:type])
      options.delete :type
      klass.new(options) if klass < Option
    end
  else
    super
  end
end

.subclassesHash

Get Option subclasses

Returns:

  • (Hash)

Since:

  • 2.5.0



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/packetgen/header/dhcpv6/option.rb', line 40

def subclasses
  return @klasses if defined? @klasses

  @klasses = []
  DHCPv6.constants.each do |cst|
    klass = DHCPv6.const_get(cst)
    next unless klass.is_a?(Class) && (klass < Option)

    @klasses[klass.new.type] = klass
  end
  @klasses
end

Instance Method Details

#human_typeString

Get human-readable #type

Returns:

  • (String)

Since:

  • 2.5.0



88
89
90
91
92
93
94
# File 'lib/packetgen/header/dhcpv6/option.rb', line 88

def human_type
  if self.instance_of?(Option)
    "option#{type}"
  else
    self.class.to_s.sub(/.*::/, '')
  end
end

#to_humanString

Get a human-readable string for this option

Returns:

  • (String)

Since:

  • 2.5.0



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

def to_human
  str = +"#{human_type}:"
  if respond_to?(:human_data) && !human_data.empty?
    str << human_data
  elsif !self[:data].nil?
    str << data.inspect
  else
    # No data: only give option name
    human_type
  end
end