Class Net::DNS::RR
In: lib/net/dns/rr/a.rb
Parent: Object

Net::DNS::RR - DNS Resource Record class

The Net::DNS::RR is the base class for DNS Resource Record (RR) objects. A RR is a pack of data that represents resources for a DNS zone. The form in which this data is shows can be drawed as follow:

  "name  ttl  class  type  data"

The name is the name of the resource, like an canonical name for an A record (internet ip address). The ttl is the time to live, expressed in seconds. type and class are respectively the type of resource (A for ip addresses, NS for nameservers, and so on) and the class, which is almost always IN, the Internet class. At the end, data is the value associated to the name for that particular type of resource record. An example:

  # A record for IP address
  "  86400  IN  A"

  # NS record for name server
  "  86400  IN  NS"

A new RR object can be created in 2 ways: passing a string such the ones above, or specifying each field as the pair of an hash. See the method for details.

Error classes

Some error classes has been defined for the Net::DNS::RR class, which are listed here to keep a light and browsable main documentation. We have:

ArgumentError:Generic argument error for class Net::DNS::RR
DataError:Error in parsing binary data, maybe from a malformed packet.


cls   comp_data   data   inspect   new   parse   parse_packet   to_a   to_s   type  

Included Modules


Classes and Modules

Class Net::DNS::RR::A
Class Net::DNS::RR::AAAA
Class Net::DNS::RR::ArgumentError
Class Net::DNS::RR::CNAME
Class Net::DNS::RR::Classes
Class Net::DNS::RR::DataError
Class Net::DNS::RR::Error
Class Net::DNS::RR::HINFO
Class Net::DNS::RR::MR
Class Net::DNS::RR::MX
Class Net::DNS::RR::NS
Class Net::DNS::RR::NULL
Class Net::DNS::RR::PTR
Class Net::DNS::RR::SOA
Class Net::DNS::RR::SRV
Class Net::DNS::RR::TXT
Class Net::DNS::RR::Types


RR_REGEXP ="^\\s*(\\S+)\\s*(\\d+)?\\s+(" + Net::DNS::RR::Classes.regexp + "|CLASS\\d+)?\\s*(" + Net::DNS::RR::Types.regexp + "|TYPE\\d+)?\\s*(.*)$", Regexp::IGNORECASE)   Regexp matching an RR string
RRFIXEDSZ = 10   Dimension of the sum of class, type, TTL and rdlength fields in a RR portion of the packet, in bytes


name  [R]  Name of the RR
rdata  [R]  Data belonging to that appropriate class, not to be used (use real accessors instead)
ttl  [R]  TTL time (in seconds) of the RR

Public Class methods

Create a new instance of Net::DNS::RR class, or an instance of any of the subclass of the appropriate type.

Argument can be a string or an hash. With a sting, we can pass a RR resource record in the canonical format:

  a     =" 86400 A")
  mx    =" 7200 MX 10")
  cname =" 300 IN CNAME")
  txt   =' 3600 HS TXT "text record"')

Incidentally, a, mx, cname and txt objects will be instances of respectively Net::DNS::RR::A, Net::DNS::RR::MX, Net::DNS::RR::CNAME and Net::DNS::RR::TXT classes.

The name and RR data are required; all other informations are optional. If omitted, the TTL defaults to 10800, type default to A and the RR class defaults to IN. Omitting the optional fields is useful for creating the empty RDATA sections required for certain dynamic update operations. All names must be fully qualified. The trailing dot (.) is optional.

The preferred method is however passing an hash with keys and values:

  rr =
                :name    => "",
                :ttl     => 86400,
                :cls     => "IN",
                :type    => "A",
                :address => ""

  rr =
                :name => "",
                :rdata => ""

Name and data are required; all the others fields are optionals like we‘ve seen before. The data field can be specified either with the right name of the resource (+:address+ in the example above) or with the generic key +:rdata+. Consult documentation to find the exact name for the resource in each subclass.


# File lib/net/dns/rr.rb, line 128
      def initialize(arg)
        instance = case arg
          when String
          when Hash
            raise ArgumentError, "Invalid argument, must be a RR string or an hash of values"

        if @type.to_s == "ANY"
          @cls ="IN")



Return a new RR object of the correct type (like Net::DNS::RR::A if the type is A) from a binary string, usually obtained from network stream.

This method is used when parsing a binary packet by the Packet class.


# File lib/net/dns/rr.rb, line 155
      def RR.parse(data)
        o = allocate
        obj,offset = o.send(:new_from_binary, data, 0)
        return obj

Same as RR.parse, but takes an entire packet binary data to perform name expansion. Default when analizing a packet just received from a network stream.

Return an instance of appropriate class and the offset pointing at the end of the data parsed.


# File lib/net/dns/rr.rb, line 168
      def RR.parse_packet(data,offset)
        o = allocate

Public Instance methods

Class accessor


# File lib/net/dns/rr.rb, line 244
      def cls

Return the RR object in binary data format, suitable for using in network streams, with names compressed. Must pass as arguments the offset inside the packet and an hash of compressed names.

This method is to be used in other classes and is not intended for user space programs.

TO FIX in one of the future releases


# File lib/net/dns/rr.rb, line 183
      def comp_data(offset,compnames)
        type,cls = @type.to_i, @cls.to_i
        str,offset,names = dn_comp(@name,offset,compnames)
        str += [type,cls,@ttl,@rdlength].pack("n2 N n")
        offset += Net::DNS::RRFIXEDSZ
        return str,offset,names

Return the RR object in binary data format, suitable for using in network streams.

  raw_data =
  puts "RR is #{raw_data.size} bytes long"


# File lib/net/dns/rr.rb, line 197
      def data
        type,cls = @type.to_i, @cls.to_i
        str = pack_name(@name)
        return str + [type,cls,@ttl,@rdlength].pack("n2 N n") + get_data

Canonical inspect method.

  mx =" 7200 MX 10")
  #=>            7200    IN      MX      10


# File lib/net/dns/rr.rb, line 208
      def inspect
        data = get_inspect 
        # Returns the preformatted string
        if @name.size < 24
          [@name, @ttl.to_s, @cls.to_s, @type.to_s, data].pack("A24 A8 A8 A8 A*")
          to_a.join("   ")

Returns an array with all the fields for the RR record.

  mx =" 7200 MX 10")
  #=> ["",7200,"IN","MX","10"]


# File lib/net/dns/rr.rb, line 234
      def to_a
        [@name, @ttl, @cls.to_s, @type.to_s, get_inspect]

Returns the RR in a string format.

  mx =" 7200 MX 10")
  #=> "            7200    IN      MX      10"


# File lib/net/dns/rr.rb, line 224
      def to_s

Type accessor


# File lib/net/dns/rr.rb, line 239
      def type