* lib/resolv.rb (Resolv::DNS::Config): make it configurable without
external file such as /etc/resolv.conf. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6296 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
10b01eddac
commit
fbb8f1964a
@ -1,3 +1,8 @@
|
|||||||
|
Wed May 12 17:41:42 2004 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* lib/resolv.rb (Resolv::DNS::Config): make it configurable without
|
||||||
|
external file such as /etc/resolv.conf.
|
||||||
|
|
||||||
Wed May 12 14:37:27 2004 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
Wed May 12 14:37:27 2004 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
||||||
|
|
||||||
* ext/openssl/ossl_x509name.c: attribute value of DC (short name of
|
* ext/openssl/ossl_x509name.c: attribute value of DC (short name of
|
||||||
|
109
lib/resolv.rb
109
lib/resolv.rb
@ -10,9 +10,10 @@ It is possible to lookup various resources of DNS using DNS module directly.
|
|||||||
Resolv.getaddress("www.ruby-lang.org")
|
Resolv.getaddress("www.ruby-lang.org")
|
||||||
Resolv.getname("210.251.121.214")
|
Resolv.getname("210.251.121.214")
|
||||||
|
|
||||||
dns = Resolv::DNS.new
|
Resolv::DNS.open {|dns|
|
||||||
dns.getresources("www.ruby-lang.org", Resolv::DNS::Resource::IN::A).collect {|r| r.address}
|
dns.getresources("www.ruby-lang.org", Resolv::DNS::Resource::IN::A).collect {|r| r.address}
|
||||||
dns.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX).collect {|r| [r.exchange.to_s, r.preference]}
|
dns.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX).collect {|r| [r.exchange.to_s, r.preference]}
|
||||||
|
}
|
||||||
|
|
||||||
== Resolv class
|
== Resolv class
|
||||||
|
|
||||||
@ -57,10 +58,17 @@ hostname resolver using /etc/hosts format.
|
|||||||
DNS stub resolver.
|
DNS stub resolver.
|
||||||
|
|
||||||
=== class methods
|
=== class methods
|
||||||
--- Resolv::DNS.new(resolv_conf='/etc/resolv.conf')
|
--- Resolv::DNS.new(config_info=nil)
|
||||||
|
|
||||||
--- Resolv::DNS.open(resolv_conf='/etc/resolv.conf')
|
((|config_info|)) should be nil, a string or a hash.
|
||||||
--- Resolv::DNS.open(resolv_conf='/etc/resolv.conf') {|dns| ...}
|
If nil is given, /etc/resolv.conf and platform specific information is used.
|
||||||
|
If a string is given, it should be a filename which format is same as /etc/resolv.conf.
|
||||||
|
If a hash is given, it may contains information for nameserver, search and ndots as follows.
|
||||||
|
|
||||||
|
Resolv::DNS.new({:nameserver=>["210.251.121.21"], :search=>["ruby-lang.org"], :ndots=>1})
|
||||||
|
|
||||||
|
--- Resolv::DNS.open(config_info=nil)
|
||||||
|
--- Resolv::DNS.open(config_info=nil) {|dns| ...}
|
||||||
|
|
||||||
=== methods
|
=== methods
|
||||||
--- Resolv::DNS#close
|
--- Resolv::DNS#close
|
||||||
@ -369,9 +377,9 @@ class Resolv
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(config="/etc/resolv.conf")
|
def initialize(config_info=nil)
|
||||||
@mutex = Mutex.new
|
@mutex = Mutex.new
|
||||||
@config = Config.new(config)
|
@config = Config.new(config_info)
|
||||||
@initialized = nil
|
@initialized = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -704,20 +712,17 @@ class Resolv
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Config
|
class Config
|
||||||
def initialize(filename="/etc/resolv.conf")
|
def initialize(config_info=nil)
|
||||||
@mutex = Mutex.new
|
@mutex = Mutex.new
|
||||||
@filename = filename
|
@config_info = config_info
|
||||||
@initialized = nil
|
@initialized = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def lazy_initialize
|
def Config.parse_resolv_conf(filename)
|
||||||
@mutex.synchronize {
|
nameserver = []
|
||||||
unless @initialized
|
search = nil
|
||||||
@nameserver = []
|
ndots = 1
|
||||||
@search = nil
|
open(filename) {|f|
|
||||||
@ndots = 1
|
|
||||||
begin
|
|
||||||
open(@filename) {|f|
|
|
||||||
f.each {|line|
|
f.each {|line|
|
||||||
line.sub!(/[#;].*/, '')
|
line.sub!(/[#;].*/, '')
|
||||||
keyword, *args = line.split(/\s+/)
|
keyword, *args = line.split(/\s+/)
|
||||||
@ -727,24 +732,61 @@ class Resolv
|
|||||||
next unless keyword
|
next unless keyword
|
||||||
case keyword
|
case keyword
|
||||||
when 'nameserver'
|
when 'nameserver'
|
||||||
@nameserver += args
|
nameserver += args
|
||||||
when 'domain'
|
when 'domain'
|
||||||
@search = [Label.split(args[0])]
|
search = [args[0]]
|
||||||
when 'search'
|
when 'search'
|
||||||
@search = args.map {|arg| Label.split(arg)}
|
search = args
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rescue Errno::ENOENT
|
return { :nameserver => nameserver, :search => search, :ndots => ndots }
|
||||||
if /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
|
|
||||||
search, nameserver = Win32::Resolv.get_resolv_info
|
|
||||||
@search = [search] if search
|
|
||||||
@nameserver = nameserver if nameserver
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def Config.default_config_hash(filename="/etc/resolv.conf")
|
||||||
|
if File.exist? filename
|
||||||
|
config_hash = Config.parse_resolv_conf(filename)
|
||||||
|
else
|
||||||
|
if /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
|
||||||
|
search, nameserver = Win32::Resolv.get_resolv_info
|
||||||
|
config_hash = {}
|
||||||
|
config_hash[:nameserver] = nameserver if nameserver
|
||||||
|
config_hash[:search] = [search] if search
|
||||||
|
end
|
||||||
|
end
|
||||||
|
config_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def lazy_initialize
|
||||||
|
@mutex.synchronize {
|
||||||
|
unless @initialized
|
||||||
|
@nameserver = []
|
||||||
|
@search = nil
|
||||||
|
@ndots = 1
|
||||||
|
case @config_info
|
||||||
|
when nil
|
||||||
|
config_hash = Config.default_config_hash
|
||||||
|
when String
|
||||||
|
config_hash = Config.parse_resolv_conf(@config_info)
|
||||||
|
when Hash
|
||||||
|
config_hash = @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
|
||||||
|
@nameserver = config_hash[:nameserver] if config_hash.include? :nameserver
|
||||||
|
@search = config_hash[:search] if config_hash.include? :search
|
||||||
|
@ndots = config_hash[:ndots] if config_hash.include? :ndots
|
||||||
|
|
||||||
@nameserver = ['0.0.0.0'] if @nameserver.empty?
|
@nameserver = ['0.0.0.0'] if @nameserver.empty?
|
||||||
unless @search
|
if @search
|
||||||
|
@search = @search.map {|arg| Label.split(arg) }
|
||||||
|
else
|
||||||
hostname = Socket.gethostname
|
hostname = Socket.gethostname
|
||||||
if /\./ =~ hostname
|
if /\./ =~ hostname
|
||||||
@search = [Label.split($')]
|
@search = [Label.split($')]
|
||||||
@ -752,6 +794,21 @@ class Resolv
|
|||||||
@search = [[]]
|
@search = [[]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if !@nameserver.kind_of?(Array) ||
|
||||||
|
!@nameserver.all? {|ns| String === ns }
|
||||||
|
raise ArgumentError.new("invalid nameserver config: #{@nameserver.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
|
if !@search.kind_of?(Array) ||
|
||||||
|
!@search.all? {|ls| ls.all? {|l| Label::Str === l } }
|
||||||
|
raise ArgumentError.new("invalid search config: #{@search.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
|
if !@ndots.kind_of?(Integer)
|
||||||
|
raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
@initialized = true
|
@initialized = true
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user