class Dnsruby::Config
Description¶ ↑
The Config class determines the system configuration for DNS. In particular, it determines the nameserver to target queries to. It also specifies whether and how the search list and default domain should be applied to queries, according to the following algorithm :
-
If the name is absolute, then it is used as is.
-
If the name is not absolute, then :
If apply_domain is true, and ndots is greater than the number of labels in the name, then the default domain is added to the name. If apply_search_list is true, then each member of the search list is appended to the name.
The Config class has now been modified for lazy loading. Previously, the config was loaded when a Resolver was instantiated. Now, the config is only loaded if a query is performed (or a config parameter requested on) a Resolver which has not yet been configured.
Constants
- DEFAULT_PORT
Attributes
Should the default domain be applied?
Should the search list be applied?
Public Class Methods
Create a new Config
with system default values
# File lib/dnsruby/config.rb, line 85 def initialize() @mutex = Mutex.new @configured = false end
Reset the config to default values
# File lib/dnsruby/config.rb, line 90 def Config.reset @configured = false end
Public Instance Methods
Add a nameserver to the list of nameservers.
Can take either a single String or an array of Strings. The new nameservers are added at a higher priority.
# File lib/dnsruby/config.rb, line 227 def add_nameserver(ns) @configured = true if (ns.kind_of?String) ns=[ns] end check_ns(ns) ns.reverse_each do |n| if (!@nameserver.include?(n)) self.nameserver=[n]+@nameserver end end end
Return the default domain
# File lib/dnsruby/config.rb, line 418 def domain if (!@configured) parse_config end if (@domain==nil) return nil end return Name.create(@domain).to_s end
Set the default domain
# File lib/dnsruby/config.rb, line 142 def domain=(dom) # @configured = true if (dom) if !dom.kind_of?(String) raise ArgumentError.new("invalid domain config: #{@domain.inspect}") end @domain = Name::split(dom) else @domain=nil end end
# File lib/dnsruby/config.rb, line 436 def get_ready if (!@configured) parse_config end end
The list of nameservers to query
# File lib/dnsruby/config.rb, line 50 def nameserver if (!@configured) parse_config end return @nameserver end
Set the config to point to a single nameserver
# File lib/dnsruby/config.rb, line 241 def nameserver=(ns) @configured = true check_ns(ns) # @nameserver = ['0.0.0.0'] if (@nameserver.class != Array || @nameserver.empty?) # Now go through and ensure that all ns point to IP addresses, not domain names @nameserver=ns Dnsruby.log.debug{"Nameservers = #{@nameserver.join(", ")}"} end
The minimum number of labels in the query name (if it is not absolute) before it is considered complete
# File lib/dnsruby/config.rb, line 61 def ndots if (!@configured) parse_config end return @ndots end
Set ndots
# File lib/dnsruby/config.rb, line 155 def ndots=(nd) @configured = true @ndots=nd if !@ndots.kind_of?(Integer) raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}") end end
Set port
# File lib/dnsruby/config.rb, line 164 def port=(p) @configured = true @port=p if p if !@port.kind_of?(Integer) raise ArgumentError.new("invalid port config: #{@port.inspect}") end end
Return the search path
# File lib/dnsruby/config.rb, line 406 def search if (!@configured) parse_config end search = [] @search.each do |s| search.push(Name.new(s).to_s) end return search end
Set the default search path
# File lib/dnsruby/config.rb, line 173 def search=(s) @configured = true @search=s if @search if @search.class == Array @search = @search.map {|arg| Name::split(arg) } else raise ArgumentError.new("invalid search config: search must be an array!") end else hostname = Socket.gethostname if /\./ =~ hostname @search = [Name.split($')] else @search = [[]] end end if !@search.kind_of?(Array) || # !@search.all? {|ls| ls.all? {|l| Label::Str === l } } !@search.all? {|ls| ls.all? {|l| Name::Label === l } } raise ArgumentError.new("invalid search config: #{@search.inspect}") end end
Set the config. Parameter can be :
-
A String containing the name of the config file to load
e.g. /etc/resolv.conf
-
A hash with the following elements :
nameserver (String) domain (String) search (String) ndots (Integer)
This method should not normally be called by client code.
# File lib/dnsruby/config.rb, line 80 def set_config_info(config_info) parse_config(config_info) end
# File lib/dnsruby/config.rb, line 362 def to_s if (!@configured) parse_config end ret = "Config - nameservers : " @nameserver.each {|n| ret += n.to_s + ", "} domain_string="empty" if (@domain!=nil) domain_string=@domain.to_s end ret += " domain : #{domain_string}, search : " search.each {|s| ret += s + ", " } ret += " ndots : #{@ndots}" ret += " port : #{@port}" return ret end