Async::DNS SourceAsyncDNSSystem

module System

This module encapsulates system dependent name lookup functionality.

Nested

Definitions

def self.hosts_path

Get the path to the hosts file.

Implementation

def self.hosts_path
	if RUBY_PLATFORM =~ /mswin32|mingw|bccwin/
		Win32::Resolv.get_hosts_path
	else
		HOSTS
	end
end

def self.ipv6?

Signature

returns Boolean

True if the system supports IPv6.

Implementation

def self.ipv6?
	begin
		list = Socket.ip_address_list
	rescue NotImplementedError
		return true
	end
	
	list.any? {|a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? }
end

def self.parse_resolv_configuration(path)

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

Implementation

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

def self.resolver(**options)

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.

Implementation

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
	
	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