Class: Nmap::Command::PortRangeList Private

Inherits:
CommandMapper::Types::List
  • Object
show all
Defined in:
lib/nmap/command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a list of ports or port ranges.

Constant Summary collapse

PORT_RANGE_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression for validating a port or port range.

PortRange::PORT_RANGE_REGEXP
REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression for validating an nmap port range.

/\A(?:[TUS]:)?#{PORT_RANGE_REGEXP}(?:,(?:[TUS]:)?#{PORT_RANGE_REGEXP})*\z/
PROTOCOL_LETTERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

{
  tcp:  'T',
  udp:  'U',
  sctp: 'S'
}

Instance Method Summary collapse

Constructor Details

#initializePortRangeList

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the port range list type.



345
346
347
# File 'lib/nmap/command.rb', line 345

def initialize
  super(type: PortRange.new)
end

Instance Method Details

#format(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats the given port range list value.

Parameters:

  • value (Hash, Range, String, Integer)

    The port range list value.

Returns:

  • (String)

    The formatted port range list.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/nmap/command.rb', line 408

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) ⇒ true, (false, String)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates the given port range list value.

Parameters:

  • value (Object)

    The given port range list value to validate.

Returns:

  • (true, (false, String))

    Returns true if the value is valid, or false and a validation error message if the value is not compatible.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/nmap/command.rb', line 359

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