class Nmap::Command::PortRangeList

Represents a list of ports or port ranges.

@api private

Constants

PORT_RANGE_REGEXP

Regular expression for validating a port or port range.

PROTOCOL_LETTERS

Mapping of protocol names to single letters used in port range syntax.

REGEXP

Regular expression for validating an nmap port range.

Public Class Methods

new() click to toggle source

Initializes the port range list type.

Calls superclass method
# File lib/nmap/command.rb, line 343
def initialize
  super(type: PortRange.new)
end

Public Instance Methods

format(value) click to toggle source

Formats the given port range list value.

@param [Hash, Range, String, Integer] value

The port range list value.

@return [String]

The formatted port range list.
Calls superclass method
# File lib/nmap/command.rb, line 406
def format(value)
  case value
  when Hash
    # format a hash of protocols and port ranges
    value.map { |protocol,ports|
      letter = PROTOCOL_LETTERS.fetch(protocol)

      "#{letter}:#{format(ports)}"
    }.join(',')
  when Range
    # format an individual port range
    @type.format(value)
  when String
    # pass strings directly through
    value
  else
    super(value)
  end
end
validate(value) click to toggle source

Validates the given port range list value.

@param [Object] value

The given port range list value to validate.

@return [true, (false, String)]

Returns true if the value is valid, or `false` and a validation error
message if the value is not compatible.
Calls superclass method
# File lib/nmap/command.rb, line 357
def validate(value)
  case value
  when Hash
    if value.empty?
      return [false, "cannot be empty"]
    end

    value.each do |protocol,ports|
      unless PROTOCOL_LETTERS.has_key?(protocol)
        return [false, "unknown protocol (#{protocol.inspect}) must be :tcp, :udp, or :sctp"]
      end

      valid, message = validate(ports)

      unless valid
        return [valid, message]
      end
    end

    return true
  when Range
    @type.validate(value)
  when String
    unless value =~ REGEXP
      return [false, "not a valid port range list (#{value.inspect})"]
    end

    return true
  else
    super(value)
  end
end