Class: PacketGen::Header::TCP::Option

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

Overview

Base class to describe a TCP option

Author:

  • Sylvain Daubert

Direct Known Subclasses

ECHO, ECHOREPLY, EOL, MSS, NOP, SACK, SACKOK, TS, WS

Constant Summary collapse

EOL_KIND =

EOL option value

0
NOP_KIND =

NOP option value

1
MSS_KIND =

MSS option value

2
WS_KIND =

WS option value

3
SACKOK_KIND =

SACKOK option value

4
SACK_KIND =

SACK option value

5
ECHO_KIND =

ECHO option value

6
ECHOREPLY_KIND =

ECHOREPLY option value

7
TS_KIND =

TS option value

8

Instance Attribute 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, #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.

Parameters:

  • options (hash) (defaults to: {})

Options Hash (options):

  • :kind (Integer)
  • :length (Integer)
  • :value (Integer, String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/packetgen/header/tcp/option.rb', line 53

def initialize(options={})
  super
  case options[:value]
  when Integer
    klass = case self[:length].to_i
            when 3 then Types::Int8
            when 4 then Types::Int16
            when 6 then Types::Int32
            else
              raise ArgumentError, 'impossible length'
            end
    self[:value] = klass.new(options[:value])
  when NilClass
    # Nothing to do
  else
    self[:value] = Types::String.new.read(options[:value])
    self[:length].read(self[:value].sz + 2) unless options[:length]
  end
end

Instance Attribute Details

#kindInteger

Option kind

Returns:

  • (Integer)

    8-bit option kind



39
# File 'lib/packetgen/header/tcp/option.rb', line 39

define_field :kind, Types::Int8

#lengthInteger

Option length

Returns:

  • (Integer)

    8-bit option length



43
# File 'lib/packetgen/header/tcp/option.rb', line 43

define_field :length, Types::Int8, optional: ->(h) { h.length? }

#valueString, Integer

Getter for value attribute

Returns:

  • (String, Integer)


46
47
# File 'lib/packetgen/header/tcp/option.rb', line 46

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

Instance Method Details

#inspectString

Returns:

  • (String)


124
125
126
127
128
# File 'lib/packetgen/header/tcp/option.rb', line 124

def inspect
  str = +"#<#{self.class} kind=#{self[:kind].value.inspect} "
  str << "length=#{self[:length].value.inspect} " if self[:length].value
  str << "value=#{self[:value].inspect}>"
end

#length?Boolean

Say if given option has a length field.

Returns:

  • (Boolean)

Since:

  • 2.7.0



76
77
78
# File 'lib/packetgen/header/tcp/option.rb', line 76

def length?
  kind >= 2
end

#old_set_valueInteger, String

Returns option value.

Returns:

  • (Integer, String)

    option value



93
94
# File 'lib/packetgen/header/tcp/option.rb', line 93

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

#to_humanString

Get option as a human readable string

Returns:

  • (String)


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

def to_human
  str = self.instance_of?(Option) ? +"unk-#{kind}" : self.class.to_s.sub(/.*::/, '')
  str << ":#{self[:value].to_s.inspect}" if (length > 2) && !self[:value].to_s.empty?
  str
end

#to_sString

Get binary string

Returns:

  • (String)


110
111
112
113
# File 'lib/packetgen/header/tcp/option.rb', line 110

def to_s
  self.length = 2 + self[:value].sz if length?
  super
end