home *** CD-ROM | disk | FTP | other *** search
- /*
- * hostent.c
- *
- * Program to print out a "hostent" structure given a hostname or
- * "dotted quad" address. Therefore this is exactly what FTP Telnet
- * or any utility that uses gethostbyname(3) sees.
- *
- * Using nslookup alone can often give you a false sense of security.
- *
- * Usage: hostent {hostname | address}
- *
- * P. Blanchfield <phil@dgbt.doc.ca>
- * The Communications Research Centre
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
-
- main(argc,argv)
- char **argv;
- {
- struct hostent *gethostbyaddr(), *gethostbyname(), *p2hostent;
- int iaddr[4]; /* scanf can only convert integers */
- char addr[4]; /* but we will need them as bytes */
- int len;
-
- if( argc != 2 )
- {
- fprintf(stderr,"Usage: %s {hostname | address}\n",argv[0]);
- exit(1);
- }
-
- if( (*argv[1] > '0') && (*argv[1] < '9') ) /* name or number? */
- { /* number */
- sscanf(argv[1],"%d.%d.%d.%d",&iaddr[0],&iaddr[1],&iaddr[2],&iaddr[3]);
- addr[0] = (char) iaddr[0];
- addr[1] = (char) iaddr[1];
- addr[2] = (char) iaddr[2];
- addr[3] = (char) iaddr[3];
- p2hostent = gethostbyaddr(addr, 4, AF_INET);
- }
- else /* name */
- p2hostent = gethostbyname(argv[1]);
-
- if( p2hostent == NULL )
- {
- fprintf(stderr,"Lookup failure on host \"%s\"\n",argv[1]);
- exit(1);
- }
-
- /* all is well, print the entry */
-
- print_hostentry(p2hostent);
-
- }
-
- /* Function to print a "hostent" structure */
-
- print_hostentry(p2hostent)
- struct hostent *p2hostent;
- {
-
- int i,j;
- char *none;
-
- /* Official name */
-
- printf("Cannonical (Official) hostname = %s\n\n",p2hostent->h_name);
-
- /* list of aliases */
-
- if(p2hostent->h_aliases[0]) none = ""; else none = " <NONE EXIST>";
- printf("List of aliases:%s\n\n",none);
-
- for(i=0;p2hostent->h_aliases[i];i++)
- printf("alias# %d = %s\n",i,p2hostent->h_aliases[i]);
- printf("\n");
-
- /* print out the list of addresses */
-
- printf("List of IP addresses:\n\n");
-
- for(i=0;p2hostent->h_addr_list[i];i++)
- {
- printf("address# %d = ",i);
- for(j=0;j<3;j++)
- printf("%u.",(unsigned char)p2hostent->h_addr_list[i][j]);
- printf("%u\n",(unsigned char)p2hostent->h_addr_list[i][j]);
- }
-
- }
-