Forwarding requests UDPSocket

I have a basic ruby program, that listens on a port (53), receives the data and then sends to another location (Google DNS server - 8.8.8.8). The responses are not going back to their original destination, or I'm not forwarding them correctly.

Here is the code. NB I'm using EventMachine

require 'rubygems'
require 'eventmachine'

module DNSServer
    def post_init
        puts 'connected'
    end

    def receive_data(data)
        # Forward all data
        conn = UDPSocket.new
        conn.connect '8.8.8.8', 53
        conn.send data, 0
        conn.close

        p data.unpack("H*")
    end

    def unbind
        puts 'disconnected'
    end
end
EM.run do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end

Any thoughts as to why or tips to debug, would be most appreciated.

Answers


The obvious problems are:

  1. UDP comms are usually connectionless, use the 4 argument version of send instead of connect
  2. You're not receiving any data from the socket talking to 8.8.8.8
  3. You're not sending any data back (#send_data) to the original client

This seems to work:

require 'socket'
require 'rubygems'
require 'eventmachine'

module DNSServer
    def receive_data(data)
        # Forward all data
        conn = UDPSocket.new
        conn.send data, 0, '8.8.8.8', 53
        send_data conn.recv 4096
    end
end

EM.run do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end

Need Your Help

Using a bash script in linux to start and stop tail by polling for keypress

linux bash keypress interrupt tail

Here's what I'm trying to do, I have an arduino with temperature information going to a linux box over serial. I'm successfully reading the serial and recording it to a file with cat and then tail -f

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.