Class: PacketGen::Header::IP::Option

Inherits:
Types::Fields show all
Includes:
Types::Fieldable
Defined in:
lib/packetgen/header/ip/option.rb

Overview

Base class for IP options

Author:

  • Sylvain Daubert

Direct Known Subclasses

EOL, LSRR, RA, SI

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::Fieldable

#format_inspect, #read, #sz, #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, update_field

Constructor Details

#initialize(options = {}) ⇒ Option

Returns a new instance of Option.



101
102
103
104
105
106
107
# File 'lib/packetgen/header/ip/option.rb', line 101

def initialize(options={})
  options[:type] = class2type unless options[:type]

  super
  initialize_length_if_needed(options)
  initialize_data_if_needed(options)
end

Instance Attribute Details

#copiedBoolean

1-bit copied flag from #type field

Returns:

  • (Boolean)


71
# File 'lib/packetgen/header/ip/option.rb', line 71

define_bit_fields_on :type, :copied, :option_class, 2, :number, 5

#dataObject

option data



58
59
# File 'lib/packetgen/header/ip/option.rb', line 58

define_field :data, Types::String, optional: ->(h) { h.length > 2 },
builder: ->(h, t) { t.new(length_from: -> { h.length - 2 }) }

#lengthObject

8-bit option length. If 0, there is no length field in option



54
# File 'lib/packetgen/header/ip/option.rb', line 54

define_field :length, Types::Int8, default: 0, optional: ->(h) { h.type > 1 }

#option_classInteger

2-bit option class (0: control, 2: debug and measurement, 1 and 3: reserved) from #type field

Returns:

  • (Integer)


71
# File 'lib/packetgen/header/ip/option.rb', line 71

define_bit_fields_on :type, :copied, :option_class, 2, :number, 5

#typeObject

8-bit option type



50
# File 'lib/packetgen/header/ip/option.rb', line 50

define_field :type, Types::Int8

Class Method Details

.build(options = {}) ⇒ Option

Factory to build an option from its type

Returns:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/packetgen/header/ip/option.rb', line 89

def self.build(options={})
  type = options[: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
  options.delete(:type) if klass != self
  klass.new(options)
end

.typesHash

Returns:

  • (Hash)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/packetgen/header/ip/option.rb', line 74

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_humanString

Get a human readable string

Returns:

  • (String)


118
119
120
121
122
# File 'lib/packetgen/header/ip/option.rb', line 118

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_sString

Get binary string. Set #length field.

Returns:

  • (String)


111
112
113
114
# File 'lib/packetgen/header/ip/option.rb', line 111

def to_s
  self.length = super.size if respond_to? :length
  super
end