TcpOption is the base class for all TCP options. Note that TcpOption#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.

Methods
D
E
H
K
N
O
R
T
V
Included Modules
Classes and Modules
Class Public methods
new(args={})
# File lib/packetfu/protos/tcp.rb, line 228
                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
Instance Public methods
decode()

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

# File lib/packetfu/protos/tcp.rb, line 267
                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)

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.rb, line 291
                def encode(str)
                        self[:value] = self.class.new(:value => str).value
                end
has_optlen?()

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

# File lib/packetfu/protos/tcp.rb, line 296
                def has_optlen?
                        (kind.value && kind.value < 2) ? false : true
                end
has_value?()

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

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

Setter for the “kind” byte of this option.

# File lib/packetfu/protos/tcp.rb, line 273
                def kind=(i); typecast i; end
optlen=(i)

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

# File lib/packetfu/protos/tcp.rb, line 275
                def optlen=(i); typecast i; end
read(str)

Reads a string to populate the object.

# File lib/packetfu/protos/tcp.rb, line 253
                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()

Returns the object in string form.

# File lib/packetfu/protos/tcp.rb, line 246
                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)

Setter for the value of this option.

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