home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / UNZIPPED / MSWATTCP / APPS / PING.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  4.0 KB  |  154 lines

  1.  
  2. /******************************************************************************
  3.     PING - internet diagnostic tool
  4.     Copyright (C) 1991, University of Waterloo
  5.     portions Copyright (C) 1990, National Center for Supercomputer Applications
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it, but you may not sell it.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but without any warranty; without even the implied warranty of
  12.     merchantability or fitness for a particular purpose.
  13.  
  14.     Erick Engelke                   or via E-Mail
  15.     Faculty of Engineering
  16.     University of Waterloo          Erick@development.watstar.uwaterloo.ca
  17.     200 University Ave.,
  18.     Waterloo, Ont., Canada
  19.     N2L 3G1
  20.  
  21. ******************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <tcp.h>
  25.  
  26. /* extern long set_timeout(); */
  27.  
  28. longword sent = 0L;
  29. longword received = 0L;
  30. longword tot_delays = 0L;
  31. longword last_rcvd = 0L;
  32. char *name;
  33.  
  34. void stats(){
  35.  
  36.     longword temp;
  37.  
  38.     puts("\nPing Statistics");
  39.     printf("Sent        : %lu \n", sent );
  40.     printf("Received    : %lu \n", received );
  41.     if (sent)        printf("Success     : %lu %\n", (100L*received)/sent);
  42.     if (!received)   printf("There was no response from %s\n", name );
  43.     else {
  44.     temp = ( tot_delays * 2813L)/(512L*received);
  45.     printf("Average RTT : %lu.%02lu seconds\n", temp / 100L, temp % 100L);
  46.     }
  47.     exit( received ? 0 : 1 );
  48.     }
  49.  
  50.  
  51. void help() {
  52.     puts("PING [-s|/s] [-d|/d] hostname [number]");
  53.     exit( 3 );
  54.     }
  55.  
  56.  
  57. void main(int argc, char **argv) {
  58.  
  59.     longword host, timer, new_rcvd;
  60.     longword tot_timeout = 0L, itts = 0L, send_timeout = 0L;
  61.     word i;
  62.     word sequence_mode = 0, is_new_line = 1;
  63.     char tempbuffer[255];
  64.  
  65.     sock_init();
  66.  
  67.     if ( argc < 2 )  help();  /* e qui esco dal programma */
  68.  
  69.     name = NULL;
  70.     
  71.     for ( i = 1; i < (word)argc ; ++i ) {
  72.     
  73.     if ( !stricmp( argv[i], "-d") || !stricmp( argv[i],"/d")) {
  74.         puts("Debug mode activated\n");
  75.         tcp_set_debug_state( 1 );
  76.         } 
  77.     else if ( !stricmp( argv[i], "-s") || !stricmp( argv[i],"/s"))
  78.         sequence_mode = 1;
  79.     
  80.     else if ( !name )  name = argv[i];
  81.  
  82.     else {
  83.         sequence_mode = 1;
  84.         itts = atol( argv[i] );
  85.         }
  86.     }
  87.     
  88.     if (!name)  help();
  89.  
  90.     if (!(host = resolve( name ))) {
  91.     printf("Unable to resolve '%s'\n", name );
  92.     exit( 3 );
  93.     }
  94.  
  95.     if ( isaddr( name )) 
  96.     printf("\nPinging [%s]",w_inet_ntoa(tempbuffer, host));
  97.     
  98.     else 
  99.     printf("\nPinging '%s' [%s]",name, w_inet_ntoa(tempbuffer, host));
  100.  
  101.     if (itts) printf(" %u times", itts);
  102.     
  103.     else      itts = sequence_mode ? 0xffffffffL : 1;
  104.  
  105.     if (sequence_mode) printf(" once per_second");
  106.     
  107.     printf("\n\n");
  108.  
  109.     if (!sequence_mode) tot_timeout = (longword)set_timeout( itts + 10 );
  110.  
  111.     _arp_resolve( host , NULL );   /* resolution it before timer starts */
  112.  
  113.     
  114.     do {          /* once per second - do all this */
  115.     if ( chk_timeout( send_timeout ) || !send_timeout ) {
  116.         send_timeout = set_timeout( 1 );
  117.         if ( chk_timeout( tot_timeout ) && tot_timeout )
  118.                 stats();
  119.         if ( sent < itts ) {
  120.             sent++;
  121.             if (_ping( host , sent ))  stats();
  122.             if (!is_new_line)          printf("\n");
  123.             printf("sent PING # %lu ", sent );
  124.             is_new_line = 0;
  125.             }
  126.         }
  127.  
  128.     if ( kbhit() ) {
  129.         getch();            /* trash the character */
  130.         stats();
  131.         }
  132.  
  133.     tcp_tick(NULL);
  134.  
  135.     if ((timer = _chk_ping( host , &new_rcvd)) != 0xffffffffL) {
  136.         tot_delays += timer;
  137.         ++received;
  138.         if ( new_rcvd != last_rcvd + 1 ) {
  139.              if (!is_new_line) printf("\n");
  140.              puts("PING receipt received out of order!");
  141.              is_new_line = 1;
  142.              }
  143.         last_rcvd = new_rcvd;
  144.         if (!is_new_line && !debug_on) printf(", ");
  145.         printf("PING receipt # %lu : response time %lu.%02lu \
  146.         seconds\n\n",received, timer / 18L, ((timer %18L)*55)/10 );
  147.         is_new_line = 1;
  148.         if ( received == itts )
  149.         stats();
  150.         }
  151.     
  152.     } while (1);
  153.     }
  154.