ruby picture

RCR 176: Extend resolv.rb

Submitted by danielhobe (Sat Dec 20 01:12:37 UTC 2003)

Abstract

It would be very useful to be able to tell the Resolv::DNS class what DNS servers to use without reading them in from a file.

Problem

Resolv::DNS requires that the list of DNS servers to query comes from a specified file (/etc/resolv.conf is the default). To use arbitrary DNS servers for name resolution one must create a resolv.conf format file with the servers in it and then pass that to Resolv::DNS. This should be much easier to do.

Proposal

There should be a second class (DNS2) within the Resolv class that is designed to allow the user to tell it what DNS servers to use when they instantiate the object.

A second solution might be to modify the current class to accept either a filename or the DNS server list.

Analysis

Telling the object what DNS servers to use directly is much cleaner than creating files for the object to read in.

Implementation

The following is a sample implementation of a DNS2 class that takes all the information found in a resolv.conf file as arguments:
require 'resolv'

class Resolv::DNS2 < Resolv::DNS
  def initialize(nameserver=['localhost'],search = nil,ndots=1)<BR>
    @mutex = Mutex.new
    @config = Config.new(nameserver,search,ndots)
    @initialized = nil
  end
  class Config < Resolv::DNS::Config
    def initialize(nameserver,search,ndots)
      @mutex = Mutex.new
      @nameserver = nameserver
      @search = search
      @ndots = ndots
      @initialized = nil
    end
    def lazy_initialize
      unless @search
        hostname = Socket.gethostname
        if /\./ =~ hostname
          @search = [Resolv::DNS::Label.split($')]
        else
          @search = [[]]
        end
      end
    end
  end
end
ruby picture
Comments Current voting

I like the idea, but I don't like calling the class DNS2. It's not very descriptive, IMO. Could the DNS initialize method be changed to accept either a filename or a Hash of named arguments? -- Paul Brannan


Strongly opposed 0
Opposed 0
Neutral 0
In favor 10
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .