Method: Async::DNS::System.resolver

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

.resolver(**options) ⇒ Object

Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with. There is no equivalent facility to use the ‘hosts` file at present.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/async/dns/system.rb', line 166

def self.resolver(**options)
  nameservers = []
  
  if File.exist? RESOLV_CONF
    options.update(parse_resolv_configuration(RESOLV_CONF))
    nameservers = options.delete(:nameservers)
  elsif defined?(Win32::Resolv) and RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
    search, nameservers = Win32::Resolv.get_resolv_info
    options.update(search: search)
  end
  
  if search = options[:search]
    unless search.include?(".")
      search << nil
    end
  else
    options[:search] = [nil]
  end
  
  if hosts = Hosts.local
    cache = options.fetch(:cache) do
      options[:cache] = Cache.new
    end
    
    hosts.each do |name, addresses|
      addresses.each do |address|
        resource = Resource.new(address, nil)
        case address
        when Resolv::IPv4
          cache.store(name, Resolv::DNS::Resource::IN::A, resource)
        when Resolv::IPv6
          cache.store(name, Resolv::DNS::Resource::IN::AAAA, resource)
        end
      end
    end
  end
  
  timeout = options.delete(:timeout) || DEFAULT_TIMEOUT
  endpoint = Endpoint.for(nameservers, timeout: timeout)
  
  if block_given?
    yield endpoint, **options
  else
    return Resolver.new(endpoint, **options)
  end
end