Class Net::DNS::Question
In: lib/net/dns/question.rb
Parent: Object


Net::DNS::Question - DNS packet question class


  require 'net/dns/question'


This class represent the Question portion of a DNS packet. The number of question entries is stored in the qdCount variable of an Header object.

A new object can be created passing the name of the query and the type of answer desired, plus an optional argument containing the class:

  question ="", Net::DNS::A)
     #=> "                   A       IN"

Alternatevly, a new object is created when processing a binary packet, as when an answer is received. To obtain the binary data from a question object you can use the method Question#data:
     #=> "\006google\003com\000\000\001\000\001"

A lot of methods were written to keep a compatibility layer with the Perl version of the library, as long as methods name which are more or less the same.

Error classes

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

ArgumentError:Argument Error for class Net::DNS::Question
NameError:An error in the name part of a Question entry


Copyright (c) 2006 Marco Ceresa

All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Ruby itself.


comp_data   data   inspect   inspect   new   parse   to_s  

Included Modules


Classes and Modules

Class Net::DNS::Question::ArgumentError
Class Net::DNS::Question::Error
Class Net::DNS::Question::NameError


qClass  [R]  class part of a Question entry
qName  [R]  name part of a Question entry
qType  [R]  type part of a Question entry

Public Class methods

If not specified, type and cls arguments defaults to Net::DNS::A and Net::DNS::IN respectively.


# File lib/net/dns/question.rb, line 93
      def initialize(name,type=Net::DNS::A,cls=Net::DNS::IN)
        @qName = check_name name
        @qType = type
        @qClass = cls

Return a new Net::DNS::Question object created by parsing binary data, such as an answer from the nameserver.

  question = Net::DNS::Question.parse(data)
  puts "Queried for #{question.qName} type #{question.qType.to_s}"
    #=> Queried for type A


# File lib/net/dns/question.rb, line 107
      def self.parse(arg)
        o = allocate
        o.send(:new_from_binary, arg.to_s)

Public Instance methods

Return the binary data of the objects, plus an offset and an Hash with references to compressed names. For use in Net::DNS::Packet compressed packet creation.


# File lib/net/dns/question.rb, line 136
      def comp_data
        arr = @qName.split(".")
        str = pack_name(@qName)
        string = ""
        names = {}
        offset = Net::DNS::HFIXEDSZ
        arr.size.times do |i|
          x = i+1
          elem = arr[-x]
          len = elem.size
          string = ((string.reverse)+([len,elem].pack("Ca*")).reverse).reverse
          names[string] = offset
          offset += len
        offset += 2 * Net::DNS::INT16SZ
        str += "\000"

Outputs binary data from a Question object
     #=> "\006google\003com\000\000\001\000\001"


# File lib/net/dns/question.rb, line 128
      def data

Returns a printable version of question with nice formatting.

  q ="", Net::DNS::A)
  q.inspect # => "                  IN      A       "


# File lib/net/dns/question.rb, line 165
      def inspect
        if @qName.size > 29 then
          len = @qName.size + 1
          len = 29
        [@qName, @qClass.to_s, @qType.to_s].pack("A#{len} A8 A8")

Known inspect method with nice formatting


# File lib/net/dns/question.rb, line 114
      def inspect
        if @qName.size > 29 then 
          len = @qName.size + 1 
          len = 29
        [@qName,@qClass.to_s,@qType.to_s].pack("A#{len} A8 A8")

Returns a string representation of question. It is the same as inspect.

  q ="", Net::DNS::A)
  q.inspect # => "                  IN      A       "


# File lib/net/dns/question.rb, line 184
      def to_s