class Net::DNS::RR::Types

This is an auxiliary class to handle RR type field in a DNS packet.

Constants

TYPES

Public Class Methods

default=(str) click to toggle source

Be able to control the default type to assign when type is nil. Default to A

# File lib/net/dns/rr/types.rb, line 76
def self.default=(str)
  if TYPES.has_key? str
    @@default = TYPES[str]
  else
    raise ArgumentError, "Unknown type #{str}"
  end
end
new(type) click to toggle source

Creates a new object representing an RR type. Performs some checks on the argument validity too. Il type is nil, the default value is ANY or the one set with ::default=

# File lib/net/dns/rr/types.rb, line 121
def initialize(type)
  case type
  when String
    # type in the form "A" or "NS"
    new_from_string(type.upcase)
  when Fixnum
    # type in numeric form
    new_from_num(type)
  when nil
    # default type, control with Types.default=
    @str = TYPES.invert[@@default]
    @num = @@default
  else
    raise ArgumentError, "Wrong type class: #{type.class}"
  end
end
regexp() click to toggle source

Gives in output the keys from the Types hash in a format suited for regexps

# File lib/net/dns/rr/types.rb, line 113
def self.regexp
  # Longest ones go first, so the regex engine will match AAAA before A.
  TYPES.keys.sort { |a,b| b.length <=> a.length }.join("|")
end
to_str(type) click to toggle source

Returns the type in string format, as “A” or “NS”, given the numeric value

# File lib/net/dns/rr/types.rb, line 98
def self.to_str(type)
  case type
    when Fixnum
      if TYPES.invert.has_key? type
        TYPES.invert[type]
      else
        raise ArgumentError, "Unknown type number #{type}"
      end
    else
      raise ArgumentError, "Wrong type class: #{type.class}"
  end
end
valid?(type) click to toggle source

Checks whether type is a valid RR type.

# File lib/net/dns/rr/types.rb, line 85
def self.valid?(type)
  case type
    when String
      TYPES.has_key?(type)
    when Fixnum
      TYPES.invert.has_key?(type)
    else
      raise ArgumentError, "Wrong type class: #{type.class}"
  end
end

Public Instance Methods

inspect() click to toggle source

Returns the type in number format (default for normal use)

# File lib/net/dns/rr/types.rb, line 140
def inspect
  @num
end
to_i() click to toggle source

Returns the type in numeric format, usable by the pack methods for data transfers

# File lib/net/dns/rr/types.rb, line 152
def to_i
  @num.to_i
end
to_s() click to toggle source

Returns the type in string format, i.d. “A” or “NS” or such a string.

# File lib/net/dns/rr/types.rb, line 146
def to_s
  @str
end
to_str() click to toggle source
# File lib/net/dns/rr/types.rb, line 156
def to_str
  @num.to_s
end

Private Instance Methods

new_from_num(type) click to toggle source

Contructor for numeric data type.

# File lib/net/dns/rr/types.rb, line 180
def new_from_num(type)
  if TYPES.invert.has_key? type
    @num = type
    @str = TYPES.invert[type]
  else
    raise ArgumentError, "Unkown type number #{type}"
  end
end
new_from_string(type) click to toggle source

Constructor for string data type.

# File lib/net/dns/rr/types.rb, line 164
def new_from_string(type)
  case type
  when /^TYPE\d+/
    # TODO!!!
  else
    # String with name of type
    if TYPES.has_key? type
      @str = type
      @num = TYPES[type]
    else
      raise ArgumentError, "Unknown type #{type}"
    end
  end
end