Sha256: 947fb70b3baaab28746304b4f5fb41c3dc343be76eecc5a8b7f5f8a6b107ffdb

Contents?: true

Size: 1.47 KB

Versions: 119

Compression:

Stored size: 1.47 KB

Contents

# A single IPv4 address, in standard, dotted decimal notation.
#
# @example Dotted Decimal Notation
#   '1.2.3.4'
#
class MetasploitDataModels::IPAddress::V4::Single < MetasploitDataModels::IPAddress::V4::Segmented
  #
  # Segments
  #

  segment class_name: 'MetasploitDataModels::IPAddress::V4::Segment::Single'

  #
  # Instance Methods
  #

  # Adds `other` IPv4 address to this IPv4 address.
  #
  # @return [MetasploitDataModels::IPAddress::V4::Single] a new IPv4 address contain the sum of the two addresses
  #   segments with carries from lower to higher segments.
  # @raise [TypeError] if `other` isn't the same class.
  # @raise [ArgmentError] if `self` plus `other` yields an IP address greater than 255.255.255.255.
  # @see succ
  def +(other)
    unless other.is_a? self.class
      raise TypeError, "Cannot add #{other.class} to #{self.class}"
    end

    carry = 0
    sum_segments = []
    low_to_high_segments = segments.zip(other.segments).reverse

    low_to_high_segments.each do |self_segment, other_segment|
      segment, carry = self_segment.add_with_carry(other_segment, carry)
      sum_segments.unshift segment
    end

    unless carry == 0
      raise ArgumentError,
            "#{self} + #{other} is not a valid IP address.  It is #{sum_segments.join('.')} with a carry (#{carry})"
    end

    self.class.new(segments: sum_segments)
  end

  # The succeeding IPv4 address.
  #
  # @see #+
  # @raise (see #+)
  def succ
    self + self.class.new(value: '0.0.0.1')
  end
end

Version data entries

119 entries across 119 versions & 2 rubygems

Version Path
wg-metasploit_data_models-4.1.4.02 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.5 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.4 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.3 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.2 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.1 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-6.0.0 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.6 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.5 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.4 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.3 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.2 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.1 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-5.0.0 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.1.4 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.1.3 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.1.2 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.1.1 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.1.0 app/models/metasploit_data_models/ip_address/v4/single.rb
metasploit_data_models-4.0.2 app/models/metasploit_data_models/ip_address/v4/single.rb