class Async::DNS::DatagramHandler

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

Public Instance Methods

respond(socket, input_data, remote_address) click to toggle source
# 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) click to toggle source
# 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