Class Dnsruby::Resolver
In: lib/dnsruby/resolver.rb
Parent: Object

Description

Dnsruby::Resolver is a DNS stub resolver. This class performs queries with retries across multiple nameservers. The system configured resolvers are used by default.

The retry policy is a combination of the Net::DNS and dnsjava approach, and has the option of :

  • A total timeout for the query (defaults to 0, meaning "no total timeout")
  • A retransmission system that targets the namervers concurrently once the first query round is
 complete, but in which the total time per query round is split between the number of nameservers
 targetted for the first round. and total time for query round is doubled for each query round

Note that, if a total timeout is specified, then that will apply regardless of the retry policy (i.e. it may cut retries short).

Note also that these timeouts are distinct from the SingleResolver‘s packet_timeout

Timeouts apply to the initial query and response. If DNSSEC validation is to be performed, then additional queries may be required (these are performed automatically by Dnsruby). Each additional query will be performed with its own timeouts. So, even with a query_timeout of 5 seconds, a response which required extensive validation may take several times that long. (Future versions of Dnsruby may expose finer-grained events for client tracking of responses and validation)

Methods

Synchronous

These methods raise an exception or return a response message with rcode==NOERROR

  There are "!" versions of these two methods that return an array [response, error]
  instead of raising an error on failure.  They can be called as follows:

  response, error = resolver.send_message!(...)
  response, error = resolver.query!(...)

  If the request succeeds, response will contain the Dnsruby::Message response
  and error will be nil.

  If the request fails, response will be nil and error will contain the error raised.

Asynchronous

These methods use a response queue to return the response and the error

Event Loop

Dnsruby runs a pure Ruby event loop to handle I/O in a single thread. Support for EventMachine has been deprecated.

Methods

Classes and Modules

Class Dnsruby::Resolver::EventType

Constants

DefaultQueryTimeout = 0
DefaultPacketTimeout = 5
DefaultRetryTimes = 1
DefaultRetryDelay = 5
DefaultPipeLiningMaxQueries = 5
DefaultPort = 53
DefaultDnssec = false
AbsoluteMinDnssecUdpSize = 1220
MinDnssecUdpSize = 4096
DefaultUDPSize = MinDnssecUdpSize

Attributes

config  [R]  The current Config
dnssec  [R]  Use DNSSEC for this Resolver
do_caching  [R]  Does this Resolver cache answers, and attempt to retrieve answer from the cache?
do_caching  [RW]  Defines whether we will cache responses, or pass every request to the upstream resolver. This is only really useful when querying authoritative servers (as the upstream recursive resolver is likely to cache)
do_validation  [RW]  Defines whether validation is performed by default on this Resolver when the query method is called. Note that send_message and send_async expect a Message object to be passed in, which is already configured to the callers requirements.
ignore_truncation  [R]  Should truncation be ignored? i.e. the TC bit is ignored and thus the resolver will not requery over TCP if TC is set
no_tcp  [R]  If no_tcp==true, then ONLY UDP will be used as a transport. This should not generally be used, but is provided as a debugging aid.
packet_timeout  [R]  The timeout for any individual packet. This is the timeout used by SingleResolver
port  [R]  The port to send queries to on the resolver
query_timeout  [RW]  Note that this timeout represents the total time a query may run for - multiple packets can be sent to multiple nameservers in this time. This is distinct from the SingleResolver per-packet timeout The query_timeout is not required - it will default to 0, which means "do not use query_timeout". If this is the case then the timeout will be dictated by the retry_times and retry_delay attributes
recurse  [R]  Should the Recursion Desired bit be set?
retry_delay  [RW]  The query will be tried across nameservers retry_times times, with a delay of retry_delay seconds between each retry. The first time round, retry_delay will be divided by the number of nameservers being targetted, and a new nameserver will be queried with the resultant delay.
retry_times  [RW]  The query will be tried across nameservers retry_times times, with a delay of retry_delay seconds between each retry. The first time round, retry_delay will be divided by the number of nameservers being targetted, and a new nameserver will be queried with the resultant delay.
src_address  [R]  The source address to send queries from for IPv4
src_address6  [R]  The source address to send queries from for IPv6
tcp_pipelining  [R]  If tcp_pipelining==true, then we reuse the TCP connection
tcp_pipelining_max_queries  [R]  How many times (number of messages) to reuse the pipelining connection before closing, :infinite for infinite number of requests per connection
tsig  [R] 
udp_size  [R]  The maximum UDP size to be used
use_tcp  [R]  Should TCP be used as a transport rather than UDP? If use_tcp==true, then ONLY TCP will be used as a transport.

Public Class methods

Create a new Resolver object. If no parameters are passed in, then the default system configuration will be used. Otherwise, a Hash may be passed in with the following optional elements :

  • :port
  • :use_tcp
  • :tsig
  • :ignore_truncation
  • :src_address
  • :src_address6
  • :src_port
  • :recurse
  • :udp_size
  • :config_info - see Config
  • :nameserver - can be either a String or an array of Strings
  • :packet_timeout
  • :query_timeout
  • :retry_times
  • :retry_delay
  • :do_caching
  • :tcp_pipelining
  • :tcp_pipelining_max_queries - can be a number or :infinite symbol

Protected Class methods

Public Instance methods

