Class: RubyDNS::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rubydns/resolver.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(servers, options = {}) ⇒ Resolver

Servers are specified in the same manor as options[:listen], e.g. [:tcp/:udp, address, port] In the case of multiple servers, they will be checked in sequence.



37
38
39
40
41
# File 'lib/rubydns/resolver.rb', line 37

def initialize(servers, options = {})
	@servers = servers
	
	@options = options
end

Instance Method Details

#addresses_for(name, resource_class = Resolv::DNS::Resource::IN::A, &block) ⇒ Object

Yields a list of Resolv::IPv4 and Resolv::IPv6 addresses for the given name and resource_class.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubydns/resolver.rb', line 63

def addresses_for(name, resource_class = Resolv::DNS::Resource::IN::A, &block)
	query(name, resource_class) do |response|
		# Resolv::DNS::Name doesn't retain the trailing dot.
		name = name.sub(/\.$/, '')
		
		case response
		when Message
			yield response.answer.select{|record| record[0].to_s == name}.collect{|record| record[2].address}
		else
			yield []
		end
	end
end

#next_id!Object

Provides the next sequence identification number which is used to keep track of DNS messages.



44
45
46
47
# File 'lib/rubydns/resolver.rb', line 44

def next_id!
	# Using sequential numbers for the query ID is generally a bad thing because over UDP they can be spoofed. 16-bits isn't hard to guess either, but over UDP we also use a random port, so this makes effectively 32-bits of entropy to guess per request.
	SecureRandom.random_number(2**16)
end

#query(name, resource_class = Resolv::DNS::Resource::IN::A, &block) ⇒ Object

Look up a named resource of the given resource_class.



50
51
52
53
54
55
56
# File 'lib/rubydns/resolver.rb', line 50

def query(name, resource_class = Resolv::DNS::Resource::IN::A, &block)
	message = Resolv::DNS::Message.new(next_id!)
	message.rd = 1
	message.add_question name, resource_class
	
	send_message(message, &block)
end

#send_message(message, &block) ⇒ Object



58
59
60
# File 'lib/rubydns/resolver.rb', line 58

def send_message(message, &block)
	Request.fetch(message, @servers, @options, &block)
end