Method: Async::DNS::System.parse_resolv_configuration

Defined in:
lib/async/dns/system.rb

.parse_resolv_configuration(path) ⇒ Object

Parse the ‘resolv.conf` file and return a list of nameservers.



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
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/async/dns/system.rb', line 119

def self.parse_resolv_configuration(path)
  nameservers = []
  search = nil
  ndots = 1
  edns = nil
  timeout = DEFAULT_TIMEOUT
  
  File.open(path) do |file|
    file.each do |line|
      # Remove any comments:
      line.sub!(/[#;].*/, "")
      
      # Extract resolv.conf command:
      keyword, *arguments = line.split(/\s+/)
      
      case keyword
      when "nameserver"
        nameservers.concat(arguments)
      when "domain", "search"
        search = arguments
      when "options"
        arguments.each do |argument|
          key, value = argument.split(":", 2)
          
          case key
          when "ndots"
            ndots = value.to_i
          when "edns0"
            edns = 0
          when "timeout"
            timeout = value.to_f
          end
        end
      end
    end
  end
  
  return {
    nameservers: nameservers,
    search: search,
    ndots: ndots,
    edns: edns,
    timeout: timeout,
  }
end