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

Inherits:
BinStruct::Struct
  • Object
show all
Includes:
BinStruct::Structable
Defined in:
lib/packetgen/header/dhcpv6/option.rb

Overview

A DHCPv6 option consists of:

  • a #type (BinStruct::Int16),

  • a #length (BinStruct::Int16),

  • and a #data (BinStruct::String).

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataString

variable length option data.

Returns:

  • (String)


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

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

Instance Attribute Details

#lengthInteger

16-bit option length. Length of data part.

Returns:

  • (Integer)


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

define_attr :length, BinStruct::Int16

#typeInteger

16-bit option type

Returns:

  • (Integer)


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

define_attr :type, BinStruct::Int16

Class Method Details

.human_typeString

Get human-readable #type

Returns:

  • (String)

Since:

  • 2.5.0



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

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

.initialize(options = {}) ⇒ Object

Create an Option

Parameters:

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

Since:

  • 2.5.0



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

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

.new(options = {}) ⇒ Option

Create a new Option object (or a subclass)

Parameters:

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

Returns:

Since:

  • 2.5.0



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

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



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

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

.to_humanString

Get a human-readable string for this option

Returns:

  • (String)

Since:

  • 2.5.0



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

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