Class: PacketGen::Header::TCP::Option
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- 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.
Attributes inherited from Base
Instance Method Summary collapse
-
#has_length? ⇒ Boolean
deprecated
Deprecated.
Use #length?.
-
#initialize(options = {}) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ String
-
#length? ⇒ Boolean
Say if given option has a length field.
-
#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 inherited from Base
#added_to_packet, bind, bind_header, calculate_and_set_length, #header_id, inherited, #ip_header, known_headers, #ll_header, #method_name, #parse?, #protocol_name, protocol_name
Methods inherited from Types::Fields
#[], #[]=, #bits_on, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, delete_field, fields, #fields, #force_binary, inherited, #is_optional?, #is_present?, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, update_field
Constructor Details
#initialize(options = {}) ⇒ Option
Returns a new instance of Option.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/packetgen/header/tcp/option.rb', line 49 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 self[:value] = Types::String.new 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 |
#length ⇒ Integer
Option length
40 |
# File 'lib/packetgen/header/tcp/option.rb', line 40 define_field :length, Types::Int8 |
#value ⇒ String, Integer
Getter for value attribute
43 |
# File 'lib/packetgen/header/tcp/option.rb', line 43 define_field :value, Types::String |
Instance Method Details
#has_length? ⇒ Boolean
Use #length?.
92 93 94 95 |
# File 'lib/packetgen/header/tcp/option.rb', line 92 def has_length? Deprecation.deprecated(self.class, __method__, 'length?') length? end |
#inspect ⇒ String
128 129 130 131 132 |
# File 'lib/packetgen/header/tcp/option.rb', line 128 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.
86 87 88 |
# File 'lib/packetgen/header/tcp/option.rb', line 86 def length? self[:kind].value && kind >= 2 end |
#read(str) ⇒ self
Read a TCP option from a string
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/packetgen/header/tcp/option.rb', line 72 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] self[:value].read str[2, length - 2] if str[2, 1] && length > 2 end self end |
#to_human ⇒ String
Get option as a human readable string
119 120 121 122 123 124 125 |
# File 'lib/packetgen/header/tcp/option.rb', line 119 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
110 111 112 113 114 115 |
# File 'lib/packetgen/header/tcp/option.rb', line 110 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 |