The DNS Host entry contains DNS information about a domin name or IP address in DNS database.
1 struct hostent{2 Char *h_name; 3 Char *h_aliases; 4 int H_addrtype; 5 int h_length; 6 Char * *h_addr_list; 7 };
The Hostinfo program, which is used to resolve DNS info from IP or domin name.
1 /*modified from CSAPP:HOSTINFO.C2 */3#include <stdio.h>4#include <stdlib.h>5#include <unistd.h>6#include <string.h>7#include <ctype.h>8#include <setjmp.h>9#include <signal.h>Ten#include <sys/time.h> One#include <sys/types.h> A#include <sys/wait.h> -#include <sys/stat.h> -#include <fcntl.h> the#include <sys/mman.h> -#include <errno.h> -#include <math.h> -#include <pthread.h> +#include <semaphore.h> -#include <sys/socket.h> +#include <netdb.h> A#include <netinet/inch.h> at#include <arpa/inet.h> -#include <netinet/inch.h> -#include <netdb.h> - - voidDns_error (Char*msg) - { infprintf (stderr,"%s:dns Error%d\n", MSG, h_errno); - } to + structHostent *gethostbyaddr (Const Char* Addr,intLenintflag) - { the structHostent *p; * if(p = gethostbyaddr (addr, len, flag) = =NULL) $Dns_error ("gethostbyaddr Error");Panax Notoginseng returnp; - } the + structHostent *gethostbyname (Const Char*name) A { the structHostent *p; + if(p = gethostbyname (name)) = =NULL) -Dns_error ("gethostbyname Error"); $ returnp; $ } - - intMainintargcChar**argv) the { - Char**pp;Wuyi structin_addr addr; the structHostent *HOSTP; - Wu if(ARGC! =2) { -fprintf (stderr,"Usage:%s <domain name or dotted-decimal>\n", Aboutargv[0]); $Exit0); - } - intAf_inet =af_inet; - if(Inet_aton (argv[1], &addr)! =0) AHOSTP = gethostbyaddr ((Const Char*) &addr,sizeof(addr), af_inet); + Else theHOSTP = gethostbyname (argv[1]); - $ if(HOSTP = =NULL) theExit0); the theprintf"official hostname:%s\n", hostp->h_name); the - for(pp = hostp->h_aliases;*pp! = null;pp++) inprintf"alias:%s\n", *pp); the the for(pp = hostp->h_addr_list;*pp = NULL; pp++) { AboutAddr.s_addr = ((structIN_ADDR *) *pp)s_addr; theprintf"Address:%s\n", Inet_ntoa (addr)); the } theExit0); +}
Hostinfo