Class: PacketGen::Header::TCP::Option
- Includes:
- StructFu
- Defined in:
- lib/packetgen/header/tcp/option.rb
Overview
Base class to describe a TCP option
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
-
#kind ⇒ Integer
Getter for kind attribute.
-
#length ⇒ Integer
Getter for length attribute.
-
#value ⇒ String, Integer
Getter for value attribute.
Instance Method Summary collapse
-
#has_length? ⇒ Boolean
Say if option has a length.
-
#initialize(options = {}) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ String
-
#read(str) ⇒ self
Read a TCP option from a string.
-
#to_human ⇒ String
Get option as a human readable string.
-
#to_s ⇒ String
Get binary string.
Methods included from StructFu
#body=, #clone, #set_endianness, #sz, #typecast
Methods inherited from Struct
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
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(={}) super Int8.new([:kind]), Int8.new([:length]) case [: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([:value]) when NilClass self[:value] = StructFu::String.new else self[:value] = StructFu::String.new.read([:value]) self[:length].read(self[:value].sz + 2) unless [:length] end end |
Instance Attribute Details
#kind ⇒ Integer
Getter for kind attribute
7 8 9 |
# File 'lib/packetgen/header/tcp/option.rb', line 7 def kind @kind end |
#length ⇒ Integer
Getter for length attribute
7 8 9 |
# File 'lib/packetgen/header/tcp/option.rb', line 7 def length @length end |
#value ⇒ String, Integer
Getter for value attribute
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
90 91 92 |
# File 'lib/packetgen/header/tcp/option.rb', line 90 def has_length? self[:kind].value && kind >= 2 end |
#inspect ⇒ String
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
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_human ⇒ String
Get option as a human readable string
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_s ⇒ String
Get binary string
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 |