home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / misc / hostent.shar / hostent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-25  |  2.1 KB  |  94 lines

  1. /*
  2.  *    hostent.c
  3.  *
  4.  *    Program to print out a "hostent" structure given a hostname or
  5.  *    "dotted quad" address. Therefore this is exactly what FTP Telnet
  6.  *    or any utility that uses gethostbyname(3) sees.
  7.  *
  8.  *    Using nslookup alone can often give you a false sense of security.
  9.  *
  10.  *    Usage: hostent {hostname | address}
  11.  *
  12.  *    P. Blanchfield <phil@dgbt.doc.ca>
  13.  *    The Communications Research Centre
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netdb.h>
  20.  
  21. main(argc,argv)
  22. char **argv;
  23. {
  24.     struct hostent *gethostbyaddr(), *gethostbyname(), *p2hostent;
  25.     int iaddr[4];    /* scanf can only convert integers */
  26.     char addr[4];    /* but we will need them as bytes */
  27.     int len;
  28.  
  29.      if( argc != 2 )
  30.     {
  31.      fprintf(stderr,"Usage: %s {hostname | address}\n",argv[0]);
  32.      exit(1);
  33.     }
  34.  
  35.     if( (*argv[1] > '0') && (*argv[1] < '9') )    /* name or number? */
  36.     { /* number */
  37.       sscanf(argv[1],"%d.%d.%d.%d",&iaddr[0],&iaddr[1],&iaddr[2],&iaddr[3]);
  38.       addr[0] = (char) iaddr[0];
  39.       addr[1] = (char) iaddr[1];
  40.       addr[2] = (char) iaddr[2];
  41.       addr[3] = (char) iaddr[3];
  42.       p2hostent = gethostbyaddr(addr, 4, AF_INET);
  43.     }
  44.     else /* name */
  45.       p2hostent = gethostbyname(argv[1]);
  46.  
  47.     if( p2hostent == NULL )
  48.     {
  49.         fprintf(stderr,"Lookup failure on host \"%s\"\n",argv[1]);
  50.         exit(1);
  51.     }
  52.  
  53.     /* all is well, print the entry */
  54.  
  55.     print_hostentry(p2hostent);
  56.  
  57. }
  58.  
  59. /* Function to print a "hostent" structure */
  60.  
  61. print_hostentry(p2hostent)
  62. struct hostent *p2hostent;
  63.  
  64.     int i,j;
  65.     char *none;
  66.  
  67.     /* Official name */
  68.  
  69.     printf("Cannonical (Official) hostname = %s\n\n",p2hostent->h_name);
  70.  
  71.     /* list of aliases */
  72.  
  73.     if(p2hostent->h_aliases[0]) none = ""; else none = " <NONE EXIST>";
  74.     printf("List of aliases:%s\n\n",none);
  75.  
  76.     for(i=0;p2hostent->h_aliases[i];i++)
  77.       printf("alias# %d  = %s\n",i,p2hostent->h_aliases[i]);
  78.     printf("\n");
  79.  
  80.     /* print out the list of addresses */
  81.  
  82.     printf("List of IP addresses:\n\n");
  83.  
  84.     for(i=0;p2hostent->h_addr_list[i];i++)
  85.     {
  86.       printf("address# %d  = ",i);
  87.       for(j=0;j<3;j++)
  88.         printf("%u.",(unsigned char)p2hostent->h_addr_list[i][j]);
  89.       printf("%u\n",(unsigned char)p2hostent->h_addr_list[i][j]);
  90.     }
  91.  
  92. }
  93.