Class: PacketGen::Header::DHCP::Option
- Inherits:
-
Types::TLV
- Object
- Types::Fields
- Types::TLV
- PacketGen::Header::DHCP::Option
- Defined in:
- lib/packetgen/header/dhcp/option.rb
Overview
DHCP option
A DHCP option is a TLV, so it has:
-
and a Types::TLV#value. Defalt type is Types::String but some options may use more suitable type (by example, a IP::Addr for
routeroption).
Constant Summary collapse
- TYPES =
Option types
Instance Attribute Summary
Attributes inherited from Types::TLV
Instance Method Summary collapse
-
#human_type ⇒ String
Get human-readable type.
- #human_types? ⇒ true
-
#initialize(options = {}) ⇒ Option
constructor
A new instance of Option.
- #private_read ⇒ Object
-
#read(str) ⇒ Option, ...
Populate object from a binary string.
- #to_human ⇒ String
Methods inherited from Types::TLV
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?, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/packetgen/header/dhcp/option.rb', line 95 def initialize(={}) super return unless DHCP_OPTIONS.key?(self.type) h = DHCP_OPTIONS[self.type].last return unless h.is_a? Hash h.each do |k, v| self.length = v if k == :length if k == :v self[:value] = v.new self.value = [:value] if [:value] end end end |
Instance Method Details
#human_type ⇒ String
Get human-readable type
138 139 140 141 142 143 144 |
# File 'lib/packetgen/header/dhcp/option.rb', line 138 def human_type if DHCP_OPTIONS.key?(type) DHCP_OPTIONS[type].first.dup else type.to_s end end |
#human_types? ⇒ true
132 133 134 |
# File 'lib/packetgen/header/dhcp/option.rb', line 132 def human_types? true end |
#private_read ⇒ Object
112 |
# File 'lib/packetgen/header/dhcp/option.rb', line 112 alias private_read read |
#read(str) ⇒ Option, ...
Populate object from a binary string
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/packetgen/header/dhcp/option.rb', line 117 def read(str) read_type = str[0].unpack('C').first if read_type.zero? Pad.new.read(str) elsif read_type == 255 End.new.read(str) elsif DHCP_OPTIONS.key?(read_type) Option.new(DHCP_OPTIONS[read_type][1] || {}).private_read(str) else super end end |
#to_human ⇒ String
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/packetgen/header/dhcp/option.rb', line 147 def to_human s = human_type if length > 0 s << if value.respond_to? :to_human ":#{value.to_human}" elsif self[:value].is_a? Types::Int ":#{self.value.to_i}" else ":#{value.inspect}" end end s end |