tags:

views:

205

answers:

1

Hi am a newbie.. I just want to write DNS client program using C sockets that takes three arguments: a query name (e.g., host name or domain name) and a query type (A, or NS, or MX), and DNS server name. Print out the responses in the answer section of the DNS record received.

I know there is a command getaddrinfo.. but I just want to connect to lookup table and then get the DNS server name...

so when i give the input ./a.out www.google.com A 144.20.190.70

it will show something similar to this:

Server: 144.20.190.70 Address: 144.20.190.70#53

Non-authoritative answer:

Name : www.google.com

Canonical name : www.l.google.com

Name : www.l.google.com Address : 74.125.19.104

Name : www.l.google.com Address : 74.125.19.105

Name : www.l.google.com Address : 74.125.19.106

Name : www.l.google.com Address : 74.125.19.147

Name : www.l.google.com Address : 74.125.19.99

Name : www.l.google.com Address : 74.125.19.103

A: 

Yes you need to see Bev.net.dns class that Rob-philpott made for .net

Click Here

building requests to send to DNS servers is not easy but once you can get the answer back from the server then you need to send it back to the browser and this is the bit i've got stuck on.

i listen on port 53/UDP and get the request and send it to the DNS server and get a valid responce but then i send that back to the browser using the remote client port as a UDP but the browser will not except the reply.

Robs code is real easy to use as shown below "Resolver.Lookup" and i just neded to add a bit so that the original byte array sent from the DNS server as saved in Resolver.Message ready to send back to the browser.

    public void Listen()
    {
        receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
        receiveEndPoint = new IPEndPoint(IPAddress.Any, receivePort); receiveSocket.Bind(receiveEndPoint); 
        receivePort = (receiveSocket.LocalEndPoint as IPEndPoint).Port; 
        receiveBuffer = new byte[BufferSize]; 
        receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
    }


    public void NetworkMessageReceivedCallback(IAsyncResult asyncResult)
    {
        EndPoint remoteEndPoint = null;            
        byte[] bytes = null;                        
        remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); //Will contain the clients port                
        int bytesRead = receiveSocket.EndReceiveFrom(asyncResult, ref remoteEndPoint);                                              
        bytes = new Byte[bytesRead];                
        Buffer.BlockCopy(receiveBuffer, 0, bytes, 0, bytesRead);
       //string ip = "208.67.222.222";
       string ip = "192.168.1.254";
       IPAddress dnsServer = IPAddress.Parse(ip);
       Response R = Resolver.Lookup(bytes, dnsServer);
       receiveSocket.SendTo(R.Message , remoteEndPoint);//127.0.0.1
       receiveSocket.Close();
   Listen();
}
Andrew smith