Class: RubyDNS::Server

Inherits:
Async::DNS::Server
  • Object
show all
Defined in:
lib/rubydns/server.rb

Overview

Provides the core of the RubyDNS domain-specific language (DSL). It contains a list of rules which are used to match against incoming DNS questions. These rules are used to generate responses which are either DNS resource records or failures.

Instance Method Summary collapse

Constructor Details

#initializeServer

Instantiate a server with a block

server = Server.new do match(/server.mydomain.com/, IN::A) do |transaction| transaction.respond!(“1.2.3.4”) end end



24
25
26
27
28
29
# File 'lib/rubydns/server.rb', line 24

def initialize(...)
	super
	
	@rules = []
	@otherwise = nil
end

Instance Method Details

#match(*pattern, &block) ⇒ Object

This function connects a pattern with a block. A pattern is either a String or a Regex instance. Optionally, a second argument can be provided which is either a String, Symbol or Array of resource record types which the rule matches against.

match(“www.google.com”) match(“gmail.com”, IN::MX) match(/g?mail.(com|org|net)/, [IN::MX, IN::A])



37
38
39
# File 'lib/rubydns/server.rb', line 37

def match(*pattern, &block)
	@rules << Rule.new(pattern, block)
end

#next!Object

If you match a rule, but decide within the rule that it isn’t the correct one to use, you can call ‘next!` to evaluate the next rule - in other words, to continue falling down through the list of rules.



52
53
54
# File 'lib/rubydns/server.rb', line 52

def next!
	throw :next
end

#otherwise(&block) ⇒ Object

Specify a default block to execute if all other rules fail to match. This block is typially used to pass the request on to another server (i.e. recursive request).

otherwise do |transaction| transaction.passthrough!($R) end



47
48
49
# File 'lib/rubydns/server.rb', line 47

def otherwise(&block)
	@otherwise = block
end

#process(name, resource_class, transaction) ⇒ Object

Give a name and a record type, try to match a rule and use it for processing the given arguments.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubydns/server.rb', line 57

def process(name, resource_class, transaction)
	Console.debug(self) {"<#{transaction.query.id}> Searching for #{name} #{resource_class.name}"}
	
	@rules.each do |rule|
		Console.debug(self) {"<#{transaction.query.id}> Checking rule #{rule}..."}
		
		catch (:next) do
			# If the rule returns true, we assume that it was successful and no further rules need to be evaluated.
			return if rule.call(self, name, resource_class, transaction)
		end
	end
	
	if @otherwise
		@otherwise.call(transaction)
	else
		@logger.warn "<#{transaction.query.id}> Failed to handle #{name} #{resource_class.name}!"
	end
end