#include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int is_addr; struct hostent *hn; int i; struct in_addr addr; if(argc < 2) { fprintf(stderr,"usages: my_dns2 dns_name\nmy_dns2 ip_addr\n"); fflush(stderr); exit(1); } /* * first, try to make an ip address out of the string passed as the * first argument */ #ifdef sun is_addr = inet_addr(argv[1]); if(is_addr == -1) { is_addr = 0; } else { memcpy(&addr,&is_addr,sizeof(addr)); is_addr = 1; } #else is_addr = inet_aton(argv[1],&addr); #endif /* * if this worked, argv[1] is a dotted notation IP address => * gethostbyaddr() */ if(is_addr) { hn = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); if(hn == NULL) { fprintf(stderr,"gethostbyaddr() failed for %s\n", argv[1]); fflush(stderr); exit(1); } } else { hn = gethostbyname(argv[1]); if(hn == NULL) { fprintf(stderr,"gethostbyname() failed for %s\n", argv[1]); fflush(stderr); exit(1); } } /* * print its official host name */ fprintf(stdout,"host name: %s\n",hn->h_name); /* * loop through the alias list */ i = 0; while(hn->h_aliases[i] != NULL) { fprintf(stdout,"alias: %s\n",hn->h_aliases[i]); i++; } /* * loop through address list */ i = 0; while(hn->h_addr_list[i] != NULL) { /* * this is ugly -- inet_ntoa() takes a structure as a value * parameter * * plase don't ever do this */ memcpy(&addr,hn->h_addr_list[i],sizeof(addr)); fprintf(stdout,"address: %s\n", inet_ntoa(addr)); i++; } return(0); }