Class: MetasploitDataModels::IPAddress::V4::Segmented

Inherits:
Metasploit::Model::Base
  • Object
show all
Extended by:
Match::Child
Includes:
Comparable
Defined in:
app/models/metasploit_data_models/ip_address/v4/segmented.rb

Overview

Note:

Segmented.segment must be called in subclasses to set the Segmented.segment_class_name.

An IPv4 address that is composed of 4 #segments separated by '.'.

Examples:

Using single segments to make a single IPv4 address class

class MetasploitDataModels::IPAddress::V4::Single < MetasploitDataModels::IPAddress::V4::Segmented
   #
   # Segments
   #

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

Direct Known Subclasses

Nmap, Single

Constant Summary collapse

SEGMENT_COUNT =

The number of #segments

4
SEPARATOR =

Separator between segments

'.'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject

Returns the value of attribute value.



36
37
38
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 36

def value
  @value
end

Class Method Details

.regexpRegexp

Note:

Call segment with the segment_class_name before calling this method, as it uses segment_class to look up the REGEXP of the segment_class.

Regular expression that matches the part of a string that represents a IPv4 segmented IP address format.

Returns:

  • (Regexp)


69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 69

def self.regexp
  unless @regexp
    separated_segment_count = SEGMENT_COUNT - 1

    @regexp = %r{
      (#{segment_class::REGEXP}#{Regexp.escape(SEPARATOR)}){#{separated_segment_count},#{separated_segment_count}}
      #{segment_class::REGEXP}
    }x
  end

  @regexp
end

.segment(options = {}) ⇒ void

This method returns an undefined value.

Sets up the segment_class_name for the subclass.

Examples:

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

Parameters:

  • options (Hash{Symbol => String}) (defaults to: {})

Options Hash (options):



90
91
92
93
94
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 90

def self.segment(options={})
  options.assert_valid_keys(:class_name)

  @segment_class_name = options.fetch(:class_name)
end

.segment_classClass

Note:

Call segment to set the segment_class_name before calling segment_class, which will attempt to String#constantize` segment_class_name.

The Class used to parse each segment of the IPv4 address.

Returns:

  • (Class)


102
103
104
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 102

def self.segment_class
  @segment_class = segment_class_name.constantize
end

.segment_class_nameString

Note:

The name of segment_class

Returns:



111
112
113
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 111

def self.segment_class_name
  @segment_class_name
end

.segment_countInteger

The number of #segments

Returns:

  • (Integer)


118
119
120
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 118

def self.segment_count
  SEGMENT_COUNT
end

Instance Method Details

#<=>(other) ⇒ 1, ...

Compare this segment IPv4 address to other.

Returns:



132
133
134
135
136
137
138
139
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 132

def <=>(other)
  if other.is_a? self.class
    segments <=> other.segments
  else
    # The interface for <=> requires nil be returned if other is incomparable
    nil
  end
end

#segmentsArray, []

Array of segments.

Returns:

  • (Array)

    if #value is an Array.

  • ([])

    if #value is not an Array.



145
146
147
148
149
150
151
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 145

def segments
  if value.is_a? Array
    value
  else
    []
  end
end

#segments=(segments) ⇒ Array

Set #segments.

Parameters:

Returns:



157
158
159
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 157

def segments=(segments)
  @value = segments
end

#to_sString

Segments joined with SEPARATOR.

Returns:

  • (String)


164
165
166
# File 'app/models/metasploit_data_models/ip_address/v4/segmented.rb', line 164

def to_s
  segments.map(&:to_s).join(SEPARATOR)
end