Class: PacketGen::Header::DHCPv6::Option
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::DHCPv6::Option
- 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.
-
#read(str) ⇒ Option
Populate object from binary string.
-
#to_human ⇒ String
Get a human-readable string for this option.
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
74 75 76 77 78 |
# File 'lib/packetgen/header/dhcpv6/option.rb', line 74 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.
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]) } |
#length ⇒ Integer
16-bit option length
27 |
# File 'lib/packetgen/header/dhcpv6/option.rb', line 27 define_field :length, Types::Int16 |
#type ⇒ Integer
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)
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(={}) if self == Option case [:type] when Integer klass = Option.subclasses[[:type]] klass.new() if klass when String if DHCPv6.const_defined?([:type]) klass = DHCPv6.const_get([:type]) .delete :type klass.new() if klass < Option end else super end else super end end |
.subclasses ⇒ Hash
Get Option subclasses
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_type ⇒ String
Get human-readable #type
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
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_human ⇒ String
Get a human-readable string for this option
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 |