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

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