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

Inherits:
Types::Fields show all
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 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

Create an Option

Since:

  • 2.5.0



74
75
76
77
78
# File 'lib/packetgen/header/dhcpv6/option.rb', line 74

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.



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

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

#lengthInteger

16-bit option length



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

define_field :length, Types::Int16

#typeInteger

16-bit option type



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

define_field :type, Types::Int16

Class Method Details

.new(options = {}) ⇒ Option

Create a new Option object (or a subclass)

Since:

  • 2.5.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/packetgen/header/dhcpv6/option.rb', line 51

def new(options={})
  if self == Option
    case options[:type]
    when Integer
      klass = Option.subclasses[options[:type]]
      klass.new(options) if klass
    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
  else
    super
  end
end

.subclassesHash

Get Option subclasses

Since:

  • 2.5.0



37
38
39
40
41
42
43
44
45
46
# File 'lib/packetgen/header/dhcpv6/option.rb', line 37

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

Since:

  • 2.5.0



104
105
106
107
108
109
110
# File 'lib/packetgen/header/dhcpv6/option.rb', line 104

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

#read(str) ⇒ Option

Populate object from binary string

Since:

  • 2.5.0



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/packetgen/header/dhcpv6/option.rb', line 86

def read(str)
  if self.class == Option
    return self if str.nil?
    PacketGen.force_binary str
    type = Types::Int16.new.read(str).to_i
    klass = Option.subclasses[type]
    if klass
      klass.new.read(str)
    else
      private_read str
    end
  else
    private_read str
  end
end

#to_humanString

Get a human-readable string for this option

Since:

  • 2.5.0



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/packetgen/header/dhcpv6/option.rb', line 114

def to_human
  str = "#{human_type}:".dup
  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