#include "csapp.h"
/*Internet address structure*/
struct in_addr{
unsigned int s_addr; /* Network byte order(big-endian)*/
}
/* DNS host entry structure */
struct hostent{
char *h_name; /*Official domain name of host */
char **h_aliases; /*Null-terminated array of domain names */
int h_addrtypes; /* Host address type (AF_INET) */
int h_length; /* Length of an address, in bytes */
char **h_addr_list; /* Null-terminated array of in_addr structs */
}
int main(int argc, char **argv)
{
char **p;
struct in_addr addr;
struct hostent *hostp;
if(argc != 2){
fprintf(stderr,"usage: %s <domain name or dotted-decimal>\n", argv[0]);
exit(0);
}
if (inet_aton(argv[1],&addr) != 0)/*inet_aton函数将一个点分十进制串(cp)转换为一个网络字节顺序的IP地址*/
hostp = Gethostbyaddr((const char * )&addr, sizeof(addr), AF_INET);
else
hostp = Gethostbyname(argv[1]);
printf("official hostname : %s\n", hostp->h_name);
for (pp = hostp->h_aliases; *pp != Null; pp++)
printf("alias: %s\n", *pp);
for (pp = hostp->h_addr_list; *pp != Null; pp++){
addr.s_addr = ((struct in_addr *)*pp)->s_addr;
printf("address: %s\n", inet_ntoa(addr));
}
exit(0);
}
04-14
323