Class: Dnsruby::Config
- Inherits:
-
Object
- Object
- Dnsruby::Config
- Defined in:
- lib/dnsruby/config.rb
Overview
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.
Constant Summary collapse
- DEFAULT_PORT =
53
Instance Attribute Summary collapse
-
#apply_domain ⇒ Object
Should the default domain be applied?.
-
#apply_search_list ⇒ Object
Should the search list be applied?.
Instance Method Summary collapse
-
#add_nameserver(ns) ⇒ Object
Add a nameserver to the list of nameservers.
-
#check_ns(ns) ⇒ Object
:nodoc: all.
-
#domain ⇒ Object
Return the default domain.
-
#domain=(dom) ⇒ Object
Set the default domain.
-
#generate_candidates(name_in) ⇒ Object
:nodoc: all.
- #get_ready ⇒ Object
-
#initialize ⇒ Config
constructor
Create a new Config with system default values.
-
#inspect ⇒ Object
:nodoc: all.
-
#nameserver ⇒ Object
The list of nameservers to query.
-
#nameserver=(ns) ⇒ Object
Set the config to point to a single nameserver.
-
#ndots ⇒ Object
The minimum number of labels in the query name (if it is not absolute) before it is considered complete.
-
#ndots=(nd) ⇒ Object
Set ndots.
-
#parse_config(config_info = nil) ⇒ Object
:nodoc: all.
-
#port=(p) ⇒ Object
Set port.
-
#search ⇒ Object
Return the search path.
-
#search=(s) ⇒ Object
Set the default search path.
-
#set_config_info(config_info) ⇒ Object
Set the config.
-
#single? ⇒ Boolean
:nodoc: all.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Config
Create a new Config with system default values
85 86 87 88 |
# File 'lib/dnsruby/config.rb', line 85 def initialize() @mutex = Mutex.new @configured = false end |
Instance Attribute Details
#apply_domain ⇒ Object
Should the default domain be applied?
59 60 61 |
# File 'lib/dnsruby/config.rb', line 59 def apply_domain @apply_domain end |
#apply_search_list ⇒ Object
Should the search list be applied?
57 58 59 |
# File 'lib/dnsruby/config.rb', line 57 def apply_search_list @apply_search_list end |
Instance Method Details
#add_nameserver(ns) ⇒ Object
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.
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/dnsruby/config.rb', line 243 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 |
#check_ns(ns) ⇒ Object
:nodoc: all
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/dnsruby/config.rb', line 211 def check_ns(ns) #:nodoc: all if !ns.kind_of?(Array) || !ns.all? {|n| (Name === n || String === n || IPv4 === n || IPv6 === n)} raise ArgumentError.new("invalid nameserver config: #{ns.inspect}") end ns.each {|n| if (String ===n) if Config.scoped_ipv6_server?(n) next end # Make sure we can make a Name or an address from it begin IPv4.create(n) rescue ArgumentError begin IPv6.create(n) rescue ArgumentError begin Name.create(n) rescue ArgumentError raise ArgumentError.new("Can't interpret #{n} as IPv4, IPv6 or Name") end end end end } end |
#domain ⇒ Object
Return the default domain
486 487 488 489 490 491 492 493 494 |
# File 'lib/dnsruby/config.rb', line 486 def domain if (!@configured) parse_config end if (@domain==nil) return nil end return Name.create(@domain).to_s end |
#domain=(dom) ⇒ Object
Set the default domain
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/dnsruby/config.rb', line 155 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 |
#generate_candidates(name_in) ⇒ Object
:nodoc: all
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/dnsruby/config.rb', line 510 def generate_candidates(name_in) #:nodoc: all if !@configured parse_config end candidates = [] name = Name.create(name_in) if name.absolute? candidates = [name] else candidates.push(Name.create(name_in.to_s + ".")) if (@apply_domain) if @ndots > name.length - 1 if (@domain != nil) candidates.push(Name.create(name.to_a+@domain)) end end end if (!@apply_search_list) candidates.push(Name.create(name.to_a)) else if @ndots <= name.length - 1 candidates.push(Name.create(name.to_a)) end candidates.concat(@search.map {|domain| Name.create(name.to_a + domain)}) if (name.length == 1) candidates.concat([Name.create(name.to_a)]) end end end return candidates end |
#get_ready ⇒ Object
504 505 506 507 508 |
# File 'lib/dnsruby/config.rb', line 504 def get_ready if (!@configured) parse_config end end |
#inspect ⇒ Object
:nodoc: all
426 427 428 |
# File 'lib/dnsruby/config.rb', line 426 def inspect #:nodoc: all to_s end |
#nameserver ⇒ Object
The list of nameservers to query
50 51 52 53 54 55 |
# File 'lib/dnsruby/config.rb', line 50 def nameserver if (!@configured) parse_config end return @nameserver end |
#nameserver=(ns) ⇒ Object
Set the config to point to a single nameserver
257 258 259 260 261 262 263 264 |
# File 'lib/dnsruby/config.rb', line 257 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 |
#ndots ⇒ Object
The minimum number of labels in the query name (if it is not absolute) before it is considered complete
61 62 63 64 65 66 |
# File 'lib/dnsruby/config.rb', line 61 def ndots if (!@configured) parse_config end return @ndots end |
#ndots=(nd) ⇒ Object
Set ndots
168 169 170 171 172 173 174 |
# File 'lib/dnsruby/config.rb', line 168 def ndots=(nd) @configured = true @ndots=nd if !@ndots.kind_of?(Integer) raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}") end end |
#parse_config(config_info = nil) ⇒ Object
:nodoc: all
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/dnsruby/config.rb', line 94 def parse_config(config_info=nil) #:nodoc: all @mutex.synchronize { ns = [] @nameserver = [] @domain, s, @search = nil dom="" nd = 1 @ndots = 1 @port = DEFAULT_PORT @apply_search_list = true @apply_domain = true config_hash = Config.default_config_hash case config_info when nil when String config_hash.merge!(Config.parse_resolv_conf(config_info)) when Hash config_hash.merge!(config_info.dup) if String === config_hash[:nameserver] config_hash[:nameserver] = [config_hash[:nameserver]] end if String === config_hash[:search] config_hash[:search] = [config_hash[:search]] end else raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}") end ns = config_hash[:nameserver] if config_hash.include? :nameserver s = config_hash[:search] if config_hash.include? :search nd = config_hash[:ndots] if config_hash.include? :ndots p = config_hash[:port] if config_hash.include? :port @apply_search_list = config_hash[:apply_search_list] if config_hash.include? :apply_search_list @apply_domain= config_hash[:apply_domain] if config_hash.include? :apply_domain dom = config_hash[:domain] if config_hash.include? :domain if (!@configured) # A bad file entry must not hide the other servers or prevent an # explicit resolver override. Keep programmatic configuration strict. unless config_info.kind_of?(Hash) && config_info.key?(:nameserver) ns = ns.select do |server| begin check_ns([server]) true rescue ArgumentError, ResolvError => error Dnsruby.log.warn { "Ignoring invalid nameserver #{server.inspect}: #{error.}" } false end end end send("nameserver=",ns) end @configured = true send("search=",s) send("ndots=",nd) send("port=",p) send("domain=",dom) } Dnsruby.log.info{to_s} end |
#port=(p) ⇒ Object
Set port
177 178 179 180 181 182 183 |
# File 'lib/dnsruby/config.rb', line 177 def port=(p) @configured = true @port=p if p if !@port.kind_of?(Integer) raise ArgumentError.new("invalid port config: #{@port.inspect}") end end |
#search ⇒ Object
Return the search path
474 475 476 477 478 479 480 481 482 483 |
# File 'lib/dnsruby/config.rb', line 474 def search if (!@configured) parse_config end search = [] @search.each do |s| search.push(Name.new(s).to_s) end return search end |
#search=(s) ⇒ Object
Set the default search path
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/dnsruby/config.rb', line 186 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) || # [email protected]? {|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_config_info(config_info) ⇒ Object
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.
80 81 82 |
# File 'lib/dnsruby/config.rb', line 80 def set_config_info(config_info) parse_config(config_info) end |
#single? ⇒ Boolean
:nodoc: all
496 497 498 499 500 501 502 |
# File 'lib/dnsruby/config.rb', line 496 def single? #:nodoc: all if @nameserver.length == 1 return @nameserver[0] else return nil end end |
#to_s ⇒ Object
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/dnsruby/config.rb', line 430 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 |