Class: PacketGen::Header::TCP::Options
- Inherits:
-
Array
- Object
- Array
- PacketGen::Header::TCP::Options
- Defined in:
- lib/packetgen/header/tcp/options.rb
Overview
Container for TCP options in TCP header.
Class Method Summary collapse
-
.option_classes ⇒ Array<Class>
Get Option subclasses.
Instance Method Summary collapse
-
#add(opt, value = nil) ⇒ self
Add a well-known option.
-
#read(str) ⇒ self
Read TCP header options from a string.
-
#sz ⇒ Integer
Get options size in bytes.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get options binary string.
Class Method Details
.option_classes ⇒ Array<Class>
Get PacketGen::Header::TCP::Option subclasses
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/packetgen/header/tcp/options.rb', line 11 def self.option_classes return @klasses if defined? @klasses @klasses = [] Option.constants.each do |cst| next unless cst.to_s.end_with? '_KIND' optname = cst.to_s.sub(/_KIND/, '') @klasses[Option.const_get(cst)] = TCP.const_get(optname) end @klasses end |
Instance Method Details
#add(opt, value = nil) ⇒ self
Add a well-known option
55 56 57 58 59 60 61 62 |
# File 'lib/packetgen/header/tcp/options.rb', line 55 def add(opt, value=nil) raise ArgumentError, "unknown option #{opt}" unless TCP.const_defined?(opt) klass = TCP.const_get(opt) raise ArgumentError "unknown option #{opt}" unless klass < Option option = klass.new(value: value) self << option self end |
#read(str) ⇒ self
Read TCP header options from a string
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/packetgen/header/tcp/options.rb', line 25 def read(str) clear return self if str.nil? PacketGen.force_binary str i = 0 klasses = self.class.option_classes while i < str.to_s.length kind = str[i, 1].unpack('C').first this_option = if klasses[kind].nil? Option.new else klasses[kind].new end this_option.read str[i, str.size] unless this_option.has_length? this_option.length = nil this_option.value = nil end self << this_option i += this_option.sz end self end |
#sz ⇒ Integer
Get options size in bytes
78 79 80 |
# File 'lib/packetgen/header/tcp/options.rb', line 78 def sz to_s.length end |
#to_human ⇒ String
Get a human readable string
72 73 74 |
# File 'lib/packetgen/header/tcp/options.rb', line 72 def to_human map(&:to_human).join(', ') end |
#to_s ⇒ String
Get options binary string
66 67 68 |
# File 'lib/packetgen/header/tcp/options.rb', line 66 def to_s map(&:to_s).join end |