home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / host.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-05  |  1.2 KB  |  79 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <devices.h>
  4. #include <files.h>
  5. #include <errors.h>
  6.  
  7. #include <sys/types.h>
  8. #include <sys/socket.h> /* for AF_INET */
  9. #include <netdb.h>
  10. #include <arpa/inet.h>
  11. #include <netinet/in.h>
  12.  
  13. extern int h_errno;
  14.  
  15. char hostname[256];
  16.  
  17. main(argc,argv)
  18.     int argc;
  19.     char *argv[];
  20. {
  21.     int i;
  22.     
  23.     if (argc < 2)
  24.     {
  25.         gethostname(hostname,256);
  26.         get_host();
  27.         exit(0);
  28.     }
  29.     for (i=1; i<argc; i++)
  30.     {
  31.         strcpy(hostname,argv[i]);
  32.         get_host();
  33.     }
  34. }
  35.  
  36. get_host()
  37. {
  38.     struct hostent *hp;
  39.     unsigned long ipaddr;
  40.     long **addrP;
  41.  
  42.     hp = gethostbyname(hostname);
  43.     if (hp == NULL)
  44.     {
  45.         if (h_errno != HOST_NOT_FOUND)
  46.         {
  47.             herror("gethostbyname");
  48.             return(-1);;
  49.         }
  50.         ipaddr = inet_addr(hostname);
  51.         if (ipaddr == -1)
  52.         {
  53.             dprintf("host '%s' not found\n",hostname);
  54.             return(-1);;
  55.         }
  56.         hp = gethostbyaddr(&ipaddr,4,AF_INET);
  57.         if (hp == NULL)
  58.         {
  59.             herror("gethostbyaddr");
  60.             return(-1);;
  61.         }
  62.         strncpy(hostname,hp->h_name,256);
  63.         hostname[255] = 0;
  64.         hp = gethostbyname(hostname);
  65.         if (hp == NULL)
  66.         {
  67.             herror("gethostbyname");
  68.             return(-1);;
  69.         }
  70.     }
  71.     
  72.     dprintf("%s\n",hp->h_name);
  73.     for (addrP=(long **)hp->h_addr_list; *addrP; addrP++)
  74.     {
  75.         dprintf("  %s\n",inet_ntoa(**addrP));
  76.     }
  77. }
  78.  
  79.