Class: PacketGen::Header::TCP::Option
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::TCP::Option
- Includes:
- Types::Fieldable
- 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 included from Types::Fieldable
#format_inspect, #read, #sz, #type_name
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.
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 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
39 |
# File 'lib/packetgen/header/tcp/option.rb', line 39 define_field :kind, Types::Int8 |
Instance Method Details
#inspect ⇒ String
124 125 126 127 128 |
# File 'lib/packetgen/header/tcp/option.rb', line 124 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_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
117 118 119 120 121 |
# File 'lib/packetgen/header/tcp/option.rb', line 117 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
110 111 112 113 |
# File 'lib/packetgen/header/tcp/option.rb', line 110 def to_s self.length = 2 + self[:value].sz if length? super end |