Class: Net::DNS::RR::Classes

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/rr/classes.rb

Overview

Net::DNS::Classes

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

Constant Summary collapse

CLASSES =

Hash with the values of each RR class stored with the respective id number.

{
  'IN' => 1, # RFC 1035
  'CH' => 3,       # RFC 1035
  'HS' => 4,       # RFC 1035
  'NONE' => 254, # RFC 2136
  'ANY' => 255, # RFC 1035
}.freeze
@@default =

The default value when class is nil in Resource Records

CLASSES["IN"]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls) ⇒ Classes

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

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/net/dns/rr/classes.rb', line 27

def initialize(cls)
  case cls
  when String
    initialize_from_str(cls)
  when Integer
    initialize_from_num(cls)
  when nil
    initialize_from_num(@@default)
  end

  return unless @str.nil? || @num.nil?

  raise ArgumentError, "Unable to create a `Classes' from `#{cls}'"
end

Class Method Details

.defaultObject



63
64
65
# File 'lib/net/dns/rr/classes.rb', line 63

def self.default
  @@default
end

.default=(str) ⇒ Object

Be able to control the default class to assign when cls argument is nil. Default to IN

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/net/dns/rr/classes.rb', line 69

def self.default=(str)
  raise ArgumentError, "Unknown class `#{str}'" unless CLASSES[str]

  @@default = CLASSES[str]
end

.regexpObject

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



106
107
108
# File 'lib/net/dns/rr/classes.rb', line 106

def self.regexp
  CLASSES.keys.sort.join("|")
end

.valid?(cls) ⇒ Boolean

Returns whether cls is a valid RR class.

Net::DNS::RR::Classes.valid?("IN")
# => true
Net::DNS::RR::Classes.valid?(1)
# => true
Net::DNS::RR::Classes.valid?("Q")
# => false
Net::DNS::RR::Classes.valid?(256)
# => false
Net::DNS::RR::Classes.valid?(Hash.new)
# => ArgumentError

FIXME: valid? should never raise.

Raises

ArgumentError

if cls isn’t either a String or a Fixnum



93
94
95
96
97
98
99
100
101
102
# File 'lib/net/dns/rr/classes.rb', line 93

def self.valid?(cls)
  case cls
  when String
    CLASSES.key?(cls)
  when Integer
    CLASSES.invert.key?(cls)
  else
    raise ArgumentError, "Wrong cls class: #{cls.class}"
  end
end

Instance Method Details

#inspectObject

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

FIXME: inspect must return a String.



47
48
49
# File 'lib/net/dns/rr/classes.rb', line 47

def inspect
  @num
end

#to_iObject

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



59
60
61
# File 'lib/net/dns/rr/classes.rb', line 59

def to_i
  @num.to_i
end

#to_sObject

Returns the class in string format, ex. “IN” or “CH” or such a string.



53
54
55
# File 'lib/net/dns/rr/classes.rb', line 53

def to_s
  @str.to_s
end