Class: PacketGen::Header::IP::Option
- Inherits:
-
BinStruct::Struct
- Object
- BinStruct::Struct
- PacketGen::Header::IP::Option
- Includes:
- BinStruct::Structable
- 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 ⇒ Integer
1-bit copied flag from #type field.
-
#data ⇒ String
option data.
-
#length ⇒ Integer
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 ⇒ Integer
8-bit option type.
Class Method Summary collapse
-
.build(options = {}) ⇒ Option
Factory to build an option from its type.
-
.types ⇒ Hash{String => Integer}
Return a cached hash associating type name to its value.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Option
constructor
Force type value for subclasses, if not specified.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get binary string.
Constructor Details
#initialize(options = {}) ⇒ Option
Force type value for subclasses, if not specified
108 109 110 111 112 113 114 |
# File 'lib/packetgen/header/ip/option.rb', line 108 def initialize(={}) [:type] = class2type unless [:type] super initialize_length_if_needed() initialize_data_if_needed() end |
Instance Attribute Details
#copied ⇒ Integer
1-bit copied flag from #type field
64 |
# File 'lib/packetgen/header/ip/option.rb', line 64 define_bit_attr :type, copied: 1, option_class: 2, number: 5 |
#data ⇒ String
option data
72 73 |
# File 'lib/packetgen/header/ip/option.rb', line 72 define_attr :data, BinStruct::String, optional: ->(h) { h.length > 2 }, builder: ->(h, t) { t.new(length_from: -> { h.length - 2 }) } |
#length ⇒ Integer
8-bit option length. If 0, there is no length
field in option
68 |
# File 'lib/packetgen/header/ip/option.rb', line 68 define_attr :length, BinStruct::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
64 |
# File 'lib/packetgen/header/ip/option.rb', line 64 define_bit_attr :type, copied: 1, option_class: 2, number: 5 |
#type ⇒ Integer
8-bit option type
64 |
# File 'lib/packetgen/header/ip/option.rb', line 64 define_bit_attr :type, copied: 1, option_class: 2, number: 5 |
Class Method Details
.build(options = {}) ⇒ Option
Factory to build an option from its type
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/packetgen/header/ip/option.rb', line 94 def self.build(={}) type = [:type] klass = case type when String types.key?(type) ? IP.const_get(type) : self else types.value?(type) ? IP.const_get(types.key(type.to_i)) : self end .delete(:type) if klass != self klass.new() end |
.types ⇒ Hash{String => Integer}
Return a cached hash associating type name to its value.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/packetgen/header/ip/option.rb', line 78 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
125 126 127 128 129 |
# File 'lib/packetgen/header/ip/option.rb', line 125 def to_human str = self.instance_of?(Option) ? "unk-#{type}" : self.class.to_s.sub(/.*::/, '') str << ":#{self[:data].to_s.inspect}" if respond_to?(:length) && (length > 2) && !self[:data].to_s.empty? str end |
#to_s ⇒ String
Get binary string. Set #length field.
118 119 120 121 |
# File 'lib/packetgen/header/ip/option.rb', line 118 def to_s self.length = super.size if respond_to?(:length) super end |