Class: Nmap::Command::PortRangeList Private

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

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.

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.

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.

/\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.



322
323
324
# File 'lib/nmap/command.rb', line 322

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 value.

Parameters:

  • value (Hash, Range, String, Integer)

Returns:

  • (String)


380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/nmap/command.rb', line 380

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)

Returns:

  • (true, (false, String))


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/nmap/command.rb', line 333

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