class PacketFu::TcpOption

TcpOption is the base class for all TCP options. Note that StructFu#len returns the size of the entire option, while TcpOption#optlen is the struct for the TCP Option Length field.

Subclassed options should set the correct TcpOption#kind by redefining initialize. They should also deal with various value types there by setting them explicitly with an accompanying StructFu#typecast for the setter.

By default, values are presumed to be strings, unless they are Numeric, in which case a guess is made to the width of the Numeric based on the given optlen.

Note that normally, optlen is /not/ enforced for directly setting values, so the user is perfectly capable of setting incorrect lengths.

Public Class Methods

new(args={}) click to toggle source
Calls superclass method
# File lib/packetfu/protos/tcp/option.rb, line 20
def initialize(args={})
  super(
    Int8.new(args[:kind]),
    Int8.new(args[:optlen])
  )
  if args[:value].kind_of? Numeric
    self[:value] = case args[:optlen]
                   when 3; Int8.new(args[:value])
                   when 4; Int16.new(args[:value])
                   when 6; Int32.new(args[:value])
                   else; StructFu::String.new.read(args[:value])
                   end
  else
    self[:value] = StructFu::String.new.read(args[:value])
  end
end

Public Instance Methods

decode() click to toggle source

The default decode for an unknown option. Known options should redefine this.

# File lib/packetfu/protos/tcp/option.rb, line 59
def decode
  unk = "unk-#{self.kind.to_i}"
  (self[:optlen].to_i > 2 && self[:value].to_s.size > 1) ? [unk,self[:value]].join(":") : unk
end
encode(str) click to toggle source

Generally, encoding a value is going to be just a read. Some options will treat things a little differently; TS for example, takes two values and concatenates them.

# File lib/packetfu/protos/tcp/option.rb, line 83
def encode(str)
  self[:value] = self.class.new(:value => str).value
end
has_optlen?() click to toggle source

Returns true if this option has an optlen. Some don't.

# File lib/packetfu/protos/tcp/option.rb, line 88
def has_optlen?
  (kind.value && kind.value < 2) ? false : true
end
has_value?() click to toggle source

Returns true if this option has a value. Some don't.

# File lib/packetfu/protos/tcp/option.rb, line 93
def has_value?
  (value.respond_to? :to_s && value.to_s.size > 0) ? false : true
end
kind=(i) click to toggle source

Setter for the “kind” byte of this option.

# File lib/packetfu/protos/tcp/option.rb, line 65
def kind=(i); typecast i; end
optlen=(i) click to toggle source

Setter for the “option length” byte for this option.

# File lib/packetfu/protos/tcp/option.rb, line 67
def optlen=(i); typecast i; end
read(str) click to toggle source

Reads a string to populate the object.

# File lib/packetfu/protos/tcp/option.rb, line 45
def read(str)
  force_binary(str)
  return self if str.nil?
  self[:kind].read(str[0,1])
  if str[1,1]
    self[:optlen].read(str[1,1])
    if str[2,1] && optlen.value > 2
      self[:value].read(str[2,optlen.value-2])
    end
  end
  self
end
to_s() click to toggle source

Returns the object in string form.

# File lib/packetfu/protos/tcp/option.rb, line 38
def to_s
  self[:kind].to_s + 
  (self[:optlen].value.nil? ? nil : self[:optlen]).to_s +
  (self[:value].nil? ? nil : self[:value]).to_s
end
value=(i) click to toggle source

Setter for the value of this option.

# File lib/packetfu/protos/tcp/option.rb, line 70
def value=(i)
  if i.kind_of? Numeric
    typecast i
  elsif i.respond_to? :to_s
    self[:value] = i
  else
    self[:value] = ''
  end
end