Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macos: support multiple system resolvers like C #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 31 additions & 4 deletions lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def self.each_name(address, &proc)
##
# Creates a new Resolv using +resolvers+.

def initialize(resolvers=[Hosts.new, DNS.new])
@resolvers = resolvers
def initialize(resolvers=nil)
@resolvers = resolvers || default_resolvers
end

##
Expand Down Expand Up @@ -152,6 +152,23 @@ def each_name(address)
}
end

private def default_resolvers # :nodoc:
resolvers = [Hosts.new, DNS.new]

# macOS supports multiple DNS resolvers via additional configs
# (e.g. local domain resolvers, VPNs, etc.)
# ref: `man 5 resolver` on macOS/darwin
if /darwin/ =~ RUBY_PLATFORM && Dir.exist?('/etc/resolver')
Dir.each_child('/etc/resolver') do |filename|
resolver = DNS::Config.parse_resolv_conf("/etc/resolver/#{filename}")
resolver[:search] = [filename] unless resolver[:search]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the config filename implicitly specifies domain option, which is the key for DNS routing and is a different thing than search option or the search suffix.

resolvers << DNS.new(resolver)
end
end

resolvers
end

##
# Indicates a failure to resolve a name or address.

Expand Down Expand Up @@ -958,6 +975,7 @@ def Config.parse_resolv_conf(filename)
nameserver = []
search = nil
ndots = 1
port = Port
File.open(filename, 'rb') {|f|
f.each {|line|
line.sub!(/[#;].*/, '')
Expand All @@ -979,10 +997,20 @@ def Config.parse_resolv_conf(filename)
ndots = $1.to_i
end
}
when 'port'
# Only valid for macOS
next unless /darwin/ =~ RUBY_PLATFORM
next if args.empty?
port = args[0].to_i
end
}
}
return { :nameserver => nameserver, :search => search, :ndots => ndots }

return {
:nameserver_port => nameserver.to_enum(:each_with_index).map { |ns, i| [ns, port] },
:search => search,
:ndots => ndots
}
end

def Config.default_config_hash(filename="/etc/resolv.conf")
Expand Down Expand Up @@ -2907,4 +2935,3 @@ def DefaultResolver.replace_resolvers new_resolvers
AddressRegex = /(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/

end

17 changes: 17 additions & 0 deletions test/resolv/test_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ def test_resolv_conf_by_command
end
end

def test_macos_resolver_port
return unless /darwin/ =~ RUBY_PLATFORM

Tempfile.create('resolv_test_macos_resolver_port_') do |tmpfile|
tmpfile.puts "domain localdomain"
tmpfile.puts "nameserver 127.0.0.1"
tmpfile.puts "port 55353"
tmpfile.close
config_hash = Resolv::DNS::Config.parse_resolv_conf(tmpfile.path)
assert_equal({
:nameserver_port => [['127.0.0.1', 55353]],
:search => ['localdomain'],
:ndots => 1,
}, config_hash)
end
end

def test_dots_diffences
name1 = Resolv::DNS::Name.create("example.org")
name2 = Resolv::DNS::Name.create("ex.ampl.eo.rg")
Expand Down