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

Inherits:
Struct
  • Object
show all
Includes:
StructFu
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 =
0
NOP_KIND =
1
MSS_KIND =
2
WS_KIND =
3
SACKOK_KIND =
4
SACK_KIND =
5
ECHO_KIND =
6
ECHOREPLY_KIND =
7
TS_KIND =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

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)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/packetgen/header/tcp/option.rb', line 24

def initialize(options={})
  super Int8.new(options[:kind]),
        Int8.new(options[:length])

  case options[:value]
  when Integer
    klass = case self[:length].to_i
            when 3; Int8
            when 4; Int16
            when 6; Int32
            else
              raise ArgumentError, 'impossible length'
            end
    self[:value] = klass.new(options[:value])
  when NilClass
    self[:value] = StructFu::String.new
  else
    self[:value] = StructFu::String.new.read(options[:value])
    self[:length].read(self[:value].sz + 2) unless options[:length]
  end
end

Instance Attribute Details

#kindInteger

Getter for kind attribute

Returns:

  • (Integer)


7
8
9
# File 'lib/packetgen/header/tcp/option.rb', line 7

def kind
  @kind
end

#lengthInteger

Getter for length attribute

Returns:

  • (Integer)


7
8
9
# File 'lib/packetgen/header/tcp/option.rb', line 7

def length
  @length
end

#valueString, Integer

Getter for value attribute

Returns:



7
8
9
# File 'lib/packetgen/header/tcp/option.rb', line 7

def value
  @value
end

Instance Method Details

#has_length?Boolean

Say if option has a length

Returns:

  • (Boolean)


90
91
92
# File 'lib/packetgen/header/tcp/option.rb', line 90

def has_length?
  self[:kind].value && kind >= 2
end

#inspectString

Returns:



132
133
134
135
136
# File 'lib/packetgen/header/tcp/option.rb', line 132

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

#read(str) ⇒ self

Read a TCP option from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/packetgen/header/tcp/option.rb', line 49

def read(str)
  return self if str.nil?
  force_binary str
  self[:kind].read str[0, 1]
  if str[1, 1]
    self[:length].read str[1, 1]
    if str[2, 1] && length > 2
      self[:value].read str[2, length - 2]
    end
  end
  self
end

#to_humanString

Get option as a human readable string

Returns:



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

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

#to_sString

Get binary string

Returns:



114
115
116
117
118
119
# File 'lib/packetgen/header/tcp/option.rb', line 114

def to_s
  str = self[:kind].to_s
  str << self[:length].to_s unless self[:length].value.nil?
  str << self[:value].to_s if length > 2
  str
end