Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1132,14 +1132,15 @@ def Config.parse_resolv_conf(filename)
end

def Config.default_config_hash(filename="/etc/resolv.conf")
if File.exist? filename
Config.parse_resolv_conf(filename)
elsif defined?(Win32::Resolv)
# Native Windows resolves the path against the current drive, while Cygwin's own resolver honors it.
if defined?(Win32::Resolv) and !(/cygwin/ =~ RUBY_PLATFORM and File.exist?(filename))
search, nameserver = Win32::Resolv.get_resolv_info
config_hash = {}
config_hash[:nameserver] = nameserver if nameserver
config_hash[:search] = [search].flatten if search
config_hash
elsif File.exist? filename
Config.parse_resolv_conf(filename)
else
{}
end
Expand Down
14 changes: 14 additions & 0 deletions test/resolv/test_win32_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'test/unit'
require 'resolv'
require 'tempfile'

# JRuby loads its bundled win32/resolv, which lacks the registry access of the extension.
if defined?(Win32::Resolv) && Win32::Resolv.respond_to?(:tcpip_params, true)
Expand All @@ -23,5 +24,18 @@ def test_nonexistent_key
def test_nonexistent_value
assert_nil(Win32::Resolv.send(:tcpip_params) {|reg| reg.value('NonExistentKeyThatShouldNotExist')})
end

def test_default_config_hash_with_resolv_conf
Tempfile.create("resolv.conf") do |f|
f.puts "nameserver 192.0.2.1"
f.close
nameserver = Resolv::DNS::Config.default_config_hash(f.path)[:nameserver]
if /cygwin/ =~ RUBY_PLATFORM
assert_equal(["192.0.2.1"], nameserver)
else
assert_not_include(Array(nameserver), "192.0.2.1")
end
end
end
end
end
Loading