Class: PacketGen::Header::TCP::Option
- Inherits:
-
BinStruct::Struct
- Object
- BinStruct::Struct
- PacketGen::Header::TCP::Option
- Includes:
- BinStruct::Structable
- Defined in:
- lib/packetgen/header/tcp/option.rb
Overview
Base class to describe a TCP option
Constant Summary collapse
- EOL_KIND =
EOL option value
0
- NOP_KIND =
NOP option value
1
- MSS_KIND =
MSS option value
2
- WS_KIND =
WS option value
3
- SACKOK_KIND =
SACKOK option value
4
- SACK_KIND =
SACK option value
5
- ECHO_KIND =
ECHO option value
6
- ECHOREPLY_KIND =
ECHOREPLY option value
7
- TS_KIND =
TS option value
8
Instance Attribute Summary collapse
-
#kind ⇒ Integer
Option kind.
-
#length ⇒ Integer
Option length.
-
#value ⇒ String, Integer
Getter for value attribute.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ String
-
#length? ⇒ Boolean
Say if given option has a length field.
-
#old_set_value ⇒ Integer, String
Option value.
-
#to_human ⇒ String
Get option as a human readable string.
-
#to_s ⇒ String
Get binary string.
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/packetgen/header/tcp/option.rb', line 53 def initialize(={}) super case [:value] when Integer klass = case self[:length].to_i when 3 then BinStruct::Int8 when 4 then BinStruct::Int16 when 6 then BinStruct::Int32 else raise ArgumentError, 'impossible length' end self[:value] = klass.new(value: [:value]) when NilClass # Nothing to do else self[:value] = BinStruct::String.new.read([:value]) self[:length].from_human(self[:value].sz + 2) unless [:length] end end |
Instance Attribute Details
#kind ⇒ Integer
Option kind
39 |
# File 'lib/packetgen/header/tcp/option.rb', line 39 define_attr :kind, BinStruct::Int8 |
#length ⇒ Integer
Option length
43 |
# File 'lib/packetgen/header/tcp/option.rb', line 43 define_attr :length, BinStruct::Int8, optional: lambda(&:length?) |
#value ⇒ String, Integer
Getter for value attribute
46 47 |
# File 'lib/packetgen/header/tcp/option.rb', line 46 define_attr :value, BinStruct::String, optional: ->(h) { h.length? && h.length > 2 }, builder: ->(h, t) { t.new(length_from: -> { h.length - 2 }) } |
Instance Method Details
#inspect ⇒ String
130 131 132 133 134 |
# File 'lib/packetgen/header/tcp/option.rb', line 130 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 |
#length? ⇒ Boolean
Say if given option has a length field.
76 77 78 |
# File 'lib/packetgen/header/tcp/option.rb', line 76 def length? kind >= 2 end |
#old_set_value ⇒ Integer, String
Returns option value.
93 94 |
# File 'lib/packetgen/header/tcp/option.rb', line 93 define_attr :value, BinStruct::String, optional: ->(h) { h.length? && h.length > 2 }, builder: ->(h, t) { t.new(length_from: -> { h.length - 2 }) } |
#to_human ⇒ String
Get option as a human readable string
123 124 125 126 127 |
# File 'lib/packetgen/header/tcp/option.rb', line 123 def to_human str = self.instance_of?(Option) ? "unk-#{kind}" : self.class.to_s.sub(/.*::/, '') str << ":#{self[:value].to_s.inspect}" if (length > 2) && !self[:value].to_s.empty? str end |
#to_s ⇒ String
Get binary string
116 117 118 119 |
# File 'lib/packetgen/header/tcp/option.rb', line 116 def to_s self.length = 2 + self[:value].sz if length? super end |