Class: PacketGen::Header::IP::Option
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::IP::Option
- Defined in:
- lib/packetgen/header/ip/option.rb
Overview
Base class for IP options
Constant Summary collapse
- EOL_TYPE =
EOL option type
0x00- NOP_TYPE =
NOP option type
0x01- LSRR_TYPE =
LSRR option type
0x83- SSRR_TYPE =
SSRR option type
0x84- RR_TYPE =
RR option type
0x07- SI_TYPE =
SI option type
0x88- RA_TYPE =
RA option type
0x94
Instance Attribute Summary collapse
-
#copied ⇒ Boolean
1-bit copied flag from #type field.
-
#data ⇒ Object
option data.
-
#length ⇒ Object
8-bit option length.
-
#option_class ⇒ Integer
2-bit option class (0: control, 2: debug and measurement, 1 and 3: reserved) from #type field.
-
#type ⇒ Object
8-bit option type.
Class Method Summary collapse
-
.build(options = {}) ⇒ Option
Factory to build an option from its type.
- .types ⇒ Hash
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Option
constructor
A new instance of Option.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get binary string.
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, update_field
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/packetgen/header/ip/option.rb', line 99 def initialize(={}) unless [:type] opt_name = self.class.to_s.gsub(/.*::/, '') if Option.const_defined? "#{opt_name}_TYPE" [:type] = Option.const_get("#{opt_name}_TYPE") end end super self.length = sz if respond_to?(:length) && [:length].nil? end |
Instance Attribute Details
#copied ⇒ Boolean
1-bit copied flag from #type field
68 |
# File 'lib/packetgen/header/ip/option.rb', line 68 define_bit_fields_on :type, :copied, :option_class, 2, :number, 5 |
#data ⇒ Object
option data
55 56 |
# File 'lib/packetgen/header/ip/option.rb', line 55 define_field :data, Types::String, optional: ->(h) { h.length > 2 }, builder: ->(h, t) { t.new(length_from: -> { h.length - 2 }) } |
#length ⇒ Object
8-bit option length. If 0, there is no length field in option
51 |
# File 'lib/packetgen/header/ip/option.rb', line 51 define_field :length, Types::Int8, default: 0, optional: ->(h) { h.type > 1 } |
#option_class ⇒ Integer
2-bit option class (0: control, 2: debug and measurement, 1 and 3: reserved) from #type field
68 |
# File 'lib/packetgen/header/ip/option.rb', line 68 define_bit_fields_on :type, :copied, :option_class, 2, :number, 5 |
Class Method Details
.build(options = {}) ⇒ Option
Factory to build an option from its type
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/packetgen/header/ip/option.rb', line 86 def Option.build(={}) type = .delete(:type) klass = case type when String types.key?(type) ? IP.const_get(type) : self when Integer types.value?(type) ? IP.const_get(types.key(type)) : self else self end klass.new() end |
.types ⇒ Hash
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/packetgen/header/ip/option.rb', line 71 def self.types return @types if defined? @types @types = {} Option.constants.each do |cst| next unless cst.to_s.end_with? '_TYPE' optname = cst.to_s.sub(/_TYPE/, '') @types[optname] = Option.const_get(cst) end @types end |
Instance Method Details
#to_human ⇒ String
Get a human readable string
119 120 121 122 123 124 125 |
# File 'lib/packetgen/header/ip/option.rb', line 119 def to_human str = self.class == Option ? +"unk-#{type}" : self.class.to_s.sub(/.*::/, '') if respond_to?(:length) && (length > 2) && !self[:data].to_s.empty? str << ":#{self[:data].to_s.inspect}" end str end |
#to_s ⇒ String
Get binary string. Set #length field.
112 113 114 115 |
# File 'lib/packetgen/header/ip/option.rb', line 112 def to_s self.length = super.size if respond_to? :length super end |