lib/packetgen/header/tcp/option.rb in packetgen-1.2.0 vs lib/packetgen/header/tcp/option.rb in packetgen-1.3.0

- old
+ new

@@ -2,45 +2,63 @@ module Header class TCP # Base class to describe a TCP option # @author Sylvain Daubert - class Option < Struct.new(:kind, :length, :value) - include StructFu + class Option < Base + # EOL option value EOL_KIND = 0 + # NOP option value NOP_KIND = 1 + # MSS option value MSS_KIND = 2 + # WS option value WS_KIND = 3 + # SACKOK option value SACKOK_KIND = 4 + # SACK option value SACK_KIND = 5 + # ECHO option value ECHO_KIND = 6 + # ECHOREPLY option value ECHOREPLY_KIND = 7 + # TS option value TS_KIND = 8 + # @!attribute kind + # Option kind + # @return [Integer] 8-bit option kind + define_field :kind, Types::Int8 + # @!attribute length + # Option length + # @return [Integer] 8-bit option length + define_field :length, Types::Int8 + # @!attribute value + # @return [Integer,String] option value + define_field :value, Types::String + # @param [hash] options # @option options [Integer] :kind # @option options [Integer] :length # @option options [Integer,String] :value def initialize(options={}) - super Int8.new(options[:kind]), - Int8.new(options[:length]) - + super case options[:value] when Integer klass = case self[:length].to_i - when 3; Int8 - when 4; Int16 - when 6; Int32 + when 3; Types::Int8 + when 4; Types::Int16 + when 6; Types::Int32 else raise ArgumentError, 'impossible length' end self[:value] = klass.new(options[:value]) when NilClass - self[:value] = StructFu::String.new + self[:value] = Types::String.new else - self[:value] = StructFu::String.new.read(options[:value]) + self[:value] = Types::String.new.read(options[:value]) self[:length].read(self[:value].sz + 2) unless options[:length] end end # Read a TCP option from a string @@ -57,60 +75,26 @@ end end self end - # Getter for kind attribute - # @return [Integer] - def kind - self[:kind].to_i - end - - # Setter for kind attribute - # @param [Integer] i - # @return [Integer] - def kind=(i) - self[:kind].read i - end - - # Getter for length attribute - # @return [Integer] - def length - self[:length].to_i - end - - # Setter for length attribute - # @param [Integer] i - # @return [Integer] - def length=(i) - self[:length].read i - end - - # Say if option has a length # @return [Boolean] def has_length? self[:kind].value && kind >= 2 end # Getter for value attribute # @return [String, Integer] def value case self[:value] - when StructFu::Int + when Types::Int self[:value].to_i else self[:value].to_s end end - # Setter for value attribute - # @param [String, Integer] v - # @return [String, Integer] - def value=(v) - self[:value].read v - end - # Get binary string # @return [String] def to_s str = self[:kind].to_s str << self[:length].to_s unless self[:length].value.nil? @@ -158,11 +142,11 @@ # @author Sylvain Daubert class MSS < Option # @see Option#initialize def initialize(options={}) super options.merge!(kind: MSS_KIND, length: 4) - self[:value] = Int16.new(options[:value]) + self[:value] = Types::Int16.new(options[:value]) end # @return [String] def to_human "MSS:#{value}" @@ -173,11 +157,11 @@ # @author Sylvain Daubert class WS < Option # @see Option#initialize def initialize(options={}) super options.merge!(kind: WS_KIND, length: 3) - self[:value] = Int8.new(options[:value]) + self[:value] = Types::Int8.new(options[:value]) end # @return [String] def to_human "WS:#{value}" @@ -207,11 +191,11 @@ # @author Sylvain Daubert class ECHO < Option # @see Option#initialize def initialize(options={}) super options.merge!(kind: ECHO_KIND, length: 6) - self[:value] = Int32.new(options[:value]) + self[:value] = Types::Int32.new(options[:value]) end # @return [String] def to_human "WS:#{value}" @@ -222,11 +206,11 @@ # @author Sylvain Daubert class ECHOREPLY < Option # @see Option#initialize def initialize(options={}) super options.merge!(kind: ECHOREPLY_KIND, length: 6) - self[:value] = Int32.new(options[:value]) + self[:value] = Types::Int32.new(options[:value]) end # @return [String] def to_human "WS:#{value}" @@ -237,10 +221,10 @@ # @author Sylvain Daubert class TS < Option # @see Option#initialize def initialize(options={}) super options.merge!(kind: TS_KIND, length: 10) - self[:value] = StructFu::String.new.read(options[:value] || "\0" * 8) + self[:value] = Types::String.new.read(options[:value] || "\0" * 8) end # @return [String] def to_human value, echo_reply = self[:value].unpack('NN')