class MetasploitDataModels::IPAddress::V4::Segment::Single

A segment number in an IPv4 address or the {MetasploitDataModels::IPAddress::V4::Segment::Nmap::Range#begin} or {MetasploitDataModels::IPAddress::V4::Segment::Nmap::Range#send}.

Constants

BITS

Number of bits in a IPv4 segment

LIMIT

Limit that {#value} can never reach

MAXIMUM

Maximum segment {#value}

MINIMUM

Minimum segment {#value}

REGEXP

Regular expression for a segment (octet) of an IPv4 address in decimal dotted notation.

@see stackoverflow.com/a/17871737/470451

Attributes

value[R]

@!attribute value

The segment number.

@return [Integer, String]

Public Class Methods

bits() click to toggle source

(see BITS)

@return [Integer] {BITS}

# File app/models/metasploit_data_models/ip_address/v4/segment/single.rb, line 58
def self.bits
  BITS
end

Public Instance Methods

<=>(other) click to toggle source

Compare this segment to `other`.

@param other [#value] another segent to compare against. @return [1] if this segment is greater than `other`. @return [0] if this segment is equal to `other`. @return [-1] if this segment is less than `other`.

# File app/models/metasploit_data_models/ip_address/v4/segment/single.rb, line 72
def <=>(other)
  value <=> other.value
end
add_with_carry(other, carry=0) click to toggle source

Full add (as in [full adder](en.wikipedia.org/wiki/Full_adder)) two (this segment and `other`) segments and a carry from the previous {#add_with_carry}.

@param other [MetasploitDataModels:IPAddress::V4::Segment::Single] segment to add to this segment. @param carry [Integer] integer to add to this segment and other segment from a previous call to {#add_with_carry}

for lower segments.

@return [Array<(MetasploitDataModels::IPAddress::V4::Segment::Single, Integer)>] Array containing a proper segment

(where {#value} is less than {LIMIT}) and a carry integer to pass to next call to {#add_with_carry}.

@return (see half_add)

# File app/models/metasploit_data_models/ip_address/v4/segment/single.rb, line 85
def add_with_carry(other, carry=0)
  improper_value = self.value + other.value + carry
  proper_value = improper_value % LIMIT
  carry = improper_value / LIMIT
  segment = self.class.new(value: proper_value)

  [segment, carry]
end
succ() click to toggle source

The succeeding segment. Used in `Range`s when walking the `Range`.

@return [MetasploitDataModels::IPAddress::V4::Segment::Single] if {#value} responds to `#succ`. @return [nil] otherwise

# File app/models/metasploit_data_models/ip_address/v4/segment/single.rb, line 98
def succ
  if value.respond_to? :succ
    self.class.new(value: value.succ)
  end
end
value=(formatted_value) click to toggle source

Sets {#value} by type casting String to Integer.

@param formatted_value [#to_s] @return [Integer] if `formatted_value` contains only an Integer#to_s @return [#to_s] `formatted_value` if it does not contain an Integer#to_s

# File app/models/metasploit_data_models/ip_address/v4/segment/single.rb, line 112
def value=(formatted_value)
  @value_before_type_cast = formatted_value

  begin
    # use Integer() instead of String#to_i as String#to_i will ignore trailing letters (i.e. '1two' -> 1) and turn all
    # string without an integer in it to 0.
    @value = Integer(formatted_value.to_s)
  rescue ArgumentError
    @value = formatted_value
  end
end