Can be a single Integer or a Range or an Array If an invalid port is selected (one reserved by IANA), then an ArgumentError will be raised. "0" means "any valid port" - this is only a viable option if it is the only port in the list. An ArgumentError will be raised if "0" is added to an existing set of source ports.

       res.add_src_port(60000)
       res.add_src_port([60001,60005,60010])
       res.add_src_port(60015..60115)

Close the Resolver. Unfinished queries are terminated with OtherResolvError.

— @TODO@ Should really auto-generate these methods. Also, any way to tie them up with SingleResolver RDoc? ++

Query for a name. If a valid Message is received, then it is returned to the caller. Otherwise an exception (a Dnsruby::ResolvError or Dnsruby::ResolvTimeout) is raised.

  require 'dnsruby'
  res = Dnsruby::Resolver.new
  response = res.query('example.com') # defaults to Types.A, Classes.IN
  response = res.query('example.com', Types.MX)
  response = res.query('208.77.188.166') # IPv4 address so PTR query will be made
  response = res.query('208.77.188.166', Types.PTR)

Like query, but does not raise an error when an error occurs. Instead, it returns it. @return a 2 element array: [response, error]

Sends a message with send_plain_message. Effectively a wrapper around send_plain_message, but adds the ability to configure whether an error will be raised or returned if it occurs.

@param message the message to send to the DNS server @param error_strategy :return to return [response, error] (default),

                      :raise to return response only, or raise an error if one occurs

Asynchronously send a Message to the server. The send can be done using just Dnsruby. Support for EventMachine has been deprecated.

Dnsruby pure Ruby event loop :

A client_queue is supplied by the client, along with an optional client_query_id to identify the response. The client_query_id is generated, if not supplied, and returned to the client. When the response is known, a tuple of (query_id, response_message, exception) will be added to the client_queue.

The query is sent synchronously in the caller‘s thread. The select thread is then used to listen for and process the response (up to pushing it to the client_queue). The client thread is then used to retrieve the response and deal with it.

Takes :

  • msg - the message to send
  • client_queue - a Queue to push the response to, when it arrives
  • client_query_id - an optional ID to identify the query to the client
  • use_tcp - whether to use only TCP (defaults to SingleResolver.use_tcp)

Returns :

  • client_query_id - to identify the query response to the client. This ID is

generated if it is not passed in by the client

Example invocations :

   id = res.send_async(msg, queue)
   NOT SUPPORTED : id = res.send_async(msg, queue, use_tcp)
   id = res.send_async(msg, queue, id)
   id = res.send_async(msg, queue, id, use_tcp)

Example code :

  require 'dnsruby'
  res = Dnsruby::Resolver.newsend
  query_id = 10 # can be any object you like
  query_queue = Queue.new
  res.send_async(Message.new('example.com', Types.MX),  query_queue, query_id)
  query_id_2 = res.send_async(Message.new('example.com', Types.A), query_queue)
  # ...do a load of other stuff here...
  2.times do
    response_id, response, exception = query_queue.pop
    # You can check the ID to see which query has been answered
    if exception == nil
        # deal with good response
    else
        # deal with problem
    end
  end

Send a message, and wait for the response. If a valid Message is received, then it is returned to the caller. Otherwise an exception (a Dnsruby::ResolvError or Dnsruby::ResolvTimeout) is raised.

send_async is called internally.

example :

  require 'dnsruby'
  include Dnsruby
  res = Dnsruby::Resolver.new
  begin
  response = res.send_message(Message.new('example.com', Types.MX))
  rescue ResolvError
    # ...
  rescue ResolvTimeout
    # ...
  end

Like send_message, but does not raise an error when an error occurs. Instead, it returns it. @return a 2 element array: [response, error]

This method takes a Message (supplied by the client), and sends it to the configured nameservers. No changes are made to the Message before it is sent (TSIG signatures will be applied if configured on the Resolver). Retries are handled as the Resolver is configured to do. Incoming responses to the query are not cached or validated (although TCP fallback will be performed if the TC bit is set and the (Single)Resolver has ignore_truncation set to false). Note that the Message is left untouched - this means that no OPT records are added, even if the UDP transport for the server is specified at more than 512 bytes. If it is desired to use EDNS for this packet, then you should call the Dnsruby::PacketSender#prepare_for_dnssec(msg), or Dnsruby::PacketSender#add_opt_rr(msg) The return value from this method is the [response, error] tuple. Either of these values may be nil - it is up to the client to check.

example :

  require 'dnsruby'
  include Dnsruby
  res = Dnsruby::Resolver.new
  response, error = res.send_plain_message(Message.new('example.com', Types.MX))
  if error
    print "Error returned : #{error}\n"
  else
    process_response(response)
  end

The source port to send queries from Returns either a single Integer or an Array e.g. ‘0’, or ’[60001, 60002, 60007]’

Defaults to 0 - random port

Can be a single Integer or a Range or an Array If an invalid port is selected (one reserved by IANA), then an ArgumentError will be raised.

       res.src_port=0
       res.src_port=[60001,60005,60010]
       res.src_port=60015..60115

Sets the TSIG to sign outgoing messages with. Pass in either a Dnsruby::RR::TSIG, or a key_name and key (or just a key) Pass in nil to stop tsig signing.

  • res.tsig=(tsig_rr)
  • res.tsig=(key_name, key) # defaults to hmac-md5
  • res.tsig=(key_name, key, alg) # e.g. alg = ‘hmac-sha1‘
  • res.tsig=nil # Stop the resolver from signing

[Validate]