Class: PacketGen::Header::DHCPv6::Option
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::DHCPv6::Option
- Includes:
- Types::Fieldable
- Defined in:
- lib/packetgen/header/dhcpv6/option.rb
Overview
A DHCPv6 consists of:
-
a #type (Types::Int16),
-
a #length (Types::Int16),
-
and a #data (Types::String).
Subclasses handles known options. These subclasses may remove #data field to replace it by specific option field(s).
Direct Known Subclasses
ClientID, ElapsedTime, IAAddr, IANA, IATA, ORO, Preference, RapidCommit, RelayMessage, ServerUnicast
Instance Attribute Summary collapse
-
#data ⇒ String
variable length option data.
-
#length ⇒ Integer
16-bit option length.
-
#type ⇒ Integer
16-bit option type.
Class Method Summary collapse
-
.new(options = {}) ⇒ Option
Create a new Option object (or a subclass).
-
.subclasses ⇒ Hash
Get Option subclasses.
Instance Method Summary collapse
-
#human_type ⇒ String
Get human-readable #type.
-
#initialize(options = {}) ⇒ Option
constructor
Create an Option.
-
#to_human ⇒ String
Get a human-readable string for this option.
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
77 78 79 80 81 |
# File 'lib/packetgen/header/dhcpv6/option.rb', line 77 def initialize(={}) [:length] = [:data].to_s.size if [:data] super self.length = self.sz - 4 if [:data].nil? end |
Instance Attribute Details
#data ⇒ String
variable length option data.
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]) } |
Class Method Details
.new(options = {}) ⇒ Option
Create a new Option object (or a subclass)
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(={}) return super unless self == Option case [:type] when Integer klass = Option.subclasses[[:type]] klass&.new() when String if DHCPv6.const_defined?([:type]) klass = DHCPv6.const_get([:type]) .delete :type klass.new() if klass < Option end else super end end |
.subclasses ⇒ Hash
Get Option subclasses
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_type ⇒ String
Get human-readable #type
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_human ⇒ String
Get a human-readable string for this option
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 |