lolly pop
lolly pop

Reputation: 57

DNS client program in c

I have got this home work in which I have to make a DNS client which is connected to a DNS server with a socket. DNS servers are already build, so my job is to create a DNS client.The client should send a domain name to the server and server should respond with an equivalent IP address for that domain name. Therefore, I would like to know the basic steps that will be used in coding a DNS client.

Also, on the internet, I have found a program saying "DNS query program". What exactly is a DNS query program? I have attached a link below which will direct you to that query program. Is this program useful for me in making DNS client? http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/

Thanks in advance.

Upvotes: 1

Views: 6629

Answers (1)

4pie0
4pie0

Reputation: 29724

From what you've described it seems that all you have to do is to write usual tcp (or udp) client and use it to send specific message to server and read the message. To do this you have to start read some linux networking tutorial and I would suggest "UNIX Network Programming" by W. Richard Stevens. In short you will have to fill in struct sockaddr_in, make calls to socket(), connect(), and the write/read, close(). Here is example, functions with capital letters are simply wrappers over corresponding lowercase standard routines.

int main(int argc, char** argv) {

    int         sockfd;
    struct sockaddr_in  servaddr;

    if ( argc != 2)
        err_quit("usage: tcpcli <IPaddress>");

    sockfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons( SERV_PORT);
    Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

    Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

    str_cli( stdin, sockfd);        /* do it all: write/read from socket */

    close( sockfd);

    exit(0);
}

Upvotes: 2

Related Questions