home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / ntcode / hop / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  3.0 KB  |  113 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock.h>
  4.  
  5.  
  6.  void disp_help()
  7.  
  8.   {
  9.  
  10.    printf("\nGet host name program V 1.0 by L. Kahn (C) 1994\n");
  11.    printf("_______________________________________________\n\n");
  12.    printf("Syntax: ghostname hostname \n");
  13.    printf("WHERE: hostname is the name or ip address of a host.\n\n");
  14.   }
  15.  
  16.  
  17.   void main(argc,argv)
  18.   
  19.   int argc;
  20.   char **argv;
  21.    
  22.   {
  23.  
  24.  
  25.   WORD wVersionrequired;
  26.   WSADATA WSData;
  27.   char remotename[80];
  28.   static struct sockaddr_in destination;
  29.   static struct hostent *ptrhostentry;
  30.   LPSTR plpstr;
  31.   struct in_addr *pin_addr;
  32.   int rval = 0;
  33.    
  34.    wVersionrequired = MAKEWORD(1,1);
  35.    if (argc != 2)
  36.      disp_help();
  37.  
  38.    else
  39.    {
  40.      /* ok assume second arg is host name */
  41.   
  42.   rval = WSAStartup(wVersionrequired, &WSData);
  43.   if (rval != 0)
  44.      {
  45.       printf("Startup ERROR: could not find a usable a winsock DLL.\n");
  46.       fflush(stdout);
  47.       exit(1);
  48.      }
  49.  
  50.    if (LOBYTE(WSData.wVersion) != 1 || HIBYTE(WSData.wVersion) != 1)
  51.       {
  52.        printf("Startup ERROR: could not find a winsock DLL version 1.1 or newer.\n");
  53.        fflush(stdout);
  54.        exit(1);
  55.       }
  56.  
  57.   else
  58.    {
  59.           strcpy(remotename,argv[1]);
  60.           memset(&destination,0,sizeof(struct sockaddr));
  61.           destination.sin_family=AF_INET;
  62.  
  63.           destination.sin_addr.s_addr = inet_addr(remotename);
  64.           if(destination.sin_addr.s_addr ==INADDR_NONE)
  65.              {
  66.               ptrhostentry = gethostbyname(remotename);
  67.               if (ptrhostentry == NULL)
  68.                 {
  69.                   printf("can't get entry for host name: %s\n",remotename);
  70.                   fflush(stdout);
  71.                   WSACleanup();
  72.                   exit(1);
  73.                 }
  74.               }
  75.           else
  76.              {
  77.                ptrhostentry = gethostbyaddr((LPSTR)&destination.sin_addr.s_addr,4,PF_INET);
  78.                if (ptrhostentry == NULL)
  79.                  {
  80.                    printf("can't get ip entry for host name: %s\n",remotename);
  81.                    fflush(stdout);
  82.                    WSACleanup();
  83.                    exit(1);
  84.                  }
  85.              }
  86.              
  87.             /* if we get here we have valid entry so do it */
  88.  
  89.               memcpy(&destination.sin_addr,ptrhostentry->h_addr,ptrhostentry->h_length);
  90.               printf("Translated Name is: %s\n",ptrhostentry->h_name);
  91.  
  92.               while((pin_addr=(struct in_addr *)*(ptrhostentry->h_addr_list))!=NULL)
  93.                  {
  94.                   printf("IP Address is     : %s",inet_ntoa(*pin_addr));
  95.                   ptrhostentry->h_addr_list++;
  96.                 }
  97.               
  98.               while((plpstr=(LPSTR)*(ptrhostentry->h_aliases))!=NULL)
  99.                 {
  100.                  printf("Alias: %s",plpstr);
  101.                  ptrhostentry->h_aliases++;
  102.                 }
  103.           } /* end of ok startup */
  104.        } /* end of ok parameters */
  105.  
  106.      printf("\n");
  107.      fflush(stdout);
  108.      WSACleanup();
  109.  
  110.   } /* end of main */          
  111.  
  112.  
  113.