A simple DNS example in Linux

Source: Internet
Author: User

Question: DNS Client
Requirement: simulate a DNS ClientProgramPerforms forward and reverse resolution based on the specified DNS server.
Problem: currently, only the default DNS server can be used, and the Internet IP address cannot be reverse resolved.
Usage:
Compile: $ gcc-o dns. c
Run: $./DNS www.whu.edu.cn
$./DNS 192.168.0.51 (My Intranet IP address)

Program:
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
Int main (INT argc, char * argv [])
{
Struct sockaddr_in ADDR;
Struct hostent * Host;
Char ** alias;

If (argc! = 2)
{
Fprintf (stderr, "Usage: % s hostname | IP .. \ n \ A", argv [0]);
Exit (1 );
}

/* Assume the IP address is used to obtain the host information */
If (inet_aton (argv [1], & (ADDR. sin_addr ))! = 0)
{
Host = gethostbyaddr (char *) & (ADDR. sin_addr), 4, af_inet );
Printf ("address information of IP % s \ n", argv [1]);
}
Else
{
/* Otherwise, the user should enter a Domain Name and use the domain name to find the host information */
Host = gethostbyname (argv [1]);
Printf ("address information of host % s \ n", argv [1]);
}
If (host = NULL)
{
/* None of them. Forget it */
Printf ("No information found ");
Exit (1 );
}
/* Print the host name */
Printf ("Official Host Name: \ n % s \ n", host-> h_name );
/* Print other host names */
Printf ("name aliases: \ n ");
For (alias = Host-> h_aliases; * alias! = NULL; alias ++)
Printf ("% s \ n", * alias );
/* Print host Series IP addresses */
Printf ("\ nip address: \ n ");
For (alias = Host-> h_addr_list; * alias! = NULL; alias ++)
Printf ("% s \ n", inet_ntoa (* (struct in_addr *) (* alias )));
}

Package download:
Http://files.cnblogs.com/lzcarl/dns.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.