Class: PacketGen::Header::TCP::Option
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::TCP::Option
- 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.
Methods inherited from Types::Fields
#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, update_field
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/packetgen/header/tcp/option.rb', line 50 def initialize(={}) super case [:value] when Integer klass = case self[:length].to_i when 3 then Types::Int8 when 4 then Types::Int16 when 6 then Types::Int32 else raise ArgumentError, 'impossible length' end self[:value] = klass.new([:value]) when NilClass # Nothing to do else self[:value] = Types::String.new.read([:value]) self[:length].read(self[:value].sz + 2) unless [:length] end end |
Instance Attribute Details
#kind ⇒ Integer
Option kind
36 |
# File 'lib/packetgen/header/tcp/option.rb', line 36 define_field :kind, Types::Int8 |
Instance Method Details
#inspect ⇒ String
123 124 125 126 127 |
# File 'lib/packetgen/header/tcp/option.rb', line 123 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.
73 74 75 |
# File 'lib/packetgen/header/tcp/option.rb', line 73 def length? kind >= 2 end |
#old_set_value ⇒ Integer, String
Returns option value.
90 91 |
# File 'lib/packetgen/header/tcp/option.rb', line 90 define_field :value, Types::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
114 115 116 117 118 119 120 |
# File 'lib/packetgen/header/tcp/option.rb', line 114 def to_human str = self.class == Option ? +"unk-#{kind}" : self.class.to_s.sub(/.*::/, '') if (length > 2) && !self[:value].to_s.empty? str << ":#{self[:value].to_s.inspect}" end str end |
#to_s ⇒ String
Get binary string
107 108 109 110 |
# File 'lib/packetgen/header/tcp/option.rb', line 107 def to_s self.length = 2 + self[:value].sz if length? super end |