Class: Async::DNS::DatagramHandler

Inherits:
GenericHandler show all
Defined in:
lib/async/dns/handler.rb

Overview

Handling incoming UDP requests, which are single data packets, and pass them on to the given server.

Instance Attribute Summary

Attributes inherited from GenericHandler

#server, #socket

Instance Method Summary collapse

Methods inherited from GenericHandler

#error_response, #initialize, #process_query

Constructor Details

This class inherits a constructor from Async::DNS::GenericHandler

Instance Method Details

#respond(socket, input_data, remote_address) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/async/dns/handler.rb', line 81

def respond(socket, input_data, remote_address)
	response = process_query(input_data, remote_address: remote_address)
	
	output_data = response.encode
	
	@logger.debug "<#{response.id}> Writing #{output_data.bytesize} bytes response to client via UDP..."
	
	if output_data.bytesize > UDP_TRUNCATION_SIZE
		@logger.warn "<#{response.id}> Response via UDP was larger than #{UDP_TRUNCATION_SIZE}!"
		
		# Reencode data with truncation flag marked as true:
		truncation_error = Resolv::DNS::Message.new(response.id)
		truncation_error.tc = 1
		
		output_data = truncation_error.encode
	end
	
	socket.sendmsg(output_data, 0, remote_address)
rescue IOError => error
	@logger.warn "<> UDP response failed: #{error.inspect}!"
rescue EOFError => error
	@logger.warn "<> UDP session ended prematurely: #{error.inspect}!"
rescue DecodeError
	@logger.warn "<> Could not decode incoming UDP data!"
end

#run(task: Async::Task.current) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/async/dns/handler.rb', line 71

def run(task: Async::Task.current)
	while true
		input_data, remote_address = @socket.recvmsg(UDP_TRUNCATION_SIZE)
		
		task.async do
			respond(@socket, input_data, remote_address)
		end
	end
end