Module: Resolv::Macos

Defined in:
lib/resolv/macos.rb,
lib/resolv/macos/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.default_resolvers(resolvers_path = '/etc/resolver') ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/resolv/macos.rb', line 6

def self.default_resolvers(resolvers_path='/etc/resolver') # :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
  Dir.each_child(resolvers_path) do |filename|
    resolver = ::Resolv::Macos.parse_resolv_conf(
      File.join(resolvers_path, filename)
    )
    resolver[:search] = [filename] unless resolver[:search]
    resolvers << ::Resolv::DNS.new(resolver)
  end

  resolvers
end

.parse_resolv_conf(filename) ⇒ Object

Most of this is directly cribbed from Resolv, but with ‘port` parsing added



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/resolv/macos.rb', line 25

def self.parse_resolv_conf(filename) # :nodoc:
  nameserver = []
    search = nil
    ndots = 1
    port = ::Resolv::DNS::Port
    File.open(filename, 'rb') {|f|
      f.each {|line|
        line.sub!(/[#;].*/, '')
        keyword, *args = line.split(/\s+/)
        next unless keyword
        case keyword
        when 'nameserver'
          nameserver += args
        when 'domain'
          next if args.empty?
          search = [args[0]]
        when 'search'
          next if args.empty?
          search = args
        when 'options'
          args.each {|arg|
            case arg
            when /\Andots:(\d+)\z/
              ndots = $1.to_i
            end
          }
        when 'port'
          next if args.empty?
          port = args[0].to_i
        end
      }
    }

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