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