Class: Async::DNS::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/async/dns/server.rb

Constant Summary collapse

DEFAULT_ENDPOINTS =

The default server interfaces

[[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoints = DEFAULT_ENDPOINTS, origin: '.', logger: Async.logger) ⇒ Server

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



40
41
42
43
44
45
46
# File 'lib/async/dns/server.rb', line 40

def initialize(endpoints = DEFAULT_ENDPOINTS, origin: '.', logger: Async.logger)
	@endpoints = endpoints
	@origin = origin
	@logger = logger
	
	@handlers = []
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



51
52
53
# File 'lib/async/dns/server.rb', line 51

def logger
  @logger
end

#originObject

Records are relative to this origin:



49
50
51
# File 'lib/async/dns/server.rb', line 49

def origin
  @origin
end

Instance Method Details

#fire(event_name) ⇒ Object

Fire the named event as part of running the server.



54
55
# File 'lib/async/dns/server.rb', line 54

def fire(event_name)
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.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/async/dns/server.rb', line 58

def process(name, resource_class, transaction)
	raise NotImplementedError.new
end

#process_query(query, options = {}, &block) ⇒ Object

Process an incoming DNS message. Returns a serialized message to be sent back to the client.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/async/dns/server.rb', line 63

def process_query(query, options = {}, &block)
	start_time = Time.now
	
	# Setup response
	response = Resolv::DNS::Message::new(query.id)
	response.qr = 1                 # 0 = Query, 1 = Response
	response.opcode = query.opcode  # Type of Query; copy from query
	response.aa = 1                 # Is this an authoritative response: 0 = No, 1 = Yes
	response.rd = query.rd          # Is Recursion Desired, copied from query
	response.ra = 0                 # Does name server support recursion: 0 = No, 1 = Yes
	response.rcode = 0              # Response code: 0 = No errors
	
	transaction = nil
	
	begin
		query.question.each do |question, resource_class|
			begin
				question = question.without_origin(@origin)
				
				@logger.debug {"<#{query.id}> Processing question #{question} #{resource_class}..."}
				
				transaction = Transaction.new(self, query, question, resource_class, response, options)
				
				transaction.process
			rescue Resolv::DNS::OriginError
				# This is triggered if the question is not part of the specified @origin:
				@logger.debug {"<#{query.id}> Skipping question #{question} #{resource_class} because #{$!}"}
			end
		end
	rescue StandardError => error
		@logger.error "<#{query.id}> Exception thrown while processing #{transaction}!"
		Async::DNS.log_exception(@logger, error)
	
		response.rcode = Resolv::DNS::RCode::ServFail
	end
	
	end_time = Time.now
	@logger.debug {"<#{query.id}> Time to process request: #{end_time - start_time}s"}
	
	return response
end

#run(*args) ⇒ Object

Setup all specified interfaces and begin accepting incoming connections.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/async/dns/server.rb', line 106

def run(*args)
	@logger.info "Starting Async::DNS server (v#{Async::DNS::VERSION})..."
	
	setup_handlers if @handlers.empty?
	
	Async::Reactor.run do |task|
		@handlers.each do |handler|
			task.async do
				handler.run(*args)
			end
		end
		
		fire(:start)
	end
end