### Home Classes Methods

### In Files

- lib/packetfu/protos/tcp/option.rb

### Parent

Struct.new(:kind, :optlen, :value)

### Namespace

- CLASS PacketFu::TcpOption::ECHO
- CLASS PacketFu::TcpOption::ECHOREPLY
- CLASS PacketFu::TcpOption::EOL
- CLASS PacketFu::TcpOption::MSS
- CLASS PacketFu::TcpOption::NOP
- CLASS PacketFu::TcpOption::SACK
- CLASS PacketFu::TcpOption::SACKOK
- CLASS PacketFu::TcpOption::TS
- CLASS PacketFu::TcpOption::WS

### Methods

- ::new
- #decode
- #encode
- #has_optlen?
- #has_value?
- #kind=
- #optlen=
- #read
- #to_s
- #value=

### Included Modules

- StructFu

### Files

- .document
- README.rdoc

### Class/Module Index

Quicksearch

- PacketFu
- PacketFu::ARPHeader
- PacketFu::ARPHeaderMixin
- PacketFu::ARPPacket
- PacketFu::AddrIpv6
- PacketFu::Capture
- PacketFu::Config
- PacketFu::EthHeader
- PacketFu::EthHeaderMixin
- PacketFu::EthMac
- PacketFu::EthNic
- PacketFu::EthOui
- PacketFu::EthPacket
- PacketFu::HSRPHeader
- PacketFu::HSRPHeaderMixin
- PacketFu::HSRPPacket
- PacketFu::ICMPHeader
- PacketFu::ICMPHeaderMixin
- PacketFu::ICMPPacket
- PacketFu::IPHeader
- PacketFu::IPHeaderMixin
- PacketFu::IPPacket
- PacketFu::IPv6Header
- PacketFu::IPv6HeaderMixin
- PacketFu::IPv6Packet
- PacketFu::Inject
- PacketFu::InvalidHeader
- PacketFu::InvalidPacket
- PacketFu::LLDPHeader
- PacketFu::LLDPHeaderMixin
- PacketFu::LLDPPacket
- PacketFu::Octets
- PacketFu::Packet
- PacketFu::PcapFile
- PacketFu::PcapHeader
- PacketFu::PcapPacket
- PacketFu::PcapPackets
- PacketFu::Read
- PacketFu::TCPHeader
- PacketFu::TCPHeaderMixin
- PacketFu::TCPPacket
- PacketFu::TcpEcn
- PacketFu::TcpFlags
- PacketFu::TcpHlen
- PacketFu::TcpOption
- PacketFu::TcpOption::ECHO
- PacketFu::TcpOption::ECHOREPLY
- PacketFu::TcpOption::EOL
- PacketFu::TcpOption::MSS
- PacketFu::TcpOption::NOP
- PacketFu::TcpOption::SACK
- PacketFu::TcpOption::SACKOK
- PacketFu::TcpOption::TS
- PacketFu::TcpOption::WS
- PacketFu::TcpOptions
- PacketFu::TcpReserved
- PacketFu::Timestamp
- PacketFu::UDPHeader
- PacketFu::UDPHeaderMixin
- PacketFu::UDPPacket
- PacketFu::Utils
- PacketFu::Write
- StructFu
- StructFu::Int
- StructFu::Int16
- StructFu::Int16be
- StructFu::Int16le
- StructFu::Int32
- StructFu::Int32be
- StructFu::Int32le
- StructFu::Int8
- StructFu::IntString
- StructFu::String
- Struct

No matching classes.

# PacketFu::TcpOption

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.

### Public Class Methods

new(args={}) click to toggle source

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.