Module: DNS::Zone::RR

Defined in:
lib/dns/zone/rr.rb

Overview

The module containes resource record types supported by this gem. The #RR.load method will convert RR string data into a Ruby class.

Defined Under Namespace

Classes: A, AAAA, CDNSKEY, CDS, CNAME, DLV, DNSKEY, DS, HINFO, MX, NAPTR, NS, NSEC, NSEC3, NSEC3PARAM, PTR, RRSIG, Record, SOA, SPF, SRV, SSHFP, TXT

Constant Summary collapse

REGEX_TTL =
/\d+[wdmhs]?/i
REGEX_KLASS =
/(?<klass>IN)?/i
REGEX_TYPE =
/(?<type>A|AAAA|CDNSKEY|CDS|CNAME|DLV|DNSKEY|DS|HINFO|MX|NAPTR|NS|NSEC|NSEC3|NSEC3PARAM|RRSIG|SOA|SPF|SRV|SSHFP|TXT|PTR)\s{1}/i
REGEX_RR =
/^(?<label>\S+|\s{1})\s*(?<ttl>#{REGEX_TTL})?\s*#{REGEX_KLASS}\s*#{REGEX_TYPE}\s*(?<rdata>[\s\S]*)$/i
REGEX_DOMAINNAME =
/\S+\.?/i
REGEX_STRING =
/((?:[^"\\]+|\\.)*)/
REGEX_CHARACTER_STRING =
%r{
  "#{DNS::Zone::RR::REGEX_STRING}"|#{DNS::Zone::RR::REGEX_STRING}
}mx

Class Method Summary collapse

Class Method Details

.load(string, options = {}) ⇒ Object

Load RR string data and return an instance representing the RR.

Parameters:

  • string (String)

    RR ASCII string data

  • options (Hash) (defaults to: {})

    additional data required to correctly parse a ‘whole’ zone

Options Hash (options):

  • :last_label (String)

    The last label used by the previous RR

Returns:

  • (Object)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dns/zone/rr.rb', line 24

def self.load(string, options = {})
  # strip comments, unless its escaped.
  # skip semicolons within "quote segments" (TXT records)
  string.gsub!(/((?<!\\);)(?=(?:[^"]|"[^"]*")*$).*/o, "")

  captures = string.match(REGEX_RR)
  return nil unless captures

  case captures[:type]
  when 'A'           then A.new.load(string, options)
  when 'AAAA'        then AAAA.new.load(string, options)
  when 'CDNSKEY'     then CDNSKEY.new.load(string, options)
  when 'CDS'         then CDS.new.load(string, options)
  when 'CNAME'       then CNAME.new.load(string, options)
  when 'DLV'         then DLV.new.load(string, options)
  when 'DNSKEY'      then DNSKEY.new.load(string, options)
  when 'DS'          then DS.new.load(string, options)
  when 'HINFO'       then HINFO.new.load(string, options)
  when 'MX'          then MX.new.load(string, options)
  when 'NAPTR'       then NAPTR.new.load(string, options)
  when 'NS'          then NS.new.load(string, options)
  when 'NSEC'        then NSEC.new.load(string, options)
  when 'NSEC3'       then NSEC3.new.load(string, options)
  when 'NSEC3PARAM'  then NSEC3PARAM.new.load(string, options)
  when 'PTR'         then PTR.new.load(string, options)
  when 'RRSIG'       then RRSIG.new.load(string, options)
  when 'SOA'         then SOA.new.load(string, options)
  when 'SPF'         then SPF.new.load(string, options)
  when 'SRV'         then SRV.new.load(string, options)
  when 'SSHFP'       then SSHFP.new.load(string, options)
  when 'TXT'         then TXT.new.load(string, options)
  else
    raise 'Unknown or unsupported RR Type'          
  end
end