Changing Resolv::DNS

From: Daniel Hobe <daniel@...>
Date: 2004-05-01 22:56:38 UTC
List: ruby-core #2840
I put out a RCR a while ago (176) that subclassed the Resolv::DNS class to 
allow a user to specify the nameserver or nameservers for the resolver to 
use.  I've since thought better of that interface and wanted to propose 
something like the code below as an addition to the base Resolv::DNS class.

Having the ability to tell the library what nameserver to use is very usful if 
you want to monitor a set of DNS servers to make sure they are responding 
with the proper result for a name.

i.e.:
Resolv::DNS.new('/etc/foo')
-or-
Resolv::DNS.new({nameserver=>['192.168.10.1','192.168.10.2'],
                 search=>['ruby-lang.org','ruby-doc.org'],
                 ndots => 2})

Checking for a String or Hash explicitly is repulsive, any ideas on how to do 
this better?

class Resolv::DNS 
  def initialize(config="/etc/resolv.conf")
    @mutex = Mutex.new
    if config.class == String
      @config = Config.new(config)
    elsif config.class == Hash
      @config = ConfigUser.new
    else 
      raise
    end
    @initialized = nil
  end

 class ConfigUser < Resolv::DNS::Config
  def initialize(nameserver,search,ndots)
    @mutex = Mutex.new
    @nameserver = nameserver
    @search = search
    @ndots = ndots
    @initialized = nil
  end

  def lazy_initialize
    @mutex.synchronize {
      unless @initialized
        unless @search
          hostname = Socket.gethostname
          if /\./ =~ hostname
            @search = [Resolv::DNS::Label.split($')]
          else
            @search = [[]]
          end
        end
        @initialized = true      
      end
    }
  end
end


-- 
Daniel Hobe <daniel@nightrunner.com>
http://www.nightrunner.com

In This Thread

Prev Next