class Nmap::Command::ScanFlags

Represents one or more TCP scan flags.

@api private

Constants

FLAGS

Mapping of symbol scan flags to String values.

REGEXP

Regular expression to validate the given scan flags.

Public Instance Methods

format(value) click to toggle source

Formats a scanflags value.

@param [Hash{Symbol => Boolean}, Array<String>, to_s] value

The scanflags value to format.

@return [String]

The formatted scanflags value.
Calls superclass method
# File lib/nmap/command.rb, line 596
def format(value)
  case value
  when Hash
    string = String.new

    value.each do |key,value|
      string << FLAGS[key] if value
    end

    return string
  when Array
    string = String.new

    value.each do |flag|
      string << FLAGS[flag]
    end

    return string
  else
    super(value)
  end
end
validate(value) click to toggle source

Validates a scanflags value.

@param [String, Hash{Symbol => Boolean}, to_s] value

The scanflags value to validate.

@return [true, (false, String)]

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

    unless value.keys.all? { |key| FLAGS.has_key?(key) }
      return [false, "Hash must only contain the keys :urg, :ack, :psh, :rst, :syn, or :fin"]
    end

    unless value.values.all? { |value| value == nil || value == false || value == true }
      return [false, "Hash must only contain the values true, false, or nil"]
    end

    return true
  when Array
    if value.empty?
      return [false, "Array value cannot be empty"]
    end

    unless value.all? { |flag| FLAGS.has_key?(flag) }
      return [false, "Array must only contain the values :urg, :ack, :psh, :rst, :syn, or :fin"]
    end

    return true
  else
    valid, message = super(value)

    unless valid
      return [valid, message]
    end

    value = value.to_s

    unless value =~ REGEXP
      return [false, "must only contain URG, ACK, PSH, RST, SYN, or FIN"]
    end

    return true
  end
end