lib/packetgen/header/tcp/option.rb in packetgen-2.6.0 vs lib/packetgen/header/tcp/option.rb in packetgen-2.7.0

- old
+ new

@@ -6,15 +6,13 @@ # frozen_string_literal: true module PacketGen module Header class TCP - # Base class to describe a TCP option # @author Sylvain Daubert class Option < Base - # EOL option value EOL_KIND = 0 # NOP option value NOP_KIND = 1 # MSS option value @@ -51,13 +49,13 @@ def initialize(options={}) super case options[:value] when Integer klass = case self[:length].to_i - when 3; Types::Int8 - when 4; Types::Int16 - when 6; Types::Int32 + 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(options[:value]) when NilClass @@ -75,22 +73,29 @@ return self if str.nil? force_binary str self[:kind].read str[0, 1] if str[1, 1] self[:length].read str[1, 1] - if str[2, 1] && length > 2 - self[:value].read str[2, length - 2] - end + self[:value].read str[2, length - 2] if str[2, 1] && length > 2 end self end + # Say if given option has a length field. # @return [Boolean] - def has_length? + # @since 2.7.0 + def length? self[:kind].value && kind >= 2 end + # @deprecated Use {#length?}. + # @return [Boolean] + def has_length? + Deprecation.deprecated(self.class, __method__, 'length?') + length? + end + # Getter for value attribute # @return [String, Integer] def value case self[:value] when Types::Int @@ -111,18 +116,18 @@ # Get option as a human readable string # @return [String] def to_human str = self.class == Option ? "unk-#{kind}" : self.class.to_s.sub(/.*::/, '') - if length > 2 and self[:value].to_s.size > 0 + if (length > 2) && !self[:value].to_s.empty? str << ":#{self[:value].to_s.inspect}" end str end # @return [String] def inspect - str = "#<#{self.class} kind=#{self[:kind].value.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 end