Class: PacketGen::Header::TCP::Options

Inherits:
Types::Array show all
Defined in:
lib/packetgen/header/tcp/options.rb

Overview

Container for TCP options in TCP header.

Author:

  • Sylvain Daubert

Constant Summary

Constants inherited from Types::Array

Types::Array::HUMAN_SEPARATOR

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Types::Array

#<<, #==, #[], #clear, #clear!, #delete, #delete_at, #each, #empty?, #first, #force_binary, #initialize, #initialize_copy, #last, #push, set_of, #size, #sz, #to_a, #to_human, #to_s

Constructor Details

This class inherits a constructor from PacketGen::Types::Array

Class Method Details

.option_classesArray<Class>

Returns:

  • (Array<Class>)


16
17
18
19
20
21
22
23
24
25
# File 'lib/packetgen/header/tcp/options.rb', line 16

def self.option_classes
  return @klasses if defined? @klasses
  @klasses = []
  Option.constants.each do |cst|
    next unless cst.to_s.end_with? '_KIND'
    optname = cst.to_s.sub(/_KIND/, '')
    @klasses[Option.const_get(cst)] = TCP.const_get(optname)
  end
  @klasses
end

Instance Method Details

#add(opt, value = nil) ⇒ self

Add a well-known option

Parameters:

  • opt (String)

    option name

  • value (Object) (defaults to: nil)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    unknown option



61
62
63
64
65
66
# File 'lib/packetgen/header/tcp/options.rb', line 61

def add(opt, value=nil)
  Deprecation.deprecated(self.class, __method__, 'push')
  option = record_from_hash(opt: opt, value: value)
  self << option
  self
end

#read(str) ⇒ self

Read TCP header options from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/packetgen/header/tcp/options.rb', line 30

def read(str)
  clear
  return self if str.nil?
  PacketGen.force_binary str

  i = 0
  klasses = self.class.option_classes
  while i < str.to_s.length
    kind = str[i, 1].unpack('C').first
    this_option = if klasses[kind].nil?
                    Option.new
                  else
                    klasses[kind].new
                  end
    this_option.read str[i, str.size]
    unless this_option.length?
      this_option.length = nil
      this_option.value = nil
    end
    self << this_option
    i += this_option.sz
  end
  self
end