home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / trees / lookup.c < prev    next >
C/C++ Source or Header  |  1997-07-30  |  1KB  |  47 lines

  1. /* lookup.c -- print basic DNS information on an Internet host */
  2.  
  3. #include <netdb.h>              /* for gethostby* */
  4. #include <sys/socket.h> 
  5. #include <netinet/in.h>         /* for address structures */
  6. #include <arpa/inet.h>          /* for inet_ntoa() */
  7. #include <stdio.h>
  8.  
  9. int main(int argc, char ** argv) {
  10.     struct hostent * answer;
  11.     struct in_addr address, ** addrptr;
  12.     char ** next;
  13.  
  14.     if (argc != 2) {
  15.         fprintf(stderr, "only a single argument is supported\n");
  16.         return 1;
  17.     }
  18.  
  19.     if (inet_aton(argv[1], &address))
  20.         answer = gethostbyaddr((char *) &address, sizeof(address), 
  21.                                AF_INET);
  22.     else
  23.         answer = gethostbyname(argv[1]);
  24.  
  25.     if (!answer) {
  26.         herror("error looking up host");
  27.         return 1;
  28.     }
  29.  
  30.     printf("Canonical hostname: %s\n", answer->h_name);
  31.  
  32.     if (answer->h_aliases[0]) {
  33.         printf("Aliases:");
  34.         for (next = answer->h_aliases; *next; next++)
  35.             printf(" %s", *next);
  36.         printf("\n");
  37.     }
  38.  
  39.     printf("Addresses:");
  40.     for (addrptr = (struct in_addr **) answer->h_addr_list; 
  41.                 *addrptr; addrptr++)
  42.         printf(" %s", inet_ntoa(**addrptr));
  43.     printf("\n");
  44.  
  45.     return 0;
  46. }
  47.