Module: MetasploitDataModels::IPAddress::CIDR::ClassMethods

Includes:
Match::Child
Defined in:
lib/metasploit_data_models/ip_address/cidr.rb

Overview

Class methods added to the including Class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Match::Child

#match

Instance Attribute Details

#address_classObject

Returns the value of attribute address_class.



66
67
68
# File 'lib/metasploit_data_models/ip_address/cidr.rb', line 66

def address_class
  @address_class
end

Instance Method Details

#cidr(options = {}) ⇒ Object

Note:

address_class must respond to #segment_class and #segment_count so #maximum_prefix_length can be calculated.

Sets up the address class and allowed #maximum_prefix_length for the including Class.

Parameters:

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

Options Hash (options):



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/metasploit_data_models/ip_address/cidr.rb', line 80

def cidr(options={})
  options.assert_valid_keys(:address_class)

  @address_class = options.fetch(:address_class)

  #
  # Validations
  #

  validates :prefix_length,
            numericality: {
                only_integer: true,
                greater_than_or_equal_to: 0,
                less_than_or_equal_to: maximum_prefix_length
            }
end

#match_regexpRegexp

Regular expression that matches a string that contains only a CIDR IP address.

Returns:

  • (Regexp)


100
101
102
# File 'lib/metasploit_data_models/ip_address/cidr.rb', line 100

def match_regexp
  @match_regexp ||= /\A#{regexp}\z/
end

#maximum_prefix_lengthInteger

The maximum number of bits in a prefix for the #address_class.

Returns:

  • (Integer)

    the number of bits across all segments of #address_class.



107
108
109
# File 'lib/metasploit_data_models/ip_address/cidr.rb', line 107

def maximum_prefix_length
  @maximum_prefix_length ||= address_class.segment_count * address_class.segment_class.bits
end

#regexpRegexp

Regular expression that matches a portion of string that contains a CIDR IP address.

Returns:

  • (Regexp)


114
115
116
# File 'lib/metasploit_data_models/ip_address/cidr.rb', line 114

def regexp
  @regexp ||= /(?<address>#{address_class.regexp})#{Regexp.escape(SEPARATOR)}(?<prefix_length>\d+)/
end